Update tosca lib to version 0.5
[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):
32         super(ToscaNetwork, self).__init__(nodetemplate,
33                                            type='OS::Neutron::Net')
34         pass
35
36     def handle_properties(self):
37         tosca_props = self.get_tosca_props()
38
39         net_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
54                         break
55                 elif key == 'network_id':
56                     self.hide_resource = True
57                     self.existing_resource_id = value
58                     break
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':
66                                                 'vxlan'}
67         self.properties = net_props
68
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:
73             return
74
75         tosca_props = self.get_tosca_props()
76
77         subnet_props = {}
78
79         ip_pool_start = None
80         ip_pool_end = None
81
82         for key, value in tosca_props.items():
83             if key in self.SUBNET_PROPS:
84                 if key == 'start_ip':
85                     ip_pool_start = value
86                 elif key == 'end_ip':
87                     ip_pool_end = value
88                 elif key == 'dhcp_enabled':
89                     subnet_props['enable_dhcp'] = value
90                 else:
91                     subnet_props[key] = value
92
93         if 'network_id' in tosca_props:
94             subnet_props['network'] = tosca_props['network_id']
95         else:
96             subnet_props['network'] = '{ get_resource: %s }' % (self.name)
97
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:
102             allocation_pool = {}
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
107         elif ip_pool_start:
108             raise InvalidPropertyValueError(what=_('start_ip'))
109         elif ip_pool_end:
110             raise InvalidPropertyValueError(what=_('end_ip'))
111
112         subnet_resource_name = self.name + self.SUBNET_SUFFIX
113
114         hot_resources = [HotResource(self.nodetemplate,
115                                      type='OS::Neutron::Subnet',
116                                      name=subnet_resource_name,
117                                      properties=subnet_props)]
118         return hot_resources