Update parse nsd package logic 01/8001/1
authorying.yunlong <ying.yunlong@zte.com.cn>
Fri, 18 Aug 2017 09:12:19 +0000 (17:12 +0800)
committerying.yunlong <ying.yunlong@zte.com.cn>
Fri, 18 Aug 2017 09:12:19 +0000 (17:12 +0800)
In vfc lcm project ,add parse nsd package logic.

Change-Id: I2535e4be32c681eebfedff93d8720b145c0b8b5c
Issue-ID: VFC-110
Signed-off-by: ying.yunlong <ying.yunlong@zte.com.cn>
lcm/pub/utils/toscaparser/baseinfomodel.py
lcm/pub/utils/toscaparser/dataentityext.py [new file with mode: 0644]
lcm/pub/utils/toscaparser/etsinsdinfomodel.py

index a40383f..0e186f6 100644 (file)
@@ -1,3 +1,105 @@
+import os
+import urllib
+
+from toscaparser.tosca_template import ToscaTemplate
+
+from lcm.pub.utils.toscaparser.dataentityext import DataEntityExt
+
+
 class BaseInfoModel(object):
     def __init__(self, path, params):
-        pass
\ No newline at end of file
+        pass
+
+    def buildToscaTemplate(self, path, params):
+        file_name = None
+        try:
+            file_name = self._check_download_file(path)
+            valid_params = self._validate_input_params(file_name, params)
+            return self._create_tosca_template(file_name, valid_params)
+        finally:
+            if file_name != None and file_name != path and os.path.exists(file_name):
+                try:
+                    os.remove(file_name)
+                except Exception, e:
+                    pass
+
+    def _validate_input_params(self, path, params):
+        valid_params = {}
+        if params and len(params) > 0:
+            tmp = self._create_tosca_template(path, None)
+            for key,value in params.items():
+                if hasattr(tmp, 'inputs') and len(tmp.inputs) > 0:
+                    for input_def in tmp.inputs:
+                        if (input_def.name == key):
+                            valid_params[key] = DataEntityExt.validate_datatype(input_def.type, value)
+
+        return valid_params
+
+    def _create_tosca_template(self, file_name, valid_params):
+        tosca_tpl = None
+        try:
+            tosca_tpl = ToscaTemplate(file_name, valid_params)
+            print "-----------------------------"
+            print '\n'.join(['%s:%s' % item for item in tosca_tpl.__dict__.items()])
+            print "-----------------------------"
+            return tosca_tpl
+        finally:
+            pass
+            # if tosca_tpl != None and hasattr(tosca_tpl, "temp_dir") and os.path.exists(tosca_tpl.temp_dir):
+            #     try:
+            #         shutil.rmtree(tosca_tpl.temp_dir)
+            #     except Exception, e:
+            #         pass
+            #         # if tosca_tpl != None and tosca_tpl.temp_dir != None and os.path.exists(tosca_tpl.temp_dir):
+            #         #     try:
+            #         #         shutil.rmtree(tosca_tpl.temp_dir)
+            #         #     except Exception, e:
+            #         #         pass
+
+    def _check_download_file(self, path):
+        if (path.startswith("ftp") or path.startswith("sftp")):
+            return self.downloadFileFromFtpServer(path)
+        elif (path.startswith("http")):
+            return self.download_file_from_httpserver(path)
+        return path
+
+    def download_file_from_httpserver(self, path):
+        path = path.encode("utf-8")
+        tmps = str.split(path, '/')
+        localFileName = tmps[len(tmps) - 1]
+        urllib.urlretrieve(path, localFileName)
+        return localFileName
+
+    def downloadFileFromFtpServer(self, path):
+        path = path.encode("utf-8")
+        tmp = str.split(path, '://')
+        protocol = tmp[0]
+        tmp = str.split(tmp[1], ':')
+        if len(tmp) == 2:
+            userName = tmp[0]
+            tmp = str.split(tmp[1], '@')
+            userPwd = tmp[0]
+            index = tmp[1].index('/')
+            hostIp = tmp[1][0:index]
+            remoteFileName = tmp[1][index:len(tmp[1])]
+            if protocol.lower() == 'ftp':
+                hostPort = 21
+            else:
+                hostPort = 22
+
+        if len(tmp) == 3:
+            userName = tmp[0]
+            userPwd = str.split(tmp[1], '@')[0]
+            hostIp = str.split(tmp[1], '@')[1]
+            index = tmp[2].index('/')
+            hostPort = tmp[2][0:index]
+            remoteFileName = tmp[2][index:len(tmp[2])]
+
+        localFileName = str.split(remoteFileName, '/')
+        localFileName = localFileName[len(localFileName) - 1]
+
+        if protocol.lower() == 'sftp':
+            self.sftp_get(userName, userPwd, hostIp, hostPort, remoteFileName, localFileName)
+        else:
+            self.ftp_get(userName, userPwd, hostIp, hostPort, remoteFileName, localFileName)
+        return localFileName
diff --git a/lcm/pub/utils/toscaparser/dataentityext.py b/lcm/pub/utils/toscaparser/dataentityext.py
new file mode 100644 (file)
index 0000000..02c11ac
--- /dev/null
@@ -0,0 +1,34 @@
+#    Licensed under the Apache License, Version 2.0 (the "License"); you may
+#    not use this file except in compliance with the License. You may obtain
+#    a copy of the License at
+#
+#         http://www.apache.org/licenses/LICENSE-2.0
+#
+#    Unless required by applicable law or agreed to in writing, software
+#    distributed under the License is distributed on an "AS IS" BASIS, 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.
+
+
+from toscaparser.dataentity import DataEntity
+from toscaparser.elements.constraints import Schema
+from toscaparser.common.exception import ExceptionCollector
+
+class DataEntityExt(object):
+    '''A complex data value entity ext.'''
+    
+    @staticmethod
+    def validate_datatype(type, value, entry_schema=None, custom_def=None):
+        if value:
+            if (type == Schema.STRING):
+                return str(value)
+            elif type == Schema.FLOAT:
+                try:
+                    return float(value)
+                except Exception:
+                    ExceptionCollector.appendException(ValueError(('"%s" is not an float.') % value))
+    
+            return DataEntity.validate_datatype(type, value, entry_schema, custom_def)
+        return value
+
index 3a9cee0..693a7fd 100644 (file)
@@ -4,4 +4,9 @@ from lcm.pub.utils.toscaparser.baseinfomodel import BaseInfoModel
 class EtsiNsdInfoModel(BaseInfoModel):
 
     def __init__(self, path, params):
-        pass
+        tosca = self.buildToscaTemplate(path, params)
+        self.parseModel(tosca)
+
+
+    def parseModel(self, tosca):
+        pass
\ No newline at end of file