Update vfc catalog nsd package parser code 07/12607/1
authorying.yunlong <ying.yunlong@zte.com.cn>
Fri, 15 Sep 2017 01:59:47 +0000 (09:59 +0800)
committerying.yunlong <ying.yunlong@zte.com.cn>
Fri, 15 Sep 2017 01:59:47 +0000 (09:59 +0800)
Change-Id: I4e59107b3ed8aa7c9df6448dad02fd46f4618967
Issue-ID: VFC-359
Signed-off-by: ying.yunlong <ying.yunlong@zte.com.cn>
catalog/pub/utils/toscaparser/__init__.py
catalog/pub/utils/toscaparser/basemodel.py
catalog/pub/utils/toscaparser/nsdmodel.py

index 9ac71d3..56c020e 100644 (file)
@@ -11,6 +11,7 @@
 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 # See the License for the specific language governing permissions and
 # limitations under the License.
+
 import json
 
 from catalog.pub.utils.toscaparser.nsdmodel import EtsiNsdInfoModel
index 6e2685c..1d6d486 100644 (file)
 # limitations under the License.
 
 import copy
+import ftplib
 import json
 import os
 import re
 import shutil
 import urllib
 
+import paramiko
 from toscaparser.functions import GetInput
 from toscaparser.tosca_template import ToscaTemplate
 
@@ -115,6 +117,32 @@ class BaseInfoModel(object):
             self.ftp_get(userName, userPwd, hostIp, hostPort, remoteFileName, localFileName)
         return localFileName
 
+    def sftp_get(self, userName, userPwd, hostIp, hostPort, remoteFileName, localFileName):
+        # return
+        t = None
+        try:
+            t = paramiko.Transport(hostIp, int(hostPort))
+            t.connect(username=userName, password=userPwd)
+            sftp = paramiko.SFTPClient.from_transport(t)
+            sftp.get(remoteFileName, localFileName)
+        finally:
+            if t != None:
+                t.close()
+
+
+    def ftp_get(self, userName, userPwd, hostIp, hostPort, remoteFileName, localFileName):
+        f = None
+        try:
+            ftp = ftplib.FTP()
+            ftp.connect(hostIp, hostPort)
+            ftp.login(userName, userPwd)
+            f = open(localFileName, 'wb')
+            ftp.retrbinary('RETR ' + remoteFileName, f.write, 1024)
+            f.close()
+        finally:
+            if f != None:
+                f.close()
+
     def buidMetadata(self, tosca):
         if 'metadata' in tosca.tpl:
             self.metadata = copy.deepcopy(tosca.tpl['metadata'])
@@ -202,6 +230,9 @@ class BaseInfoModel(object):
         return node['nodeType'].upper().find('.VIRTUALLINK.') >= 0 or node['nodeType'].upper().find('.VL.') >= 0 or \
                node['nodeType'].upper().endswith('.VIRTUALLINK') or node['nodeType'].upper().endswith('.VL')
 
+    def isService(self, node):
+        return node['nodeType'].upper().find('.SERVICE.') >= 0 or node['nodeType'].upper().endswith('.SERVICE')
+
     def get_requirement_node_name(self, req_value):
         return self.get_prop_from_obj(req_value, 'node')
 
@@ -278,4 +309,34 @@ class BaseInfoModel(object):
         for node in node_templates:
             if node['name'] == name:
                 return node
-        return None
\ No newline at end of file
+        return None
+
+    def get_all_nested_ns(self, nodes):
+        nss = []
+        for node in nodes:
+            if self.is_nested_ns(node):
+                ns = {}
+                ns['ns_id'] = node['name']
+                ns['description'] = node['description']
+                ns['properties'] = node['properties']
+                ns['networks'] = self.get_networks(node)
+
+                nss.append(ns)
+        return nss
+
+    def is_nested_ns(self, node):
+        return node['nodeType'].upper().find('.NS.') >= 0 or node['nodeType'].upper().endswith('.NS')
+
+    def isVdu(self, node):
+        return node['nodeType'].upper().find('.VDU.') >= 0 or node['nodeType'].upper().endswith('.VDU')
+
+    def getCapabilityByName(self, node, capabilityName):
+        if 'capabilities' in node and capabilityName in node['capabilities']:
+            return node['capabilities'][capabilityName]
+        return None
+
+    def get_node_vdu_id(self, node):
+        vdu_ids = map(lambda x: self.get_requirement_node_name(x), self.getVirtualbindings(node))
+        if len(vdu_ids) > 0:
+            return vdu_ids[0]
+        return ""
index bc6d921..1080c6c 100644 (file)
@@ -16,6 +16,7 @@ import functools
 
 from catalog.pub.utils.toscaparser.basemodel import BaseInfoModel
 
+
 class EtsiNsdInfoModel(BaseInfoModel):
 
     def __init__(self, path, params):
@@ -38,6 +39,10 @@ class EtsiNsdInfoModel(BaseInfoModel):
         self.fps = self._get_all_fp(nodeTemplates)
         self.vnffgs = self._get_all_vnffg(tosca.topology_template.groups)
         self.server_groups = self.get_all_server_group(tosca.topology_template.groups)
+        self.ns_exposed = self.get_all_endpoint_exposed(tosca.topology_template)
+        self.policies = self._get_policies_scaling(tosca.topology_template.policies)
+        self.ns_flavours = self.get_all_flavour(tosca.topology_template.groups)
+        self.nested_ns = self.get_all_nested_ns(nodeTemplates)
 
 
     def buildInputs(self, top_inputs):
@@ -286,3 +291,71 @@ class EtsiNsdInfoModel(BaseInfoModel):
     def _isServerGroup(self, group):
         return group.type.upper().find('.AFFINITYORANTIAFFINITYGROUP.') >= 0 or group.type.upper().endswith(
             '.AFFINITYORANTIAFFINITYGROUP')
+
+    def get_all_endpoint_exposed(self, topo_tpl):
+        if 'substitution_mappings' in topo_tpl.tpl:
+            external_cps = self._get_external_cps(topo_tpl.tpl['substitution_mappings'])
+            forward_cps = self._get_forward_cps(topo_tpl.tpl['substitution_mappings'])
+            return {"external_cps": external_cps, "forward_cps": forward_cps}
+        return {}
+
+    def _get_external_cps(self, subs_mappings):
+        external_cps = []
+        if 'requirements' in subs_mappings:
+            for key, value in subs_mappings['requirements'].items():
+                if isinstance(value, list) and len(value) > 0:
+                    external_cps.append({"key_name": key, "cpd_id": value[0]})
+                else:
+                    external_cps.append({"key_name": key, "cpd_id": value})
+        return external_cps
+
+    def _get_forward_cps(self, subs_mappings):
+        forward_cps = []
+        if 'capabilities' in subs_mappings:
+            for key, value in subs_mappings['capabilities'].items():
+                if isinstance(value, list) and len(value) > 0:
+                    forward_cps.append({"key_name": key, "cpd_id": value[0]})
+                else:
+                    forward_cps.append({"key_name": key, "cpd_id": value})
+        return forward_cps
+
+    def _get_policies_scaling(self, top_policies):
+        policies_scaling = []
+        scaling_policies = self.get_scaling_policies(top_policies)
+        if len(scaling_policies) > 0:
+            policies_scaling.append({"scaling": scaling_policies})
+        return policies_scaling
+
+    def get_policies_by_keyword(self, top_policies, keyword):
+        ret = []
+        for policy in top_policies:
+            if policy.type.upper().find(keyword) >= 0:
+                tmp = {}
+                tmp['policy_id'] = policy.name
+                tmp['description'] = policy.description
+                if 'properties' in policy.entity_tpl:
+                    tmp['properties'] = policy.entity_tpl['properties']
+                tmp['targets'] = policy.targets
+                ret.append(tmp)
+
+        return ret
+
+    def get_scaling_policies(self, top_policies):
+        return self.get_policies_by_keyword(top_policies, '.SCALING')
+
+    def get_all_flavour(self, groups):
+        rets = []
+        for group in groups:
+            if self._isFlavour(group):
+                ret = {}
+                ret['flavour_id'] = group.name
+                ret['description'] = group.description
+                if 'properties' in group.tpl:
+                    ret['properties'] = group.tpl['properties']
+                ret['members'] = group.members
+
+                rets.append(ret)
+        return rets
+
+    def _isFlavour(self, group):
+        return group.type.upper().find('FLAVOUR') >= 0