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, csar_dir=None):
32 super(ToscaNetwork, self).__init__(nodetemplate,
33 type='OS::Neutron::Net',
37 def handle_properties(self):
38 tosca_props = self.get_tosca_props()
41 for key, value in tosca_props.items():
42 if key in self.NETWORK_PROPS:
43 if key == 'network_name':
44 # If CIDR is specified network_name should
45 # be used as the name for the new network.
46 if 'cidr' in tosca_props.keys():
47 net_props['name'] = value
48 # If CIDR is not specified network_name will be used
49 # to lookup existing network. If network_id is specified
50 # together with network_name then network_id should be
51 # used to lookup the network instead
52 elif 'network_id' not in tosca_props.keys():
53 self.hide_resource = True
54 self.existing_resource_id = value
56 elif key == 'network_id':
57 self.hide_resource = True
58 self.existing_resource_id = value
60 elif key == 'segmentation_id':
61 # Hardcode to vxlan for now until we add the network type
62 # and physical network to the spec.
63 net_props['value_specs'] = {'provider:segmentation_id':
64 value, 'provider:network_type':
66 self.properties = net_props
68 def handle_expansion(self):
69 # If the network resource should not be output (they are hidden),
70 # there is no need to generate subnet resource
71 if self.hide_resource:
74 tosca_props = self.get_tosca_props()
81 for key, value in tosca_props.items():
82 if key in self.SUBNET_PROPS:
87 elif key == 'dhcp_enabled':
88 subnet_props['enable_dhcp'] = value
90 subnet_props[key] = value
92 if 'network_id' in tosca_props:
93 subnet_props['network'] = tosca_props['network_id']
95 subnet_props['network'] = '{ get_resource: %s }' % (self.name)
97 # Handle allocation pools
98 # Do this only if both start_ip and end_ip are provided
99 # If one of them is missing throw an exception.
100 if ip_pool_start and ip_pool_end:
102 allocation_pool['start'] = ip_pool_start
103 allocation_pool['end'] = ip_pool_end
104 allocation_pools = [allocation_pool]
105 subnet_props['allocation_pools'] = allocation_pools
107 raise InvalidPropertyValueError(what=_('start_ip'))
109 raise InvalidPropertyValueError(what=_('end_ip'))
111 subnet_resource_name = self.name + self.SUBNET_SUFFIX
113 hot_resources = [HotResource(self.nodetemplate,
114 type='OS::Neutron::Subnet',
115 name=subnet_resource_name,
116 properties=subnet_props)]