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
,
43 def handle_properties(self
):
44 tosca_props
= self
.get_tosca_props()
46 if 'cidr' in tosca_props
.keys():
47 self
.log
.warn(_("Support for subnet not yet "
48 "available. Ignoring it"))
50 for key
, value
in tosca_props
.items():
51 if key
in self
.NETWORK_PROPS
:
52 if key
== 'network_name':
53 net_props
['name'] = value
54 elif key
== 'network_id':
55 net_props
['id'] = value
57 net_props
[key
] = value
59 net_props
['type'] = self
.get_type()
61 if 'name' not in net_props
:
62 # Use the node name as network name
63 net_props
['name'] = self
.name
65 if 'short_name' not in net_props
:
66 # Use the node name as network name
67 net_props
['short-name'] = self
.name
69 if 'id' not in net_props
:
70 net_props
['id'] = self
.id
72 if 'description' not in net_props
:
73 net_props
['description'] = self
.description
75 if 'vendor' not in net_props
:
76 net_props
['vendor'] = self
.vendor
78 if 'version' not in net_props
:
79 net_props
['version'] = self
.version
81 self
.log
.debug(_("Network {0} properties: {1}").
82 format(self
.name
, net_props
))
83 self
.properties
= net_props
86 """Get the network type based on propery or type derived from"""
87 node
= self
.nodetemplate
88 tosca_props
= self
.get_tosca_props()
90 if tosca_props
['network_type'] in ToscaNetwork
.VALID_TYPES
:
91 return tosca_props
['network_type']
95 node_type
= node
.type_definition
98 self
.log
.debug(_("Node name {0} with type {1}").
99 format(self
.name
, node_type
.type))
100 prefix
, nw_type
= node_type
.type.rsplit('.', 1)
101 if nw_type
in ToscaNetwork
.VALID_TYPES
:
105 node_type
= ManoResource
.get_parent_type(node_type
)
109 def generate_yang_model_gi(self
, nsd
, vnfds
):
110 props
= convert_keys_to_python(self
.properties
)
112 nsd
.vld
.add().from_dict(props
)
113 except Exception as e
:
114 err_msg
= _("{0} Exception vld from dict {1}: {2}"). \
115 format(self
, props
, e
)
116 self
.log
.error(err_msg
)
119 def generate_yang_model(self
, nsd
, vnfds
, use_gi
=False):
120 """Generate yang model for the node"""
121 self
.log
.debug(_("Generate YANG model for {0}").
124 # Remove the props to be ignroed:
125 for key
in ToscaNetwork
.IGNORE_PROPS
:
126 if key
in self
.properties
:
127 self
.properties
.pop(key
)
130 return self
.generate_yang_model_gi(nsd
, vnfds
)
132 vld
= self
.properties
136 nsd
['vld'].append(vld
)