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
6 # http://www.apache.org/licenses/LICENSE-2.0
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
14 from translator.hot.syntax.hot_resource import HotResource
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'
22 class ToscaNetworkPort(HotResource):
23 '''Translate TOSCA node type tosca.nodes.network.Port.'''
25 toscatype = 'tosca.nodes.network.Port'
27 def __init__(self, nodetemplate):
28 super(ToscaNetworkPort, self).__init__(nodetemplate,
29 type='OS::Neutron::Port')
34 def _generate_networks_for_compute(self, port_resources):
35 '''Generate compute networks property list from the port resources.'''
37 for resource in port_resources:
38 networks.append({'port': '{ get_resource: %s }' % (resource.name)})
41 def _insert_sorted_resource(self, resources, resource):
42 '''Insert a resource in the list of resources and keep the order.'''
47 if resource.order < resources[mid].order:
51 resources.insert(lo, resource)
53 def handle_properties(self):
54 tosca_props = self.get_tosca_props()
56 for key, value in tosca_props.items():
57 if key == 'ip_address':
59 fixed_ip['ip_address'] = value
60 port_props['fixed_ips'] = [fixed_ip]
63 # TODO(sdmonov): Need to implement the properties below
64 elif key == 'is_default':
66 elif key == 'ip_range_start':
68 elif key == 'ip_range_end':
71 port_props[key] = value
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):
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)
88 if network_resource.existing_resource_id:
89 port_props['network'] =\
90 str(network_resource.existing_resource_id)
92 port_props['network'] = '{ get_resource: %s }'\
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):
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)
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
114 self.properties = port_props