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 toscaparser.common.exception import InvalidPropertyValueError
15 from translator.hot.syntax.hot_resource import HotResource
17 # Name used to dynamically load appropriate map class.
18 TARGET_CLASS_NAME = 'ToscaNetwork'
21 class ToscaNetwork(HotResource):
22 '''Translate TOSCA node type tosca.nodes.network.Network.'''
24 toscatype = 'tosca.nodes.network.Network'
25 SUBNET_SUFFIX = '_subnet'
26 NETWORK_PROPS = ['network_name', 'network_id', 'segmentation_id']
27 SUBNET_PROPS = ['ip_version', 'cidr', 'start_ip', 'end_ip', 'gateway_ip']
29 existing_resource_id = None
31 def __init__(self, nodetemplate):
32 super(ToscaNetwork, self).__init__(nodetemplate,
33 type='OS::Neutron::Net')
36 def handle_properties(self):
37 tosca_props = self.get_tosca_props()
40 for key, value in tosca_props.items():
41 if key in self.NETWORK_PROPS:
42 if key == 'network_name':
43 # If CIDR is specified network_name should
44 # be used as the name for the new network.
45 if 'cidr' in tosca_props.keys():
46 net_props['name'] = value
47 # If CIDR is not specified network_name will be used
48 # to lookup existing network. If network_id is specified
49 # together with network_name then network_id should be
50 # used to lookup the network instead
51 elif 'network_id' not in tosca_props.keys():
52 self.hide_resource = True
53 self.existing_resource_id = value
55 elif key == 'network_id':
56 self.hide_resource = True
57 self.existing_resource_id = value
59 elif key == 'segmentation_id':
60 net_props['segmentation_id'] = \
61 tosca_props['segmentation_id']
62 # Hardcode to vxlan for now until we add the network type
63 # and physical network to the spec.
64 net_props['value_specs'] = {'provider:segmentation_id':
65 value, 'provider:network_type':
67 self.properties = net_props
69 def handle_expansion(self):
70 # If the network resource should not be output (they are hidden),
71 # there is no need to generate subnet resource
72 if self.hide_resource:
75 tosca_props = self.get_tosca_props()
82 for key, value in tosca_props.items():
83 if key in self.SUBNET_PROPS:
88 elif key == 'dhcp_enabled':
89 subnet_props['enable_dhcp'] = value
91 subnet_props[key] = value
93 if 'network_id' in tosca_props:
94 subnet_props['network'] = tosca_props['network_id']
96 subnet_props['network'] = '{ get_resource: %s }' % (self.name)
98 # Handle allocation pools
99 # Do this only if both start_ip and end_ip are provided
100 # If one of them is missing throw an exception.
101 if ip_pool_start and ip_pool_end:
103 allocation_pool['start'] = ip_pool_start
104 allocation_pool['end'] = ip_pool_end
105 allocation_pools = [allocation_pool]
106 subnet_props['allocation_pools'] = allocation_pools
108 raise InvalidPropertyValueError(what=_('start_ip'))
110 raise InvalidPropertyValueError(what=_('end_ip'))
112 subnet_resource_name = self.name + self.SUBNET_SUFFIX
114 hot_resources = [HotResource(self.nodetemplate,
115 type='OS::Neutron::Subnet',
116 name=subnet_resource_name,
117 properties=subnet_props)]