a2dd92bafbcf2fcd82fe316a6bf0ecb3b006980b
[osm/SO.git] / common / python / rift / mano / tosca_translator / rwmano / 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 # Copyright 2016 RIFT.io Inc
15
16
17 from rift.mano.tosca_translator.common.utils import _
18 from rift.mano.tosca_translator.common.utils import convert_keys_to_python
19 from rift.mano.tosca_translator.rwmano.syntax.mano_resource import ManoResource
20
21
22 # Name used to dynamically load appropriate map class.
23 TARGET_CLASS_NAME = 'ToscaNetwork'
24
25
26 class ToscaNetwork(ManoResource):
27 '''Translate TOSCA node type tosca.nodes.network.Network.'''
28
29 toscatype = 'tosca.nodes.network.Network'
30 NETWORK_PROPS = ['network_name', 'network_id']
31 REQUIRED_PROPS = ['name', 'id', 'type', 'version', 'short-name',
32 'description', 'vendor']
33 OPTIONAL_PROPS = ['vnfd-connection-point-ref']
34 IGNORE_PROPS = ['ip_version', 'dhcp_enabled']
35 VALID_TYPES = ['ELAN']
36
37 def __init__(self, log, nodetemplate, metadata=None):
38 super(ToscaNetwork, self).__init__(log,
39 nodetemplate,
40 type_='vld',
41 metadata=metadata)
42 self._vld = {}
43 self._ip_profile = {}
44
45 def handle_vld_properties(self, nodes, vnf_type_substitution_mapping):
46 def get_vld_props(specs):
47 vld_prop = {}
48 vld_prop['id'] = self.id
49 vld_prop['name'] = self.name
50 vld_prop['short-name'] = self.name
51 vld_prop['type'] = self.get_type()
52 if 'description' in specs:
53 vld_prop['description'] = specs['description']
54 if 'vendor' in specs:
55 vld_prop['vendor'] = specs['vendor']
56
57 index_count = 1
58 vld_connection_point_list = []
59 for node in nodes:
60 if node.type == "vnfd":
61 substitution_mapping_list = vnf_type_substitution_mapping[node.vnf_type];
62 for req_key, req_value in node._reqs.items():
63 for mapping in substitution_mapping_list:
64 if req_key in mapping:
65 # link the VLD to the connection point
66 node = self.get_node_with_name(mapping[req_key][0], nodes)
67 if node:
68 #print()
69 prop = {}
70 prop['member-vnf-index-ref'] = index_count
71 prop['vnfd-connection-point-ref'] = node.cp_name
72 prop['vnfd-id-ref'] = node.vnf._id
73 vld_connection_point_list.append(prop)
74 index_count += 1
75 if len(vld_connection_point_list) > 1:
76 vld_prop['vnfd-connection-point-ref'] = vld_connection_point_list
77 return vld_prop
78
79 def get_ip_profile_props(specs):
80 ip_profile_prop = {}
81 ip_profile_param = {}
82 if 'name' in specs:
83 ip_profile_prop['name'] = specs['name']
84 elif 'description' in specs:
85 ip_profile_prop['name'] = specs['description']
86
87 if 'description' in specs:
88 ip_profile_prop['description'] = specs['description']
89 if 'gateway_ip' in specs:
90 ip_profile_param['gateway-address'] = specs['gateway_ip']
91 if 'ip_version' in specs:
92 ip_profile_param['ip-version'] = 'ipv' + str(specs['ip_version'])
93 if 'ip_version' in specs:
94 ip_profile_param['subnet-address'] = specs['cidr']
95
96 ip_profile_prop['ip-profile-params'] = ip_profile_param
97 return ip_profile_prop
98 tosca_props = self.get_tosca_props()
99 self._vld = get_vld_props(tosca_props)
100 self._ip_profile = get_ip_profile_props(tosca_props)
101
102 def get_type(self):
103 """Get the network type based on propery or type derived from"""
104 node = self.nodetemplate
105 tosca_props = self.get_tosca_props()
106 try:
107 if tosca_props['network_type'] in ToscaNetwork.VALID_TYPES:
108 return tosca_props['network_type']
109 except KeyError:
110 pass
111
112 node_type = node.type_definition
113
114 while node_type.type:
115 self.log.debug(_("Node name {0} with type {1}").
116 format(self.name, node_type.type))
117 prefix, nw_type = node_type.type.rsplit('.', 1)
118 if nw_type in ToscaNetwork.VALID_TYPES:
119 return nw_type
120 else:
121 # Get the parent
122 node_type = ManoResource.get_parent_type(node_type)
123
124 return "ELAN"
125
126 def generate_yang_model_gi(self, nsd, vnfds):
127 props = convert_keys_to_python(self.properties)
128 vld_props = convert_keys_to_python(self._vld)
129 ip_profile_props = convert_keys_to_python(self._ip_profile)
130 try:
131 nsd.vld.add().from_dict(vld_props)
132 nsd.ip_profiles.add().from_dict(ip_profile_props)
133 except Exception as e:
134 err_msg = _("{0} Exception vld from dict {1}: {2}"). \
135 format(self, props, e)
136 self.log.error(err_msg)
137 raise e
138
139 def generate_yang_model(self, nsd, vnfds, use_gi=False):
140 """Generate yang model for the node"""
141 self.log.debug(_("Generate YANG model for {0}").
142 format(self))
143
144 # Remove the props to be ignroed:
145 for key in ToscaNetwork.IGNORE_PROPS:
146 if key in self.properties:
147 self.properties.pop(key)
148
149 if use_gi:
150 return self.generate_yang_model_gi(nsd, vnfds)
151
152 vld = self.properties
153
154 if 'vld' not in nsd:
155 nsd['vld'] = []
156 nsd['vld'].append(vld)