test_schema.py 25 KB

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