1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
2 # not use this file except in compliance with the License. You may obtain
3 # a copy of the License at
5 # 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, WITHOUT
9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 # License for the specific language governing permissions and limitations
13 from toscaparser.common.exception import ExceptionCollector
14 from toscaparser.common.exception import UnknownFieldError
15 from toscaparser.elements.capabilitytype import CapabilityTypeDef
16 import toscaparser.elements.interfaces as ifaces
17 from toscaparser.elements.interfaces import InterfacesDef
18 from toscaparser.elements.relationshiptype import RelationshipType
19 from toscaparser.elements.statefulentitytype import StatefulEntityType
22 class NodeType(StatefulEntityType):
23 '''TOSCA built-in node type.'''
24 SECTIONS = (DERIVED_FROM, METADATA, PROPERTIES, VERSION, DESCRIPTION, ATTRIBUTES, REQUIREMENTS, CAPABILITIES, INTERFACES, ARTIFACTS) = \
25 ('derived_from', 'metadata', 'properties', 'version',
26 'description', 'attributes', 'requirements', 'capabilities',
27 'interfaces', 'artifacts')
29 def __init__(self, ntype, custom_def=None):
30 super(NodeType, self).__init__(ntype, self.NODE_PREFIX, custom_def)
32 self.custom_def = custom_def
36 def parent_type(self):
37 '''Return a node this node is derived from.'''
38 if not hasattr(self, 'defs'):
40 pnode = self.derived_from(self.defs)
42 return NodeType(pnode, self.custom_def)
45 def relationship(self):
46 '''Return a dictionary of relationships to other node types.
48 This method returns a dictionary of named relationships that nodes
49 of the current node type (self) can have to other nodes (of specific
50 types) in a TOSCA template.
54 requires = self.get_all_requirements()
56 # NOTE(sdmonov): Check if requires is a dict.
57 # If it is a dict convert it to a list of dicts.
58 # This is needed because currently the code below supports only
59 # lists as requirements definition. The following check will
60 # make sure if a map (dict) was provided it will be converted to
61 # a list before proceeding to the parsing.
62 if isinstance(requires, dict):
63 requires = [{key: value} for key, value in requires.items()]
67 for require in requires:
68 for key, req in require.items():
69 if 'relationship' in req:
70 relation = req.get('relationship')
71 if 'type' in relation:
72 relation = relation.get('type')
73 node_type = req.get('node')
78 # If value is a dict and has a type key
79 # we need to lookup the node type using
82 if isinstance(value, dict):
83 captype = value['capability']
85 _get_node_type_by_cap(key, captype))
86 relation = self._get_relation(key, value)
89 rtype = RelationshipType(relation, keyword, self.custom_def)
90 relatednode = NodeType(node_type, self.custom_def)
91 relationship[rtype] = relatednode
94 def _get_node_type_by_cap(self, key, cap):
95 '''Find the node type that has the provided capability
97 This method will lookup all node types if they have the
101 # Filter the node types
102 node_types = [node_type for node_type in self.TOSCA_DEF.keys()
103 if node_type.startswith(self.NODE_PREFIX) and
104 node_type != 'tosca.nodes.Root']
106 for node_type in node_types:
107 node_def = self.TOSCA_DEF[node_type]
108 if isinstance(node_def, dict) and 'capabilities' in node_def:
109 node_caps = node_def['capabilities']
110 for value in node_caps.values():
111 if isinstance(value, dict) and \
112 'type' in value and value['type'] == cap:
115 def _get_relation(self, key, ndtype):
117 ntype = NodeType(ndtype)
118 caps = ntype.get_capabilities()
119 if caps and key in caps.keys():
121 for r in self.RELATIONSHIP_TYPE:
122 rtypedef = ntype.TOSCA_DEF[r]
123 for properties in rtypedef.values():
124 if c.type in properties:
130 for properties in rtypedef.values():
131 if c.parent_type in properties:
136 def get_capabilities_objects(self):
137 '''Return a list of capability objects.'''
138 typecapabilities = []
139 caps = self.get_value(self.CAPABILITIES, None, True)
141 caps = self.get_value(self.CAPABILITIES, None, True)
143 # 'name' is symbolic name of the capability
144 # 'value' is a dict { 'type': <capability type name> }
145 for name, value in caps.items():
146 ctype = value.get('type')
147 cap = CapabilityTypeDef(name, ctype, self.type,
149 typecapabilities.append(cap)
150 return typecapabilities
152 def get_capabilities(self):
153 '''Return a dictionary of capability name-objects pairs.'''
154 return {cap.name: cap
155 for cap in self.get_capabilities_objects()}
158 def requirements(self):
159 return self.get_value(self.REQUIREMENTS, None, True)
161 def get_all_requirements(self):
162 return self.requirements
165 def interfaces(self):
166 return self.get_value(self.INTERFACES)
169 def lifecycle_inputs(self):
170 '''Return inputs to life cycle operations if found.'''
172 interfaces = self.interfaces
174 for name, value in interfaces.items():
175 if name == ifaces.LIFECYCLE:
176 for x, y in value.items():
178 for i in y.iterkeys():
183 def lifecycle_operations(self):
184 '''Return available life cycle operations if found.'''
186 interfaces = self.interfaces
188 i = InterfacesDef(self.type, ifaces.LIFECYCLE)
189 ops = i.lifecycle_ops
192 def get_capability(self, name):
193 caps = self.get_capabilities()
194 if caps and name in caps.keys():
195 return caps[name].value
197 def get_capability_type(self, name):
198 captype = self.get_capability(name)
199 if captype and name in captype.keys():
200 return captype[name].value
202 def _validate_keys(self):
204 for key in self.defs.keys():
205 if key not in self.SECTIONS:
206 ExceptionCollector.appendException(
207 UnknownFieldError(what='Nodetype"%s"' % self.ntype,