Fix StatefulEntityType when entitytype is not define
[parser.git] / tosca2heat / tosca-parser / toscaparser / tpl_relationship_graph.py
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
4 #
5 #         http://www.apache.org/licenses/LICENSE-2.0
6 #
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
11 #    under the License.
12
13
14 class ToscaGraph(object):
15     '''Graph of Tosca Node Templates.'''
16     def __init__(self, nodetemplates):
17         self.nodetemplates = nodetemplates
18         self.vertices = {}
19         self._create()
20
21     def _create_vertex(self, node):
22         if node not in self.vertices:
23             self.vertices[node.name] = node
24
25     def _create_edge(self, node1, node2, relationship):
26         if node1 not in self.vertices:
27             self._create_vertex(node1)
28         self.vertices[node1.name]._add_next(node2,
29                                             relationship)
30
31     def vertex(self, node):
32         if node in self.vertices:
33             return self.vertices[node]
34
35     def __iter__(self):
36         return iter(self.vertices.values())
37
38     def _create(self):
39         for node in self.nodetemplates:
40             relation = node.relationships
41             if relation:
42                 for rel, nodetpls in relation.items():
43                     for tpl in self.nodetemplates:
44                         if tpl.name == nodetpls.name:
45                             self._create_edge(node, tpl, rel)
46             self._create_vertex(node)