Update parse nsd package logic
[vfc/nfvo/lcm.git] / lcm / pub / utils / toscaparser / baseinfomodel.py
1 import os
2 import urllib
3
4 from toscaparser.tosca_template import ToscaTemplate
5
6 from lcm.pub.utils.toscaparser.dataentityext import DataEntityExt
7
8
9 class BaseInfoModel(object):
10     def __init__(self, path, params):
11         pass
12
13     def buildToscaTemplate(self, path, params):
14         file_name = None
15         try:
16             file_name = self._check_download_file(path)
17             valid_params = self._validate_input_params(file_name, params)
18             return self._create_tosca_template(file_name, valid_params)
19         finally:
20             if file_name != None and file_name != path and os.path.exists(file_name):
21                 try:
22                     os.remove(file_name)
23                 except Exception, e:
24                     pass
25
26     def _validate_input_params(self, path, params):
27         valid_params = {}
28         if params and len(params) > 0:
29             tmp = self._create_tosca_template(path, None)
30             for key,value in params.items():
31                 if hasattr(tmp, 'inputs') and len(tmp.inputs) > 0:
32                     for input_def in tmp.inputs:
33                         if (input_def.name == key):
34                             valid_params[key] = DataEntityExt.validate_datatype(input_def.type, value)
35
36         return valid_params
37
38     def _create_tosca_template(self, file_name, valid_params):
39         tosca_tpl = None
40         try:
41             tosca_tpl = ToscaTemplate(file_name, valid_params)
42             print "-----------------------------"
43             print '\n'.join(['%s:%s' % item for item in tosca_tpl.__dict__.items()])
44             print "-----------------------------"
45             return tosca_tpl
46         finally:
47             pass
48             # if tosca_tpl != None and hasattr(tosca_tpl, "temp_dir") and os.path.exists(tosca_tpl.temp_dir):
49             #     try:
50             #         shutil.rmtree(tosca_tpl.temp_dir)
51             #     except Exception, e:
52             #         pass
53             #         # if tosca_tpl != None and tosca_tpl.temp_dir != None and os.path.exists(tosca_tpl.temp_dir):
54             #         #     try:
55             #         #         shutil.rmtree(tosca_tpl.temp_dir)
56             #         #     except Exception, e:
57             #         #         pass
58
59     def _check_download_file(self, path):
60         if (path.startswith("ftp") or path.startswith("sftp")):
61             return self.downloadFileFromFtpServer(path)
62         elif (path.startswith("http")):
63             return self.download_file_from_httpserver(path)
64         return path
65
66     def download_file_from_httpserver(self, path):
67         path = path.encode("utf-8")
68         tmps = str.split(path, '/')
69         localFileName = tmps[len(tmps) - 1]
70         urllib.urlretrieve(path, localFileName)
71         return localFileName
72
73     def downloadFileFromFtpServer(self, path):
74         path = path.encode("utf-8")
75         tmp = str.split(path, '://')
76         protocol = tmp[0]
77         tmp = str.split(tmp[1], ':')
78         if len(tmp) == 2:
79             userName = tmp[0]
80             tmp = str.split(tmp[1], '@')
81             userPwd = tmp[0]
82             index = tmp[1].index('/')
83             hostIp = tmp[1][0:index]
84             remoteFileName = tmp[1][index:len(tmp[1])]
85             if protocol.lower() == 'ftp':
86                 hostPort = 21
87             else:
88                 hostPort = 22
89
90         if len(tmp) == 3:
91             userName = tmp[0]
92             userPwd = str.split(tmp[1], '@')[0]
93             hostIp = str.split(tmp[1], '@')[1]
94             index = tmp[2].index('/')
95             hostPort = tmp[2][0:index]
96             remoteFileName = tmp[2][index:len(tmp[2])]
97
98         localFileName = str.split(remoteFileName, '/')
99         localFileName = localFileName[len(localFileName) - 1]
100
101         if protocol.lower() == 'sftp':
102             self.sftp_get(userName, userPwd, hostIp, hostPort, remoteFileName, localFileName)
103         else:
104             self.ftp_get(userName, userPwd, hostIp, hostPort, remoteFileName, localFileName)
105         return localFileName