* YANG to TOSCA translator
[osm/SO.git] / common / python / rift / mano / tosca_translator / rwmano / tosca / tosca_config_primitives.py
1 #
2 # Copyright 2016 RIFT.io Inc
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 #
16
17
18 from rift.mano.tosca_translator.common.utils import _
19 from rift.mano.tosca_translator.common.utils import convert_keys_to_python
20 from rift.mano.tosca_translator.rwmano.syntax.mano_resource import ManoResource
21
22 from toscaparser.common.exception import ValidationError
23
24 # Name used to dynamically load appropriate map class.
25 TARGET_CLASS_NAME = 'ToscaConfigPrimitives'
26
27
28 class ToscaConfigPrimitives(ManoResource):
29 '''Translate TOSCA node type tosca.groups.riftio.config_primitives.'''
30
31 toscatype = 'tosca.groups.riftio.ConfigPrimitives'
32
33 def __init__(self, log, name, details, metadata=None):
34 # TODO(Philip):Not inheriting for ManoResource, as there is no
35 # instance from parser
36 self.log = log
37 self.name = name
38 self.details = details
39 self.type_ = 'config-prim'
40 self.metadata = metadata
41 self.nodes = []
42
43 def __str__(self):
44 return "%s(%s)" % (self.name, self.type)
45
46 def handle_properties(self, nodes):
47 tosca_props = self.details['properties']
48 self.log.debug(_("{0} with tosca properties: {1}").
49 format(self, tosca_props))
50
51 members = self.details['members']
52 for member in members:
53 found = False
54 for node in nodes:
55 if member == node.name:
56 self.nodes.append(node)
57 found = True
58 break
59 if not found:
60 err_msg = _("{0}: Did not find the member node {1} in "
61 "resources list"). \
62 format(self, node)
63 self.log.error(err_msg)
64 raise ValidationError(message=err_msg)
65
66 self.primitives = tosca_props['primitives']
67
68 def get_primitive(self, primitive):
69 if primitive in self.primitives:
70 return self.primitives[primitive]
71
72 def validate_primitive(self, primitive):
73 if primitive in self.primitives:
74 return True
75 return False
76
77 def generate_yang_model_gi(self, nsd, vnfds):
78 for name, value in self.primitives.items():
79 prim = {'name': name}
80 props = convert_keys_to_python(value)
81 try:
82 prim.update(props)
83 except Exception as e:
84 err_msg = _("{0} Exception nsd config primitives {1}: {2}"). \
85 format(self, props, e)
86 self.log.error(err_msg)
87 raise e
88 nsd.service_primitive.add().from_dict(prim)
89
90 def generate_yang_model(self, nsd, vnfds, use_gi=False):
91 """Generate yang model for the node"""
92 self.log.debug(_("Generate YANG model for {0}").
93 format(self))
94
95 if use_gi:
96 return self.generate_yang_model_gi(nsd, vnfds)
97
98 nsd['service-primitive'] = []
99 for name, value in self.primitives.items():
100 prim = {'name': name}
101 prim.update(self.map_keys_to_mano(value))
102 nsd['service-primitive'].append(prim)