test_message_conversion.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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
  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. DATE_FORMAT = "%Y-%m-%dT%H:%M:%S.%fZ"
  19. def test_messages():
  20. for message_type, data in testcases:
  21. _test_message(message_type, **data)
  22. def _test_message(message_type, **data):
  23. message = create_message(message_type, **data)
  24. # print(message)
  25. parsed = parse_message(message)[1]
  26. if parsed == data:
  27. print(colored(f"pass {message_type}", "green"))
  28. else:
  29. pprint(data)
  30. print(message)
  31. pprint(parsed)
  32. print(colored(f"fail {message_type}", "red"))
  33. quit(1)
  34. def create_dummy_event(ven_id):
  35. """
  36. Creates a dummy event
  37. """
  38. now = datetime.now(timezone.utc)
  39. event_id = generate_id()
  40. active_period = {"dtstart": (now + timedelta(minutes=1)),
  41. "duration": timedelta(minutes=9)}
  42. event_descriptor = {"event_id": event_id,
  43. "modification_number": 1,
  44. "modification_date_time": now,
  45. "priority": 1,
  46. "market_context": "http://MarketContext1",
  47. "created_date_time": now,
  48. "event_status": "near",
  49. "test_event": False,
  50. "vtn_comment": "This is an event"}
  51. event_signals = [{"intervals": [{"duration": timedelta(minutes=1), "uid": 0, "signal_payload": 8.0},
  52. {"duration": timedelta(minutes=1), "uid": 1, "signal_payload": 10.0},
  53. {"duration": timedelta(minutes=1), "uid": 2, "signal_payload": 12.0},
  54. {"duration": timedelta(minutes=1), "uid": 3, "signal_payload": 14.0},
  55. {"duration": timedelta(minutes=1), "uid": 4, "signal_payload": 16.0},
  56. {"duration": timedelta(minutes=1), "uid": 5, "signal_payload": 18.0},
  57. {"duration": timedelta(minutes=1), "uid": 6, "signal_payload": 20.0},
  58. {"duration": timedelta(minutes=1), "uid": 7, "signal_payload": 10.0},
  59. {"duration": timedelta(minutes=1), "uid": 8, "signal_payload": 20.0}],
  60. "signal_name": "LOAD_CONTROL",
  61. #"signal_name": "simple",
  62. #"signal_type": "level",
  63. "signal_type": "x-loadControlCapacity",
  64. "signal_id": generate_id(),
  65. "current_value": 0.0}]
  66. event_targets = [{"ven_id": 'VEN001'}, {"ven_id": 'VEN002'}]
  67. event = {'active_period': active_period,
  68. 'event_descriptor': event_descriptor,
  69. 'event_signals': event_signals,
  70. 'targets': event_targets,
  71. 'response_required': 'always'}
  72. return event
  73. testcases = [
  74. ('oadrCanceledOpt', dict(response={'response_code': 200, 'response_description': 'OK', 'request_id': generate_id()}, opt_id=generate_id())),
  75. ('oadrCanceledPartyRegistration', dict(response={'response_code': 200, 'response_description': 'OK', 'request_id': generate_id()}, registration_id=generate_id(), ven_id='123ABC')),
  76. ('oadrCanceledReport', dict(response={'response_code': 200, 'response_description': 'OK', 'request_id': generate_id()}, pending_reports=[{'request_id': generate_id()}, {'request_id': generate_id()}])),
  77. ('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')),
  78. ('oadrCancelOpt', dict(request_id=generate_id(), ven_id='123ABC', opt_id=generate_id())),
  79. ('oadrCancelPartyRegistration', dict(request_id=generate_id(), ven_id='123ABC', registration_id=generate_id())),
  80. ('oadrCancelReport', dict(request_id=generate_id(), ven_id='123ABC', report_request_id=generate_id(), report_to_follow=True)),
  81. ('oadrCreatedEvent', dict(response={'response_code': 200, 'response_description': 'OK', 'request_id': generate_id()},
  82. event_responses=[{'response_code': 200, 'response_description': 'OK', 'request_id': generate_id(), 'event_id': generate_id(), 'modification_number': 1, 'opt_type': 'optIn'},
  83. {'response_code': 200, 'response_description': 'OK', 'request_id': generate_id(), 'event_id': generate_id(), 'modification_number': 1, 'opt_type': 'optIn'},
  84. {'response_code': 200, 'response_description': 'OK', 'request_id': generate_id(), 'event_id': generate_id(), 'modification_number': 1, 'opt_type': 'optIn'}],
  85. ven_id='123ABC')),
  86. ('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')),
  87. ('oadrCreatedEvent', dict(response={'response_code': 200, 'response_description': 'OK', 'request_id': generate_id()},
  88. event_responses=[{'response_code': 200, 'response_description': 'OK', 'request_id': generate_id(),
  89. 'event_id': generate_id(),
  90. 'modification_number': 1,
  91. 'opt_type': 'optIn'},
  92. {'response_code': 200, 'response_description': 'OK', 'request_id': generate_id(),
  93. 'event_id': generate_id(),
  94. 'modification_number': 1,
  95. 'opt_type': 'optOut'}],
  96. ven_id='123ABC')),
  97. ('oadrCreatedPartyRegistration', dict(response={'response_code': 200, 'response_description': 'OK', 'request_id': generate_id()},
  98. registration_id=generate_id(),
  99. ven_id='123ABC',
  100. profiles=[{'profile_name': '2.0b',
  101. 'transports': [{'transport_name': 'simpleHttp'}]}],
  102. vtn_id='VTN123')),
  103. ('oadrCreatedReport', dict(response={'response_code': 200, 'response_description': 'OK', 'request_id': generate_id()}, pending_reports=[{'request_id': generate_id()}, {'request_id': generate_id()}])),
  104. ('oadrCreateOpt', dict(opt_id=generate_id(),
  105. opt_type='optIn',
  106. opt_reason='participating',
  107. created_date_time=datetime.now(timezone.utc),
  108. request_id=generate_id(),
  109. event_id=generate_id(),
  110. modification_number=1,
  111. targets=[{'ven_id': '123ABC'}],
  112. ven_id='VEN123')),
  113. ('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)),
  114. ('oadrCreateReport', dict(request_id=generate_id(),
  115. ven_id='123ABC',
  116. report_requests=[{'report_request_id': 'd2b7bade5f',
  117. 'report_specifier': {'granularity': timedelta(seconds=900),
  118. 'report_back_duration': timedelta(seconds=900),
  119. 'report_interval': {'dtstart': datetime(2019, 11, 19, 11, 0, 18, 672768, tzinfo=timezone.utc),
  120. 'duration': timedelta(seconds=7200),
  121. 'tolerance': {'tolerate': {'startafter': timedelta(seconds=300)}}},
  122. 'report_specifier_id': '9c8bdc00e7',
  123. 'specifier_payload': {'r_id': 'd6e2e07485',
  124. 'reading_type': 'Direct Read'}}}])),
  125. ('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')),
  126. ('oadrPoll', dict(ven_id='123ABC')),
  127. ('oadrQueryRegistration', dict(request_id=generate_id())),
  128. ('oadrRegisteredReport', dict(ven_id='VEN123', response={'response_code': 200, 'response_description': 'OK', 'request_id': generate_id()},
  129. report_requests=[{'report_request_id': generate_id(),
  130. 'report_specifier': {'report_specifier_id': generate_id(),
  131. 'granularity': timedelta(minutes=15),
  132. 'report_back_duration': timedelta(minutes=15),
  133. 'report_interval': {'dtstart': datetime.now(timezone.utc),
  134. 'duration': timedelta(hours=2),
  135. 'tolerance': {'tolerate': {'startafter': timedelta(minutes=5)}},
  136. 'notification': timedelta(minutes=30),
  137. 'ramp_up': timedelta(minutes=15),
  138. 'recovery': timedelta(minutes=5)},
  139. 'specifier_payload': {'r_id': generate_id(),
  140. 'reading_type': 'Direct Read'}}},
  141. {'report_request_id': generate_id(),
  142. 'report_specifier': {'report_specifier_id': generate_id(),
  143. 'granularity': timedelta(minutes=15),
  144. 'report_back_duration': timedelta(minutes=15),
  145. 'report_interval': {'dtstart': datetime.now(timezone.utc),
  146. 'duration': timedelta(hours=2),
  147. 'tolerance': {'tolerate': {'startafter': timedelta(minutes=5)}},
  148. 'notification': timedelta(minutes=30),
  149. 'ramp_up': timedelta(minutes=15),
  150. 'recovery': timedelta(minutes=5)},
  151. 'specifier_payload': {'r_id': generate_id(),
  152. 'reading_type': 'Direct Read'}}}])),
  153. ('oadrRequestEvent', dict(request_id=generate_id(), ven_id='123ABC')),
  154. ('oadrRequestReregistration', dict(ven_id='123ABC')),
  155. ('oadrRegisterReport', dict(request_id=generate_id(), reports=[{'report_id': generate_id(),
  156. 'report_descriptions': {
  157. generate_id(): {
  158. 'report_subjects': [{'ven_id': '123ABC'}],
  159. 'report_data_sources': [{'ven_id': '123ABC'}],
  160. 'report_type': 'reading',
  161. 'reading_type': 'Direct Read',
  162. 'market_context': 'http://localhost',
  163. 'sampling_rate': {'min_period': timedelta(minutes=1), 'max_period': timedelta(minutes=1), 'on_change': True}}},
  164. 'report_request_id': generate_id(),
  165. 'report_specifier_id': generate_id(),
  166. 'report_name': 'HISTORY_USAGE',
  167. 'created_date_time': datetime.now(timezone.utc)}],
  168. ven_id='123ABC',
  169. report_request_id=generate_id())),
  170. ('oadrRegisterReport', {'request_id': '8a4f859883', 'reports': [{'report_id': generate_id(),
  171. 'duration': timedelta(seconds=7200),
  172. 'report_descriptions': {'resource1_status': {
  173. 'report_data_sources': [{'resource_id': 'resource1'}],
  174. 'report_type': 'x-resourceStatus',
  175. 'reading_type': 'x-notApplicable',
  176. 'market_context': 'http://MarketContext1',
  177. 'sampling_rate': {'min_period': timedelta(seconds=60), 'max_period': timedelta(seconds=60), 'on_change': False}}},
  178. 'report_request_id': '0',
  179. 'report_specifier_id': '789ed6cd4e_telemetry_status',
  180. 'report_name': 'METADATA_TELEMETRY_STATUS',
  181. 'created_date_time': datetime(2019, 11, 20, 15, 4, 52, 638621, tzinfo=timezone.utc)},
  182. {'report_id': generate_id(),
  183. 'duration': timedelta(seconds=7200),
  184. 'report_descriptions': {'resource1_energy': {
  185. 'report_data_sources': [{'resource_id': 'resource1'}],
  186. 'report_type': 'usage',
  187. 'energy_real': {'item_description': 'RealEnergy',
  188. 'item_units': 'Wh',
  189. 'si_scale_code': 'n'},
  190. 'reading_type': 'Direct Read',
  191. 'market_context': 'http://MarketContext1',
  192. 'sampling_rate': {'min_period': timedelta(seconds=60), 'max_period': timedelta(seconds=60), 'on_change': False}},
  193. 'resource1_power': {
  194. 'report_data_sources': [{'resource_id': 'resource1'}],
  195. 'report_type': 'usage',
  196. 'power_real': {'item_description': 'RealPower',
  197. 'item_units': 'W',
  198. 'si_scale_code': 'n',
  199. 'power_attributes': {'hertz': 60, 'voltage': 110, 'ac': False}},
  200. 'reading_type': 'Direct Read',
  201. 'market_context': 'http://MarketContext1',
  202. 'sampling_rate': {'min_period': timedelta(seconds=60), 'max_period': timedelta(seconds=60), 'on_change': False}}},
  203. 'report_request_id': '0',
  204. 'report_specifier_id': '789ed6cd4e_telemetry_usage',
  205. 'report_name': 'METADATA_TELEMETRY_USAGE',
  206. 'created_date_time': datetime(2019, 11, 20, 15, 4, 52, 638621, tzinfo=timezone.utc)},
  207. {'report_id': generate_id(),
  208. 'duration': timedelta(seconds=7200),
  209. 'report_descriptions': {'resource1_energy': {
  210. 'report_data_sources': [{'resource_id': 'resource1'}],
  211. 'report_type': 'usage',
  212. 'energy_real': {'item_description': 'RealEnergy',
  213. 'item_units': 'Wh',
  214. 'si_scale_code': 'n'},
  215. 'reading_type': 'Direct Read',
  216. 'market_context': 'http://MarketContext1',
  217. 'sampling_rate': {'min_period': timedelta(seconds=60), 'max_period': timedelta(seconds=60), 'on_change': False}},
  218. 'resource1_power': {
  219. 'report_data_sources': [{'resource_id': 'resource1'}],
  220. 'report_type': 'usage',
  221. 'power_real': {'item_description': 'RealPower',
  222. 'item_units': 'W', 'si_scale_code': 'n',
  223. 'power_attributes': {'hertz': 60, 'voltage': 110, 'ac': False}},
  224. 'reading_type': 'Direct Read',
  225. 'market_context': 'http://MarketContext1',
  226. 'sampling_rate': {'min_period': timedelta(seconds=60), 'max_period': timedelta(seconds=60), 'on_change': False}}},
  227. 'report_request_id': '0',
  228. 'report_specifier_id': '789ed6cd4e_history_usage',
  229. 'report_name': 'METADATA_HISTORY_USAGE',
  230. 'created_date_time': datetime(2019, 11, 20, 15, 4, 52, 638621, tzinfo=timezone.utc)}], 'ven_id': 's3cc244ee6'}),
  231. ('oadrResponse', dict(response={'response_code': 200, 'response_description': 'OK', 'request_id': generate_id()}, ven_id='123ABC')),
  232. ('oadrResponse', dict(response={'response_code': 200, 'response_description': 'OK', 'request_id': None}, ven_id='123ABC')),
  233. ('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'})),
  234. ('oadrUpdateReport', dict(request_id=generate_id(), reports=[{'report_id': generate_id(),
  235. 'report_name': enums.REPORT_NAME.values[0],
  236. 'created_date_time': datetime.now(timezone.utc),
  237. 'report_request_id': generate_id(),
  238. 'report_specifier_id': generate_id(),
  239. 'report_descriptions': {generate_id(): {'report_subjects': [{'ven_id': '123ABC'}, {'ven_id': 'DEF456'}],
  240. 'report_data_sources': [{'ven_id': '123ABC'}],
  241. 'report_type': enums.REPORT_TYPE.values[0],
  242. 'reading_type': enums.READING_TYPE.values[0],
  243. 'market_context': 'http://localhost',
  244. 'sampling_rate': {'min_period': timedelta(minutes=1),
  245. 'max_period': timedelta(minutes=2),
  246. 'on_change': False}}}}], ven_id='123ABC'))
  247. # for report_name in enums.REPORT_NAME.values:
  248. # for reading_type in enums.READING_TYPE.values:
  249. # for report_type in enums.REPORT_TYPE.values:
  250. # ('oadrUpdateReport', dict(request_id=generate_id(), reports=[{'report_id': generate_id()),
  251. # 'report_name': report_name,
  252. # 'created_date_time': datetime.now(timezone.utc),
  253. # 'report_request_id': generate_id(),
  254. # 'report_specifier_id': generate_id(),
  255. # 'report_descriptions': [{'r_id': generate_id(),
  256. # 'report_subjects': [{'ven_id': '123ABC'}, {'ven_id': 'DEF456'}],
  257. # 'report_data_sources': [{'ven_id': '123ABC'}],
  258. # 'report_type': report_type,
  259. # 'reading_type': reading_type,
  260. # 'market_context': 'http://localhost',
  261. # 'sampling_rate': {'min_period': timedelta(minutes=1),
  262. # 'max_period': timedelta(minutes=2),
  263. # 'on_change': False}}]}], ven_id='123ABC')
  264. ]