nodeset.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. ###
  4. ### Author: Chris Iatrou (ichrispa@core-vector.net)
  5. ### Version: rev 13
  6. ###
  7. ### This program was created for educational purposes and has been
  8. ### contributed to the open62541 project by the author. All licensing
  9. ### terms for this source is inherited by the terms and conditions
  10. ### specified for by the open62541 project (see the projects readme
  11. ### file for more information on the LGPL terms and restrictions).
  12. ###
  13. ### This program is not meant to be used in a production environment. The
  14. ### author is not liable for any complications arising due to the use of
  15. ### this program.
  16. ###
  17. from __future__ import print_function
  18. import sys
  19. import xml.dom.minidom as dom
  20. import logging
  21. import codecs
  22. import re
  23. from datatypes import *
  24. from nodes import *
  25. from opaque_type_mapping import opaque_type_mapping
  26. __all__ = ['NodeSet', 'getSubTypesOf']
  27. logger = logging.getLogger(__name__)
  28. if sys.version_info[0] >= 3:
  29. # strings are already parsed to unicode
  30. def unicode(s):
  31. return s
  32. string_types = str
  33. else:
  34. string_types = basestring
  35. ####################
  36. # Helper Functions #
  37. ####################
  38. hassubtype = NodeId("ns=0;i=45")
  39. def getSubTypesOf(nodeset, node, skipNodes=[]):
  40. if node in skipNodes:
  41. return []
  42. re = set()
  43. re.add(node)
  44. for ref in node.references:
  45. if (ref.referenceType == hassubtype):
  46. skipAll = set()
  47. skipAll.update(skipNodes)
  48. skipAll.update(re)
  49. if (ref.source == node.id and ref.isForward):
  50. re.update(getSubTypesOf(nodeset, nodeset.nodes[ref.target], skipNodes=skipAll))
  51. elif (ref.target == node.id and not ref.isForward):
  52. re.update(getSubTypesOf(nodeset, nodeset.nodes[ref.source], skipNodes=skipAll))
  53. return re
  54. def extractNamespaces(xmlfile):
  55. # Extract a list of namespaces used. The first namespace is always
  56. # "http://opcfoundation.org/UA/". minidom gobbles up
  57. # <NamespaceUris></NamespaceUris> elements, without a decent way to reliably
  58. # access this dom2 <uri></uri> elements (only attribute xmlns= are accessible
  59. # using minidom). We need them for dereferencing though... This function
  60. # attempts to do just that.
  61. namespaces = ["http://opcfoundation.org/UA/"]
  62. infile = codecs.open(xmlfile.name, encoding='utf-8')
  63. foundURIs = False
  64. nsline = ""
  65. for line in infile:
  66. if "<namespaceuris>" in line.lower():
  67. foundURIs = True
  68. elif "</namespaceuris>" in line.lower():
  69. nsline = nsline + line
  70. break
  71. if foundURIs:
  72. nsline = nsline + line
  73. if len(nsline) > 0:
  74. ns = dom.parseString(nsline).getElementsByTagName("NamespaceUris")
  75. for uri in ns[0].childNodes:
  76. if uri.nodeType != uri.ELEMENT_NODE:
  77. continue
  78. if uri.firstChild.data in namespaces:
  79. continue
  80. namespaces.append(uri.firstChild.data)
  81. infile.close()
  82. return namespaces
  83. def buildAliasList(xmlelement):
  84. """Parses the <Alias> XML Element present in must XML NodeSet definitions.
  85. Contents the Alias element are stored in a dictionary for further
  86. dereferencing during pointer linkage (see linkOpenPointer())."""
  87. aliases = {}
  88. for al in xmlelement.childNodes:
  89. if al.nodeType == al.ELEMENT_NODE:
  90. if al.hasAttribute("Alias"):
  91. aliasst = al.getAttribute("Alias")
  92. aliasnd = unicode(al.firstChild.data)
  93. aliases[aliasst] = aliasnd
  94. return aliases
  95. class NodeSet(object):
  96. """ This class handles parsing XML description of namespaces, instantiating
  97. nodes, linking references, graphing the namespace and compiling a binary
  98. representation.
  99. Note that nodes assigned to this class are not restricted to having a
  100. single namespace ID. This class represents the entire physical address
  101. space of the binary representation and all nodes that are to be included
  102. in that segment of memory.
  103. """
  104. def __init__(self):
  105. self.nodes = {}
  106. self.aliases = {}
  107. self.namespaces = ["http://opcfoundation.org/UA/"]
  108. self.namespaceMapping = {};
  109. def sanitize(self):
  110. for n in self.nodes.values():
  111. if n.sanitize() == False:
  112. raise Exception("Failed to sanitize node " + str(n))
  113. # Sanitize reference consistency
  114. for n in self.nodes.values():
  115. for ref in n.references:
  116. if not ref.source == n.id:
  117. raise Exception("Reference " + str(ref) + " has an invalid source")
  118. if not ref.referenceType in self.nodes:
  119. raise Exception("Reference " + str(ref) + " has an unknown reference type")
  120. if not ref.target in self.nodes:
  121. raise Exception("Reference " + str(ref) + " has an unknown target")
  122. def addNamespace(self, nsURL):
  123. if not nsURL in self.namespaces:
  124. self.namespaces.append(nsURL)
  125. def createNamespaceMapping(self, orig_namespaces):
  126. """Creates a dict that maps from the nsindex in the original nodeset to the
  127. nsindex in the combined nodeset"""
  128. m = {}
  129. for index, name in enumerate(orig_namespaces):
  130. m[index] = self.namespaces.index(name)
  131. return m
  132. def getNodeByBrowseName(self, idstring):
  133. return next((n for n in self.nodes.values() if idstring == n.browseName.name), None)
  134. def getNodeById(self, namespace, id):
  135. nodeId = NodeId()
  136. nodeId.ns = namespace
  137. nodeId.i = id
  138. return self.nodes[nodeId]
  139. def getRoot(self):
  140. return self.getNodeByBrowseName("Root")
  141. def createNode(self, xmlelement, modelUri, hidden=False):
  142. ndtype = xmlelement.localName.lower()
  143. if ndtype[:2] == "ua":
  144. ndtype = ndtype[2:]
  145. node = None
  146. if ndtype == 'variable':
  147. node = VariableNode(xmlelement)
  148. if ndtype == 'object':
  149. node = ObjectNode(xmlelement)
  150. if ndtype == 'method':
  151. node = MethodNode(xmlelement)
  152. if ndtype == 'objecttype':
  153. node = ObjectTypeNode(xmlelement)
  154. if ndtype == 'variabletype':
  155. node = VariableTypeNode(xmlelement)
  156. if ndtype == 'methodtype':
  157. node = MethodNode(xmlelement)
  158. if ndtype == 'datatype':
  159. node = DataTypeNode(xmlelement)
  160. if ndtype == 'referencetype':
  161. node = ReferenceTypeNode(xmlelement)
  162. if node is None:
  163. return None
  164. node.modelUri = modelUri
  165. node.hidden = hidden
  166. return node
  167. def hide_node(self, nodeId, hidden=True):
  168. if not nodeId in self.nodes:
  169. return False
  170. node = self.nodes[nodeId]
  171. node.hidden = hidden
  172. return True
  173. def merge_dicts(self, *dict_args):
  174. """
  175. Given any number of dicts, shallow copy and merge into a new dict,
  176. precedence goes to key value pairs in latter dicts.
  177. """
  178. result = {}
  179. for dictionary in dict_args:
  180. result.update(dictionary)
  181. return result
  182. def addNodeSet(self, xmlfile, hidden=False, typesArray="UA_TYPES"):
  183. # Extract NodeSet DOM
  184. fileContent = xmlfile.read()
  185. # Remove BOM since the dom parser cannot handle it on python 3 windows
  186. if fileContent.startswith( codecs.BOM_UTF8 ):
  187. fileContent = fileContent.lstrip( codecs.BOM_UTF8 )
  188. if (sys.version_info >= (3, 0)):
  189. fileContent = fileContent.decode("utf-8")
  190. # Remove the uax namespace from tags. UaModeler adds this namespace to some elements
  191. fileContent = re.sub(r"<([/]?)uax:(.+?)([/]?)>", "<\g<1>\g<2>\g<3>>", fileContent)
  192. nodesets = dom.parseString(fileContent).getElementsByTagName("UANodeSet")
  193. if len(nodesets) == 0 or len(nodesets) > 1:
  194. raise Exception(self, self.originXML + " contains no or more then 1 nodeset")
  195. nodeset = nodesets[0]
  196. # Extract the modelUri
  197. try:
  198. modelTag = nodeset.getElementsByTagName("Models")[0].getElementsByTagName("Model")[0]
  199. modelUri = modelTag.attributes["ModelUri"].nodeValue
  200. except Exception:
  201. # Ignore exception and try to use namespace array
  202. modelUri = None
  203. # Create the namespace mapping
  204. orig_namespaces = extractNamespaces(xmlfile) # List of namespaces used in the xml file
  205. if modelUri is None and len(orig_namespaces) > 1:
  206. modelUri = orig_namespaces[1]
  207. if modelUri is None:
  208. raise Exception(self, self.originXML + " does not define the nodeset URI in Models/Model/ModelUri or NamespaceUris array.")
  209. for ns in orig_namespaces:
  210. self.addNamespace(ns)
  211. self.namespaceMapping[modelUri] = self.createNamespaceMapping(orig_namespaces)
  212. # Extract the aliases
  213. for nd in nodeset.childNodes:
  214. if nd.nodeType != nd.ELEMENT_NODE:
  215. continue
  216. ndtype = nd.localName.lower()
  217. if 'aliases' in ndtype:
  218. self.aliases = self.merge_dicts(self.aliases, buildAliasList(nd))
  219. # Instantiate nodes
  220. newnodes = []
  221. for nd in nodeset.childNodes:
  222. if nd.nodeType != nd.ELEMENT_NODE:
  223. continue
  224. node = self.createNode(nd, modelUri, hidden)
  225. if not node:
  226. continue
  227. node.replaceAliases(self.aliases)
  228. node.replaceNamespaces(self.namespaceMapping[modelUri])
  229. node.typesArray = typesArray
  230. # Add the node the the global dict
  231. if node.id in self.nodes:
  232. raise Exception("XMLElement with duplicate ID " + str(node.id))
  233. self.nodes[node.id] = node
  234. newnodes.append(node)
  235. def getBinaryEncodingIdForNode(self, nodeId):
  236. """
  237. The node should have a 'HasEncoding' forward reference which points to the encoding ids.
  238. These can be XML Encoding or Binary Encoding. Therefore we also need to check if the SymbolicName
  239. of the target node is "DefaultBinary"
  240. """
  241. node = self.nodes[nodeId]
  242. for ref in node.references:
  243. if ref.referenceType.ns == 0 and ref.referenceType.i == 38:
  244. refNode = self.nodes[ref.target]
  245. if refNode.symbolicName.value == "DefaultBinary":
  246. return ref.target
  247. raise Exception("No DefaultBinary encoding defined for node " + str(nodeId))
  248. def buildEncodingRules(self):
  249. """ Calls buildEncoding() for all DataType nodes (opcua_node_dataType_t).
  250. No return value
  251. """
  252. stat = {True: 0, False: 0}
  253. for n in self.nodes.values():
  254. if isinstance(n, DataTypeNode):
  255. n.buildEncoding(self)
  256. stat[n.isEncodable()] = stat[n.isEncodable()] + 1
  257. logger.debug("Type definitions built/passed: " + str(stat))
  258. def allocateVariables(self):
  259. for n in self.nodes.values():
  260. if isinstance(n, VariableNode):
  261. n.allocateValue(self)
  262. def getBaseDataType(self, node):
  263. if node is None:
  264. return None
  265. if node.browseName.name not in opaque_type_mapping:
  266. return node
  267. for ref in node.references:
  268. if ref.isForward:
  269. continue
  270. if ref.referenceType.i == 45:
  271. return self.getBaseDataType(self.nodes[ref.target])
  272. return node
  273. def getDataTypeNode(self, dataType):
  274. if isinstance(dataType, string_types):
  275. if not valueIsInternalType(dataType):
  276. logger.error("Not a valid dataType string: " + dataType)
  277. return None
  278. return self.nodes[NodeId(self.aliases[dataType])]
  279. if isinstance(dataType, NodeId):
  280. if dataType.i == 0:
  281. return None
  282. dataTypeNode = self.nodes[dataType]
  283. if not isinstance(dataTypeNode, DataTypeNode):
  284. logger.error("Node id " + str(dataType) + " is not reference a valid dataType.")
  285. return None
  286. if not dataTypeNode.isEncodable():
  287. logger.warn("DataType " + str(dataTypeNode.browseName) + " is not encodable.")
  288. return dataTypeNode
  289. return None
  290. def getRelevantOrderingReferences(self):
  291. relevant_types = set()
  292. relevant_types.update(getSubTypesOf(self, self.getNodeByBrowseName("HierarchicalReferences"), []))
  293. relevant_types.update(getSubTypesOf(self, self.getNodeByBrowseName("HasEncoding"), []))
  294. relevant_types.update(getSubTypesOf(self, self.getNodeByBrowseName("HasTypeDefinition"), []))
  295. return list(map(lambda x: x.id, relevant_types))
  296. def addInverseReferences(self):
  297. # Ensure that every reference has an inverse reference in the target
  298. for u in self.nodes.values():
  299. for ref in u.references:
  300. back = Reference(ref.target, ref.referenceType, ref.source, not ref.isForward)
  301. self.nodes[ref.target].references.add(back) # ref set does not make a duplicate entry