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 # Copyright 2016 RIFT.io Inc
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
22 # Name used to dynamically load appropriate map class.
23 TARGET_CLASS_NAME
= 'ToscaNetwork'
26 class ToscaNetwork(ManoResource
):
27 '''Translate TOSCA node type tosca.nodes.network.Network.'''
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']
37 def __init__(self
, log
, nodetemplate
, metadata
=None):
38 super(ToscaNetwork
, self
).__init
__(log
,
45 def handle_vld_properties(self
, nodes
, vnf_type_substitution_mapping
):
46 def get_vld_props(specs
):
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']
56 vld_prop
['vendor'] = specs
['vendor']
59 vld_connection_point_list
= []
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
)
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
)
76 if len(vld_connection_point_list
) > 1:
77 vld_prop
['vnfd-connection-point-ref'] = vld_connection_point_list
80 def get_ip_profile_props(specs
):
83 if 'ip_profile_ref' in self
._vld
:
84 ip_profile_prop
['name'] = self
._vld
['ip_profile_ref']
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'])
93 ip_profile_param
['subnet-address'] = specs
['cidr']
94 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
)
102 """Get the network type based on propery or type derived from"""
103 node
= self
.nodetemplate
104 tosca_props
= self
.get_tosca_props()
106 if tosca_props
['network_type'] in ToscaNetwork
.VALID_TYPES
:
107 return tosca_props
['network_type']
111 node_type
= node
.type_definition
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
:
121 node_type
= ManoResource
.get_parent_type(node_type
)
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
)
130 nsd
.vld
.add().from_dict(vld_props
)
131 if len(ip_profile_props
) > 1:
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
)
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}").
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
)
150 return self
.generate_yang_model_gi(nsd
, vnfds
)
152 vld
= self
.properties
156 nsd
['vld'].append(vld
)