test_schema.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. from pyopenadr.utils import generate_id, indent_xml
  2. from pyopenadr.messaging import create_message
  3. from pyopenadr import enums
  4. from lxml import etree
  5. import os
  6. from datetime import datetime, timedelta, timezone
  7. from termcolor import colored
  8. import jinja2
  9. from pprint import pprint
  10. DATE_FORMAT = "%Y-%m-%dT%H:%M:%S.%fZ"
  11. SCHEMA_LOCATION = os.path.join('schema', 'oadr_20b.xsd')
  12. schema_root = etree.parse(SCHEMA_LOCATION)
  13. schema = etree.XMLSchema(schema_root)
  14. parser = etree.XMLParser(schema=schema)
  15. def create_dummy_event(ven_id):
  16. """
  17. Creates a dummy event
  18. """
  19. now = datetime.now(timezone.utc)
  20. event_id = generate_id()
  21. active_period = {"dtstart": now + timedelta(minutes=1),
  22. "duration": timedelta(minutes=9)}
  23. event_descriptor = {"event_id": event_id,
  24. "modification_number": 1,
  25. "modification_date_time": now,
  26. "priority": 1,
  27. "market_context": "http://MarketContext1",
  28. "created_date_time": now,
  29. "event_status": "near",
  30. "test_event": "false",
  31. "vtn_comment": "This is an event"}
  32. event_signals = [{"intervals": [{"duration": timedelta(minutes=1), "uid": 1, "signal_payload": 8},
  33. {"duration": timedelta(minutes=1), "uid": 2, "signal_payload": 10},
  34. {"duration": timedelta(minutes=1), "uid": 3, "signal_payload": 12},
  35. {"duration": timedelta(minutes=1), "uid": 4, "signal_payload": 14},
  36. {"duration": timedelta(minutes=1), "uid": 5, "signal_payload": 16},
  37. {"duration": timedelta(minutes=1), "uid": 6, "signal_payload": 18},
  38. {"duration": timedelta(minutes=1), "uid": 7, "signal_payload": 20},
  39. {"duration": timedelta(minutes=1), "uid": 8, "signal_payload": 10},
  40. {"duration": timedelta(minutes=1), "uid": 9, "signal_payload": 20}],
  41. "signal_name": "LOAD_CONTROL",
  42. #"signal_name": "simple",
  43. #"signal_type": "level",
  44. "signal_type": "x-loadControlCapacity",
  45. "signal_id": generate_id(),
  46. "current_value": 9.99}]
  47. event_targets = [{"ven_id": 'VEN001'}, {"ven_id": 'VEN002'}]
  48. event = {'active_period': active_period,
  49. 'event_descriptor': event_descriptor,
  50. 'event_signals': event_signals,
  51. 'targets': event_targets,
  52. 'response_required': 'always'}
  53. return event
  54. def test_message(msg_type, **payload):
  55. try:
  56. with open(os.path.join('example_messages', f'{msg_type}.xml'), 'w') as file:
  57. message = create_message(msg_type, **payload)
  58. print(indent_xml(message),file=file)
  59. # print(type, file=file)
  60. # print("="*len(type), file=file)
  61. # print("", file=file)
  62. # print("OpenADR payload:", file=file)
  63. # print("..code-block:xml", file=file)
  64. # print("", file=file)
  65. # print(indent_xml(message), file=file)
  66. # print("", file=file)
  67. # print("pyOpenADR representation:", file=file)
  68. # print("..code-block:python3", file=file)
  69. # print("", file=file)
  70. # pprint(payload, stream=file)
  71. etree.fromstring(message.encode('utf-8'), parser)
  72. print(colored(f"pass: {msg_type} OK", "green"))
  73. except etree.XMLSyntaxError as err:
  74. print(colored(f"fail: {msg_type} failed validation: {err}", "red"))
  75. print(message)
  76. except jinja2.exceptions.UndefinedError as err:
  77. print(colored(f"fail: {msg_type} failed message construction: {err}", "yellow"))
  78. test_message('oadrCanceledOpt', response={'response_code': 200, 'response_description': 'OK', 'request_id': generate_id()}, opt_id=generate_id())
  79. test_message('oadrCanceledPartyRegistration', response={'response_code': 200, 'response_description': 'OK', 'request_id': generate_id()}, registration_id=generate_id(), ven_id='123ABC')
  80. test_message('oadrCanceledReport', response={'response_code': 200, 'response_description': 'OK', 'request_id': generate_id()}, pending_reports=[{'request_id': generate_id()}, {'request_id': generate_id()}])
  81. 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')
  82. test_message('oadrCancelOpt', request_id=generate_id(), ven_id='123ABC', opt_id=generate_id())
  83. test_message('oadrCancelPartyRegistration', request_id=generate_id(), ven_id='123ABC', registration_id=generate_id())
  84. test_message('oadrCancelReport', request_id=generate_id(), ven_id='123ABC', report_request_id=generate_id(), report_to_follow=True)
  85. test_message('oadrCreatedEvent', response={'response_code': 200, 'response_description': 'OK', 'request_id': generate_id()},
  86. event_responses=[{'response_code': 200, 'response_description': 'OK', 'request_id': generate_id(), 'event_id': generate_id(), 'modification_number': 1, 'opt_type': 'optIn'},
  87. {'response_code': 200, 'response_description': 'OK', 'request_id': generate_id(), 'event_id': generate_id(), 'modification_number': 1, 'opt_type': 'optIn'},
  88. {'response_code': 200, 'response_description': 'OK', 'request_id': generate_id(), 'event_id': generate_id(), 'modification_number': 1, 'opt_type': 'optIn'}],
  89. ven_id='123ABC')
  90. 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')
  91. test_message('oadrCreatedEvent', response={'response_code': 200, 'response_description': 'OK', 'request_id': generate_id()},
  92. event_responses=[{'response_code': 200, 'response_description': 'OK', 'request_id': generate_id(),
  93. 'event_id': generate_id(),
  94. 'modification_number': 1,
  95. 'opt_type': 'optIn'},
  96. {'response_code': 200, 'response_description': 'OK', 'request_id': generate_id(),
  97. 'event_id': generate_id(),
  98. 'modification_number': 1,
  99. 'opt_type': 'optOut'}],
  100. ven_id='123ABC')
  101. test_message('oadrCreatedPartyRegistration', response={'response_code': 200, 'response_description': 'OK', 'request_id': generate_id()},
  102. registration_id=generate_id(),
  103. ven_id='123ABC',
  104. profiles=[{'profile_name': '2.0b',
  105. 'transports': [{'transport_name': 'simpleHttp'}]}],
  106. vtn_id='VTN123')
  107. test_message('oadrCreatedReport', response={'response_code': 200, 'response_description': 'OK', 'request_id': generate_id()}, pending_reports=[{'request_id': generate_id()}, {'request_id': generate_id()}])
  108. test_message('oadrCreateOpt', opt_id=generate_id(),
  109. opt_type='optIn',
  110. opt_reason='participating',
  111. created_date_time=datetime.now(timezone.utc),
  112. request_id=generate_id(),
  113. event_id=generate_id(),
  114. modification_number=1,
  115. targets=[{'ven_id': '123ABC'}],
  116. ven_id='VEN123')
  117. 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)
  118. test_message('oadrCreateReport', request_id=generate_id(),
  119. ven_id='123ABC',
  120. report_requests=[{'report_request_id': 'd2b7bade5f',
  121. 'report_specifier': {'granularity': timedelta(seconds=900),
  122. 'report_back_duration': timedelta(seconds=900),
  123. 'report_interval': {'dtstart': datetime(2019, 11, 19, 11, 0, 18, 672768, tzinfo=timezone.utc),
  124. 'duration': timedelta(seconds=7200),
  125. 'tolerance': {'tolerate': {'startafter': timedelta(seconds=300)}}},
  126. 'report_specifier_id': '9c8bdc00e7',
  127. 'specifier_payload': {'r_id': 'd6e2e07485',
  128. 'reading_type': 'Direct Read'}}}])
  129. 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')
  130. test_message('oadrPoll', ven_id='123ABC')
  131. test_message('oadrQueryRegistration', request_id=generate_id())
  132. test_message('oadrRegisteredReport', ven_id='VEN123', response={'response_code': 200, 'response_description': 'OK', 'request_id': generate_id()},
  133. report_requests=[{'report_request_id': generate_id(),
  134. 'report_specifier': {'report_specifier_id': generate_id(),
  135. 'granularity': timedelta(minutes=15),
  136. 'report_back_duration': timedelta(minutes=15),
  137. 'report_interval': {'dtstart': datetime.now(timezone.utc),
  138. 'duration': timedelta(hours=2),
  139. 'tolerance': {'tolerate': {'startafter': timedelta(minutes=5)}},
  140. 'notification': timedelta(minutes=30),
  141. 'ramp_up': timedelta(minutes=15),
  142. 'recovery': timedelta(minutes=5)},
  143. 'specifier_payload': {'r_id': generate_id(),
  144. 'reading_type': 'Direct Read'}}},
  145. {'report_request_id': generate_id(),
  146. 'report_specifier': {'report_specifier_id': generate_id(),
  147. 'granularity': timedelta(minutes=15),
  148. 'report_back_duration': timedelta(minutes=15),
  149. 'report_interval': {'dtstart': datetime.now(timezone.utc),
  150. 'duration': timedelta(hours=2),
  151. 'tolerance': {'tolerate': {'startafter': timedelta(minutes=5)}},
  152. 'notification': timedelta(minutes=30),
  153. 'ramp_up': timedelta(minutes=15),
  154. 'recovery': timedelta(minutes=5)},
  155. 'specifier_payload': {'r_id': generate_id(),
  156. 'reading_type': 'Direct Read'}}}])
  157. test_message('oadrRequestEvent', request_id=generate_id(), ven_id='123ABC')
  158. test_message('oadrRequestReregistration', ven_id='123ABC')
  159. test_message('oadrRegisterReport', request_id=generate_id(), reports=[{'report_id': generate_id(),
  160. 'report_descriptions': [{
  161. 'r_id': generate_id(),
  162. 'report_subjects': {'ven_id': '123ABC'},
  163. 'report_data_sources': {'ven_id': '123ABC'},
  164. 'report_type': 'reading',
  165. 'reading_type': 'Direct Read',
  166. 'market_context': 'http://localhost',
  167. 'sampling_rate': {'min_period': timedelta(minutes=1), 'max_period': timedelta(minutes=1), 'on_change': True}}],
  168. 'report_request_id': generate_id(),
  169. 'report_specifier_id': generate_id(),
  170. 'report_name': 'HISTORY_USAGE',
  171. 'created_date_time': datetime.now(timezone.utc)}],
  172. ven_id='123ABC',
  173. report_request_id=generate_id())
  174. test_message('oadrRegisterReport', **{'request_id': '8a4f859883', 'reports': [{'duration': timedelta(seconds=7200), 'report_descriptions': [{'r_id': 'resource1_status', 'report_data_sources': [{'resource_id': 'resource1'}], 'report_type': 'x-resourceStatus', 'reading_type': 'x-notApplicable', 'market_context': 'http://MarketContext1', 'sampling_rate': {'min_period': timedelta(seconds=60), 'max_period': timedelta(seconds=60), 'on_change': False}}], 'report_request_id': '0', 'report_specifier_id': '789ed6cd4e_telemetry_status', 'report_name': 'METADATA_TELEMETRY_STATUS', 'created_date_time': datetime(2019, 11, 20, 15, 4, 52, 638621, tzinfo=timezone.utc)}, {'duration': timedelta(seconds=7200), 'report_descriptions': [{'r_id': 'resource1_energy', 'report_data_sources': [{'resource_id': 'resource1'}], 'report_type': 'usage', 'energy_real': {'item_description': 'RealEnergy', 'item_units': 'Wh', 'si_scale_code': 'n'}, 'reading_type': 'Direct Read', 'market_context': 'http://MarketContext1', 'sampling_rate': {'min_period': timedelta(seconds=60), 'max_period': timedelta(seconds=60), 'on_change': False}}, {'r_id': 'resource1_power', 'report_data_sources': [{'resource_id': 'resource1'}], 'report_type': 'usage', 'power_real': {'item_description': 'RealPower', 'item_units': 'W', 'si_scale_code': 'n', 'power_attributes': {'hertz': 60, 'voltage': 110, 'ac': False}}, 'reading_type': 'Direct Read', 'market_context': 'http://MarketContext1', 'sampling_rate': {'min_period': timedelta(seconds=60), 'max_period': timedelta(seconds=60), 'on_change': False}}], 'report_request_id': '0', 'report_specifier_id': '789ed6cd4e_telemetry_usage', 'report_name': 'METADATA_TELEMETRY_USAGE', 'created_date_time': datetime(2019, 11, 20, 15, 4, 52, 638621, tzinfo=timezone.utc)}, {'duration': timedelta(seconds=7200), 'report_descriptions': [{'r_id': 'resource1_energy', 'report_data_sources': [{'resource_id': 'resource1'}], 'report_type': 'usage', 'energy_real': {'item_description': 'RealEnergy', 'item_units': 'Wh', 'si_scale_code': 'n'}, 'reading_type': 'Direct Read', 'market_context': 'http://MarketContext1', 'sampling_rate': {'min_period': timedelta(seconds=60), 'max_period': timedelta(seconds=60), 'on_change': False}}, {'r_id': 'resource1_power', 'report_data_sources': [{'resource_id': 'resource1'}], 'report_type': 'usage', 'power_real': {'item_description': 'RealPower', 'item_units': 'W', 'si_scale_code': 'n', 'power_attributes': {'hertz': 60, 'voltage': 110, 'ac': False}}, 'reading_type': 'Direct Read', 'market_context': 'http://MarketContext1', 'sampling_rate': {'min_period': timedelta(seconds=60), 'max_period': timedelta(seconds=60), 'on_change': False}}], 'report_request_id': '0', 'report_specifier_id': '789ed6cd4e_history_usage', 'report_name': 'METADATA_HISTORY_USAGE', 'created_date_time': datetime(2019, 11, 20, 15, 4, 52, 638621, tzinfo=timezone.utc)}], 'ven_id': 's3cc244ee6'})
  175. test_message('oadrResponse', response={'response_code': 200, 'response_description': 'OK', 'request_id': generate_id()}, ven_id='123ABC')
  176. test_message('oadrResponse', response={'response_code': 200, 'response_description': 'OK', 'request_id': None}, ven_id='123ABC')
  177. 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'})
  178. test_message('oadrUpdateReport', request_id=generate_id(), reports=[{'report_id': generate_id(),
  179. 'report_name': enums.REPORT_NAME.values[0],
  180. 'created_date_time': datetime.now(timezone.utc),
  181. 'report_request_id': generate_id(),
  182. 'report_specifier_id': generate_id(),
  183. 'report_descriptions': [{'r_id': generate_id(),
  184. 'report_subjects': [{'ven_id': '123ABC'}, {'ven_id': 'DEF456'}],
  185. 'report_data_sources': [{'ven_id': '123ABC'}],
  186. 'report_type': enums.REPORT_TYPE.values[0],
  187. 'reading_type': enums.READING_TYPE.values[0],
  188. 'market_context': 'http://localhost',
  189. 'sampling_rate': {'min_period': timedelta(minutes=1),
  190. 'max_period': timedelta(minutes=2),
  191. 'on_change': False}}]}], ven_id='123ABC')
  192. # for report_name in enums.REPORT_NAME.values:
  193. # for reading_type in enums.READING_TYPE.values:
  194. # for report_type in enums.REPORT_TYPE.values:
  195. # test_message('oadrUpdateReport', request_id=generate_id(), reports=[{'report_id': generate_id(),
  196. # 'report_name': report_name,
  197. # 'created_date_time': datetime.now(timezone.utc),
  198. # 'report_request_id': generate_id(),
  199. # 'report_specifier_id': generate_id(),
  200. # 'report_descriptions': [{'r_id': generate_id(),
  201. # 'report_subjects': [{'ven_id': '123ABC'}, {'ven_id': 'DEF456'}],
  202. # 'report_data_sources': [{'ven_id': '123ABC'}],
  203. # 'report_type': report_type,
  204. # 'reading_type': reading_type,
  205. # 'market_context': 'http://localhost',
  206. # 'sampling_rate': {'min_period': timedelta(minutes=1),
  207. # 'max_period': timedelta(minutes=2),
  208. # 'on_change': False}}]}], ven_id='123ABC')