utils.py 13 KB

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