2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.so.bpmn.common.scripts
23 import java.text.SimpleDateFormat
25 import org.apache.commons.codec.binary.Base64
26 import org.apache.commons.lang3.StringEscapeUtils
27 import org.onap.so.bpmn.core.BPMNLogger
28 import org.onap.so.bpmn.core.xml.XmlTool
29 import org.onap.so.logger.MessageEnum
30 import org.onap.so.logger.MsoLogger
31 import org.onap.so.utils.CryptoUtils
33 import org.w3c.dom.Element
35 import groovy.util.slurpersupport.NodeChild
36 import groovy.xml.XmlUtil
39 private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, MsoUtils.class);
41 def initializeEndPoints(execution){
42 // use this placeholder to initialize end points, if called independently, this need to be set
43 execution.setVariable("AAIEndPoint","http://localhost:28080/SoapUIMocks")
47 * Returns the unescaped contents of element
53 def getNodeText(xmlInput,element){
56 def xml= new XmlSlurper().parseText(xmlInput)
57 rtn= xml.'**'.find{node->node.name()==element}
64 def getMultNodes(xmlInput, element){
67 def xml= new XmlSlurper().parseText(xmlInput)
68 nodes = xml.'**'.findAll{ node-> node.name() == element }*.text()
73 * Note: this uses XmlParser instead of XmlSlurper, thus it is not as
74 * efficient because it instantiates the whole DOM tree.
77 * @return a list of Nodes, or {@code null} if <i>xmlInput</i> is {@code null}
79 def getMultNodeObjects(xmlInput, element){
82 def xml= new XmlParser().parseText(xmlInput)
83 nodes = xml.'**'.findAll{ node-> node.name() == element }
88 def getNodeXml(xmlInput,element){
89 return getNodeXml(xmlInput, element, true)
91 def getNodeXml(xmlInput,element,incPreamble){
92 def fxml= new XmlSlurper().parseText(xmlInput)
93 NodeChild nodeToSerialize = fxml.'**'.find {it.name() == element}
94 if(nodeToSerialize==null){
97 def nodeAsText = XmlUtil.serialize(nodeToSerialize)
99 nodeAsText = removeXmlPreamble(nodeAsText)
102 return unescapeNodeContents(nodeToSerialize, nodeAsText)
105 def unescapeNodeContents(NodeChild node, String text) {
106 if (!node.childNodes().hasNext()) {
107 return StringEscapeUtils.unescapeXml(text)
113 def nodeExists(xmlInput,element){
115 def fxml= new XmlSlurper().parseText(xmlInput)
116 def nodeToSerialize = fxml.'**'.find {it.name() == element}
117 return nodeToSerialize!=null
118 } catch(Exception e) {
124 /***** Utilities when using XmlParser *****/
127 * Convert a Node into a String by deserializing it and formatting it.
129 * @param node Node to be converted.
130 * @return the Node as a String.
132 def String nodeToString(Node node) {
133 def String nodeAsString = groovy.xml.XmlUtil.serialize(node)
134 nodeAsString = removeXmlPreamble(nodeAsString)
135 return formatXml(nodeAsString)
139 * Get the specified child Node of the specified parent. If there are
140 * multiple children of the same name, only the first one is returned.
141 * If there are no children with the specified name, 'null' is returned.
143 * @param parent Parent Node in which to find a child.
144 * @param childNodeName Name of the child Node to get.
145 * @return the (first) child Node with the specified name or 'null'
146 * if a child Node with the specified name does not exist.
148 def Node getChildNode(Node parent, String childNodeName) {
149 def NodeList nodeList = getIdenticalChildren(parent, childNodeName)
150 if (nodeList.size() == 0) {
153 return nodeList.get(0)
158 * Get the textual value of the specified child Node of the specified parent.
159 * If there are no children with the specified name, 'null' is returned.
161 * @param parent Parent Node in which to find a child.
162 * @param childNodeName Name of the child Node whose value to get.
163 * @return the textual value of child Node with the specified name or 'null'
164 * if a child Node with the specified name does not exist.
166 def String getChildNodeText(Node parent, String childNodeName) {
167 def Node childNode = getChildNode(parent, childNodeName)
168 if (childNode == null) {
171 return childNode.text()
176 * Get all of the child nodes from the specified parent that have the
177 * specified name. The returned NodeList could be empty.
179 * @param parent Parent Node in which to find children.
180 * @param childNodeName Name of the children to get.
181 * @return a NodeList of all the children from the parent with the specified
182 * name. The list could be empty.
184 def NodeList getIdenticalChildren(Node parent, String childNodeName) {
185 return (NodeList) parent.get(childNodeName)
188 /***** End of Utilities when using XmlParser *****/
191 /** these are covered under the common function above**/
192 def getSubscriberName(xmlInput,element){
195 def xml= new XmlSlurper().parseText(xmlInput)
196 rtn= xml.'**'.find{node->node.name()==element}.text()
200 def getTenantInformation(xmlInput,element){
201 def xml= new XmlSlurper().parseText(xmlInput)
202 def nodeToSerialize = xml.'**'.find {it.name() == 'service-information'}
203 def nodeAsText = XmlUtil.serialize(nodeToSerialize)
206 def getServiceInstanceId(xmlInput,element){
207 def xml= new XmlSlurper().parseText(xmlInput)
208 return ( xml.'**'.find{node->node.name()==element}.text() )
211 def searchResourceLink(xmlInput, resourceType){
212 def fxml= new XmlSlurper().parseText(xmlInput)
213 def element = fxml.'**'.find {it.'resource-type' == resourceType}
214 return (element == null) ? null : element.'resource-link'.text()
217 def searchMetaData(xmlInput, searchName, searchValue){
218 def fxml= new XmlSlurper().parseText(xmlInput)
219 def ret = fxml.'**'.find {it.metaname.text() == searchName && it.metaval.text() == searchValue}
221 return ret.parent().parent()
226 def searchMetaDataNode(fxml, searchName, searchValue){
227 def ret = fxml.'**'.find {it.metaname.text() == searchName && it.metaval.text() == searchValue}
229 return ret.parent().parent()
234 // for Trinity L3 add/delete bonding
235 def getPBGFList(isDebugLogEnabled, xmlInput){
236 log("DEBUG", "getPBGFList: xmlInput " + xmlInput,isDebugLogEnabled)
237 ArrayList myNodes = new ArrayList()
238 if(nodeExists(xmlInput,"nbnc-response-information")){
239 def respInfo=getNodeXml(xmlInput,"nbnc-response-information", false)
241 def fxml= new XmlSlurper().parseText(respInfo)
242 fxml.'virtual-datacenter-list'.each { vdc ->
243 //we only want to add two BGF per VDC, BGF1 and BGF2
244 def routerList = vdc.'router-list'.first()
245 routerList.each{ myList ->
246 def physNodes = myList.'**'.findAll {it.'border-element-tangibility'.text() =~ /PHYSICAL/}
252 def beid = nodeToAdd.'border-element-id'.text() +
253 " " + nodeToAdd.'border-element-type'.text() +
255 it.'border-element-id'.text() +
256 " " + it.'border-element-type'.text()
257 def mytag = nodeToAdd.'border-element-id'
258 mytag[0].replaceBody(beid)
261 def mytag = nodeToAdd.'vlan-id'
262 def ind = mytag.text().indexOf('.')
264 def vlan = mytag.text().substring(0,ind)
265 mytag[0].replaceBody(vlan)
267 myNodes.add(XmlUtil.serialize(nodeToAdd))
279 def getPBGFList(xmlInput){
280 getPBGFList("false", xmlInput)
283 def getMetaVal(node, name){
285 return node.'**'.find {it.metaname.text() == name}.metaval.text()
290 def getRelationshipVal(node, name){
292 return node.'**'.find {it.'relationship-key'.text() == name}.'relationship-value'.text()
299 def log(logmode,logtxt,isDebugLogEnabled="false"){
300 if ("INFO"==logmode) {
301 msoLogger.info(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, logtxt, "BPMN", MsoLogger.getServiceName());
302 } else if ("WARN"==logmode) {
303 // to see the warning text displayed in the log entry, the text must also be passed as arg0 (2nd argument) to invoke the correct MsoLogger warn() method
304 msoLogger.warn (MessageEnum.BPMN_GENERAL_WARNING, logtxt, "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, logtxt);
305 } else if ("ERROR"==logmode) {
306 // to see the error text displayed in the log entry, the text must also be passed as arg0 (2nd argument) to invoke the correct MsoLogger error() method
307 msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, logtxt, "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, logtxt);
310 BPMNLogger.debug(isDebugLogEnabled, logtxt);
314 def logContext(requestId, serviceInstanceId){
315 msoLogger.setLogContext(requestId, serviceInstanceId);
318 def logMetrics(elapsedTime, logtxt){
320 msoLogger.recordMetricEvent (elapsedTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc,
321 logtxt, "BPMN", MsoLogger.getServiceName(), null);
324 def logAudit(logtxt){
325 long startTime = System.currentTimeMillis();
327 msoLogger.recordAuditEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, logtxt);
330 // headers: header - name-value
331 def getHeaderNameValue(xmlInput, nameAttribute){
334 def xml= new XmlSlurper().parseText(xmlInput)
335 rtn= xml.'**'.find {header->header.'@name'.text() == nameAttribute}.'@value'
341 * Gets the children of the specified element.
343 public String getChildNodes(xmlInput, element) {
344 def xml= new XmlSlurper().parseText(xmlInput)
345 def thisElement = xml.'**'.find {it.name() == element}
346 StringBuilder out = new StringBuilder()
347 if (thisElement != null) {
348 thisElement.children().each() {
349 String nodeAsText = removeXmlPreamble(XmlUtil.serialize(it))
350 if (out.length() > 0) {
351 out.append(System.lineSeparator())
353 out.append(nodeAsText)
356 return out.toString();
360 * Encodes a value so it can be used inside an XML text element.
362 * <b>Will double encode</b>
363 * @param s the string to encode
364 * @return the encoded string
366 public static String xmlEscape(Object value) {
367 return XmlTool.encode(value)
371 * Removes the preamble, if present, from an XML document.
372 * Also, for historical reasons, this also trims leading and trailing
373 * whitespace from the resulting document. TODO: remove the trimming
374 * and fix unit tests that depend on EXACT xml format.
375 * @param xml the XML document
376 * @return a possibly modified document
378 public String removeXmlPreamble(def xml) {
383 return XmlTool.removePreamble(xml).trim()
387 * Removes namespaces and namespace declarations from an XML document.
388 * @param xml the XML document
389 * @return a possibly modified document
391 public String removeXmlNamespaces(def xml) {
392 return XmlTool.removeNamespaces(xml);
396 * Use formatXml instead. Note: this method inserts an XML preamble.
399 def formatXML(xmlInput) {
402 if (xmlInput !=null) {
403 parseXml = new XmlParser().parseText(xmlInput)
404 formatXml = XmlUtil.serialize(parseXml)
409 * Reformats an XML document. The result will not contain an XML preamble
410 * or a trailing newline.
411 * @param xml the XML document
412 * @return a reformatted document
414 public String formatXml(def xml) {
415 return XmlTool.normalize(xml);
418 // build single elements
419 def buildElements(xmlInput, elementList, parentName) {
422 if (parentName != "") {
423 xmlBuild += "<tns2:"+parentName+">"
425 if (xmlInput != null) {
426 for (element in elementList) {
427 def xml= new XmlSlurper().parseText(xmlInput)
428 var = xml.'**'.find {it.name() == element}
430 xmlBuild += "<tns2:"+element+">"+var.toString()+"</tns2:"+element+">"
434 if (parentName != "") {
435 xmlBuild += "</tns2:"+parentName+">"
440 // build the Unbounded elements
441 def buildElementsUnbounded(xmlInput, elementList, parentName) {
444 def xmlBuildUnbounded = ""
445 if (xmlInput != null) {
446 def xml= new XmlSlurper().parseText(xmlInput)
447 varParents = xml.'**'.findAll {it.name() == parentName}
448 //println " Unbounded ${parentName} - varParent.Size() - " + varParents.size()
449 for (i in 0..varParents.size()-1) {
450 if (parentName != "") {
451 xmlBuildUnbounded += "<tns2:"+parentName+">"
453 for (element in elementList) {
454 var = varParents[i].'*'.find {it.name() == element}
456 xmlBuildUnbounded += "<tns2:"+element+">"+var.toString()+"</tns2:"+element+">"
457 //println " i = " + i + ", element: " + element + " = " + var.toString()
460 if (parentName != "") {
461 xmlBuildUnbounded += "</tns2:"+parentName+">"
465 return xmlBuildUnbounded
468 // Build l2-homing-information
469 def buildL2HomingInformation(xmlInput) {
470 def elementsL2HomingList = ["evc-name", "topology", "preferred-aic-clli","aic-version"]
471 def rebuildL2Home = ''
472 if (xmlInput != null) {
473 rebuildL2Home = buildElements(xmlInput, elementsL2HomingList, "l2-homing-information")
478 // Build internet-evc-access-information
479 def buildInternetEvcAccessInformation(xmlInput) {
480 def elementsInternetEvcAccessInformationList = ["internet-evc-speed-value", "internet-evc-speed-units", "ip-version"]
481 def rebuildInternetEvcAccess = ''
482 if (xmlInput != null) {
483 rebuildInternetEvcAccess = buildElements(xmlInput, elementsInternetEvcAccessInformationList, "internet-evc-access-information")
485 return rebuildInternetEvcAccess
488 // Build ucpe-vms-service-information
489 def buildUcpeVmsServiceInformation(xmlInput) {
490 def rebuildUcpeVmsServiceInformation = ''
491 if (xmlInput != null) {
492 def ucpeVmsServiceInformation = getNodeXml(xmlInput, "ucpe-vms-service-information").drop(38).trim()
493 rebuildUcpeVmsServiceInformation = "<tns2:ucpe-vms-service-information>"
494 // transport-service-information
495 rebuildUcpeVmsServiceInformation += "<tns2:transport-service-information>"
496 def transportServiceInformation = getNodeXml(ucpeVmsServiceInformation, "transport-service-information").drop(38).trim()
497 def elementsTransportServiceInformationList = ["transport-service-type"]
498 rebuildUcpeVmsServiceInformation += buildElements(transportServiceInformation, elementsTransportServiceInformationList, "")
500 def accessCircuitInfoList = ["access-circuit-id", "dual-mode"]
501 rebuildUcpeVmsServiceInformation += buildElementsUnbounded(transportServiceInformation, accessCircuitInfoList, "access-circuit-info")
502 } catch (Exception e) {
503 log("ERROR", " Optional - Exception ACCESS-CIRCUIT-INFO - 'access-circuit-info' ")
505 rebuildUcpeVmsServiceInformation += "</tns2:transport-service-information>"
507 def elementsUcpeInformationList = ["ucpe-host-name", "ucpe-activation-code", "out-of-band-management-modem" ]
508 rebuildUcpeVmsServiceInformation += buildElements(ucpeVmsServiceInformation, elementsUcpeInformationList, "ucpe-information")
510 rebuildUcpeVmsServiceInformation += "<tns2:vnf-list>"
511 def vnfListList = ["vnf-instance-id", "vnf-sequence-number", "vnf-type", "vnf-vendor", "vnf-model", "vnf-id", "prov-status", "operational-state", "orchestration-status", "equipment-role" ]
512 rebuildUcpeVmsServiceInformation += buildElementsUnbounded(ucpeVmsServiceInformation, vnfListList, "vnf-information")
513 rebuildUcpeVmsServiceInformation += "</tns2:vnf-list>"
514 rebuildUcpeVmsServiceInformation += "</tns2:ucpe-vms-service-information>"
516 log("DEBUG", " rebuildUcpeVmsServiceInformation - " + rebuildUcpeVmsServiceInformation)
517 return rebuildUcpeVmsServiceInformation
520 // Build internet-service-change-details
521 def buildInternetServiceChangeDetails(xmlInput) {
522 def rebuildInternetServiceChangeDetails = ""
523 if (xmlInput != null) {
525 def internetServiceChangeDetails = getNodeXml(xmlInput, "internet-service-change-details").drop(38).trim()
526 rebuildInternetServiceChangeDetails = "<tns:internet-service-change-details>"
527 rebuildInternetServiceChangeDetails += buildElements(internetServiceChangeDetails, ["internet-evc-speed-value"], "")
528 rebuildInternetServiceChangeDetails += buildElements(internetServiceChangeDetails, ["internet-evc-speed-units"], "")
529 rebuildInternetServiceChangeDetails += buildElements(internetServiceChangeDetails, ["v4-vr-lan-address"], "")
530 rebuildInternetServiceChangeDetails += buildElements(internetServiceChangeDetails, ["v4-vr-lan-prefix-length"], "")
532 def tProvidedV4LanPublicPrefixesChangesList = ["request-index", "v4-next-hop-address", "v4-lan-public-prefix", "v4-lan-public-prefix-length"]
533 rebuildInternetServiceChangeDetails += buildElementsUnbounded(internetServiceChangeDetails, tProvidedV4LanPublicPrefixesChangesList, "t-provided-v4-lan-public-prefixes")
534 } catch (Exception e) {
535 log("ERROR"," Optional - Exception in INTERNET-SERVICE-CHANGE-DETAILS 't-provided-v4-lan-public-prefixes ")
538 def tProvidedV6LanPublicPrefixesChangesList = ["request-index", "v6-next-hop-address", "v6-lan-public-prefix", "v6-lan-public-prefix-length"]
539 rebuildInternetServiceChangeDetails += buildElementsUnbounded(internetServiceChangeDetails, tProvidedV6LanPublicPrefixesChangesList, "t-provided-v6-lan-public-prefixes")
540 } catch (Exception e) {
541 log("ERROR"," Optional - Exception INTERNET-SERVICE-CHANGE-DETAILS 't-provided-v6-lan-public-prefixes ")
543 rebuildInternetServiceChangeDetails += "</tns:internet-service-change-details>"
544 } catch (Exception e) {
545 log("ERROR", " Optional - Exception INTERNET-SERVICE-CHANGE-DETAILS 'internet-service-change-details' ")
548 return rebuildInternetServiceChangeDetails
552 def buildVrLan(xmlInput) {
554 def rebuildVrLan = ''
555 if (xmlInput != null) {
557 rebuildVrLan = "<tns2:vr-lan>"
558 def vrLan = getNodeXml(xmlInput, "vr-lan").drop(38).trim()
559 rebuildVrLan += buildElements(vrLan, ["routing-protocol"], "")
562 def rebuildVrLanInterface = "<tns2:vr-lan-interface>"
563 def vrLanInterface = getNodeXml(vrLan, "vr-lan-interface").drop(38).trim()
564 rebuildVrLanInterface += buildVrLanInterfacePartial(vrLanInterface)
567 def dhcp = getNodeXml(vrLan, "dhcp").drop(38).trim()
568 def rebuildDhcp = buildDhcp(dhcp)
569 rebuildVrLanInterface += rebuildDhcp
572 def pat = getNodeXml(vrLan, "pat").drop(38).trim()
573 def rebuildPat = buildPat(pat)
574 rebuildVrLanInterface += rebuildPat
579 def nat = getNodeXml(vrLan, "nat").drop(38).trim()
580 rebuildNat = buildNat(nat)
581 } catch (Exception e) {
582 log("ERROR", " Optional - Exception 'nat' ")
584 rebuildVrLanInterface += rebuildNat
587 def firewallLite = getNodeXml(vrLan, "firewall-lite").drop(38).trim()
588 def rebuildFirewallLite = buildFirewallLite(firewallLite)
589 rebuildVrLanInterface += rebuildFirewallLite
592 def rebuildStaticRoutes = ""
594 def staticRoutes = getNodeXml(vrLan, "static-routes").drop(38).trim()
595 rebuildStaticRoutes = buildStaticRoutes(staticRoutes)
596 } catch (Exception e) {
597 log("ERROR", " Optional - Exception 'static-routes' ")
599 rebuildVrLanInterface += rebuildStaticRoutes
601 rebuildVrLan += rebuildVrLanInterface
602 rebuildVrLan += "</tns2:vr-lan-interface>"
603 rebuildVrLan += "</tns2:vr-lan>"
606 log("DEBUG", " rebuildVrLan - " + rebuildVrLan)
610 // Build vr-lan-interface
611 def buildVrLanInterfacePartial(xmlInput) {
612 def rebuildingVrLanInterface = ''
613 if (xmlInput != null) {
614 def vrLanInterfaceList = ["vr-designation", "v4-vr-lan-prefix", "v4-vr-lan-address", "v4-vr-lan-prefix-length", "v6-vr-lan-prefix", "v6-vr-lan-address", "v6-vr-lan-prefix-length", "v4-vce-loopback-address", "v6-vce-wan-address"]
615 rebuildingVrLanInterface += buildElements(xmlInput, vrLanInterfaceList, "")
616 rebuildingVrLanInterface += "<tns2:v4-public-lan-prefixes>"
618 def tProvidedV4LanPublicPrefixes = getNodeXml(xmlInput, "v4-public-lan-prefixes").drop(38).trim()
619 def tProvidedV4LanPublicPrefixesList = ["request-index", "v4-next-hop-address", "v4-lan-public-prefix", "v4-lan-public-prefix-length" ]
620 rebuildingVrLanInterface += buildElementsUnbounded(xmlInput, tProvidedV4LanPublicPrefixesList, "t-provided-v4-lan-public-prefixes")
621 } catch (Exception ex) {
622 log("ERROR", " Optional - Exception VR-LAN INTERFACE 'v4-public-lan-prefixes' ")
624 rebuildingVrLanInterface += "</tns2:v4-public-lan-prefixes>"
625 rebuildingVrLanInterface += "<tns2:v6-public-lan-prefixes>"
627 def tProvidedV6LanPublicPrefixes = getNodeXml(xmlInput, "v6-public-lan-prefixes").drop(38).trim()
628 def tProvidedV6LanPublicPrefixesList = ["request-index", "v6-next-hop-address", "v6-lan-public-prefix", "v6-lan-public-prefix-length" ]
629 rebuildingVrLanInterface += buildElementsUnbounded(xmlInput, tProvidedV6LanPublicPrefixesList, "t-provided-v6-lan-public-prefixes")
630 } catch (Exception e) {
631 log("ERROR", " Optional - Exception VR-LAN INTERFACE 'v6-public-lan-prefixes' ")
633 rebuildingVrLanInterface += "</tns2:v6-public-lan-prefixes>"
635 log("DEBUG", " rebuildingVrLanInterface - " + rebuildingVrLanInterface)
636 return rebuildingVrLanInterface
640 def buildDhcp(xmlInput) {
641 def rebuildingDhcp = ''
642 if (xmlInput != null) {
643 def dhcpData = new XmlSlurper().parseText(xmlInput)
644 rebuildingDhcp = "<tns2:dhcp>"
645 def dhcpList1 = ["v4-dhcp-server-enabled", "v6-dhcp-server-enabled", "use-v4-default-pool", "v4-dhcp-default-pool-prefix", "v4-dhcp-default-pool-prefix-length"]
646 rebuildingDhcp += buildElements(xmlInput, dhcpList1, "")
648 def excludedV4DhcpAddressesFromDefaultPoolList = ["excluded-v4-address"]
649 rebuildingDhcp += buildElementsUnbounded(xmlInput, excludedV4DhcpAddressesFromDefaultPoolList, "excluded-v4-dhcp-addresses-from-default-pool")
650 } catch (Exception e) {
651 log("ERROR", " Optional - Exception DHCP 'excluded-v4-dhcp-addresses-from-default-pool' ")
654 def v4DhcpPools = dhcpData.'**'.findAll {it.name() == "v4-dhcp-pools"}
655 def v4DhcpPoolsSize = v4DhcpPools.size()
656 // println " v4DhcpPoolsSize = " + v4DhcpPools.size()
657 for (i in 0..v4DhcpPoolsSize-1) {
658 def v4DhcpPool = v4DhcpPools[i]
659 def v4DhcpPoolXml = XmlUtil.serialize(v4DhcpPool)
660 rebuildingDhcp += "<tns2:v4-dhcp-pools>"
661 def v4DhcpPoolsList1 = ["v4-dhcp-pool-prefix", "v4-dhcp-pool-prefix-length" ]
662 rebuildingDhcp += buildElements(v4DhcpPoolXml, v4DhcpPoolsList1, "")
664 def excludedV4AddressesList = ["excluded-v4-address"]
665 rebuildingDhcp += buildElementsUnbounded(v4DhcpPoolXml, excludedV4AddressesList, "excluded-v4-addresses")
666 } catch (Exception e) {
667 log("ERROR", " Optional - Exception DHCP 'excluded-v4-addresses' ")
669 def v4DhcpPoolsList2 = ["v4-dhcp-relay-gateway-address", "v4-dhcp-relay-next-hop-address"]
670 rebuildingDhcp += buildElements(v4DhcpPoolXml, v4DhcpPoolsList2, "")
671 rebuildingDhcp += "</tns2:v4-dhcp-pools>"
673 } catch (Exception e) {
674 log("ERROR"," Optional - Exception DHCP 'v4-dhcp-pools' ")
676 def dhcpList2 = ["use-v6-default-pool", "v6-dhcp-default-pool-prefix", "v6-dhcp-default-pool-prefix-length"]
677 rebuildingDhcp += buildElements(xmlInput, dhcpList2, "")
679 def excludedV6DhcpAddressesFromDdefaultPoolList = ["excluded-v6-address"]
680 rebuildingDhcp += buildElementsUnbounded(xmlInput, excludedV6DhcpAddressesFromDdefaultPoolList, "excluded-v6-dhcp-addresses-from-default-pool")
681 } catch (Exception e) {
682 log("ERROR", " Optional - Exception DHCP 'excluded-v6-dhcp-addresses-from-default-pool' ")
685 def v6DhcpPools = dhcpData.'**'.findAll {it.name() == "v6-dhcp-pools"}
686 def v6DhcpPoolsSize = v6DhcpPools.size()
687 //println " v6DhcpPoolsSize = " + v6DhcpPools.size()
688 for (i in 0..v6DhcpPoolsSize-1) {
689 def v6DhcpPool = v6DhcpPools[i]
690 def v6DhcpPoolXml = XmlUtil.serialize(v6DhcpPool)
691 rebuildingDhcp += "<tns2:v6-dhcp-pools>"
692 def v6DhcpPoolsList1 = ["v6-dhcp-pool-prefix", "v6-dhcp-pool-prefix-length"]
693 rebuildingDhcp += buildElements(v6DhcpPoolXml, v6DhcpPoolsList1, "")
695 def excludedV6AddressesList = ["excluded-v6-address"]
696 rebuildingDhcp += buildElementsUnbounded(v6DhcpPoolXml, excludedV6AddressesList, "excluded-v6-addresses")
697 } catch (Exception e) {
698 log("ERROR", " Optional - Exception DHCP 'excluded-v6-addresses' ")
700 def v6DhcpPoolsList2 = ["v6-dhcp-relay-gateway-address", "v6-dhcp-relay-next-hop-address"]
701 rebuildingDhcp += buildElements(v6DhcpPoolXml, v6DhcpPoolsList2, "")
702 rebuildingDhcp += "</tns2:v6-dhcp-pools>"
704 } catch (Exception e) {
705 log("ERROR", " Optional - Exception DHCP 'v6-dhcp-pools' ")
707 rebuildingDhcp += "</tns2:dhcp>"
709 log("DEBUG", " rebuildingDhcp - " + rebuildingDhcp)
710 return rebuildingDhcp
714 def buildPat(xmlInput) {
715 def rebuildingPat = ''
716 if (xmlInput != null) {
717 rebuildingPat = "<tns2:pat>"
718 def patList = ["v4-pat-enabled", "use-v4-default-pool", "v4-pat-default-pool-prefix", "v4-pat-default-pool-prefix-length"]
719 rebuildingPat += buildElements(xmlInput, patList, "")
721 def v4PatPools = getNodeXml(xmlInput, "v4-pat-pools").drop(38).trim()
722 def v4PatPoolsList = ["v4-pat-pool-prefix", "v4-pat-pool-prefix-length", "v4-pat-pool-next-hop-address"]
723 rebuildingPat += buildElementsUnbounded(xmlInput, v4PatPoolsList, "v4-pat-pools")
724 } catch (Exception e) {
725 log("ERROR", " Optional - Exception 'v4-pat-pool-next-hop-address' ")
727 rebuildingPat += "</tns2:pat>"
729 log("DEBUG", " rebuildingPat - " + rebuildingPat)
734 def buildNat(xmlInput) {
735 def rebuildingNat = ''
736 if (xmlInput != null) {
737 rebuildingNat = "<tns2:nat>"
738 rebuildingNat += buildElements(xmlInput, ["v4-nat-enabled"], "")
740 def v4NatMappingEntries = getNodeXml(xmlInput, "v4-nat-mapping-entries").drop(38).trim()
741 def v4NatMappingEntriesList = ["v4-nat-internal", "v4-nat-next-hop-address", "v4-nat-external"]
742 rebuildingNat += buildElementsUnbounded(xmlInput, v4NatMappingEntriesList, "v4-nat-mapping-entries")
743 } catch (Exception e) {
744 log("ERROR", " Optional - Exception 'v4-nat-external' ")
746 rebuildingNat += "</tns2:nat>"
748 log("DEBUG", " rebuildingNat - " + rebuildingNat)
752 // Build firewall-lite
753 def buildFirewallLite(xmlInput) {
754 def rebuildingFirewallLite = ''
756 if (xmlInput != null) {
758 def firewallLiteData = new XmlSlurper().parseText(xmlInput)
759 rebuildingFirewallLite = "<tns2:firewall-lite>"
760 def firewallLiteList = ["stateful-firewall-lite-v4-enabled", "stateful-firewall-lite-v6-enabled"]
761 rebuildingFirewallLite += buildElements(xmlInput, firewallLiteList, "")
764 def v4FirewallPacketFilters = firewallLiteData.'**'.findAll {it.name() == "v4-firewall-packet-filters"}
765 def v4FirewallPacketFiltersSize = v4FirewallPacketFilters.size()
766 //println " v4FirewallPacketFiltersSize = " + v4FirewallPacketFilters.size()
767 for (i in 0..v4FirewallPacketFiltersSize-1) {
768 def v4FirewallPacketFilter = v4FirewallPacketFilters[i]
769 def v4FirewallPacketFilterXml = XmlUtil.serialize(v4FirewallPacketFilter)
770 rebuildingFirewallLite += "<tns2:v4-firewall-packet-filters>"
771 def v4FirewallPacketFiltersList = ["v4-firewall-prefix", "v4-firewall-prefix-length", "allow-icmp-ping"]
772 rebuildingFirewallLite += buildElements(v4FirewallPacketFilterXml, v4FirewallPacketFiltersList, "")
774 def udpPortsList = ["port-number"]
775 rebuildingFirewallLite += buildElementsUnbounded(v4FirewallPacketFilterXml, udpPortsList, "udp-ports")
776 } catch (Exception e) {
777 log("ERROR", " Optional - Exception FIREWALL-LITE v4 'udp-ports' ")
780 def tcpPortsList = ["port-number"]
781 rebuildingFirewallLite += buildElementsUnbounded(v4FirewallPacketFilterXml, tcpPortsList, "tcp-ports")
782 } catch (Exception e) {
783 log("ERROR", " Optional - Exception FIREWALL-LITE v4 'tcp-ports' ")
785 rebuildingFirewallLite += "</tns2:v4-firewall-packet-filters>"
787 } catch (Exception e) {
788 log("ERROR", " Optional - Exception FIREWALL-LITE 'v4-firewall-packet-filters' ")
792 def v6FirewallPacketFilters = firewallLiteData.'**'.findAll {it.name() == "v6-firewall-packet-filters"}
793 def v6FirewallPacketFiltersSize = v6FirewallPacketFilters.size()
794 //println " v6FirewallPacketFiltersSize = " + v6FirewallPacketFilters.size()
795 for (i in 0..v6FirewallPacketFiltersSize-1) {
796 def v6FirewallPacketFilter = v6FirewallPacketFilters[i]
797 def v6FirewallPacketFilterXml = XmlUtil.serialize(v6FirewallPacketFilter)
798 rebuildingFirewallLite += "<tns2:v6-firewall-packet-filters>"
799 def v6FirewallPacketFiltersList = ["v6-firewall-prefix", "v6-firewall-prefix-length", "allow-icmp-ping"]
800 rebuildingFirewallLite += buildElements(v6FirewallPacketFilterXml, v6FirewallPacketFiltersList, "")
802 def udpPortsList = ["port-number"]
803 rebuildingFirewallLite += buildElementsUnbounded(v6FirewallPacketFilterXml, udpPortsList, "udp-ports")
804 } catch (Exception e) {
805 log("ERROR", " Optional - Exception FIREWALL-LITE v6 'udp-ports' ")
808 def tcpPortsList = ["port-number"]
809 rebuildingFirewallLite += buildElementsUnbounded(v6FirewallPacketFilterXml, tcpPortsList, "tcp-ports")
810 } catch (Exception e) {
811 log("ERROR", " Optional - Exception FIREWALL-LITE v6 'tcp-ports' ")
813 rebuildingFirewallLite += "</tns2:v6-firewall-packet-filters>"
815 } catch (Exception e) {
816 log("ERROR", " Optional - Exception FIREWALL-LITE 'v6-firewall-packet-filters' ")
818 rebuildingFirewallLite+= "</tns2:firewall-lite>"
820 log("DEBUG", " rebuildingFirewallLite - " + rebuildingFirewallLite)
821 return rebuildingFirewallLite
824 def buildStaticRoutes(xmlInput) {
825 def rebuildingStaticRoutes = ''
826 if (xmlInput != null) {
827 rebuildingStaticRoutes = "<tns2:static-routes>"
828 def v4StaticRouteslist = ["v4-static-route-prefix","v4-static-route-prefix-length", "v4-next-hop-address"]
829 rebuildingStaticRoutes += buildElementsUnbounded(xmlInput, v4StaticRouteslist, "v4-static-routes")
830 def v6StaticRouteslist = ["v6-static-route-prefix","v6-static-route-prefix-length", "v6-next-hop-address"]
831 rebuildingStaticRoutes += buildElementsUnbounded(xmlInput, v6StaticRouteslist, "v6-static-routes")
832 rebuildingStaticRoutes += "</tns2:static-routes>"
834 log("DEBUG", " rebuildingStaticRoutes - " + rebuildingStaticRoutes)
835 return rebuildingStaticRoutes
838 public String generateCurrentTimeInUtc(){
839 final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
840 sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
841 final String utcTime = sdf.format(new Date());
845 public String generateCurrentTimeInGMT(){
846 final SimpleDateFormat sdf = new SimpleDateFormat("E, d MMM yyyy h:m:s z");
847 sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
848 final String utcTime = sdf.format(new Date());
854 * @param encryptedAuth: encrypted credentials from urn properties
855 * @param msoKey: key to use to decrypt from urn properties
856 * @return base 64 encoded basic auth credentials
858 def getBasicAuth(encryptedAuth, msoKey){
859 if ((encryptedAuth == null || encryptedAuth.isEmpty()) || (msoKey == null || msoKey.isEmpty()))
862 def auth = decrypt(encryptedAuth, msoKey)
863 byte[] encoded = Base64.encodeBase64(auth.getBytes())
864 String encodedString = new String(encoded)
865 encodedString = "Basic " + encodedString
867 } catch (Exception ex) {
868 log("ERROR", "Unable to encode basic auth")
873 def encrypt(toEncrypt, msokey){
875 String result = CryptoUtils.encrypt(toEncrypt, msokey);
878 catch (Exception e) {
879 log("ERROR", "Failed to encrypt credentials")
883 def decrypt(toDecrypt, msokey){
885 String result = CryptoUtils.decrypt(toDecrypt, msokey);
888 catch (Exception e) {
889 log("ERROR", "Failed to decrypt credentials")
895 * Return URL with qualified host name (if any) or urn mapping
896 * @param String url from urn mapping
897 * @return String url with qualified host name
899 public String getQualifiedHostNameForCallback(String urnCallbackUrl) {
900 def callbackUrlToUse = urnCallbackUrl
902 //swap host name with qualified host name from the jboss properties
903 def qualifiedHostName = System.getProperty("jboss.qualified.host.name")
904 if(qualifiedHostName!=null){
905 log("DEBUG", "qualifiedHostName:\n" + qualifiedHostName)
906 callbackUrlToUse = callbackUrlToUse.replaceAll("(http://)(.*)(:28080*)", {orig, first, torepl, last -> "${first}${qualifiedHostName}${last}"})
909 log("DEBUG", "unable to grab qualified host name, using what's in urn properties for callbackurl. Exception was: " + e.printStackTrace())
911 return callbackUrlToUse
916 * Retrieves text context of the element if the element exists, returns empty string otherwise
917 * @param com.sun.org.apache.xerces.internal.dom.DeferredElementNSImpl element to parse
918 * param String tagName tagName
919 * @return String text content of the element
921 public String getElementText(Element element, String tagName) {
923 org.w3c.dom.NodeList nodeList = element.getElementsByTagNameNS("*", tagName)
924 if (nodeList != null && nodeList.length > 0) {
925 text = nodeList.item(0).getTextContent()
932 * Find the lowest unused module-index value in a given xml
934 public String getLowestUnusedIndex(String xml) {
935 if (xml == null || xml.isEmpty()) {
938 def moduleIndexList = getMultNodes(xml, "module-index")
939 if (moduleIndexList == null || moduleIndexList.size() == 0) {
942 def sortedModuleIndexList = moduleIndexList.sort { a, b -> a.compareTo b }
944 for (i in 0..sortedModuleIndexList.size()-1) {
945 if (Integer.parseInt(sortedModuleIndexList[i]) != i) {
949 return sortedModuleIndexList.size().toString()
952 * This utility checks if there is transaction id already present in MDC.
953 * If found, it returns same else creates new, sets in MDC for future use before returning
954 * @return String RequestId in UUID format.
956 public String getRequestID()
958 String requestId = MDC.get("RequestId")
959 if(requestId == null || requestId.isEmpty())
961 requestId = java.util.UUID.randomUUID()
962 MDC.put("RequestId",requestId)
963 log("DEBUG","MsoUtils - Created new RequestId: " + requestId)
967 log("DEBUG","MsoUtils - Using existing RequestId: " + requestId)
974 * Remove all the empty nodes and attributes from the within the given node
976 * @return true if all empty nodes and attributes were removed.
978 public boolean cleanNode( Node node ) {
979 node.attributes().with { a ->
980 a.findAll { !it.value }.each { a.remove( it.key ) }
982 node.children().with { kids ->
983 kids.findAll { it instanceof Node ? !cleanNode( it ) : false }
984 .each { kids.remove( it ) }
986 node.attributes() || node.children() || node.text()
992 * @return String representation of xml after removing the empty nodes and attributes
994 public String cleanNode(String xmlString) {
995 def xml = new XmlParser(false, false).parseText(xmlString)
997 return XmlUtil.serialize(xml)