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, csar_dir=None):
28 super(ToscaNetworkPort, self).__init__(nodetemplate,
29 type='OS::Neutron::Port',
35 def _generate_networks_for_compute(self, port_resources):
36 '''Generate compute networks property list from the port resources.'''
38 for resource in port_resources:
39 networks.append({'port': '{ get_resource: %s }' % (resource.name)})
42 def _insert_sorted_resource(self, resources, resource):
43 '''Insert a resource in the list of resources and keep the order.'''
48 if resource.order < resources[mid].order:
52 resources.insert(lo, resource)
54 def handle_properties(self):
55 tosca_props = self.get_tosca_props()
57 for key, value in tosca_props.items():
58 if key == 'ip_address':
60 fixed_ip['ip_address'] = value
61 port_props['fixed_ips'] = [fixed_ip]
64 # TODO(sdmonov): Need to implement the properties below
65 elif key == 'is_default':
67 elif key == 'ip_range_start':
69 elif key == 'ip_range_end':
72 port_props[key] = value
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):
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)
89 if network_resource.existing_resource_id:
90 port_props['network'] =\
91 str(network_resource.existing_resource_id)
93 port_props['network'] = '{ get_resource: %s }'\
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):
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)
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
115 self.properties = port_props