+# Copyright 2017 ZTE Corporation.
+#
+# 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.
+
import copy
+import ftplib
import json
+import logging
import os
import re
import shutil
import urllib
+import paramiko
from toscaparser.functions import GetInput
from toscaparser.tosca_template import ToscaTemplate
from lcm.pub.utils.toscaparser.dataentityext import DataEntityExt
+logger = logging.getLogger(__name__)
+
class BaseInfoModel(object):
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):
+ if file_name is not None and file_name != path and os.path.exists(file_name):
try:
os.remove(file_name)
- except Exception, e:
- pass
+ except Exception as e:
+ logger.error("Failed to parse package, error: %s", e.message)
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():
+ 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):
print "-----------------------------"
return tosca_tpl
finally:
- if tosca_tpl != None and hasattr(tosca_tpl, "temp_dir") and os.path.exists(tosca_tpl.temp_dir):
+ if tosca_tpl is not 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
+ except Exception as e:
+ logger.error("Failed to create tosca template, error: %s", e.message)
def _check_download_file(self, path):
if (path.startswith("ftp") or path.startswith("sftp")):
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 is not 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 is not None:
+ f.close()
+
def buidMetadata(self, tosca):
if 'metadata' in tosca.tpl:
self.metadata = copy.deepcopy(tosca.tpl['metadata'])
properties[k] = str(item)
return properties
-
def verify_properties(self, props, inputs, parsed_params):
ret_props = {}
if (props and len(props) > 0):
if (isinstance(req_value, dict)):
if ('node' in req_value and req_value['node'] not in node_template.templates):
continue # No target requirement for aria parser, not add to result.
- rets.append({req_name : req_value})
+ rets.append({req_name: req_value})
return rets
def buildCapabilities(self, nodeTemplate, inputs, ret):
capabilities = json.dumps(nodeTemplate.entity_tpl.get('capabilities', None))
- match = re.findall(r'\{"get_input":\s*"([\w|\-]+)"\}',capabilities)
+ match = re.findall(r'\{"get_input":\s*"([\w|\-]+)"\}', capabilities)
for m in match:
- aa= [input_def for input_def in inputs
- if m == input_def.name][0]
- capabilities = re.sub(r'\{"get_input":\s*"([\w|\-]+)"\}', json.dumps(aa.default), capabilities,1)
+ aa = [input_def for input_def in inputs if m == input_def.name][0]
+ capabilities = re.sub(r'\{"get_input":\s*"([\w|\-]+)"\}', json.dumps(aa.default), capabilities, 1)
if capabilities != 'null':
ret['capabilities'] = json.loads(capabilities)
def buildArtifacts(self, nodeTemplate, inputs, ret):
artifacts = json.dumps(nodeTemplate.entity_tpl.get('artifacts', None))
- match = re.findall(r'\{"get_input":\s*"([\w|\-]+)"\}',artifacts)
+ match = re.findall(r'\{"get_input":\s*"([\w|\-]+)"\}', artifacts)
for m in match:
- aa= [input_def for input_def in inputs
- if m == input_def.name][0]
- artifacts = re.sub(r'\{"get_input":\s*"([\w|\-]+)"\}', json.dumps(aa.default), artifacts,1)
+ aa = [input_def for input_def in inputs if m == input_def.name][0]
+ artifacts = re.sub(r'\{"get_input":\s*"([\w|\-]+)"\}', json.dumps(aa.default), artifacts, 1)
if artifacts != 'null':
ret['artifacts'] = json.loads(artifacts)
return node['nodeType'].upper().find('.CP.') >= 0 or node['nodeType'].upper().endswith('.CP')
def isVl(self, node):
- 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')
+ isvl = node['nodeType'].upper().find('.VIRTUALLINK.') >= 0 or node['nodeType'].upper().find('.VL.') >= 0
+ isvl = isvl or node['nodeType'].upper().endswith('.VIRTUALLINK') or node['nodeType'].upper().endswith('.VL')
+ return isvl
def isService(self, node):
return node['nodeType'].upper().find('.SERVICE.') >= 0 or node['nodeType'].upper().endswith('.SERVICE')
def getVirtualbindings(self, node):
return self.getRequirementByName(node, 'virtualbinding')
-
def getRequirementByName(self, node, requirementName):
requirements = []
if 'requirements' in node:
if 'requirements' in node:
for item in node['requirements']:
for key, value in item.items():
- if key.upper().find('VIRTUALLINK') >=0:
- rets.append({"key_name":key, "vl_id":self.get_requirement_node_name(value)})
+ if key.upper().find('VIRTUALLINK') >= 0:
+ rets.append({"key_name": key, "vl_id": self.get_requirement_node_name(value)})
return rets
def _verify_value(self, value, inputs, parsed_params):
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 ""