2 # Copyright 2016 RIFT.io Inc
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
8 # http://www.apache.org/licenses/LICENSE-2.0
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.
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
22 from toscaparser
.common
.exception
import ValidationError
24 # Name used to dynamically load appropriate map class.
25 TARGET_CLASS_NAME
= 'ToscaConfigPrimitives'
28 class ToscaConfigPrimitives(ManoResource
):
29 '''Translate TOSCA node type tosca.groups.riftio.config_primitives.'''
31 toscatype
= 'tosca.groups.riftio.ConfigPrimitives'
33 def __init__(self
, log
, name
, details
, metadata
=None):
34 # TODO(Philip):Not inheriting for ManoResource, as there is no
35 # instance from parser
38 self
.details
= details
39 self
.type_
= 'config-prim'
40 self
.metadata
= metadata
44 return "%s(%s)" % (self
.name
, self
.type)
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
))
51 members
= self
.details
['members']
52 for member
in members
:
55 if member
== node
.name
:
56 self
.nodes
.append(node
)
60 err_msg
= _("{0}: Did not find the member node {1} in "
63 self
.log
.error(err_msg
)
64 raise ValidationError(message
=err_msg
)
66 self
.primitives
= tosca_props
['primitives']
68 def get_primitive(self
, primitive
):
69 if primitive
in self
.primitives
:
70 return self
.primitives
[primitive
]
72 def validate_primitive(self
, primitive
):
73 if primitive
in self
.primitives
:
77 def generate_yang_model_gi(self
, nsd
, vnfds
):
78 for name
, value
in self
.primitives
.items():
80 props
= convert_keys_to_python(value
)
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
)
88 nsd
.service_primitive
.add().from_dict(prim
)
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}").
96 return self
.generate_yang_model_gi(nsd
, vnfds
)
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
)