utils.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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 asyncio import iscoroutine
  13. from datetime import datetime, timedelta, timezone
  14. import random
  15. import string
  16. from collections import OrderedDict
  17. import itertools
  18. import re
  19. import ssl
  20. import hashlib
  21. import uuid
  22. from openleadr import config
  23. DATETIME_FORMAT = "%Y-%m-%dT%H:%M:%S.%fZ"
  24. DATETIME_FORMAT_NO_MICROSECONDS = "%Y-%m-%dT%H:%M:%SZ"
  25. def generate_id(*args, **kwargs):
  26. """
  27. Generate a string that can be used as an identifier in OpenADR messages.
  28. """
  29. return str(uuid.uuid4())
  30. def indent_xml(message):
  31. """
  32. Indents the XML in a nice way.
  33. """
  34. INDENT_SIZE = 2
  35. lines = [line.strip() for line in message.split("\n") if line.strip() != ""]
  36. indent = 0
  37. for i, line in enumerate(lines):
  38. if i == 0:
  39. continue
  40. if re.search(r'^</[^>]+>$', line):
  41. indent = indent - INDENT_SIZE
  42. lines[i] = " " * indent + line
  43. if not (re.search(r'</[^>]+>$', line) or line.endswith("/>")):
  44. indent = indent + INDENT_SIZE
  45. return "\n".join(lines)
  46. def flatten_xml(message):
  47. """
  48. Flatten the entire XML structure.
  49. """
  50. lines = [line.strip() for line in message.split("\n") if line.strip() != ""]
  51. for line in lines:
  52. line = re.sub(r'\n', '', line)
  53. line = re.sub(r'\s\s+', ' ', line)
  54. return "".join(lines)
  55. def normalize_dict(ordered_dict):
  56. """
  57. Main conversion function for the output of xmltodict to the OpenLEADR
  58. representation of OpenADR contents.
  59. :param ordered_dict dict: The OrderedDict, dict or dataclass that you wish to convert.
  60. """
  61. if is_dataclass(ordered_dict):
  62. ordered_dict = asdict(ordered_dict)
  63. def normalize_key(key):
  64. if key.startswith('oadr'):
  65. key = key[4:]
  66. elif key.startswith('ei'):
  67. key = key[2:]
  68. key = re.sub(r'([a-z])([A-Z])', r'\1_\2', key)
  69. if '-' in key:
  70. key = key.replace('-', '_')
  71. return key.lower()
  72. d = {}
  73. for key, value in ordered_dict.items():
  74. # Interpret values from the dict
  75. if key.startswith("@"):
  76. continue
  77. key = normalize_key(key)
  78. if isinstance(value, (OrderedDict, dict)):
  79. d[key] = normalize_dict(value)
  80. elif isinstance(value, list):
  81. d[key] = []
  82. for item in value:
  83. if isinstance(item, (OrderedDict, dict)):
  84. dict_item = normalize_dict(item)
  85. d[key].append(normalize_dict(dict_item))
  86. else:
  87. d[key].append(item)
  88. elif key in ("duration", "startafter", "max_period", "min_period"):
  89. d[key] = parse_duration(value)
  90. elif ("date_time" in key or key == "dtstart") and isinstance(value, str):
  91. d[key] = parse_datetime(value)
  92. elif value in ('true', 'false'):
  93. d[key] = parse_boolean(value)
  94. elif isinstance(value, str):
  95. d[key] = parse_int(value) or parse_float(value) or value
  96. else:
  97. d[key] = value
  98. # Do our best to make the dictionary structure as pythonic as possible
  99. if key.startswith("x_ei_"):
  100. d[key[5:]] = d.pop(key)
  101. key = key[5:]
  102. # Group all targets as a list of dicts under the key "target"
  103. if key in ("target", "report_subject", "report_data_source"):
  104. targets = d.pop(key)
  105. new_targets = []
  106. if targets:
  107. for ikey in targets:
  108. if isinstance(targets[ikey], list):
  109. new_targets.extend([{ikey: value} for value in targets[ikey]])
  110. else:
  111. new_targets.append({ikey: targets[ikey]})
  112. d[key + "s"] = new_targets
  113. key = key + "s"
  114. # Dig up the properties inside some specific target identifiers
  115. # if key in ("aggregated_pnode", "pnode", "service_delivery_point"):
  116. # d[key] = d[key]["node"]
  117. # if key in ("end_device_asset", "meter_asset"):
  118. # d[key] = d[key]["mrid"]
  119. # Group all reports as a list of dicts under the key "pending_reports"
  120. if key == "pending_reports":
  121. if isinstance(d[key], dict) and 'report_request_id' in d[key] and isinstance(d[key]['report_request_id'], list):
  122. d['pending_reports'] = [{'request_id': rrid} for rrid in d['pending_reports']['report_request_id']]
  123. # Group all events al a list of dicts under the key "events"
  124. elif key == "event" and isinstance(d[key], list):
  125. events = d.pop("event")
  126. new_events = []
  127. for event in events:
  128. new_event = event['event']
  129. new_event['response_required'] = event['response_required']
  130. new_events.append(new_event)
  131. d["events"] = new_events
  132. # If there's only one event, also put it into a list
  133. elif key == "event" and isinstance(d[key], dict) and "event" in d[key]:
  134. oadr_event = d.pop('event')
  135. ei_event = oadr_event['event']
  136. ei_event['response_required'] = oadr_event['response_required']
  137. d['events'] = [ei_event]
  138. elif key in ("request_event", "created_event") and isinstance(d[key], dict):
  139. d = d[key]
  140. # Plurarize some lists
  141. elif key in ('report_request', 'report'):
  142. if isinstance(d[key], list):
  143. d[key + 's'] = d.pop(key)
  144. else:
  145. d[key + 's'] = [d.pop(key)]
  146. elif key == 'report_description':
  147. if isinstance(d[key], list):
  148. original_descriptions = d.pop(key)
  149. report_descriptions = {}
  150. for item in original_descriptions:
  151. r_id = item.pop('r_id')
  152. report_descriptions[r_id] = item
  153. d[key + 's'] = report_descriptions
  154. else:
  155. original_description = d.pop(key)
  156. r_id = original_description.pop('r_id')
  157. d[key + 's'] = {r_id: original_description}
  158. # Promote the contents of the Qualified Event ID
  159. elif key == "qualified_event_id" and isinstance(d['qualified_event_id'], dict):
  160. qeid = d.pop('qualified_event_id')
  161. d['event_id'] = qeid['event_id']
  162. d['modification_number'] = qeid['modification_number']
  163. # Promote the contents of the tolerance items
  164. # if key == "tolerance" and "tolerate" in d["tolerance"] and len(d["tolerance"]["tolerate"]) == 1:
  165. # d["tolerance"] = d["tolerance"]["tolerate"].values()[0]
  166. # Durations are encapsulated in their own object, remove this nesting
  167. elif isinstance(d[key], dict) and "duration" in d[key] and len(d[key]) == 1:
  168. d[key] = d[key]["duration"]
  169. # In general, remove all double nesting
  170. elif isinstance(d[key], dict) and key in d[key] and len(d[key]) == 1:
  171. d[key] = d[key][key]
  172. # In general, remove the double nesting of lists of items
  173. elif isinstance(d[key], dict) and key[:-1] in d[key] and len(d[key]) == 1:
  174. if isinstance(d[key][key[:-1]], list):
  175. d[key] = d[key][key[:-1]]
  176. else:
  177. d[key] = [d[key][key[:-1]]]
  178. # Payload values are wrapped in an object according to their type. We don't need that information.
  179. elif key in ("signal_payload", "current_value"):
  180. value = d[key]
  181. if isinstance(d[key], dict):
  182. if 'payload_float' in d[key] and 'value' in d[key]['payload_float'] and d[key]['payload_float']['value'] is not None:
  183. d[key] = float(d[key]['payload_float']['value'])
  184. elif 'payload_int' in d[key] and 'value' in d[key]['payload_int'] and d[key]['payload_int'] is not None:
  185. d[key] = int(d[key]['payload_int']['value'])
  186. # All values other than 'false' must be interpreted as True for testEvent (rule 006)
  187. elif key == 'test_event' and not isinstance(d[key], bool):
  188. d[key] = True
  189. # Promote the 'text' item
  190. elif isinstance(d[key], dict) and "text" in d[key] and len(d[key]) == 1:
  191. if key == 'uid':
  192. d[key] = int(d[key]["text"])
  193. else:
  194. d[key] = d[key]["text"]
  195. # Promote a 'date-time' item
  196. elif isinstance(d[key], dict) and "date_time" in d[key] and len(d[key]) == 1:
  197. d[key] = d[key]["date_time"]
  198. # Promote 'properties' item, discard the unused? 'components' item
  199. elif isinstance(d[key], dict) and "properties" in d[key] and len(d[key]) <= 2:
  200. d[key] = d[key]["properties"]
  201. # Remove all empty dicts
  202. elif isinstance(d[key], dict) and len(d[key]) == 0:
  203. d.pop(key)
  204. return d
  205. def parse_datetime(value):
  206. """
  207. Parse an ISO8601 datetime into a datetime.datetime object.
  208. """
  209. matches = re.match(r'(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})\.?(\d{1,6})?\d*Z', value)
  210. if matches:
  211. year, month, day, hour, minute, second, microsecond = (int(value) for value in matches.groups())
  212. return datetime(year, month, day, hour, minute, second, microsecond=microsecond, tzinfo=timezone.utc)
  213. else:
  214. print(f"{value} did not match format")
  215. return value
  216. def parse_duration(value):
  217. """
  218. Parse a RFC5545 duration.
  219. """
  220. # TODO: implement the full regex: matches = re.match(r'(\+|\-)?P((\d+Y)?(\d+M)?(\d+D)?T?(\d+H)?(\d+M)?(\d+S)?)|(\d+W)', value)
  221. if isinstance(value, timedelta):
  222. return value
  223. matches = re.match(r'P(\d+(?:D|W))?T(\d+H)?(\d+M)?(\d+S)?', value)
  224. if not matches:
  225. return False
  226. days = hours = minutes = seconds = 0
  227. _days, _hours, _minutes, _seconds = matches.groups()
  228. if _days:
  229. if _days.endswith("D"):
  230. days = int(_days[:-1])
  231. elif _days.endswith("W"):
  232. days = int(_days[:-1]) * 7
  233. if _hours:
  234. hours = int(_hours[:-1])
  235. if _minutes:
  236. minutes = int(_minutes[:-1])
  237. if _seconds:
  238. seconds = int(_seconds[:-1])
  239. return timedelta(days=days, hours=hours, minutes=minutes, seconds=seconds)
  240. def parse_int(value):
  241. matches = re.match(r'^[\d-]+$', value)
  242. if not matches:
  243. return False
  244. else:
  245. return int(value)
  246. def parse_float(value):
  247. matches = re.match(r'^[\d.-]+$', value)
  248. if not matches:
  249. return False
  250. else:
  251. return float(value)
  252. def parse_boolean(value):
  253. if value == 'true':
  254. return True
  255. else:
  256. return False
  257. def peek(iterable):
  258. """
  259. Peek into an iterable.
  260. """
  261. try:
  262. first = next(iterable)
  263. except StopIteration:
  264. return None
  265. else:
  266. return itertools.chain([first], iterable)
  267. def datetimeformat(value, format=DATETIME_FORMAT):
  268. """
  269. Format a given datetime as a UTC ISO3339 string.
  270. """
  271. if not isinstance(value, datetime):
  272. return value
  273. return value.astimezone(timezone.utc).strftime(format)
  274. def timedeltaformat(value):
  275. """
  276. Format a timedelta to a RFC5545 Duration.
  277. """
  278. if not isinstance(value, timedelta):
  279. return value
  280. days = value.days
  281. hours, seconds = divmod(value.seconds, 3600)
  282. minutes, seconds = divmod(seconds, 60)
  283. formatted = "P"
  284. if days:
  285. formatted += f"{days}D"
  286. if hours or minutes or seconds:
  287. formatted += f"T"
  288. if hours:
  289. formatted += f"{hours}H"
  290. if minutes:
  291. formatted += f"{minutes}M"
  292. if seconds:
  293. formatted += f"{seconds}S"
  294. return formatted
  295. def booleanformat(value):
  296. """
  297. Format a boolean value
  298. """
  299. if isinstance(value, bool):
  300. if value == True:
  301. return "true"
  302. elif value == False:
  303. return "false"
  304. elif value in ("true", "false"):
  305. return value
  306. else:
  307. raise ValueError(f"A boolean value must be provided, not {value}.")
  308. def ensure_bytes(obj):
  309. """
  310. Converts a utf-8 str object to bytes.
  311. """
  312. if isinstance(obj, bytes):
  313. return obj
  314. if isinstance(obj, str):
  315. return bytes(obj, 'utf-8')
  316. else:
  317. raise TypeError("Must be bytes or str")
  318. def ensure_str(obj):
  319. """
  320. Converts bytes to a utf-8 string.
  321. """
  322. if isinstance(obj, str):
  323. return obj
  324. if isinstance(obj, bytes):
  325. return obj.decode('utf-8')
  326. else:
  327. raise TypeError("Must be bytes or str")
  328. def certificate_fingerprint(certificate_str):
  329. """
  330. Calculate the fingerprint for the given certificate, as defined by OpenADR.
  331. """
  332. der_cert = ssl.PEM_cert_to_DER_cert(ensure_str(certificate_str))
  333. hash = hashlib.sha256(der_cert).digest().hex()
  334. return ":".join([hash[i-2:i].upper() for i in range(-20, 0, 2)])
  335. def extract_pem_cert(tree):
  336. """
  337. Extract a given X509 certificate inside an XML tree and return the standard
  338. form of a PEM-encoded certificate.
  339. :param tree lxml.etree: The tree that contains the X509 element. This is
  340. usually the KeyInfo element from the XMLDsig Signature
  341. part of the message.
  342. """
  343. cert = tree.find('.//{http://www.w3.org/2000/09/xmldsig#}X509Certificate').text
  344. return "-----BEGIN CERTIFICATE-----\n" + cert + "-----END CERTIFICATE-----\n"