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