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
23 from toscaparser
.common
.exception
import ValidationError
26 # Name used to dynamically load appropriate map class.
27 TARGET_CLASS_NAME
= 'ToscaInitialConfig'
30 class ToscaInitialConfig(ManoResource
):
31 '''Translate TOSCA node type tosca.policies.InitialConfigPrimitive.'''
33 toscatype
= 'tosca.policies.nfv.riftio.initial_config_primitive'
37 def __init__(self
, log
, policy
, metadata
=None):
38 # TODO(Philip):Not inheriting for ManoResource, as there is no
39 # instance from parser
41 self
.name
= policy
.name
43 self
.type_
= 'initial-cfg'
44 self
.metadata
= metadata
49 return "%s(%s)" % (self
.name
, self
.type)
51 def handle_properties(self
, nodes
, groups
):
52 tosca_props
= self
.get_policy_props()
53 self
.log
.debug(_("{0} with tosca properties: {1}").
54 format(self
, tosca_props
))
55 self
.properties
['name'] = tosca_props
['name']
56 self
.properties
['seq'] = \
58 self
.properties
['user-defined-script'] = \
59 tosca_props
['user_defined_script']
60 self
.scripts
.append('../scripts/{}'. \
61 format(tosca_props
['user_defined_script']))
63 if 'parameter' in tosca_props
:
64 self
.properties
['parameter'] = []
65 for name
, value
in tosca_props
['parameter'].items():
66 self
.properties
['parameter'].append({
71 self
.log
.debug(_("{0} properties: {1}").format(self
, self
.properties
))
73 def get_policy_props(self
):
75 for prop
in self
.policy
.get_properties_objects():
76 if isinstance(prop
.value
, GetInput
):
77 tosca_props
[prop
.name
] = {'get_param': prop
.value
.input_name
}
79 tosca_props
[prop
.name
] = prop
.value
81 def get_yang_model_gi(self
, nsd
, vnfds
):
82 props
= convert_keys_to_python(self
.properties
)
84 nsd
.initial_config_primitive
.add().from_dict(props
)
85 except Exception as e
:
86 err_msg
= _("{0} Exception nsd initial config from dict {1}: {2}"). \
87 format(self
, props
, e
)
88 self
.log
.error(err_msg
)
91 def generate_yang_model(self
, nsd
, vnfds
, use_gi
=False):
92 """Generate yang model for the node"""
93 self
.log
.debug(_("Generate YANG model for {0}").
96 for key
in ToscaInitialConfig
.IGNORE_PROPS
:
97 if key
in self
.properties
:
98 self
.properties
.pop(key
)
101 return self
.get_yang_model_gi(nsd
, vnfds
)
103 if 'initial-config-primitive' not in nsd
:
104 nsd
['initial-config-primitive'] = []
106 for key
, value
in self
.properties
.items():
108 nsd
['initial-config-primitive'].append(prim
)
110 def get_supporting_files(self
, files
, desc_id
=None):
111 if not len(self
.scripts
):
114 if desc_id
not in files
:
117 for script
in self
.scripts
:
118 files
[desc_id
].append({