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