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 if 'description' in specs
:
53 vld_prop
['description'] = specs
['description']
55 vld_prop
['vendor'] = specs
['vendor']
58 vld_connection_point_list
= []
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
)
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
)
75 if len(vld_connection_point_list
) > 1:
76 vld_prop
['vnfd-connection-point-ref'] = vld_connection_point_list
79 def get_ip_profile_props(specs
):
83 ip_profile_prop
['name'] = specs
['name']
84 elif 'description' in specs
:
85 ip_profile_prop
['name'] = specs
['description']
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']
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
)
103 """Get the network type based on propery or type derived from"""
104 node
= self
.nodetemplate
105 tosca_props
= self
.get_tosca_props()
107 if tosca_props
['network_type'] in ToscaNetwork
.VALID_TYPES
:
108 return tosca_props
['network_type']
112 node_type
= node
.type_definition
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
:
122 node_type
= ManoResource
.get_parent_type(node_type
)
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
)
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
)
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
)