diff options
-rw-r--r-- | lcm/pub/utils/toscaparser/__init__.py | 32 | ||||
-rw-r--r-- | lcm/pub/utils/toscaparser/basemodel.py | 347 | ||||
-rw-r--r-- | lcm/pub/utils/toscaparser/dataentityext.py | 33 | ||||
-rw-r--r-- | lcm/pub/utils/toscaparser/nsdmodel.py | 361 | ||||
-rw-r--r-- | lcm/pub/utils/toscaparser/vnfdmodel.py | 302 |
5 files changed, 0 insertions, 1075 deletions
diff --git a/lcm/pub/utils/toscaparser/__init__.py b/lcm/pub/utils/toscaparser/__init__.py deleted file mode 100644 index 075ae273..00000000 --- a/lcm/pub/utils/toscaparser/__init__.py +++ /dev/null @@ -1,32 +0,0 @@ -# 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 json - -from lcm.pub.utils.toscaparser.nsdmodel import EtsiNsdInfoModel -from lcm.pub.utils.toscaparser.vnfdmodel import EtsiVnfdInfoModel - - -def parse_nsd(path, input_parameters=[]): - tosca_obj = EtsiNsdInfoModel(path, input_parameters) - strResponse = json.dumps(tosca_obj, default=lambda obj: obj.__dict__) - strResponse = strResponse.replace(': null', ': ""') - return strResponse - - -def parse_vnfd(path, input_parameters=[]): - tosca_obj = EtsiVnfdInfoModel(path, input_parameters) - strResponse = json.dumps(tosca_obj, default=lambda obj: obj.__dict__) - strResponse = strResponse.replace(': null', ': ""') - return strResponse diff --git a/lcm/pub/utils/toscaparser/basemodel.py b/lcm/pub/utils/toscaparser/basemodel.py deleted file mode 100644 index 461e8e11..00000000 --- a/lcm/pub/utils/toscaparser/basemodel.py +++ /dev/null @@ -1,347 +0,0 @@ -# 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): - - 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 is not None and file_name != path and os.path.exists(file_name): - try: - os.remove(file_name) - 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(): - 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(path=file_name, - parsed_params=valid_params, - no_required_paras_check=True, - debug_mode=True) - except Exception as e: - print e.message - finally: - 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: - logger.error("Failed to create tosca template, error: %s", e.message) - print "-----------------------------" - print '\n'.join(['%s:%s' % item for item in tosca_tpl.__dict__.items()]) - print "-----------------------------" - return tosca_tpl - - 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 - - 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']) - - 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 verify_properties(self, props, inputs, parsed_params): - ret_props = {} - if (props and len(props) > 0): - for key, value in props.items(): - ret_props[key] = self._verify_value(value, inputs, parsed_params) - # if isinstance(value, str): - # ret_props[key] = self._verify_string(inputs, parsed_params, value); - # continue - # if isinstance(value, list): - # ret_props[key] = map(lambda x: self._verify_dict(inputs, parsed_params, x), value) - # continue - # if isinstance(value, dict): - # ret_props[key] = self._verify_map(inputs, parsed_params, value) - # continue - # ret_props[key] = value - return ret_props - - def build_requirements(self, node_template): - rets = [] - for req in node_template.requirements: - for req_name, req_value in req.items(): - 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}) - 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) - 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) - 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) - 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) - if artifacts != 'null': - ret['artifacts'] = json.loads(artifacts) - - def build_interfaces(self, node_template): - if 'interfaces' in node_template.entity_tpl: - return node_template.entity_tpl['interfaces'] - return None - - def isVnf(self, node): - # return node['nodeType'].upper().find('.VNF.') >= 0 or node['nodeType'].upper().endswith('.VNF') - return node['nodeType'].upper().find('.VF.') >= 0 or node['nodeType'].upper().endswith('.VF') - - 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): - 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 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 "" diff --git a/lcm/pub/utils/toscaparser/dataentityext.py b/lcm/pub/utils/toscaparser/dataentityext.py deleted file mode 100644 index 825e93bb..00000000 --- a/lcm/pub/utils/toscaparser/dataentityext.py +++ /dev/null @@ -1,33 +0,0 @@ -# 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. - -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 diff --git a/lcm/pub/utils/toscaparser/nsdmodel.py b/lcm/pub/utils/toscaparser/nsdmodel.py deleted file mode 100644 index 9792dd99..00000000 --- a/lcm/pub/utils/toscaparser/nsdmodel.py +++ /dev/null @@ -1,361 +0,0 @@ -# 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 functools - -from lcm.pub.utils.toscaparser.basemodel import BaseInfoModel - - -class EtsiNsdInfoModel(BaseInfoModel): - - def __init__(self, path, params): - tosca = self.buildToscaTemplate(path, params) - self.parseModel(tosca) - - def parseModel(self, tosca): - self.buidMetadata(tosca) - if hasattr(tosca, 'topology_template') and hasattr(tosca.topology_template, 'inputs'): - self.inputs = self.buildInputs(tosca.topology_template.inputs) - - nodeTemplates = map(functools.partial(self.buildNode, inputs=tosca.inputs, parsed_params=tosca.parsed_params), - tosca.nodetemplates) - - self.vnfs = self._get_all_vnf(nodeTemplates) - self.pnfs = self._get_all_pnf(nodeTemplates) - self.vls = self.get_all_vl(nodeTemplates) - self.cps = self.get_all_cp(nodeTemplates) - self.routers = self.get_all_router(nodeTemplates) - 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): - ret = {} - for tmpinput in top_inputs: - tmp = {} - tmp['type'] = tmpinput.type - tmp['description'] = tmpinput.description - tmp['default'] = tmpinput.default - - ret[tmpinput.name] = tmp - return ret - - def buildNode(self, nodeTemplate, inputs, parsed_params): - ret = {} - ret['name'] = nodeTemplate.name - ret['nodeType'] = nodeTemplate.type - if 'description' in nodeTemplate.entity_tpl: - ret['description'] = nodeTemplate.entity_tpl['description'] - else: - ret['description'] = '' - props = self.buildProperties(nodeTemplate, parsed_params) - ret['properties'] = self.verify_properties(props, inputs, parsed_params) - ret['requirements'] = self.build_requirements(nodeTemplate) - self.buildCapabilities(nodeTemplate, inputs, ret) - self.buildArtifacts(nodeTemplate, inputs, ret) - interfaces = self.build_interfaces(nodeTemplate) - if interfaces: - ret['interfaces'] = interfaces - return ret - - def _get_all_vnf(self, nodeTemplates): - vnfs = [] - for node in nodeTemplates: - if self.isVnf(node): - vnf = {} - vnf['vnf_id'] = node['name'] - vnf['description'] = node['description'] - vnf['properties'] = node['properties'] - vnf['dependencies'] = map(lambda x: self.get_requirement_node_name(x), self.getNodeDependencys(node)) - vnf['networks'] = self.get_networks(node) - - vnfs.append(vnf) - return vnfs - - def _get_all_pnf(self, nodeTemplates): - pnfs = [] - for node in nodeTemplates: - if self.isPnf(node): - pnf = {} - pnf['pnf_id'] = node['name'] - pnf['description'] = node['description'] - pnf['properties'] = node['properties'] - pnf['cps'] = self.getVirtalBindingCpIds(node, nodeTemplates) - - pnfs.append(pnf) - return pnfs - - def getVirtalBindingCpIds(self, node, nodeTemplates): - return map(lambda x: x['name'], self.getVirtalBindingCps(node, nodeTemplates)) - - def getVirtalBindingCps(self, node, nodeTemplates): - cps = [] - for tmpnode in nodeTemplates: - if 'requirements' in tmpnode: - for item in tmpnode['requirements']: - for key, value in item.items(): - if key.upper().startswith('VIRTUALBINDING'): - req_node_name = self.get_requirement_node_name(value) - if req_node_name is not None and req_node_name == node['name']: - cps.append(tmpnode) - return cps - - def get_all_vl(self, nodeTemplates): - vls = [] - for node in nodeTemplates: - if self.isVl(node): - vl = {} - vl['vl_id'] = node['name'] - vl['description'] = node['description'] - vl['properties'] = node['properties'] - vl['route_external'] = False - vl['route_id'] = self._get_vl_route_id(node) - vls.append(vl) - if self._isExternalVL(node): - vl = {} - vl['vl_id'] = node['name'] - vl['description'] = node['description'] - vl['properties'] = node['properties'] - vl['route_external'] = True - vls.append(vl) - return vls - - def _get_vl_route_id(self, node): - route_ids = map(lambda x: self.get_requirement_node_name(x), - self.getRequirementByName(node, 'virtual_route')) - if len(route_ids) > 0: - return route_ids[0] - return "" - - def _isExternalVL(self, node): - return node['nodeType'].upper().find('.ROUTEEXTERNALVL') >= 0 - - def get_all_cp(self, nodeTemplates): - cps = [] - for node in nodeTemplates: - if self.isCp(node): - cp = {} - cp['cp_id'] = node['name'] - cp['cpd_id'] = node['name'] - cp['description'] = node['description'] - cp['properties'] = node['properties'] - cp['vl_id'] = self.get_node_vl_id(node) - binding_node_ids = map(lambda x: self.get_requirement_node_name(x), self.getVirtualbindings(node)) - # cp['vnf_id'] = self._filter_vnf_id(binding_node_ids, nodeTemplates) - cp['pnf_id'] = self._filter_pnf_id(binding_node_ids, nodeTemplates) - vls = self.buil_cp_vls(node) - if len(vls) > 1: - cp['vls'] = vls - cps.append(cp) - return cps - - def buil_cp_vls(self, node): - return map(lambda x: self._build_cp_vl(x), self.getVirtualLinks(node)) - - def _build_cp_vl(self, req): - cp_vl = {} - cp_vl['vl_id'] = self.get_prop_from_obj(req, 'node') - relationship = self.get_prop_from_obj(req, 'relationship') - if relationship is not None: - properties = self.get_prop_from_obj(relationship, 'properties') - if properties is not None and isinstance(properties, dict): - for key, value in properties.items(): - cp_vl[key] = value - return cp_vl - - def _filter_pnf_id(self, node_ids, node_templates): - for node_id in node_ids: - node = self.get_node_by_name(node_templates, node_id) - if self.isPnf(node): - return node_id - return "" - - def get_all_router(self, nodeTemplates): - rets = [] - for node in nodeTemplates: - if self._isRouter(node): - ret = {} - ret['router_id'] = node['name'] - ret['description'] = node['description'] - ret['properties'] = node['properties'] - ret['external_vl_id'] = self._get_router_external_vl_id(node) - ret['external_ip_addresses'] = self._get_external_ip_addresses(node) - - rets.append(ret) - return rets - - def _isRouter(self, node): - return node['nodeType'].upper().find('.ROUTER.') >= 0 or node['nodeType'].upper().endswith('.ROUTER') - - def _get_router_external_vl(self, node): - return self.getRequirementByName(node, 'external_virtual_link') - - def _get_router_external_vl_id(self, node): - ids = map(lambda x: self.get_requirement_node_name(x), self._get_router_external_vl(node)) - if len(ids) > 0: - return ids[0] - return "" - - def _get_external_ip_addresses(self, node): - external_vls = self._get_router_external_vl(node) - if len(external_vls) > 0: - if 'relationship' in external_vls[0] and 'properties' in external_vls[0]['relationship'] and 'router_ip_address' in external_vls[0]['relationship']['properties']: - return external_vls[0]['relationship']['properties']['router_ip_address'] - return [] - - def _get_all_fp(self, nodeTemplates): - fps = [] - for node in nodeTemplates: - if self._isFp(node): - fp = {} - fp['fp_id'] = node['name'] - fp['description'] = node['description'] - fp['properties'] = node['properties'] - fp['forwarder_list'] = self._getForwarderList(node, nodeTemplates) - - fps.append(fp) - return fps - - def _isFp(self, node): - return node['nodeType'].upper().find('.FP.') >= 0 or node['nodeType'].upper().find('.SFP.') >= 0 or node[ - 'nodeType'].upper().endswith('.FP') or node['nodeType'].upper().endswith('.SFP') - - def _getForwarderList(self, node, node_templates): - forwarderList = [] - if 'requirements' in node: - for item in node['requirements']: - for key, value in item.items(): - if key == 'forwarder': - tmpnode = self.get_node_by_req(node_templates, value) - type = 'cp' if self.isCp(tmpnode) else 'vnf' - req_node_name = self.get_requirement_node_name(value) - if isinstance(value, dict) and 'capability' in value: - forwarderList.append( - {"type": type, "node_name": req_node_name, "capability": value['capability']}) - else: - forwarderList.append({"type": type, "node_name": req_node_name, "capability": ""}) - - return forwarderList - - def get_node_by_req(self, node_templates, req): - req_node_name = self.get_requirement_node_name(req) - return self.get_node_by_name(node_templates, req_node_name) - - def _get_all_vnffg(self, groups): - vnffgs = [] - for group in groups: - if self._isVnffg(group): - vnffg = {} - vnffg['vnffg_id'] = group.name - vnffg['description'] = group.description - if 'properties' in group.tpl: - vnffg['properties'] = group.tpl['properties'] - vnffg['members'] = group.members - - vnffgs.append(vnffg) - return vnffgs - - def _isVnffg(self, group): - return group.type.upper().find('.VNFFG.') >= 0 or group.type.upper().find( - '.SFC.') >= 0 or group.type.upper().endswith('.VNFFG') or group.type.upper().endswith('.SFC') - - def get_all_server_group(self, groups): - rets = [] - for group in groups: - if self._isServerGroup(group): - ret = {} - ret['group_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 _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 diff --git a/lcm/pub/utils/toscaparser/vnfdmodel.py b/lcm/pub/utils/toscaparser/vnfdmodel.py deleted file mode 100644 index a665efe7..00000000 --- a/lcm/pub/utils/toscaparser/vnfdmodel.py +++ /dev/null @@ -1,302 +0,0 @@ -# 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 functools - -from lcm.pub.utils.toscaparser import EtsiNsdInfoModel - - -class EtsiVnfdInfoModel(EtsiNsdInfoModel): - - def __init__(self, path, params): - super(EtsiVnfdInfoModel, self).__init__(path, params) - - def parseModel(self, tosca): - self.buidMetadata(tosca) - if hasattr(tosca, 'topology_template') and hasattr(tosca.topology_template, 'inputs'): - self.inputs = self.buildInputs(tosca.topology_template.inputs) - - nodeTemplates = map(functools.partial(self.buildNode, inputs=tosca.inputs, parsed_params=tosca.parsed_params), - tosca.nodetemplates) - - self.services = self._get_all_services(nodeTemplates) - self.vcloud = self._get_all_vcloud(nodeTemplates) - self.vcenter = self._get_all_vcenter(nodeTemplates) - self.image_files = self._get_all_image_file(nodeTemplates) - self.local_storages = self._get_all_local_storage(nodeTemplates) - self.volume_storages = self._get_all_volume_storage(nodeTemplates) - self.vdus = self._get_all_vdu(nodeTemplates) - self.vls = self.get_all_vl(nodeTemplates) - self.cps = self.get_all_cp(nodeTemplates) - self.plugins = self.get_all_plugin(nodeTemplates) - self.routers = self.get_all_router(nodeTemplates) - self.server_groups = self.get_all_server_group(tosca.topology_template.groups) - self.element_groups = self._get_all_element_group(tosca.topology_template.groups) - self.policies = self._get_policies(tosca.topology_template.policies) - self.vnf_exposed = self.get_all_endpoint_exposed(tosca.topology_template) - self.vnf_flavours = self.get_all_flavour(tosca.topology_template.groups) - - def _get_all_services(self, nodeTemplates): - ret = [] - for node in nodeTemplates: - if self.isService(node): - service = {} - service['serviceId'] = node['name'] - if 'description' in node: - service['description'] = node['description'] - service['properties'] = node['properties'] - service['dependencies'] = map(lambda x: self.get_requirement_node_name(x), - self.getNodeDependencys(node)) - service['networks'] = map(lambda x: self.get_requirement_node_name(x), self.getVirtualLinks(node)) - - ret.append(service) - return ret - - def _get_all_vcloud(self, nodeTemplates): - rets = [] - for node in nodeTemplates: - if self._isVcloud(node): - ret = {} - if 'vdc_name' in node['properties']: - ret['vdc_name'] = node['properties']['vdc_name'] - else: - ret['vdc_name'] = "" - if 'storage_clusters' in node['properties']: - ret['storage_clusters'] = node['properties']['storage_clusters'] - else: - ret['storage_clusters'] = [] - - rets.append(ret) - return rets - - def _isVcloud(self, node): - return node['nodeType'].upper().find('.VCLOUD.') >= 0 or node['nodeType'].upper().endswith('.VCLOUD') - - def _get_all_vcenter(self, nodeTemplates): - rets = [] - for node in nodeTemplates: - if self._isVcenter(node): - ret = {} - if 'compute_clusters' in node['properties']: - ret['compute_clusters'] = node['properties']['compute_clusters'] - else: - ret['compute_clusters'] = [] - if 'storage_clusters' in node['properties']: - ret['storage_clusters'] = node['properties']['storage_clusters'] - else: - ret['storage_clusters'] = [] - if 'network_clusters' in node['properties']: - ret['network_clusters'] = node['properties']['network_clusters'] - else: - ret['network_clusters'] = [] - - rets.append(ret) - return rets - - def _isVcenter(self, node): - return node['nodeType'].upper().find('.VCENTER.') >= 0 or node['nodeType'].upper().endswith('.VCENTER') - - def _get_all_image_file(self, nodeTemplates): - rets = [] - for node in nodeTemplates: - if self._isImageFile(node): - ret = {} - ret['image_file_id'] = node['name'] - if 'description' in node: - ret['description'] = node['description'] - ret['properties'] = node['properties'] - - rets.append(ret) - return rets - - def _isImageFile(self, node): - return node['nodeType'].upper().find('.IMAGEFILE.') >= 0 or node['nodeType'].upper().endswith('.IMAGEFILE') - - def _get_all_local_storage(self, nodeTemplates): - rets = [] - for node in nodeTemplates: - if self._isLocalStorage(node): - ret = {} - ret['local_storage_id'] = node['name'] - if 'description' in node: - ret['description'] = node['description'] - ret['properties'] = node['properties'] - - rets.append(ret) - return rets - - def _isLocalStorage(self, node): - return node['nodeType'].upper().find('.LOCALSTORAGE.') >= 0 or node['nodeType'].upper().endswith( - '.LOCALSTORAGE') - - def _get_all_volume_storage(self, nodeTemplates): - rets = [] - for node in nodeTemplates: - if self._isVolumeStorage(node): - ret = {} - ret['volume_storage_id'] = node['name'] - if 'description' in node: - ret['description'] = node['description'] - ret['properties'] = node['properties'] - ret['image_file'] = map(lambda x: self.get_requirement_node_name(x), - self.getRequirementByName(node, 'image_file')) - - rets.append(ret) - return rets - - def _isVolumeStorage(self, node): - return node['nodeType'].upper().find('.VOLUMESTORAGE.') >= 0 or node['nodeType'].upper().endswith( - '.VOLUMESTORAGE') - - def _get_all_vdu(self, nodeTemplates): - rets = [] - for node in nodeTemplates: - if self.isVdu(node): - ret = {} - ret['vdu_id'] = node['name'] - if 'description' in node: - ret['description'] = node['description'] - ret['properties'] = node['properties'] - ret['image_file'] = self.get_node_image_file(node) - local_storages = self.getRequirementByName(node, 'local_storage') - ret['local_storages'] = map(lambda x: self.get_requirement_node_name(x), local_storages) - volume_storages = self.getRequirementByName(node, 'volume_storage') - ret['volume_storages'] = map(functools.partial(self._trans_volume_storage), volume_storages) - ret['dependencies'] = map(lambda x: self.get_requirement_node_name(x), self.getNodeDependencys(node)) - - nfv_compute = self.getCapabilityByName(node, 'nfv_compute') - if nfv_compute is not None and 'properties' in nfv_compute: - ret['nfv_compute'] = nfv_compute['properties'] - - ret['vls'] = self.get_linked_vl_ids(node, nodeTemplates) - - scalable = self.getCapabilityByName(node, 'scalable') - if scalable is not None and 'properties' in scalable: - ret['scalable'] = scalable['properties'] - - ret['cps'] = self.getVirtalBindingCpIds(node, nodeTemplates) - ret['artifacts'] = self._build_artifacts(node) - - rets.append(ret) - return rets - - def get_node_image_file(self, node): - rets = map(lambda x: self.get_requirement_node_name(x), self.getRequirementByName(node, 'guest_os')) - if len(rets) > 0: - return rets[0] - return "" - - def _trans_volume_storage(self, volume_storage): - if isinstance(volume_storage, str): - return {"volume_storage_id": volume_storage} - else: - ret = {} - ret['volume_storage_id'] = self.get_requirement_node_name(volume_storage) - if 'relationship' in volume_storage and 'properties' in volume_storage['relationship']: - if 'location' in volume_storage['relationship']['properties']: - ret['location'] = volume_storage['relationship']['properties']['location'] - if 'device' in volume_storage['relationship']['properties']: - ret['device'] = volume_storage['relationship']['properties']['device'] - - return ret - - def get_linked_vl_ids(self, node, node_templates): - vl_ids = [] - cps = self.getVirtalBindingCps(node, node_templates) - for cp in cps: - vl_reqs = self.getVirtualLinks(cp) - for vl_req in vl_reqs: - vl_ids.append(self.get_requirement_node_name(vl_req)) - return vl_ids - - def _build_artifacts(self, node): - rets = [] - if 'artifacts' in node and len(node['artifacts']) > 0: - artifacts = node['artifacts'] - for name, value in artifacts.items(): - ret = {} - if isinstance(value, dict): - ret['artifact_name'] = name - ret['type'] = value.get('type', '') - ret['file'] = value.get('file', '') - ret['repository'] = value.get('repository', '') - ret['deploy_path'] = value.get('deploy_path', '') - else: - ret['artifact_name'] = name - ret['type'] = '' - ret['file'] = value - ret['repository'] = '' - ret['deploy_path'] = '' - rets.append(ret) - return rets - - def get_all_cp(self, nodeTemplates): - cps = [] - for node in nodeTemplates: - if self.isCp(node): - cp = {} - cp['cp_id'] = node['name'] - cp['cpd_id'] = node['name'] - cp['description'] = node['description'] - cp['properties'] = node['properties'] - cp['vl_id'] = self.get_node_vl_id(node) - cp['vdu_id'] = self.get_node_vdu_id(node) - vls = self.buil_cp_vls(node) - if len(vls) > 1: - cp['vls'] = vls - cps.append(cp) - return cps - - def get_all_plugin(self, node_templates): - plugins = [] - for node in node_templates: - if self._isPlugin(node): - plugin = {} - plugin['plugin_id'] = node['name'] - plugin['description'] = node['description'] - plugin['properties'] = node['properties'] - if 'interfaces' in node: - plugin['interfaces'] = node['interfaces'] - - plugins.append(plugin) - return plugins - - def _isPlugin(self, node): - return node['nodeType'].lower().find('.plugin.') >= 0 or node['nodeType'].lower().endswith('.plugin') - - def _get_all_element_group(self, groups): - rets = [] - for group in groups: - if self._isVnfdElementGroup(group): - ret = {} - ret['group_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 _isVnfdElementGroup(self, group): - return group.type.upper().find('.VNFDELEMENTGROUP.') >= 0 or group.type.upper().endswith('.VNFDELEMENTGROUP') - - def _get_policies(self, top_policies): - policies = [] - scaling_policies = self.get_scaling_policies(top_policies) - healing_policies = self.get_healing_policies(top_policies) - policies.append({"scaling": scaling_policies, 'healing': healing_policies}) - return policies - - def get_healing_policies(self, top_policies): - return self.get_policies_by_keyword(top_policies, '.HEALING') |