test_message_conversion.py 27 KB

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