* YANG to TOSCA VNFFG Support
[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 vld_prop['ip_profile_ref'] = "{0}_{1}".format(self.nodetemplate.name, "ip")
53 if 'description' in specs:
54 vld_prop['description'] = specs['description']
55 if 'vendor' in specs:
56 vld_prop['vendor'] = specs['vendor']
57
58 index_count = 1
59 vld_connection_point_list = []
60 for node in nodes:
61 if node.type == "vnfd":
62 substitution_mapping_list = vnf_type_substitution_mapping[node.vnf_type];
63 for req_key, req_value in node._reqs.items():
64 for mapping in substitution_mapping_list:
65 if req_key in mapping:
66 # link the VLD to the connection point
67 node_vld = self.get_node_with_name(mapping[req_key][0], nodes)
68 if node:
69 #print()
70 prop = {}
71 prop['member-vnf-index-ref'] = node.get_member_vnf_index()
72 prop['vnfd-connection-point-ref'] = node_vld.cp_name
73 prop['vnfd-id-ref'] = node_vld.vnf._id
74 vld_connection_point_list.append(prop)
75 index_count += 1
76 if len(vld_connection_point_list) > 1:
77 vld_prop['vnfd-connection-point-ref'] = vld_connection_point_list
78 return vld_prop
79
80 def get_ip_profile_props(specs):
81 ip_profile_prop = {}
82 ip_profile_param = {}
83 if 'ip_profile_ref' in self._vld:
84 ip_profile_prop['name'] = self._vld['ip_profile_ref']
85
86 if 'description' in specs:
87 ip_profile_prop['description'] = specs['description']
88 if 'gateway_ip' in specs:
89 ip_profile_param['gateway-address'] = specs['gateway_ip']
90 if 'ip_version' in specs:
91 ip_profile_param['ip-version'] = 'ipv' + str(specs['ip_version'])
92 if 'cidr' in specs:
93 ip_profile_param['subnet-address'] = specs['cidr']
94
95 ip_profile_prop['ip-profile-params'] = ip_profile_param
96 return ip_profile_prop
97 tosca_props = self.get_tosca_props()
98 self._vld = get_vld_props(tosca_props)
99 self._ip_profile = get_ip_profile_props(tosca_props)
100
101 def get_type(self):
102 """Get the network type based on propery or type derived from"""
103 node = self.nodetemplate
104 tosca_props = self.get_tosca_props()
105 try:
106 if tosca_props['network_type'] in ToscaNetwork.VALID_TYPES:
107 return tosca_props['network_type']
108 except KeyError:
109 pass
110
111 node_type = node.type_definition
112
113 while node_type.type:
114 self.log.debug(_("Node name {0} with type {1}").
115 format(self.name, node_type.type))
116 prefix, nw_type = node_type.type.rsplit('.', 1)
117 if nw_type in ToscaNetwork.VALID_TYPES:
118 return nw_type
119 else:
120 # Get the parent
121 node_type = ManoResource.get_parent_type(node_type)
122
123 return "ELAN"
124
125 def generate_yang_model_gi(self, nsd, vnfds):
126 props = convert_keys_to_python(self.properties)
127 vld_props = convert_keys_to_python(self._vld)
128 ip_profile_props = convert_keys_to_python(self._ip_profile)
129 try:
130 nsd.vld.add().from_dict(vld_props)
131 nsd.ip_profiles.add().from_dict(ip_profile_props)
132 except Exception as e:
133 err_msg = _("{0} Exception vld from dict {1}: {2}"). \
134 format(self, props, e)
135 self.log.error(err_msg)
136 raise e
137
138 def generate_yang_model(self, nsd, vnfds, use_gi=False):
139 """Generate yang model for the node"""
140 self.log.debug(_("Generate YANG model for {0}").
141 format(self))
142
143 # Remove the props to be ignroed:
144 for key in ToscaNetwork.IGNORE_PROPS:
145 if key in self.properties:
146 self.properties.pop(key)
147
148 if use_gi:
149 return self.generate_yang_model_gi(nsd, vnfds)
150
151 vld = self.properties
152
153 if 'vld' not in nsd:
154 nsd['vld'] = []
155 nsd['vld'].append(vld)