Update tosca lib to version 0.5
[parser.git] / tosca2heat / heat-translator / translator / hot / tosca / tosca_network_port.py
1 #
2 # Licensed under the Apache License, Version 2.0 (the "License"); you may
3 # not use this file except in compliance with the License. You may obtain
4 # a copy of the License at
5 #
6 # http://www.apache.org/licenses/LICENSE-2.0
7 #
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11 # License for the specific language governing permissions and limitations
12 # under the License.
13
14 from translator.hot.syntax.hot_resource import HotResource
15
16 # Name used to dynamically load appropriate map class.
17 TARGET_CLASS_NAME = 'ToscaNetworkPort'
18 TOSCA_LINKS_TO = 'tosca.relationships.network.LinksTo'
19 TOSCA_BINDS_TO = 'tosca.relationships.network.BindsTo'
20
21
22 class ToscaNetworkPort(HotResource):
23     '''Translate TOSCA node type tosca.nodes.network.Port.'''
24
25     toscatype = 'tosca.nodes.network.Port'
26
27     def __init__(self, nodetemplate):
28         super(ToscaNetworkPort, self).__init__(nodetemplate,
29                                                type='OS::Neutron::Port')
30         # Default order
31         self.order = 0
32         pass
33
34     def _generate_networks_for_compute(self, port_resources):
35         '''Generate compute networks property list from the port resources.'''
36         networks = []
37         for resource in port_resources:
38             networks.append({'port': '{ get_resource: %s }' % (resource.name)})
39         return networks
40
41     def _insert_sorted_resource(self, resources, resource):
42         '''Insert a resource in the list of resources and keep the order.'''
43         lo = 0
44         hi = len(resources)
45         while lo < hi:
46             mid = (lo + hi) // 2
47             if resource.order < resources[mid].order:
48                 hi = mid
49             else:
50                 lo = mid + 1
51         resources.insert(lo, resource)
52
53     def handle_properties(self):
54         tosca_props = self.get_tosca_props()
55         port_props = {}
56         for key, value in tosca_props.items():
57             if key == 'ip_address':
58                 fixed_ip = {}
59                 fixed_ip['ip_address'] = value
60                 port_props['fixed_ips'] = [fixed_ip]
61             elif key == 'order':
62                 self.order = value
63             # TODO(sdmonov): Need to implement the properties below
64             elif key == 'is_default':
65                 pass
66             elif key == 'ip_range_start':
67                 pass
68             elif key == 'ip_range_end':
69                 pass
70             else:
71                 port_props[key] = value
72
73         links_to = None
74         binds_to = None
75         for rel, node in self.nodetemplate.relationships.items():
76             # Check for LinksTo relations. If found add a network property with
77             # the network name into the port
78             if not links_to and rel.is_derived_from(TOSCA_LINKS_TO):
79                 links_to = node
80
81                 network_resource = None
82                 for hot_resource in self.depends_on_nodes:
83                     if links_to.name == hot_resource.name:
84                         network_resource = hot_resource
85                         self.depends_on.remove(hot_resource)
86                         break
87
88                 if network_resource.existing_resource_id:
89                     port_props['network'] =\
90                         str(network_resource.existing_resource_id)
91                 else:
92                     port_props['network'] = '{ get_resource: %s }'\
93                         % (links_to.name)
94
95             # Check for BindsTo relationship. If found add network to the
96             # network property of the corresponding compute resource
97             elif not binds_to and rel.is_derived_from(TOSCA_BINDS_TO):
98                 binds_to = node
99                 compute_resource = None
100                 for hot_resource in self.depends_on_nodes:
101                     if binds_to.name == hot_resource.name:
102                         compute_resource = hot_resource
103                         self.depends_on.remove(hot_resource)
104                         break
105                 if compute_resource:
106                     port_rsrcs = compute_resource.assoc_port_resources
107                     self._insert_sorted_resource(port_rsrcs, self)
108                     # TODO(sdmonov): Using generate networks every time we add
109                     # a network is not the fastest way to do the things. We
110                     # should do this only once at the end.
111                     networks = self._generate_networks_for_compute(port_rsrcs)
112                     compute_resource.properties['networks'] = networks
113
114         self.properties = port_props