/*- * ============LICENSE_START======================================================= * ONAP - SO * ================================================================================ * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. * ================================================================================ * 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. * ============LICENSE_END========================================================= */ package org.onap.so.bpmn.common.scripts import groovy.xml.XmlUtil import org.camunda.bpm.engine.delegate.DelegateExecution import org.onap.aai.domain.yang.L3Network import org.onap.aai.domain.yang.Subnet import org.onap.aai.domain.yang.Subnets import org.onap.so.bpmn.core.UrnPropertiesReader import org.w3c.dom.Document import org.w3c.dom.Element import org.w3c.dom.Node import org.w3c.dom.NodeList import org.xml.sax.InputSource import javax.xml.parsers.DocumentBuilder import javax.xml.parsers.DocumentBuilderFactory import javax.xml.transform.Transformer import javax.xml.transform.TransformerFactory import javax.xml.transform.dom.DOMSource import javax.xml.transform.stream.StreamResult /** * This groovy class supports the any Network processes that need the methods defined here. */ class NetworkUtils { public MsoUtils utils = new MsoUtils() private AbstractServiceTaskProcessor taskProcessor public NetworkUtils(AbstractServiceTaskProcessor taskProcessor) { this.taskProcessor = taskProcessor } /** * This method returns the string for Network request * V2 for Contrail 3.x will populate cloud-region data in same cloudSiteId filed * Network adapter will handle it properly * @param requestId either 'request-id' or 'mso-request-id' * @param requestInput the request in the process * @param queryIdResponse the response of REST AAI query by Id * @param routeCollection the collection * @param policyFqdns the policy * @param tableCollection the collection * @param cloudRegionId the cloud-region-region * @return String request */ def CreateNetworkRequestV2(execution, requestId, messageId, requestInput,L3Network queryIdResponse, routeCollection, policyFqdns, tableCollection, cloudRegionId, backoutOnFailure, source) { String createNetworkRequest = null if(requestInput!=null && queryIdResponse!=null) { String serviceInstanceId = "" String sharedValue = "" String externalValue = "" if (source == "VID") { sharedValue = queryIdResponse.isIsSharedNetwork() != null ? queryIdResponse.isIsSharedNetwork() : "false" externalValue = queryIdResponse.isIsExternalNetwork() != null ? queryIdResponse.isIsExternalNetwork() : "false" serviceInstanceId = utils.getNodeText(requestInput, "service-instance-id") } else { // source = 'PORTAL' sharedValue = getParameterValue(requestInput, "shared") externalValue = getParameterValue(requestInput, "external") serviceInstanceId = utils.getNodeText(requestInput, "service-instance-id") != null ? utils.getNodeText(requestInput, "service-instance-id") : "" } String networkParams = "" if (utils.nodeExists(requestInput, "network-params")) { String netParams = utils.getNodeXml(requestInput, "network-params", false).replace("tag0:","").replace(":tag0","") networkParams = buildParams(netParams) } String failIfExists = "false" // requestInput String cloudRegion = cloudRegionId String tenantId = utils.getNodeText(requestInput, "tenant-id") String networkType = "" String modelCustomizationUuid = "" if (utils.nodeExists(requestInput, "networkModelInfo")) { String networkModelInfo = utils.getNodeXml(requestInput, "networkModelInfo", false).replace("tag0:","").replace(":tag0","") networkType = utils.getNodeText(networkModelInfo, "modelName") modelCustomizationUuid = utils.getNodeText(networkModelInfo, "modelCustomizationUuid") } else { networkType = queryIdResponse.getNetworkType() modelCustomizationUuid = utils.getNodeText(requestInput, "modelCustomizationId") } // queryIdResponse String networkName = queryIdResponse.getNetworkName() String networkId = queryIdResponse.getNetworkId() String networkTechnology = queryIdResponse.getNetworkTechnology() // contrailNetwork - networkTechnology = 'Contrail' vs. 'AIC_SR_IOV') String contrailNetwork = "" if (networkTechnology.contains('Contrail') || networkTechnology.contains('contrail') || networkTechnology.contains('CONTRAIL')) { contrailNetwork = """ ${MsoUtils.xmlEscape(sharedValue)} ${MsoUtils.xmlEscape(externalValue)} ${routeCollection} ${policyFqdns} ${tableCollection} """ networkTechnology = "CONTRAIL" // replace } // rebuild subnets String subnets = "" if (queryIdResponse.getSubnets() != null) { subnets = buildSubnets(queryIdResponse) } String physicalNetworkName = "" physicalNetworkName = queryIdResponse.getPhysicalNetworkName() String vlansCollection = buildVlans(queryIdResponse) String notificationUrl = "" //TODO - is this coming from URN? What variable/value to use? //String notificationUrl = execution.getVariable("URN_?????") //TODO - is this coming from URN? What variable/value to use? createNetworkRequest = """ ${MsoUtils.xmlEscape(cloudRegion)} ${MsoUtils.xmlEscape(tenantId)} ${MsoUtils.xmlEscape(networkId)} ${MsoUtils.xmlEscape(networkName)} ${MsoUtils.xmlEscape(networkType)} ${MsoUtils.xmlEscape(modelCustomizationUuid)} ${MsoUtils.xmlEscape(networkTechnology)} ${MsoUtils.xmlEscape(physicalNetworkName)} ${vlansCollection} ${contrailNetwork} ${subnets} true ${MsoUtils.xmlEscape(backoutOnFailure)} ${MsoUtils.xmlEscape(failIfExists)} ${networkParams} ${MsoUtils.xmlEscape(requestId)} ${MsoUtils.xmlEscape(serviceInstanceId)} ${MsoUtils.xmlEscape(messageId)} ${MsoUtils.xmlEscape(notificationUrl)} """.trim() } return createNetworkRequest } /** * This method returns the string for Network request * V2 for Contrail 3.x will populate cloud-region data in same cloudSiteId filed * Network adapter will handle it properly * @param requestId either 'request-id' or 'mso-request-id' * @param requestInput the request in the process * @param queryIdResponse the response of REST AAI query by Id * @param routeCollection the collection * @param policyFqdns the policy * @param cloudRegionId the cloud-region-region * @return String request */ def UpdateNetworkRequestV2(execution, requestId, messageId, requestInput, L3Network queryIdResponse, routeCollection, policyFqdns, tableCollection, cloudRegionId, backoutOnFailure, source) { String updateNetworkRequest = null if(requestInput!=null && queryIdResponse!=null) { String serviceInstanceId = "" String sharedValue = "" String externalValue = "" if (source == "VID") { sharedValue = queryIdResponse.isIsSharedNetwork() != null ? queryIdResponse.isIsSharedNetwork() : "false" externalValue = queryIdResponse.isIsExternalNetwork() != null ? queryIdResponse.isIsExternalNetwork() : "false" serviceInstanceId = utils.getNodeText(requestInput, "service-instance-id") } else { // source = 'PORTAL' sharedValue = getParameterValue(requestInput, "shared") externalValue = getParameterValue(requestInput, "external") serviceInstanceId = utils.getNodeText(requestInput, "service-instance-id") != null ? utils.getNodeText(requestInput, "service-instance-id") : "" } String failIfExists = "false" // requestInput String cloudRegion = cloudRegionId String tenantId = utils.getNodeText(requestInput, "tenant-id") // queryIdResponse String networkName = queryIdResponse.getNetworkName() String networkId = queryIdResponse.getNetworkId() String networkType = "" String modelCustomizationUuid = "" if (utils.nodeExists(requestInput, "networkModelInfo")) { String networkModelInfo = utils.getNodeXml(requestInput, "networkModelInfo", false).replace("tag0:","").replace(":tag0","") networkType = utils.getNodeText(networkModelInfo, "modelName") modelCustomizationUuid = utils.getNodeText(networkModelInfo, "modelCustomizationUuid") } else { networkType = queryIdResponse.getNetworkType() modelCustomizationUuid = utils.getNodeText(requestInput, "modelCustomizationId") } // rebuild subnets String subnets = "" if (queryIdResponse.getSubnets() != null) { subnets = buildSubnets(queryIdResponse) } String networkParams = "" if (utils.nodeExists(requestInput, "network-params")) { String netParams = utils.getNodeXml(requestInput, "network-params", false).replace("tag0:","").replace(":tag0","") networkParams = buildParams(netParams) } String networkStackId = queryIdResponse.getHeatStackId() if (networkStackId == 'null' || networkStackId == "" || networkStackId == null) { networkStackId = "force_update" } String physicalNetworkName = queryIdResponse.getPhysicalNetworkName() String vlansCollection = buildVlans(queryIdResponse) updateNetworkRequest = """ ${MsoUtils.xmlEscape(cloudRegion)} ${MsoUtils.xmlEscape(tenantId)} ${MsoUtils.xmlEscape(networkId)} ${MsoUtils.xmlEscape(networkStackId)} ${MsoUtils.xmlEscape(networkName)} ${MsoUtils.xmlEscape(networkType)} ${MsoUtils.xmlEscape(modelCustomizationUuid)} CONTRAIL ${MsoUtils.xmlEscape(physicalNetworkName)} ${vlansCollection} ${MsoUtils.xmlEscape(sharedValue)} ${MsoUtils.xmlEscape(externalValue)} ${routeCollection} ${policyFqdns} ${tableCollection} ${subnets} true ${MsoUtils.xmlEscape(backoutOnFailure)} ${MsoUtils.xmlEscape(failIfExists)} ${networkParams} ${MsoUtils.xmlEscape(requestId)} ${MsoUtils.xmlEscape(serviceInstanceId)} ${MsoUtils.xmlEscape(messageId)} """.trim() } return updateNetworkRequest } /** * This method returns the value for the name paramName. * Ex: * 1 * 0 * * * @param xmlInput the XML document * @param paramName the param name (ex: 'shared') * @return a param value for 'shared' (ex: '1') */ def getParameterValue(xmlInput, paramName) { def rtn="" if(xmlInput!=null){ def xml= new XmlSlurper().parseText(xmlInput) rtn= xml.'**'.find {param->param.'@name'.text() == paramName} } if (rtn==null) { return "" } else { return rtn } } /** * This method returns the name of param if found/match with paramName. * Ex: * 1 * 0 * * * @param xmlInput the XML document * @param paramName the param name (ex: 'shared', ) * @return a param name for 'shared' (ex: 'shared' if found) */ def getParameterName(xmlInput, paramName) { def rtn="" if(xmlInput!=null){ def xml= new XmlSlurper().parseText(xmlInput) try { rtn= xml.'**'.find {param->param.'@name' == paramName}.'@name' } catch (Exception ex) { rtn="" } } if (rtn==null || rtn=="") { return "" } else { return rtn } } /** * This method returns the networkParams xml string. * Ex: input: * * 1 * 0 * * * Sample result: * * 1 * 0 * * */ def buildParams(networkParams) { def build = "" def netParams = new XmlParser().parseText(networkParams) try { def paramsList = netParams.'**'.findAll {param->param.'@name'}.'@name' if (paramsList.size() > 0) { build += "" for (i in 0..paramsList.size()-1) { def name = netParams.'**'.find {param->param.'@name' == paramsList[i]}.'@name' def value= netParams.'**'.find {param->param.'@name' == paramsList[i]}.text() build += "<${name}>${MsoUtils.xmlEscape(value)}" } build += "" } } catch (Exception ex) { println ' buildParams error - ' + ex.getMessage() build = "" } return build } def getVlans(xmlInput) { def rtn = "" if (xmlInput!=null) { def vlansList = getListWithElements(xmlInput, 'vlans') def vlansListSize = vlansList.size() if (vlansListSize > 0) { for (i in 0..vlansListSize-1) { rtn += ''+vlansList[i]+'' } } } return rtn } /** * similar to VNF bindings method * @param xmlInput the XML document * @return a list of network policy values * ex: ['aai/v$/network/network-policies/network-policy/cee6d136-e378-4678-a024-2cd15f0ee0cg', 'aai/v$/network/network-policies/network-policy/cee6d136-e378-4678-a024-2cd15f0ee0cg'] * **/ def getNetworkPolicyObject(xmlInput) { //def rtn = null List rtn = [] if (xmlInput!=null) { def relationshipList = getListWithElements(xmlInput, 'relationship') def relationshipListSize = relationshipList.size() if (relationshipListSize > 0) { for (i in 0..relationshipListSize-1) { def relationshipXml = XmlUtil.serialize(relationshipList[i]) if (utils.getNodeText(relationshipXml, 'related-to') == "network-policy") { def relatedLink = utils.getNodeText(relationshipXml, 'related-link') if (relatedLink != null || relatedLink != "") { rtn.add(relatedLink.substring(relatedLink.indexOf("/aai/"), relatedLink.length())) } } } } } return rtn } /** * similar to network policymethod * @param xmlInput the XML document * @return a list of network policy values * ex: ['aai/v$/network/route-table-references/route-table-reference/refFQDN1', 'aai/v$/network/route-table-references/route-table-reference/refFQDN2'] * **/ def getNetworkTableRefObject(xmlInput) { //def rtn = null List rtn = [] if (xmlInput!=null) { def relationshipList = getListWithElements(xmlInput, 'relationship') def relationshipListSize = relationshipList.size() if (relationshipListSize > 0) { for (i in 0..relationshipListSize-1) { def relationshipXml = XmlUtil.serialize(relationshipList[i]) if (utils.getNodeText(relationshipXml, 'related-to') == "route-table-reference") { def relatedLink = utils.getNodeText(relationshipXml, 'related-link') if (relatedLink != null || relatedLink != "") { rtn.add(relatedLink.substring(relatedLink.indexOf("/aai/"), relatedLink.length())) } } } } } return rtn } /** * similar to network policymethod * @param xmlInput the XML document * @return a list of IDs for related VNF instances * **/ def getRelatedVnfIdList(xmlInput) { //def rtn = null List rtn = [] if (xmlInput!=null) { def relationshipList = getListWithElements(xmlInput, 'relationship') def relationshipListSize = relationshipList.size() if (relationshipListSize > 0) { for (i in 0..relationshipListSize-1) { def relationshipXml = XmlUtil.serialize(relationshipList[i]) if (utils.getNodeText(relationshipXml, 'related-to') == "generic-vnf") { def relatedLink = utils.getNodeText(relationshipXml, 'related-link') if (relatedLink != null || relatedLink != "") { if (relatedLink.substring(relatedLink.indexOf("/generic-vnf/")+13, relatedLink.length()).contains('/')) { rtn.add(relatedLink.substring(relatedLink.indexOf("/generic-vnf/")+13, relatedLink.length()-1)) } else { rtn.add(relatedLink.substring(relatedLink.indexOf("/generic-vnf/")+13, relatedLink.length())) } } } } } } return rtn } /** * similar to network policymethod * @param xmlInput the XML document * @return a list of IDs for related Network instances * **/ def getRelatedNetworkIdList(xmlInput) { //def rtn = null List rtn = [] if (xmlInput!=null) { def relationshipList = getListWithElements(xmlInput, 'relationship') def relationshipListSize = relationshipList.size() if (relationshipListSize > 0) { for (i in 0..relationshipListSize-1) { def relationshipXml = XmlUtil.serialize(relationshipList[i]) if (utils.getNodeText(relationshipXml, 'related-to') == "l3-network") { def relatedLink = utils.getNodeText(relationshipXml, 'related-link') if (relatedLink != null || relatedLink != "") { if (relatedLink.substring(relatedLink.indexOf("/l3-network/")+12, relatedLink.length()).contains('/')) { rtn.add(relatedLink.substring(relatedLink.indexOf("/l3-network/")+12, relatedLink.length()-1)) } else { rtn.add(relatedLink.substring(relatedLink.indexOf("/l3-network/")+12, relatedLink.length())) } } } } } } return rtn } def isInstanceValueMatch(linkResource, globalSubscriberId, serviceType) { Boolean rtn = false try { String globalSubscriberIdLink = linkResource.substring(linkResource.indexOf("/customer/")+10, linkResource.indexOf("/service-subscriptions")) String serviceTypeLink = linkResource.substring(linkResource.indexOf("/service-subscription/")+22, linkResource.indexOf("/service-instances")) if (globalSubscriberIdLink == globalSubscriberId) { rtn = true } else { if (serviceTypeLink == serviceType) { rtn = true } } } catch (Exception ex) { println 'Exception - ' + ex.getMessage() return false } return rtn } def getListWithElements(xmlInput, groupName) { def rtn = "" if (xmlInput != null) { def relationshipData = new XmlSlurper().parseText(xmlInput) rtn = relationshipData.'**'.findAll {it.name() == groupName} } return rtn } // build network single elements def buildNetworkElements(l3Network, createNetworkContrailResponse, networkList) { def replaceNetworkId = "" def replaceNeutronNetworkId = "" def replaceContrailNetworkFqdn = "" if (l3Network != null && createNetworkContrailResponse != null) { if (utils.nodeExists(l3Network, 'heat-stack-id')) { replaceNetworkId = utils.getNodeText(l3Network, 'heat-stack-id') } else { if (utils.nodeExists(createNetworkContrailResponse, 'networkStackId')) { replaceNetworkId = utils.getNodeText(createNetworkContrailResponse, 'networkStackId') } } if (utils.nodeExists(l3Network, 'neutron-network-id')) { replaceNeutronNetworkId = utils.getNodeText(l3Network, 'neutron-network-id') } else { if (utils.nodeExists(createNetworkContrailResponse, 'neutronNetworkId')) { replaceNeutronNetworkId = utils.getNodeText(createNetworkContrailResponse, 'neutronNetworkId') } } if (utils.nodeExists(l3Network, 'contrail-network-fqdn')) { replaceContrailNetworkFqdn = utils.getNodeText(l3Network, 'contrail-network-fqdn') } else { if (utils.nodeExists(createNetworkContrailResponse, 'networkFqdn')) { replaceContrailNetworkFqdn = utils.getNodeText(createNetworkContrailResponse, 'networkFqdn') } } } String var = "" def xmlNetwork = "" if (l3Network != null) { for (element in networkList) { def xml= new XmlSlurper().parseText(l3Network) var = xml.'**'.find {it.name() == element} if (var == null) { if (element=="orchestration-status") { if (var.toString() == 'pending-create' || var.toString() == 'PendingCreate') { xmlNetwork += "<"+element+">"+"Created"+"" } else { //pending-update or PendingUpdate xmlNetwork += "<"+element+">"+"Active"+"" } } if (element=="heat-stack-id") { if (replaceNetworkId != "") { xmlNetwork += "<"+element+">"+replaceNetworkId+"" } } if (element=="neutron-network-id") { if (replaceNeutronNetworkId != "") { xmlNetwork += "<"+element+">"+replaceNeutronNetworkId+"" } } if (element=="contrail-network-fqdn") { if (replaceContrailNetworkFqdn != "") { xmlNetwork += "<"+element+">"+replaceContrailNetworkFqdn+"" } } } else { if (element=="orchestration-status") { if (element=="orchestration-status") { if (var.toString() == 'pending-create' || var.toString() == 'PendingCreate') { xmlNetwork += "<"+element+">"+"Created"+"" } else { //pending-update or PendingUpdate xmlNetwork += "<"+element+">"+"Active"+"" } } } else { xmlNetwork += "<"+element+">"+var.toString()+"" } } } } return xmlNetwork } def buildSubnets(L3Network network) { def rebuildingSubnets = "" Subnets subnets = network.getSubnets() try{ for(Subnet s : subnets.getSubnet()){ def orchestrationStatus = s.getOrchestrationStatus() if (orchestrationStatus == "pending-delete" || orchestrationStatus == "PendingDelete") { // skip, do not include in processing, remove!!! } else { def subnetList = ["dhcp-start", "dhcp-end", "network-start-address", "cidr-mask", "dhcp-enabled", "gateway-address", "ip-version", "subnet-id", "subnet-name", "ip-assignment-direction", "host-routes"] rebuildingSubnets += buildSubNetworkElements(s, subnetList, "subnets") //rebuildingSubnets += buildSubNetworkElements(subnetXml, subnetList, "") } } } catch (Exception ex) { // } finally { //rebuildingSubnets += "" } return rebuildingSubnets } // build subnet sub-network single elements def buildSubNetworkElements(subnetXml, createNetworkResponse, elementList, parentName) { String var = "" def xmlBuild = "" if (parentName != "") { xmlBuild += "<"+parentName+">" } if (subnetXml != null) { for (element in elementList) { def xml= new XmlSlurper().parseText(subnetXml) var = xml.'**'.find {it.name() == element} if (var != null) { if (element=="orchestration-status") { if(var.toString() == 'pending-create' || var.toString() == 'PendingCreate') { xmlBuild += "<"+element+">"+"Created"+"" } else { // pending-update or PendingUpdate' xmlBuild += "<"+element+">"+"Active"+"" } } else { // "subnet-id", "neutron-subnet-id" if (element=="subnet-id") { if (utils.nodeExists(createNetworkResponse, "subnetMap")) { xmlBuild += "<"+element+">"+var.toString()+"" String neutronSubnetId = extractNeutSubId(createNetworkResponse, var.toString()) xmlBuild += ""+neutronSubnetId+"" } } else { if (element=="neutron-subnet-id") { // skip } else { if (element=="host-routes") { if (subnetXml.contains("host-routes")) { List elementRoute = ["host-route-id", "route-prefix", "next-hop", "next-hop-type", "resource-version"] xmlBuild += buildXMLElements(subnetXml, "host-routes", "host-route", elementRoute) } } else { xmlBuild += "<"+element+">"+var.toString()+"" } } } } } } } if (parentName != "") { xmlBuild += "" } return xmlBuild } // build subnet sub-network single elements def buildSubNetworkElements(Subnet subnet, elementList, parentName) { def var = "" def xmlBuild = "" if (parentName != "") { xmlBuild += "<"+parentName+">" } if (subnet != null) { def networkStartAddress = "" for (element in elementList) { if (element == "dhcp-start") { var = subnet.getDhcpStart() xmlBuild += "" if (var == null) { xmlBuild += ""+""+"" } else { xmlBuild += ""+var+"" } } if (element == "dhcp-end") { var = subnet.getDhcpEnd() if (var == null) { xmlBuild += ""+""+"" } else { xmlBuild += ""+var+"" } xmlBuild += "" } if (element == "network-start-address" || element == "cidr-mask") { if (element == "network-start-address") { networkStartAddress = subnet.getNetworkStartAddress() } if (element == "cidr-mask") { xmlBuild += ""+networkStartAddress+"/"+var+"" } } if (element == "dhcp-enabled") { xmlBuild += ""+subnet.isDhcpEnabled()+"" } if (element == "gateway-address") { xmlBuild += ""+subnet.getGatewayAddress()+"" } if (element == "ip-version") { String ipVersion = getIpvVersion(subnet.getIpVersion()) xmlBuild += ""+ipVersion+"" } if (element == "subnet-id") { xmlBuild += ""+subnet.getSubnetId()+"" } if ((element == "subnet-name") && (subnet.getSubnetName() != null)) { xmlBuild += ""+subnet.getSubnetName()+"" } if ((element == "ip-assignment-direction") && (subnet.getIpAssignmentDirection() != null)) { xmlBuild += ""+subnet.getIpAssignmentDirection()+"" } if (element == "host-routes") { def routes = "" if (subnet.getHostRoutes() != null) { routes = buildHostRoutes(subnet) } xmlBuild += routes } } } if (parentName != "") { xmlBuild += "" } return xmlBuild } // rebuild host-routes def buildHostRoutes(subnetXml) { List routeElementList = ["host-route-id", "route-prefix", "next-hop", "next-hop-type", "resource-version"] def hostRoutes = buildXMLElements(subnetXml, "host-routes", "host-route", routeElementList) def buildHostRoutes = "" def var = "" if (hostRoutes!=null) { def routesData = new XmlSlurper().parseText(hostRoutes) def routes = routesData.'**'.findAll {it.name() == "host-route"} def routesSize = routes.size() for (i in 0..routesSize-1) { buildHostRoutes += "" def route = routes[i] def routeXml = XmlUtil.serialize(route) List elementList = ["route-prefix", "next-hop"] for (element in elementList) { def xml= new XmlSlurper().parseText(routeXml) var = xml.'**'.find {it.name() == element} if (element == "route-prefix") { buildHostRoutes += ""+var.toString()+"" } if (element == "next-hop") { buildHostRoutes += ""+var.toString()+"" } } buildHostRoutes += "" } } return buildHostRoutes } def buildVlans(queryIdResponse) { def rebuildingSubnets = "" def subnetsData = new XmlSlurper().parseText(queryIdResponse) try { def subnets = subnetsData.'**'.findAll {it.name() == "segmentation-assignments"} def subnetsSize = subnets.size() for (i in 0..subnetsSize-1) { def subnet = subnets[i] def subnetXml = XmlUtil.serialize(subnet) String vlan = utils.getNodeText(subnetXml, "segmentation-id") if (i>0){ rebuildingSubnets += "," } rebuildingSubnets += vlan } } catch (Exception ex) { // } finally { //rebuildingSubnets += "" rebuildingSubnets += "" } return rebuildingSubnets } /* Utility code to rebuild xml/elements in a list: * rebuild xml with 1) unbounded groups of elements; or * 2) one group of elements; or * 3) just one or more elements (in a list as argument) * @param xmlInput the XML document * @param parentName the parent name (ex: 'inputs') * @param childrenName the chilrendName (ex: 'entry' as unbounded/occurs>1) * @param elementList the element list of children (ex: 'key', 'value') * @return a string of rebuild xml * * Ex 1: xmlInput: * * * name * Edward * * * age * 30 * * * age * 30 * * * Usage: * List elementList = ["key", "value"] * String rebuild = buildXMLElements(xmlInput, "inputs", "entry", elementList) * * Ex 2: xmlInput // no parent tag * * fec8ec88-151a-45c9-ad60-8233e0fc8ff2 * https://localhost:8443/adapters/rest/SDNCNotify * assign * * Usage: * List elementList = ["svc-request-id", "svc-notification-url", "svc-action"] * String rebuild = buildXMLElements(xmlInput, "" , "sdnc-request-header", elementList) // no parent tag * * Ex 3: xmlInput // elements one after another (with no parent & children tag) * myTestid * myUser * Usage: * List elementList = ["test-id", "test-user"] * String rebuild = buildXMLElements(xmlInput, "" , "", elementList) * */ def buildXMLElements(xmlInput, parentName, childrenName, elementList) { def varChildren = "" def var = "" def xmlBuildUnbounded = "" if (parentName!="") {xmlBuildUnbounded += "<"+parentName+">" +'\n'} if (xmlInput != null) { def xml= new XmlSlurper().parseText(xmlInput) if (childrenName!="") { varChildren = xml.'**'.findAll {it.name() == childrenName} for (i in 0..varChildren.size()-1) { xmlBuildUnbounded += "<"+childrenName+">" +'\n' for (element in elementList) { var = varChildren[i].'*'.find {it.name() == element} if (var != null) { xmlBuildUnbounded += "<"+element+">"+var.toString()+"" +'\n' } } xmlBuildUnbounded += "" +'\n' } } else { for (element in elementList) { var = xml.'*'.find {it.name() == element} if (var != null) { xmlBuildUnbounded += "<"+element+">"+var.toString()+"" +'\n' } } } } if (parentName!="") {xmlBuildUnbounded += "" +'\n'} return xmlBuildUnbounded } def getFirstNodeXml(xmlInput, element){ def nodeAsText = "" def nodeToSerialize = "" if (xmlInput != null) { def fxml= new XmlSlurper().parseText(xmlInput) if (utils.nodeExists(xmlInput, "payload")) { nodeToSerialize = fxml.'payload'.'l3-network'.'*'.find {it.name() == element} if (nodeToSerialize!=null) { nodeAsText = XmlUtil.serialize(nodeToSerialize) } else { nodeAsText = "" } } else { nodeToSerialize = fxml.'*'.find {it.name() == element} if (nodeToSerialize!=null) { nodeAsText = XmlUtil.serialize(nodeToSerialize) } else { nodeAsText = "" } } } return nodeAsText } //TODO: This method still needs to be tested before using. /** * * This method is similar to the gennetwork:ContrailNetworUpdateCompletedObject * BPEL method. It extracts all of the required subnet information * for each subnet listed with an orch status equal to the one provided * and puts the corresponding infomation with the appropriate node for * updating aai. The method sets the orch status for each subnet to active * * @param subnetsXml the entire subnets xml * @param requestInput the request in the process * @param queryIdResponse the response of REST AAI query by Id * @param queryVpnBindingResponse the response of REST AAI query by vpn binding * @param routeCollection the collection of vpnBinding's 'global-route-target' * @return String request */ public String networkUpdateSubnetInfo(String subnetsXml, String networkResponseXml){ String subnets = "" StringBuilder sb = new StringBuilder() InputSource source = new InputSource(new StringReader(subnetsXml)); DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); docFactory.setNamespaceAware(true) DocumentBuilder docBuilder = docFactory.newDocumentBuilder() Document xml = docBuilder.parse(source) NodeList nodeList = xml.getElementsByTagNameNS("*", "subnet") for (int x = 0; x < nodeList.getLength(); x++) { Node node = nodeList.item(x) String subnet = "" if (node.getNodeType() == Node.ELEMENT_NODE) { Element eElement = (Element) node String subnetOrchStatus = eElement.getElementsByTagNameNS("*", "orchestration-status").item(0).getTextContent() if(subnetOrchStatus.equals("pending-create")){ String subnetId = eElement.getElementsByTagNameNS("*", "subnet-id").item(0).getTextContent() def netAddress = eElement.getElementsByTagNameNS("*", "network-start-address").item(0).getTextContent() def mask = eElement.getElementsByTagNameNS("*", "cidr-mask").item(0).getTextContent() def dhcpEnabledSubnet = eElement.getElementsByTagNameNS("*", "dhcp-enabled").item(0).getTextContent() def gatewayAddress = eElement.getElementsByTagNameNS("*", "gateway-address").item(0).getTextContent() def ipVersion = eElement.getElementsByTagNameNS("*", "ip-version").item(0).getTextContent() def relationshipList = eElement.getElementsByTagNameNS("*", "relationship-list").item(0).getTextContent() //TODO: test this String neutronSubnetId = extractNeutSubId(networkResponseXml, subnetId) subnet = """ ${MsoUtils.xmlEscape(subnetId)} ${MsoUtils.xmlEscape(neutronSubnetId)} ${MsoUtils.xmlEscape(gatewayAddress)} ${MsoUtils.xmlEscape(netAddress)} ${MsoUtils.xmlEscape(mask)} ${MsoUtils.xmlEscape(ipVersion)} active ${MsoUtils.xmlEscape(dhcpEnabledSubnet)} ${relationshipList} """ }else if(subnetOrchStatus.equals("pending-delete")){ StringWriter writer = new StringWriter() Transformer transformer = TransformerFactory.newInstance().newTransformer() transformer.transform(new DOMSource(node), new StreamResult(writer)) subnet = writer.toString() }else{ subnet = "" } } subnets = sb.append(subnet) } subnets = utils.removeXmlPreamble(subnets) String subnetsList = """ ${subnets} """ return subnetsList } /** * * This method extracts the "value" node text for the the given subnet Id. * * @param String inputSource - xml that contains the subnet id key and value * @param String subnetId - for which you want the value of * @return String value - node text of node named value associated with the given subnet id */ public String extractNeutSubId(String inputSource, String subnetId){ String value = "" InputSource source = new InputSource(new StringReader(inputSource)); DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); docFactory.setNamespaceAware(true) DocumentBuilder docBuilder = docFactory.newDocumentBuilder() Document xml = docBuilder.parse(source) NodeList nodeList = xml.getElementsByTagNameNS("*", "entry") for (int x = 0; x < nodeList.getLength(); x++) { Node node = nodeList.item(x) String subnet = "" if (node.getNodeType() == Node.ELEMENT_NODE) { Element eElement = (Element) node String key = eElement.getElementsByTagNameNS("*", "key").item(0).getTextContent() if(key.equals(subnetId)){ value = eElement.getElementsByTagNameNS("*", "value").item(0).getTextContent() } } } return value } public boolean isRollbackEnabled (DelegateExecution execution, String payloadXml) { def rollbackEnabled = false def rollbackValueSet = false if (utils.nodeExists(payloadXml, "backout-on-failure")) { String backoutValue = utils.getNodeText(payloadXml, "backout-on-failure") if (backoutValue != null && !backoutValue.isEmpty()) { if (backoutValue.equalsIgnoreCase("false")) { rollbackEnabled = false } else { rollbackEnabled = true } rollbackValueSet = true; } } if (!rollbackValueSet) { if (UrnPropertiesReader.getVariable("mso.rollback", execution) != null) { rollbackEnabled = UrnPropertiesReader.getVariable("mso.rollback", execution).toBoolean() } } return rollbackEnabled } /** * This method extracts the version for the the given ip-version. * * @param String ipvVersion - IP protocols version (ex: ipv4 or ipv6 or 4 or 6) * @return String version - digit version (ex: 4 or 6) */ public String getIpvVersion (String ipvVersion) { String version = "" try { if (ipvVersion.isNumber()) { version = ipvVersion } else { version = ipvVersion.substring(ipvVersion.indexOf("ipv")+3) if (!version.isNumber()) { version = ipvVersion } } } catch (Exception ex) { version = ipvVersion } return version } }