update from RIFT as of 696b75d2fe9fb046261b08c616f1bcf6c0b54a9b second try
[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, LOGO) = \
27 ('description', 'version', 'vendor', 'id', 'logo')
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 T_NS_PRIMITIVE,
102 ) = \
103 ('tosca.policies.nfv.riftio.vnf_configuration',
104 'tosca.capabilities.riftio.http_endpoint_type',
105 'tosca.capabilities.riftio.mgmt_interface_type',
106 'tosca.capabilities.riftio.monitoring_param',
107 'tosca.nodes.nfv.riftio.VNF1',
108 'tosca.nodes.nfv.riftio.VDU1',
109 'tosca.nodes.nfv.riftio.CP1',
110 'tosca.nodes.riftio.VL1',
111 'tosca.groups.riftio.ConfigPrimitives',
112 'tosca.policies.riftio.ScalingGroup',
113 'tosca.artifacts.Deployment.Image.riftio.QCOW2',
114 'tosca.policies.nfv.riftio.initial_config_primitive',
115 'tosca.artifacts.Deployment.riftio.cloud_init_file',
116 'tosca.policies.nfv.riftio.placement',
117 'tosca.nodes.nfv.riftio.ELAN',
118 'tosca.groups.nfv.VNFFG',
119 'tosca.nodes.nfv.riftio.FP1',
120 'tosca.policies.nfv.riftio.ns_service_primitives',
121 )
122
123 SUPPORT_FILES = ( SRC, DEST, EXISTING) = \
124 ('source', 'destination', 'existing')
125
126 SUPPORT_DIRS = (IMAGE_DIR, SCRIPT_DIR, CLOUD_INIT_DIR, ICON_DIR) = \
127 ('images', 'scripts','cloud_init', 'icons')
128
129 def __init__(self,
130 log,
131 name,
132 type_,
133 yang):
134 self.log = log
135 self.name = name
136 self.type_ = type_
137 self.yang = yang
138 self.id_ = None
139 log.debug(_('Translating YANG node %(name)s of type %(type)s') %
140 {'name': self.name,
141 'type': self.type_})
142
143 # Added the below property menthods to support methods that
144 # works on both toscaparser.NodeType and translator.ToscaResource
145 @property
146 def type(self):
147 return self.type_
148
149 @type.setter
150 def type(self, value):
151 self.type_ = value
152
153 def get_type(self):
154 return self.type_
155
156 @property
157 def id(self):
158 return self.id_
159
160 @id.setter
161 def id(self, value):
162 self.id_ = value
163
164 @property
165 def description(self):
166 return _("Translated from YANG")
167
168 @property
169 def vendor(self):
170 if self._vendor is None:
171 if self.metadata and 'vendor' in self.metadata:
172 self._vendor = self.metadata['vendor']
173 else:
174 self._vendor = "RIFT.io"
175 return self._vendor
176
177 @property
178 def version(self):
179 if self._version is None:
180 if self.metadata and 'version' in self.metadata:
181 self._version = str(self.metadata['version'])
182 else:
183 self._version = '1.0'
184 return self._version
185
186 def __str__(self):
187 return "%s(%s)" % (self.name, self.type)
188
189 def map_yang_name_to_tosca(self, name):
190 new_name = name.replace("_", "-")
191 return new_name
192
193 def map_keys_to_tosca(self, d):
194 if isinstance(d, dict):
195 for key in d.keys():
196 d[self.map_yang_name_to_tosca(key)] = \
197 self.map_keys_to_tosca(d.pop(key))
198 return d
199 elif isinstance(d, list):
200 arr = []
201 for memb in d:
202 arr.append(self.map_keys_to_tosca(memb))
203 return arr
204 else:
205 return d
206
207 def handle_yang(self):
208 self.log.debug(_("Need to implement handle_yang for {0}").
209 format(self))
210
211 def remove_ignored_fields(self, d):
212 '''Remove keys in dict not used'''
213 for key in self.IGNORE_FIELDS:
214 if key in d:
215 d.pop(key)
216
217 def generate_tosca_type(self, tosca):
218 self.log.debug(_("Need to implement generate_tosca_type for {0}").
219 format(self))
220
221 def generate_tosca_model(self, tosca):
222 self.log.debug(_("Need to implement generate_tosca_model for {0}").
223 format(self))
224
225 def get_supporting_files(self):
226 """Get list of other required files for each resource"""
227 pass
228
229 def get_matching_item(self, name, items, key=None):
230 if key is None:
231 key = 'name'
232 for entry in items:
233 if entry[key] == name:
234 return entry
235 return None