Support python3 uploaded to pypi websit
[parser.git] / tosca2heat / heat-translator / translator / hot / tosca / tosca_network_network.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 toscaparser.common.exception import InvalidPropertyValueError
15 from translator.hot.syntax.hot_resource import HotResource
16
17 # Name used to dynamically load appropriate map class.
18 TARGET_CLASS_NAME = 'ToscaNetwork'
19
20
21 class ToscaNetwork(HotResource):
22     '''Translate TOSCA node type tosca.nodes.network.Network.'''
23
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']
28
29     existing_resource_id = None
30
31     def __init__(self, nodetemplate, csar_dir=None):
32         super(ToscaNetwork, self).__init__(nodetemplate,
33                                            type='OS::Neutron::Net',
34                                            csar_dir=csar_dir)
35         pass
36
37     def handle_properties(self):
38         tosca_props = self.get_tosca_props()
39
40         net_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
55                         break
56                 elif key == 'network_id':
57                     self.hide_resource = True
58                     self.existing_resource_id = value
59                     break
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':
65                                                 'vxlan'}
66         self.properties = net_props
67
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:
72             return
73
74         tosca_props = self.get_tosca_props()
75
76         subnet_props = {}
77
78         ip_pool_start = None
79         ip_pool_end = None
80
81         for key, value in tosca_props.items():
82             if key in self.SUBNET_PROPS:
83                 if key == 'start_ip':
84                     ip_pool_start = value
85                 elif key == 'end_ip':
86                     ip_pool_end = value
87                 elif key == 'dhcp_enabled':
88                     subnet_props['enable_dhcp'] = value
89                 else:
90                     subnet_props[key] = value
91
92         if 'network_id' in tosca_props:
93             subnet_props['network'] = tosca_props['network_id']
94         else:
95             subnet_props['network'] = '{ get_resource: %s }' % (self.name)
96
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:
101             allocation_pool = {}
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
106         elif ip_pool_start:
107             raise InvalidPropertyValueError(what=_('start_ip'))
108         elif ip_pool_end:
109             raise InvalidPropertyValueError(what=_('end_ip'))
110
111         subnet_resource_name = self.name + self.SUBNET_SUFFIX
112
113         hot_resources = [HotResource(self.nodetemplate,
114                                      type='OS::Neutron::Subnet',
115                                      name=subnet_resource_name,
116                                      properties=subnet_props)]
117         return hot_resources