1 # Copyright 2016 RIFT.io Inc
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
16 from rift
.mano
.yang_translator
.common
.utils
import _
19 class ToscaResource(object):
20 '''Base class for YANG node type translation to RIFT.io TOSCA type.'''
22 # Used when creating the resource, so keeping separate
23 # from REQUIRED_FIELDS below
26 REQUIRED_FIELDS
= (DESC
, VERSION
, VENDOR
, ID
) = \
27 ('description', 'version', 'vendor', 'id')
29 COMMON_FIELDS
= (PATH
, PORT
, HOST
, XPATH
, TYPE
, COUNT
, FILE
) = \
30 ('path', 'port', 'host', 'xpath', 'type', 'count', 'file')
32 IGNORE_FIELDS
= ['short_name']
34 FIELD_TYPES
= (STRING
, MAP
, INTEGER
, BOOL
) = \
35 ('string', 'map', 'integer', 'boolean',)
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',)
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')
65 TOSCA_SEC
= (DATA_TYPES
, CAPABILITY_TYPES
, NODE_TYPES
,
66 GROUP_TYPES
, POLICY_TYPES
, REQUIREMENTS
,
67 ARTIFACTS
, PROPERTIES
, INTERFACES
,
68 CAPABILITIES
, RELATIONSHIP
,
70 ('data_types', 'capability_types', 'node_types',
71 'group_types', 'policy_types', 'requirements',
72 'artifacts', 'properties', 'interfaces',
73 'capabilities', 'relationship',
76 TOSCA_TMPL
= (INPUTS
, NODE_TMPL
, GROUPS
, POLICIES
,
77 METADATA
, TOPOLOGY_TMPL
, OUTPUTS
) = \
78 ('inputs', 'node_templates', 'groups', 'policies',
79 'metadata', 'topology_template', 'outputs')
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'
109 SUPPORT_FILES
= ( SRC
, DEST
, EXISTING
) = \
110 ('source', 'destination', 'existing')
112 SUPPORT_DIRS
= (IMAGE_DIR
, SCRIPT_DIR
,) = \
113 ('images', 'scripts',)
125 log
.debug(_('Translating YANG node %(name)s of type %(type)s') %
129 # Added the below property menthods to support methods that
130 # works on both toscaparser.NodeType and translator.ToscaResource
136 def type(self
, value
):
151 def description(self
):
152 return _("Translated from YANG")
156 if self
._vendor
is None:
157 if self
.metadata
and 'vendor' in self
.metadata
:
158 self
._vendor
= self
.metadata
['vendor']
160 self
._vendor
= "RIFT.io"
165 if self
._version
is None:
166 if self
.metadata
and 'version' in self
.metadata
:
167 self
._version
= str(self
.metadata
['version'])
169 self
._version
= '1.0'
173 return "%s(%s)" % (self
.name
, self
.type)
175 def map_yang_name_to_tosca(self
, name
):
176 new_name
= name
.replace("_", "-")
179 def map_keys_to_tosca(self
, d
):
180 if isinstance(d
, dict):
182 d
[self
.map_yang_name_to_tosca(key
)] = \
183 self
.map_keys_to_tosca(d
.pop(key
))
185 elif isinstance(d
, list):
188 arr
.append(self
.map_keys_to_tosca(memb
))
193 def handle_yang(self
):
194 self
.log
.debug(_("Need to implement handle_yang for {0}").
197 def remove_ignored_fields(self
, d
):
198 '''Remove keys in dict not used'''
199 for key
in self
.IGNORE_FIELDS
:
203 def generate_tosca_type(self
, tosca
):
204 self
.log
.debug(_("Need to implement generate_tosca_type for {0}").
207 def generate_tosca_model(self
, tosca
):
208 self
.log
.debug(_("Need to implement generate_tosca_model for {0}").
211 def get_supporting_files(self
):
212 """Get list of other required files for each resource"""
215 def get_matching_item(self
, name
, items
, key
=None):
219 if entry
[key
] == name
: