test_message_conversion.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. # SPDX-License-Identifier: Apache-2.0
  2. # Copyright 2020 Contributors to OpenLEADR
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. # Unless required by applicable law or agreed to in writing, software
  8. # distributed under the License is distributed on an "AS IS" BASIS,
  9. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. # See the License for the specific language governing permissions and
  11. # limitations under the License.
  12. from openleadr.utils import generate_id, group_targets_by_type
  13. from openleadr.messaging import create_message, parse_message
  14. from openleadr import enums
  15. from pprint import pprint
  16. from termcolor import colored
  17. from datetime import datetime, timezone, timedelta
  18. import pytest
  19. DATE_FORMAT = "%Y-%m-%dT%H:%M:%S.%fZ"
  20. def create_dummy_event(ven_id):
  21. """
  22. Creates a dummy event
  23. """
  24. now = datetime.now(timezone.utc)
  25. event_id = generate_id()
  26. active_period = {"dtstart": (now + timedelta(minutes=1)),
  27. "duration": timedelta(minutes=9)}
  28. event_descriptor = {"event_id": event_id,
  29. "modification_number": 1,
  30. "modification_date_time": now,
  31. "priority": 1,
  32. "market_context": "http://MarketContext1",
  33. "created_date_time": now,
  34. "event_status": "near",
  35. "test_event": False,
  36. "vtn_comment": "This is an event"}
  37. event_signals = [{"intervals": [{"duration": timedelta(minutes=1), "uid": 0, "signal_payload": 8.0},
  38. {"duration": timedelta(minutes=1), "uid": 1, "signal_payload": 10.0},
  39. {"duration": timedelta(minutes=1), "uid": 2, "signal_payload": 12.0},
  40. {"duration": timedelta(minutes=1), "uid": 3, "signal_payload": 14.0},
  41. {"duration": timedelta(minutes=1), "uid": 4, "signal_payload": 16.0},
  42. {"duration": timedelta(minutes=1), "uid": 5, "signal_payload": 18.0},
  43. {"duration": timedelta(minutes=1), "uid": 6, "signal_payload": 20.0},
  44. {"duration": timedelta(minutes=1), "uid": 7, "signal_payload": 10.0},
  45. {"duration": timedelta(minutes=1), "uid": 8, "signal_payload": 20.0}],
  46. "signal_name": "LOAD_CONTROL",
  47. "signal_type": "x-loadControlCapacity",
  48. "signal_id": generate_id(),
  49. "measurement": {"name": "voltage",
  50. "description": "Voltage",
  51. "unit": "V",
  52. "scale": "none"},
  53. "current_value": 0.0}]
  54. event_targets = [{"ven_id": 'VEN001'}, {"ven_id": 'VEN002'}]
  55. event = {'active_period': active_period,
  56. 'event_descriptor': event_descriptor,
  57. 'event_signals': event_signals,
  58. 'targets': event_targets,
  59. 'targets_by_type': group_targets_by_type(event_targets),
  60. 'response_required': 'always'}
  61. return event
  62. testcases = [
  63. ('oadrCanceledOpt', dict(response={'response_code': 200, 'response_description': 'OK', 'request_id': generate_id()}, opt_id=generate_id())),
  64. ('oadrCanceledPartyRegistration', dict(response={'response_code': 200, 'response_description': 'OK', 'request_id': generate_id()}, registration_id=generate_id(), ven_id='123ABC')),
  65. ('oadrCanceledReport', dict(response={'response_code': 200, 'response_description': 'OK', 'request_id': generate_id()}, pending_reports=[{'request_id': generate_id()}, {'request_id': generate_id()}])),
  66. ('oadrCanceledReport', dict(response={'response_code': 200, 'response_description': 'OK', 'request_id': generate_id()}, pending_reports=[{'request_id': generate_id()}, {'request_id': generate_id()}], ven_id='123ABC')),
  67. ('oadrCancelOpt', dict(request_id=generate_id(), ven_id='123ABC', opt_id=generate_id())),
  68. ('oadrCancelPartyRegistration', dict(request_id=generate_id(), ven_id='123ABC', registration_id=generate_id())),
  69. ('oadrCancelReport', dict(request_id=generate_id(), ven_id='123ABC', report_request_id=generate_id(), report_to_follow=True)),
  70. ('oadrCreatedEvent', dict(response={'response_code': 200, 'response_description': 'OK', 'request_id': generate_id()},
  71. event_responses=[{'response_code': 200, 'response_description': 'OK', 'request_id': generate_id(), 'event_id': generate_id(), 'modification_number': 1, 'opt_type': 'optIn'},
  72. {'response_code': 200, 'response_description': 'OK', 'request_id': generate_id(), 'event_id': generate_id(), 'modification_number': 1, 'opt_type': 'optIn'},
  73. {'response_code': 200, 'response_description': 'OK', 'request_id': generate_id(), 'event_id': generate_id(), 'modification_number': 1, 'opt_type': 'optIn'}],
  74. ven_id='123ABC')),
  75. ('oadrCreatedReport', dict(response={'response_code': 200, 'response_description': 'OK', 'request_id': generate_id()}, pending_reports=[{'request_id': generate_id()}, {'request_id': generate_id()}], ven_id='123ABC')),
  76. ('oadrCreatedEvent', dict(response={'response_code': 200, 'response_description': 'OK', 'request_id': generate_id()},
  77. event_responses=[{'response_code': 200, 'response_description': 'OK', 'request_id': generate_id(),
  78. 'event_id': generate_id(),
  79. 'modification_number': 1,
  80. 'opt_type': 'optIn'},
  81. {'response_code': 200, 'response_description': 'OK', 'request_id': generate_id(),
  82. 'event_id': generate_id(),
  83. 'modification_number': 1,
  84. 'opt_type': 'optOut'}],
  85. ven_id='123ABC')),
  86. ('oadrCreatedPartyRegistration', dict(response={'response_code': 200, 'response_description': 'OK', 'request_id': generate_id()},
  87. registration_id=generate_id(),
  88. ven_id='123ABC',
  89. profiles=[{'profile_name': '2.0b',
  90. 'transports': [{'transport_name': 'simpleHttp'}]}],
  91. vtn_id='VTN123')),
  92. ('oadrCreatedReport', dict(response={'response_code': 200, 'response_description': 'OK', 'request_id': generate_id()}, pending_reports=[{'request_id': generate_id()}, {'request_id': generate_id()}])),
  93. ('oadrCreateOpt', dict(opt_id=generate_id(),
  94. opt_type='optIn',
  95. opt_reason='participating',
  96. created_date_time=datetime.now(timezone.utc),
  97. request_id=generate_id(),
  98. event_id=generate_id(),
  99. modification_number=1,
  100. targets=[{'ven_id': '123ABC'}],
  101. targets_by_type=group_targets_by_type([{'ven_id': '123ABC'}]),
  102. ven_id='VEN123')),
  103. ('oadrCreatePartyRegistration', dict(request_id=generate_id(), ven_id='123ABC', profile_name='2.0b', transport_name='simpleHttp', transport_address='http://localhost', report_only=False, xml_signature=False, ven_name='test', http_pull_model=True)),
  104. ('oadrCreateReport', dict(request_id=generate_id(),
  105. ven_id='123ABC',
  106. report_requests=[{'report_request_id': 'd2b7bade5f',
  107. 'report_specifier': {'granularity': timedelta(seconds=900),
  108. 'report_back_duration': timedelta(seconds=900),
  109. 'report_interval': {'dtstart': datetime(2019, 11, 19, 11, 0, 18, 672768, tzinfo=timezone.utc),
  110. 'duration': timedelta(seconds=7200)},
  111. 'report_specifier_id': '9c8bdc00e7',
  112. 'specifier_payloads': [{'r_id': 'd6e2e07485',
  113. 'reading_type': 'Direct Read'}]}}])),
  114. ('oadrDistributeEvent', dict(request_id=generate_id(), response={'request_id': 123, 'response_code': 200, 'response_description': 'OK'}, events=[create_dummy_event(ven_id='123ABC')], vtn_id='VTN123')),
  115. ('oadrPoll', dict(ven_id='123ABC')),
  116. ('oadrQueryRegistration', dict(request_id=generate_id())),
  117. ('oadrRegisteredReport', dict(ven_id='VEN123', response={'response_code': 200, 'response_description': 'OK', 'request_id': generate_id()},
  118. report_requests=[{'report_request_id': generate_id(),
  119. 'report_specifier': {'report_specifier_id': generate_id(),
  120. 'granularity': timedelta(minutes=15),
  121. 'report_back_duration': timedelta(minutes=15),
  122. 'report_interval': {'dtstart': datetime.now(timezone.utc),
  123. 'duration': timedelta(hours=2)},
  124. 'specifier_payloads': [{'r_id': generate_id(),
  125. 'reading_type': 'Direct Read'}]}},
  126. {'report_request_id': generate_id(),
  127. 'report_specifier': {'report_specifier_id': generate_id(),
  128. 'granularity': timedelta(minutes=15),
  129. 'report_back_duration': timedelta(minutes=15),
  130. 'report_interval': {'dtstart': datetime.now(timezone.utc),
  131. 'duration': timedelta(hours=2)},
  132. 'specifier_payloads': [{'r_id': generate_id(),
  133. 'reading_type': 'Direct Read'}]}}])),
  134. ('oadrRequestEvent', dict(request_id=generate_id(), ven_id='123ABC')),
  135. ('oadrRequestReregistration', dict(ven_id='123ABC')),
  136. ('oadrRegisterReport', dict(request_id=generate_id(), reports=[{'report_id': generate_id(),
  137. 'report_descriptions': [{
  138. 'r_id': generate_id(),
  139. 'report_subject': {'resource_id': '123ABC'},
  140. 'report_data_source': {'resource_id': '123ABC'},
  141. 'report_type': 'reading',
  142. 'reading_type': 'Direct Read',
  143. 'market_context': 'http://localhost',
  144. 'sampling_rate': {'min_period': timedelta(minutes=1), 'max_period': timedelta(minutes=1), 'on_change': True}}],
  145. 'report_request_id': generate_id(),
  146. 'report_specifier_id': generate_id(),
  147. 'report_name': 'HISTORY_USAGE',
  148. 'created_date_time': datetime.now(timezone.utc)}],
  149. ven_id='123ABC',
  150. report_request_id=generate_id())),
  151. ('oadrRegisterReport', {'request_id': '8a4f859883', 'reports': [{'report_id': generate_id(),
  152. 'duration': timedelta(seconds=7200),
  153. 'report_descriptions': [{'r_id': generate_id(),
  154. 'report_data_source': {'resource_id': 'resource1'},
  155. 'report_type': 'x-resourceStatus',
  156. 'reading_type': 'x-notApplicable',
  157. 'market_context': 'http://MarketContext1',
  158. 'sampling_rate': {'min_period': timedelta(seconds=60), 'max_period': timedelta(seconds=60), 'on_change': False}}],
  159. 'report_request_id': generate_id(),
  160. 'report_specifier_id': '789ed6cd4e_telemetry_status',
  161. 'report_name': 'METADATA_TELEMETRY_STATUS',
  162. 'created_date_time': datetime(2019, 11, 20, 15, 4, 52, 638621, tzinfo=timezone.utc)},
  163. {'report_id': generate_id(),
  164. 'duration': timedelta(seconds=7200),
  165. 'report_descriptions': [{'r_id': 'resource1_energy',
  166. 'report_data_source': {'resource_id': 'resource1'},
  167. 'report_type': 'usage',
  168. 'measurement': {'name': 'energyReal',
  169. 'description': 'RealEnergy',
  170. 'unit': 'Wh',
  171. 'scale': 'n'},
  172. 'reading_type': 'Direct Read',
  173. 'market_context': 'http://MarketContext1',
  174. 'sampling_rate': {'min_period': timedelta(seconds=60), 'max_period': timedelta(seconds=60), 'on_change': False}},
  175. {'r_id': 'resource1_power',
  176. 'report_data_source': {'resource_id': 'resource1'},
  177. 'report_type': 'usage',
  178. 'measurement': {'name': 'powerReal',
  179. 'description': 'RealPower',
  180. 'unit': 'W',
  181. 'scale': 'n',
  182. 'power_attributes': {'hertz': 50, 'voltage': 230, 'ac': True}},
  183. 'reading_type': 'Direct Read',
  184. 'market_context': 'http://MarketContext1',
  185. 'sampling_rate': {'min_period': timedelta(seconds=60), 'max_period': timedelta(seconds=60), 'on_change': False}}],
  186. 'report_request_id': generate_id(),
  187. 'report_specifier_id': '789ed6cd4e_telemetry_usage',
  188. 'report_name': 'METADATA_TELEMETRY_USAGE',
  189. 'created_date_time': datetime(2019, 11, 20, 15, 4, 52, 638621, tzinfo=timezone.utc)},
  190. {'report_id': generate_id(),
  191. 'duration': timedelta(seconds=7200),
  192. 'report_descriptions': [{'r_id': 'resource1_energy',
  193. 'report_data_source': {'resource_id': 'resource1'},
  194. 'report_type': 'usage',
  195. 'measurement': {'name': 'energyReal',
  196. 'description': 'RealEnergy',
  197. 'unit': 'Wh',
  198. 'scale': 'n'},
  199. 'reading_type': 'Direct Read',
  200. 'market_context': 'http://MarketContext1',
  201. 'sampling_rate': {'min_period': timedelta(seconds=60), 'max_period': timedelta(seconds=60), 'on_change': False}},
  202. {'r_id': 'resource1_power',
  203. 'report_data_source': {'resource_id': 'resource1'},
  204. 'report_type': 'usage',
  205. 'measurement': {'name': 'powerReal',
  206. 'description': 'RealPower',
  207. 'unit': 'W',
  208. 'scale': 'n',
  209. 'power_attributes': {'hertz': 50, 'voltage': 230, 'ac': True}},
  210. 'reading_type': 'Direct Read',
  211. 'market_context': 'http://MarketContext1',
  212. 'sampling_rate': {'min_period': timedelta(seconds=60), 'max_period': timedelta(seconds=60), 'on_change': False}}],
  213. 'report_request_id': generate_id(),
  214. 'report_specifier_id': '789ed6cd4e_history_usage',
  215. 'report_name': 'METADATA_HISTORY_USAGE',
  216. 'created_date_time': datetime(2019, 11, 20, 15, 4, 52, 638621, tzinfo=timezone.utc)}], 'ven_id': 's3cc244ee6'}),
  217. ('oadrResponse', dict(response={'response_code': 200, 'response_description': 'OK', 'request_id': generate_id()}, ven_id='123ABC')),
  218. ('oadrResponse', dict(response={'response_code': 200, 'response_description': 'OK', 'request_id': None}, ven_id='123ABC')),
  219. ('oadrUpdatedReport', dict(response={'response_code': 200, 'response_description': 'OK', 'request_id': generate_id()}, ven_id='123ABC', cancel_report={'request_id': generate_id(), 'report_request_id': [generate_id(), generate_id(), generate_id()], 'report_to_follow': False, 'ven_id': '123ABC'})),
  220. ('oadrUpdateReport', dict(request_id=generate_id(), reports=[{'report_id': generate_id(),
  221. 'report_name': enums.REPORT_NAME.values[0],
  222. 'created_date_time': datetime.now(timezone.utc),
  223. 'report_request_id': generate_id(),
  224. 'report_specifier_id': generate_id(),
  225. 'report_descriptions': [{'r_id': generate_id(),
  226. 'report_subject': {'resource_id': '123ABC'},
  227. 'report_data_source': {'resource_id': '123ABC'},
  228. 'report_type': enums.REPORT_TYPE.values[0],
  229. 'reading_type': enums.READING_TYPE.values[0],
  230. 'market_context': 'http://localhost',
  231. 'sampling_rate': {'min_period': timedelta(minutes=1),
  232. 'max_period': timedelta(minutes=2),
  233. 'on_change': False}}
  234. ]}], ven_id='123ABC'))
  235. ]
  236. @pytest.mark.parametrize('message_type,data', testcases)
  237. def test_message(message_type, data):
  238. message = create_message(message_type, **data)
  239. print(message)
  240. parsed = parse_message(message)[1]
  241. assert parsed == data