update from RIFT as of 696b75d2fe9fb046261b08c616f1bcf6c0b54a9b second try
[osm/SO.git] / common / python / rift / mano / yang_translator / rwmano / yang / yang_vld.py
1 # Copyright 2016 RIFT.io Inc
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15
16 from copy import deepcopy
17
18 from rift.mano.yang_translator.common.exception import ValidationError
19 from rift.mano.yang_translator.common.utils import _
20 from rift.mano.yang_translator.rwmano.syntax.tosca_resource \
21 import ToscaResource
22
23 TARGET_CLASS_NAME = 'YangVld'
24
25
26 class YangVld(ToscaResource):
27 '''Class for RIFT.io YANG VLD descriptor translation to TOSCA type.'''
28
29 yangtype = 'vld'
30
31 OTHER_KEYS = (VNFD_CP_REF) = \
32 ('vnfd_connection_point_ref')
33
34 VLD_TYPE_MAP = {
35 'ELAN': ToscaResource.T_VL1,
36 }
37
38 def __init__(self,
39 log,
40 name,
41 type_,
42 yang):
43 super(YangVld, self).__init__(log,
44 name,
45 type_,
46 yang)
47 self.yang = yang
48 self.props = {}
49
50 def process_vld(self, vnfds):
51 self.log.debug(_("Process VLD desc {0}").format(self.name))
52
53 dic = deepcopy(self.yang)
54
55 for key in self.REQUIRED_FIELDS:
56 self.props[key] = dic.pop(key)
57
58 self.id = self.props[self.ID]
59
60 if self.TYPE_Y in dic:
61 self.props[self.TYPE] = dic.pop(self.TYPE_Y)
62 if self.props[self.TYPE] not in self.VLD_TYPE_MAP:
63 err_msg = (_("{0}: VLD type {1} not supported").
64 format(self, self.props[self.TYPE]))
65 self.log.error(err_msg)
66 raise ValidationError(message=err_msg)
67
68 if self.VNFD_CP_REF in dic:
69 for cp_ref in dic.pop(self.VNFD_CP_REF):
70 vnfd_idx = cp_ref.pop(self.MEM_VNF_INDEX_REF)
71 vnfd_id = cp_ref.pop(self.VNFD_ID_REF)
72 vnfd_cp = cp_ref.pop(self.VNFD_CP_REF)
73 if vnfd_idx in vnfds:
74 vnfd = vnfds[vnfd_idx]
75 if vnfd.id == vnfd_id:
76 # Update the CP as linked to this VLD
77 vnfd.update_cp_vld(vnfd_cp, self.name)
78 else:
79 err_msg = (_("{0}, The VNFD memebr index {1} and id "
80 "{2} did not match the VNFD {3} with "
81 "id {4}").format(self, vnfd_idx, vnfd_id,
82 vnfd.name, vnfd.id))
83 self.log.error(err_msg)
84 raise ValidationError(message=err_msg)
85 else:
86 err_msg = (_("{0}, Did not find VNFD memer index {1}").
87 format(self, vnfd_idx))
88 self.log.error(err_msg)
89 raise ValidationError(message=err_msg)
90
91 self.remove_ignored_fields(dic)
92 if len(dic):
93 self.log.warn(_("{0}, Did not process the following for "
94 "VLD {1}: {2}").
95 format(self, self.props, dic))
96 self.log.debug(_("{0}, VLD: {1}").format(self, self.props))
97
98 def generate_tosca_type(self, tosca):
99 self.log.debug(_("{0} Generate tosa types").
100 format(self, tosca))
101
102 if self.T_VL1 not in tosca[self.NODE_TYPES]:
103 tosca[self.NODE_TYPES][self.T_VL1] = {
104 self.DERIVED_FROM: 'tosca.nodes.nfv.VL.ELAN',
105 self.PROPERTIES: {
106 'description':
107 {self.TYPE: self.STRING},
108 },
109 }
110
111 return tosca
112
113 def generate_tosca_template(self, tosca):
114 self.log.debug(_("{0} Generate tosa types").
115 format(self, tosca))
116
117 node = {}
118 node[self.TYPE] = self.VLD_TYPE_MAP[self.props.pop(self.TYPE)]
119
120 # Remove
121 self.props.pop(self.ID)
122 self.props.pop(self.VERSION)
123 node[self.PROPERTIES] = self.props
124
125 tosca[self.TOPOLOGY_TMPL][self.NODE_TMPL][self.name] = node
126
127 return tosca