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
21 from toscaparser
.functions
import GetInput
22 from rift
.mano
.tosca_translator
.common
.utils
import convert_keys_to_python
24 from toscaparser
.common
.exception
import ValidationError
27 # Name used to dynamically load appropriate map class.
28 TARGET_CLASS_NAME
= 'ToscaPlacementGroup'
31 class ToscaPlacementGroup(ManoResource
):
32 '''Translate TOSCA node type tosca.policies.Scaling.'''
34 toscatype
= 'tosca.policies.nfv.riftio.placement'
38 def __init__(self
, log
, policy
, metadata
=None, vnf_name
=None):
40 self
.name
= policy
.name
41 self
.type_
= 'place-grp'
42 self
.metadata
= metadata
45 self
._vnf
_name
= vnf_name
48 return "%s(%s)" % (self
.name
, self
.type)
50 def handle_properties(self
, nodes
, groups
):
51 tosca_props
= self
.get_policy_props()
52 self
.properties
['name'] = tosca_props
['name']
53 self
.properties
['strategy'] = tosca_props
['strategy']
54 self
.properties
['requirement'] = tosca_props
['requirement']
55 if self
._vnf
_name
is None:
56 self
.properties
['member-vnfd'] = []
58 for node
in self
.policy
.get_targets_list():
59 vnf_node
= self
.get_node_with_name(node
.name
, nodes
)
61 prop
['member-vnf-index-ref'] = index_count
62 prop
['vnfd-id-ref'] = vnf_node
.id
63 self
.properties
['member-vnfd'].append(prop
)
64 index_count
= index_count
+ 1
66 self
.properties
['member-vdus'] = []
67 for node
in self
.policy
.get_targets_list():
68 vdu_node
= self
.get_node_with_name(node
.name
, nodes
)
70 prop
['member-vdu-ref'] = vdu_node
.name
71 self
.properties
['member-vdus'].append(prop
)
73 def get_yang_model_gi(self
, nsd
, vnfds
):
74 props
= convert_keys_to_python(self
.properties
)
76 if self
._vnf
_name
is None:
77 nsd
.placement_groups
.add().from_dict(props
)
78 except Exception as e
:
79 err_msg
= _("{0} Exception nsd placement-groups from dict {1}: {2}"). \
80 format(self
, props
, e
)
81 self
.log
.error(err_msg
)
84 def generate_yang_model(self
, nsd
, vnfds
, use_gi
=False):
86 return self
.get_yang_model_gi(nsd
, vnfds
)
87 if 'placement-groups' not in nsd
:
88 nsd
['placement-groups'] = []
90 for key
, value
in self
.properties
.items():
92 nsd
['placement-groups'].append(prim
)
94 def generate_yang_submodel_gi(self
, vnfd
):
98 props
= convert_keys_to_python(self
.properties
)
99 vnfd
.placement_groups
.add().from_dict(props
)
100 except Exception as e
:
101 err_msg
= _("{0} Exception policy from dict {1}: {2}"). \
102 format(self
, props
, e
)
103 self
.log
.error(err_msg
)
106 def get_policy_props(self
):
109 for prop
in self
.policy
.get_properties_objects():
110 if isinstance(prop
.value
, GetInput
):
111 tosca_props
[prop
.name
] = {'get_param': prop
.value
.input_name
}
113 tosca_props
[prop
.name
] = prop
.value