b446e512d81061882b6a40ec7f6bdfad10c77cf1
[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
43 def handle_properties(self):
44 tosca_props = self.get_tosca_props()
45
46 if 'cidr' in tosca_props.keys():
47 self.log.warn(_("Support for subnet not yet "
48 "available. Ignoring it"))
49 net_props = {}
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
56 else:
57 net_props[key] = value
58
59 net_props['type'] = self.get_type()
60
61 if 'name' not in net_props:
62 # Use the node name as network name
63 net_props['name'] = self.name
64
65 if 'short_name' not in net_props:
66 # Use the node name as network name
67 net_props['short-name'] = self.name
68
69 if 'id' not in net_props:
70 net_props['id'] = self.id
71
72 if 'description' not in net_props:
73 net_props['description'] = self.description
74
75 if 'vendor' not in net_props:
76 net_props['vendor'] = self.vendor
77
78 if 'version' not in net_props:
79 net_props['version'] = self.version
80
81 self.log.debug(_("Network {0} properties: {1}").
82 format(self.name, net_props))
83 self.properties = net_props
84
85 def get_type(self):
86 """Get the network type based on propery or type derived from"""
87 node = self.nodetemplate
88 tosca_props = self.get_tosca_props()
89 try:
90 if tosca_props['network_type'] in ToscaNetwork.VALID_TYPES:
91 return tosca_props['network_type']
92 except KeyError:
93 pass
94
95 node_type = node.type_definition
96
97 while node_type.type:
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:
102 return nw_type
103 else:
104 # Get the parent
105 node_type = ManoResource.get_parent_type(node_type)
106
107 return "ELAN"
108
109 def generate_yang_model_gi(self, nsd, vnfds):
110 props = convert_keys_to_python(self.properties)
111 try:
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)
117 raise e
118
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}").
122 format(self))
123
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)
128
129 if use_gi:
130 return self.generate_yang_model_gi(nsd, vnfds)
131
132 vld = self.properties
133
134 if 'vld' not in nsd:
135 nsd['vld'] = []
136 nsd['vld'].append(vld)