Synchronize upstream version of 0.9
[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, csar_dir=None):
28         super(ToscaNetworkPort, self).__init__(nodetemplate,
29                                                type='OS::Neutron::Port',
30                                                csar_dir=csar_dir)
31         # Default order
32         self.order = 0
33         pass
34
35     def _generate_networks_for_compute(self, port_resources):
36         '''Generate compute networks property list from the port resources.'''
37         networks = []
38         for resource in port_resources:
39             networks.append({'port': '{ get_resource: %s }' % (resource.name)})
40         return networks
41
42     def _insert_sorted_resource(self, resources, resource):
43         '''Insert a resource in the list of resources and keep the order.'''
44         lo = 0
45         hi = len(resources)
46         while lo < hi:
47             mid = (lo + hi) // 2
48             if resource.order < resources[mid].order:
49                 hi = mid
50             else:
51                 lo = mid + 1
52         resources.insert(lo, resource)
53
54     def handle_properties(self):
55         tosca_props = self.get_tosca_props()
56         port_props = {}
57         for key, value in tosca_props.items():
58             if key == 'ip_address':
59                 fixed_ip = {}
60                 fixed_ip['ip_address'] = value
61                 port_props['fixed_ips'] = [fixed_ip]
62             elif key == 'order':
63                 self.order = value
64             # TODO(sdmonov): Need to implement the properties below
65             elif key == 'is_default':
66                 pass
67             elif key == 'ip_range_start':
68                 pass
69             elif key == 'ip_range_end':
70                 pass
71             else:
72                 port_props[key] = value
73
74         links_to = None
75         binds_to = None
76         for rel, node in self.nodetemplate.relationships.items():
77             # Check for LinksTo relations. If found add a network property with
78             # the network name into the port
79             if not links_to and rel.is_derived_from(TOSCA_LINKS_TO):
80                 links_to = node
81
82                 network_resource = None
83                 for hot_resource in self.depends_on_nodes:
84                     if links_to.name == hot_resource.name:
85                         network_resource = hot_resource
86                         self.depends_on.remove(hot_resource)
87                         break
88
89                 if network_resource.existing_resource_id:
90                     port_props['network'] =\
91                         str(network_resource.existing_resource_id)
92                 else:
93                     port_props['network'] = '{ get_resource: %s }'\
94                         % (links_to.name)
95
96             # Check for BindsTo relationship. If found add network to the
97             # network property of the corresponding compute resource
98             elif not binds_to and rel.is_derived_from(TOSCA_BINDS_TO):
99                 binds_to = node
100                 compute_resource = None
101                 for hot_resource in self.depends_on_nodes:
102                     if binds_to.name == hot_resource.name:
103                         compute_resource = hot_resource
104                         self.depends_on.remove(hot_resource)
105                         break
106                 if compute_resource:
107                     port_rsrcs = compute_resource.assoc_port_resources
108                     self._insert_sorted_resource(port_rsrcs, self)
109                     # TODO(sdmonov): Using generate networks every time we add
110                     # a network is not the fastest way to do the things. We
111                     # should do this only once at the end.
112                     networks = self._generate_networks_for_compute(port_rsrcs)
113                     compute_resource.properties['networks'] = networks
114
115         self.properties = port_props