* YANG to TOSCA VNFFG Support
[osm/SO.git] / common / python / rift / mano / yang_translator / rwmano / syntax / tosca_resource.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 rift.mano.yang_translator.common.utils import _
17
18
19 class ToscaResource(object):
20 '''Base class for YANG node type translation to RIFT.io SUBSTITUTION_MAPPINGtype.'''
21
22 # Used when creating the resource, so keeping separate
23 # from REQUIRED_FIELDS below
24 NAME = 'name'
25
26 REQUIRED_FIELDS = (DESC, VERSION, VENDOR, ID) = \
27 ('description', 'version', 'vendor', 'id')
28
29 COMMON_FIELDS = (PATH, PORT, HOST, XPATH, TYPE, COUNT, FILE,
30 NFV_COMPUTE, HOST_EPA, VSWITCH_EPA, HYPERVISOR_EPA, GUEST_EPA) = \
31 ('path', 'port', 'host', 'xpath', 'type', 'count', 'file', 'nfv_compute',
32 'host_epa', 'vswitch_epa', 'hypervisor_epa', 'guest_epa')
33
34 IGNORE_FIELDS = ['short_name']
35
36 FIELD_TYPES = (STRING, MAP, INTEGER, BOOL) = \
37 ('string', 'map', 'integer', 'boolean',)
38
39 YANG_KEYS = (VLD, NSD, VNFD, VDU, DASHBOARD_PARAMS,
40 CONFIG_ATTR, CONFIG_TMPL,
41 CONFIG_TYPE, CONFIG_DETAILS, EXT_INTF,
42 VIRT_INTF, POLL_INTVL_SECS,
43 MEM_VNF_INDEX_REF, VNFD_ID_REF,
44 MEM_VNF_INDEX, VNF_CONFIG, TYPE_Y,
45 USER_DEF_SCRIPT, SEQ, PARAM,
46 VALUE, START_BY_DFLT, VNFFGD, ) = \
47 ('vld', 'nsd', 'vnfd', 'vdu', 'dashboard_params',
48 'config_attributes', 'config_template',
49 'config_type', 'config_details', 'external_interface',
50 'virtual_interface', 'polling_interval_secs',
51 'member_vnf_index_ref', 'vnfd_id_ref',
52 'member_vnf_index', 'vnf_configuration', 'type_yang',
53 'user_defined_script', 'seq', 'parameter',
54 'value', 'start_by_default', 'vnffgd',)
55
56 TOSCA_FIELDS = (DERIVED_FROM, PROPERTIES, DEFAULT, REQUIRED,
57 NO, CONSTRAINTS, REALTIONSHIPS,
58 REQUIREMENTS, UNBOUND, NODE,
59 OCCURENCES, PRIMITIVES, MEMBERS,
60 POLL_INTVL, DEFAULT, TRUE, FALSE,) = \
61 ('derived_from', 'properties', 'default', 'required',
62 'no', 'constraints', 'relationships',
63 'requirements', 'UNBOUND', 'node',
64 'occurences', 'primitives', 'members',
65 'polling_interval', 'default', 'true', 'false')
66
67 TOSCA_SEC = (DATA_TYPES, CAPABILITY_TYPES, NODE_TYPES,
68 GROUP_TYPES, POLICY_TYPES, REQUIREMENTS,
69 ARTIFACTS, PROPERTIES, INTERFACES,
70 CAPABILITIES, RELATIONSHIP,
71 ARTIFACT_TYPES, TARGETS) = \
72 ('data_types', 'capability_types', 'node_types',
73 'group_types', 'policy_types', 'requirements',
74 'artifacts', 'properties', 'interfaces',
75 'capabilities', 'relationship',
76 'artifact_types', 'targets')
77
78 TOSCA_TMPL = (INPUTS, NODE_TMPL, GROUPS, POLICIES,
79 METADATA, TOPOLOGY_TMPL, OUTPUTS, SUBSTITUTION_MAPPING, IMPORT) = \
80 ('inputs', 'node_templates', 'groups', 'policies',
81 'metadata', 'topology_template', 'outputs', 'substitution_mappings', 'imports')
82
83 TOSCA_DERIVED = (
84 T_VNF_CONFIG,
85 T_HTTP_EP,
86 T_MGMT_INTF,
87 T_MON_PARAM,
88 T_VNF1,
89 T_VDU1,
90 T_CP1,
91 T_VL1,
92 T_CONF_PRIM,
93 T_SCALE_GRP,
94 T_ARTF_QCOW2,
95 T_INITIAL_CFG,
96 T_ARTF_CLOUD_INIT,
97 T_PLACEMENT,
98 T_ELAN,
99 T_VNFFG,
100 T_FP,
101 ) = \
102 ('tosca.policies.nfv.riftio.vnf_configuration',
103 'tosca.capabilities.riftio.http_endpoint_type',
104 'tosca.capabilities.riftio.mgmt_interface_type',
105 'tosca.capabilities.riftio.monitoring_param',
106 'tosca.nodes.nfv.riftio.VNF1',
107 'tosca.nodes.nfv.riftio.VDU1',
108 'tosca.nodes.nfv.riftio.CP1',
109 'tosca.nodes.riftio.VL1',
110 'tosca.groups.riftio.ConfigPrimitives',
111 'tosca.policies.riftio.ScalingGroup',
112 'tosca.artifacts.Deployment.Image.riftio.QCOW2',
113 'tosca.policies.nfv.riftio.initial_config_primitive',
114 'tosca.artifacts.Deployment.riftio.cloud_init_file',
115 'tosca.policies.nfv.riftio.placement',
116 'tosca.nodes.nfv.riftio.ELAN',
117 'tosca.groups.nfv.VNFFG',
118 'tosca.nodes.nfv.riftio.FP1',
119 )
120
121 SUPPORT_FILES = ( SRC, DEST, EXISTING) = \
122 ('source', 'destination', 'existing')
123
124 SUPPORT_DIRS = (IMAGE_DIR, SCRIPT_DIR, CLOUD_INIT_DIR) = \
125 ('images', 'scripts','cloud_init')
126
127 def __init__(self,
128 log,
129 name,
130 type_,
131 yang):
132 self.log = log
133 self.name = name
134 self.type_ = type_
135 self.yang = yang
136 self.id_ = None
137 log.debug(_('Translating YANG node %(name)s of type %(type)s') %
138 {'name': self.name,
139 'type': self.type_})
140
141 # Added the below property menthods to support methods that
142 # works on both toscaparser.NodeType and translator.ToscaResource
143 @property
144 def type(self):
145 return self.type_
146
147 @type.setter
148 def type(self, value):
149 self.type_ = value
150
151 def get_type(self):
152 return self.type_
153
154 @property
155 def id(self):
156 return self.id_
157
158 @id.setter
159 def id(self, value):
160 self.id_ = value
161
162 @property
163 def description(self):
164 return _("Translated from YANG")
165
166 @property
167 def vendor(self):
168 if self._vendor is None:
169 if self.metadata and 'vendor' in self.metadata:
170 self._vendor = self.metadata['vendor']
171 else:
172 self._vendor = "RIFT.io"
173 return self._vendor
174
175 @property
176 def version(self):
177 if self._version is None:
178 if self.metadata and 'version' in self.metadata:
179 self._version = str(self.metadata['version'])
180 else:
181 self._version = '1.0'
182 return self._version
183
184 def __str__(self):
185 return "%s(%s)" % (self.name, self.type)
186
187 def map_yang_name_to_tosca(self, name):
188 new_name = name.replace("_", "-")
189 return new_name
190
191 def map_keys_to_tosca(self, d):
192 if isinstance(d, dict):
193 for key in d.keys():
194 d[self.map_yang_name_to_tosca(key)] = \
195 self.map_keys_to_tosca(d.pop(key))
196 return d
197 elif isinstance(d, list):
198 arr = []
199 for memb in d:
200 arr.append(self.map_keys_to_tosca(memb))
201 return arr
202 else:
203 return d
204
205 def handle_yang(self):
206 self.log.debug(_("Need to implement handle_yang for {0}").
207 format(self))
208
209 def remove_ignored_fields(self, d):
210 '''Remove keys in dict not used'''
211 for key in self.IGNORE_FIELDS:
212 if key in d:
213 d.pop(key)
214
215 def generate_tosca_type(self, tosca):
216 self.log.debug(_("Need to implement generate_tosca_type for {0}").
217 format(self))
218
219 def generate_tosca_model(self, tosca):
220 self.log.debug(_("Need to implement generate_tosca_model for {0}").
221 format(self))
222
223 def get_supporting_files(self):
224 """Get list of other required files for each resource"""
225 pass
226
227 def get_matching_item(self, name, items, key=None):
228 if key is None:
229 key = 'name'
230 for entry in items:
231 if entry[key] == name:
232 return entry
233 return None