TOSCA to YANG Translator Initial commit
[osm/SO.git] / common / python / rift / mano / tosca_translator / rwmano / tosca / tosca_scaling_group.py
index 25246af..7f427f3 100644 (file)
@@ -18,6 +18,7 @@
 from rift.mano.tosca_translator.common.utils import _
 from rift.mano.tosca_translator.common.utils import convert_keys_to_python
 from rift.mano.tosca_translator.rwmano.syntax.mano_resource import ManoResource
+from toscaparser.functions import GetInput
 
 from toscaparser.common.exception import ValidationError
 
@@ -29,54 +30,62 @@ TARGET_CLASS_NAME = 'ToscaScalingGroup'
 class ToscaScalingGroup(ManoResource):
     '''Translate TOSCA node type tosca.policies.Scaling.'''
 
-    toscatype = 'tosca.policies.riftio.ScalingGroup'
+    toscatype = 'tosca.groups.nfv.riftio.scaling'
 
     IGNORE_PROPS = []
 
-    def __init__(self, log, policy, metadata=None):
+    def __init__(self, log, group, metadata=None):
         # TODO(Philip):Not inheriting for ManoResource, as there is no
         # instance from parser
         self.log = log
-        for name, details in policy.items():
-            self.name = name
-            self.details = details
-            break
+        self.name = group.name
+        #self.details = details
+        self.group = group
         self.type_ = 'scale-grp'
         self.metadata = metadata
         self.properties = {}
 
     def __str__(self):
         return "%s(%s)" % (self.name, self.type)
+    def get_tosca_group_props(self):
+        tosca_props = {}
+        for prop in self.group.get_properties_objects():
+            if isinstance(prop.value, GetInput):
+                tosca_props[prop.name] = {'get_param': prop.value.input_name}
+            else:
+                tosca_props[prop.name] = prop.value
+        return tosca_props
 
     def handle_properties(self, nodes, groups):
-        tosca_props = self.details
+        tosca_props = self.get_tosca_group_props()
         self.log.debug(_("{0} with tosca properties: {1}").
                        format(self, tosca_props))
-        self.properties['name'] = tosca_props['name']
-        self.properties['max-instance-count'] = \
-                                tosca_props['max_instance_count']
-        self.properties['min-instance-count'] = \
-                                tosca_props['min_instance_count']
+        if 'name ' in tosca_props:
+            self.properties['name'] = tosca_props['name']
+        if 'max_instance_count' in tosca_props:
+            self.properties['max-instance-count'] = tosca_props['max_instance_count']
+        if 'min_instance_count' in tosca_props:
+            self.properties['min-instance-count'] = tosca_props['min_instance_count']
         self.properties['vnfd-member'] = []
 
         def _get_node(name):
             for node in nodes:
                 if node.name == name:
                     return node
-
-        for member, count in tosca_props['vnfd_members'].items():
-            node = _get_node(member)
-            if node:
-                memb = {}
-                memb['member-vnf-index-ref'] = node.get_member_vnf_index()
-                memb['count'] = count
-                self.properties['vnfd-member'].append(memb)
-            else:
-                err_msg = _("{0}: Did not find the member node {1} in "
-                            "resources list"). \
-                          format(self, member)
-                self.log.error(err_msg)
-                raise ValidationError(message=err_msg)
+        if 'vnfd_members' in tosca_props:
+            for member, count in tosca_props['vnfd_members'].items():
+                node = _get_node(member)
+                if node:
+                    memb = {}
+                    memb['member-vnf-index-ref'] = node.get_member_vnf_index()
+                    memb['count'] = count
+                    self.properties['vnfd-member'].append(memb)
+                else:
+                    err_msg = _("{0}: Did not find the member node {1} in "
+                                "resources list"). \
+                              format(self, member)
+                    self.log.error(err_msg)
+                    raise ValidationError(message=err_msg)
 
         def _validate_action(action):
             for group in groups:
@@ -85,25 +94,27 @@ class ToscaScalingGroup(ManoResource):
             return False
 
         self.properties['scaling-config-action'] = []
-        for action, value in tosca_props['config_actions'].items():
-            conf = {}
-            if _validate_action(value):
-                conf['trigger'] = action
-                conf['ns-config-primitive-name-ref'] = value
-                self.properties['scaling-config-action'].append(conf)
-            else:
-                err_msg = _("{0}: Did not find the action {1} in "
-                            "config primitives"). \
-                          format(self, action)
-                self.log.error(err_msg)
-                raise ValidationError(message=err_msg)
+        if 'config_actions' in tosca_props:
+            for action, value in tosca_props['config_actions'].items():
+                conf = {}
+                if _validate_action(value):
+                    conf['trigger'] = action
+                    conf['ns-config-primitive-name-ref'] = value
+                    self.properties['scaling-config-action'].append(conf)
+                else:
+                    err_msg = _("{0}: Did not find the action {1} in "
+                                "config primitives"). \
+                              format(self, action)
+                    self.log.error(err_msg)
+                    raise ValidationError(message=err_msg)
 
         self.log.debug(_("{0} properties: {1}").format(self, self.properties))
 
     def get_yang_model_gi(self, nsd, vnfds):
         props = convert_keys_to_python(self.properties)
         try:
-            nsd.scaling_group_descriptor.add().from_dict(props)
+            if len(self.properties['vnfd-member']) > 0:
+                nsd.scaling_group_descriptor.add().from_dict(props)
         except Exception as e:
             err_msg = _("{0} Exception nsd scaling group from dict {1}: {2}"). \
                       format(self, props, e)