+# 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 os
import re
import shutil
import urllib
+import paramiko
from toscaparser.functions import GetInput
from toscaparser.tosca_template import ToscaTemplate
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'])
- def buildProperties(self, nodeTemplate, parsed_params):
- properties = {}
- isMappingParams = parsed_params and len(parsed_params) > 0
- for k, item in nodeTemplate.get_properties().items():
- properties[k] = item.value
- if isinstance(item.value, GetInput):
- if item.value.result() and isMappingParams:
- properties[k] = DataEntityExt.validate_datatype(item.type, item.value.result())
- else:
- tmp = {}
- tmp[item.value.name] = item.value.input_name
- properties[k] = tmp
- if 'attributes' in nodeTemplate.entity_tpl:
- for k, item in nodeTemplate.entity_tpl['attributes'].items():
- properties[k] = str(item)
- return properties
-
def buildProperties(self, nodeTemplate, parsed_params):
properties = {}
isMappingParams = parsed_params and len(parsed_params) > 0
def build_interfaces(self, node_template):
if 'interfaces' in node_template.entity_tpl:
return node_template.entity_tpl['interfaces']
- return None
\ No newline at end of file
+ return None
+
+ def isVnf(self, node):
+ return node['nodeType'].upper().find('.VNF.') >= 0 or node['nodeType'].upper().endswith('.VNF')
+
+ def isPnf(self, node):
+ return node['nodeType'].upper().find('.PNF.') >= 0 or node['nodeType'].upper().endswith('.PNF')
+
+ def isCp(self, node):
+ 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')
+
+ 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')
+
+ def get_prop_from_obj(self, obj, prop):
+ if isinstance(obj, str):
+ return obj
+ if (isinstance(obj, dict) and prop in obj):
+ return obj[prop]
+ return None
+
+ def getNodeDependencys(self, node):
+ return self.getRequirementByName(node, 'dependency')
+
+ def getVirtualLinks(self, node):
+ return self.getRequirementByName(node, 'virtualLink')
+
+ def getVirtualbindings(self, node):
+ return self.getRequirementByName(node, 'virtualbinding')
+
+
+ def getRequirementByName(self, node, requirementName):
+ requirements = []
+ if 'requirements' in node:
+ for item in node['requirements']:
+ for key, value in item.items():
+ if key == requirementName:
+ requirements.append(value)
+ return requirements
+
+ def get_networks(self, node):
+ rets = []
+ 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)})
+ return rets
+
+ def _verify_value(self, value, inputs, parsed_params):
+ if isinstance(value, str):
+ return self._verify_string(inputs, parsed_params, value)
+ if isinstance(value, list) or isinstance(value, dict):
+ return self._verify_object(value, inputs, parsed_params)
+ return value
+
+ def _verify_object(self, value, inputs, parsed_params):
+ s = self._verify_string(inputs, parsed_params, json.dumps(value))
+ return json.loads(s)
+
+ def _get_input_name(self, getInput):
+ input_name = getInput.split(':')[1]
+ input_name = input_name.strip()
+ return input_name.replace('"', '').replace('}', '')
+
+ def _verify_string(self, inputs, parsed_params, value):
+ getInputs = re.findall(r'{"get_input": "[a-zA-Z_0-9]+"}', value)
+ for getInput in getInputs:
+ input_name = self._get_input_name(getInput)
+ if parsed_params and input_name in parsed_params:
+ value = value.replace(getInput, json.dumps(parsed_params[input_name]))
+ else:
+ for input_def in inputs:
+ if input_def.default and input_name == input_def.name:
+ value = value.replace(getInput, json.dumps(input_def.default))
+ return value
+
+ def get_node_vl_id(self, node):
+ vl_ids = map(lambda x: self.get_requirement_node_name(x), self.getVirtualLinks(node))
+ if len(vl_ids) > 0:
+ return vl_ids[0]
+ return ""
+
+ def get_node_by_name(self, node_templates, name):
+ for node in node_templates:
+ if node['name'] == name:
+ return node
+ 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 ""