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 com.att.bpm.scripts
23 import org.openecomp.mso.bpmn.core.WorkflowException
24 import org.openecomp.mso.rest.APIResponse
25 import org.openecomp.mso.rest.RESTClient
26 import org.openecomp.mso.rest.RESTConfig
28 import javax.xml.parsers.DocumentBuilder
29 import javax.xml.parsers.DocumentBuilderFactory
30 import javax.xml.transform.Transformer
31 import javax.xml.transform.TransformerFactory
32 import javax.xml.transform.TransformerException
33 import javax.xml.transform.dom.DOMSource
34 import javax.xml.transform.stream.StreamResult
36 import org.camunda.bpm.engine.delegate.BpmnError
37 import org.camunda.bpm.engine.runtime.Execution;
38 import org.w3c.dom.Document
39 import org.w3c.dom.Element
40 import org.w3c.dom.NamedNodeMap
41 import org.w3c.dom.Node
42 import org.w3c.dom.NodeList;
43 import org.xml.sax.InputSource
44 import org.apache.commons.codec.binary.Base64
45 import org.apache.commons.lang3.*
49 * Vnf Module Subflow for confirming the volume group belongs
53 * @param volumeGroupId
56 class ConfirmVolumeGroupTenant extends AbstractServiceTaskProcessor{
60 public void preProcessRequest(Execution execution){
61 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
62 execution.setVariable("prefix", Prefix)
63 utils.log("DEBUG", " ======== STARTED Confirm Volume Group Tenant Subflow ======== ", isDebugEnabled)
64 String processKey = getProcessKey(execution);
66 utils.log("DEBUG", " === Started QueryAAIForVolumeGroup Process === ", isDebugEnabled)
68 String volumeGroupId = execution.getVariable("volumeGroupId")
69 String incomingGroupName = execution.getVariable("volumeGroupName")
70 String incomingTenantId = execution.getVariable("tenantId")
71 def aicCloudRegion = execution.getVariable("aicCloudRegion")
72 String aai = execution.getVariable("URN_aai_endpoint")
74 AaiUtil aaiUriUtil = new AaiUtil(this)
75 def aai_uri = aaiUriUtil.getCloudInfrastructureCloudRegionUri(execution)
76 logDebug('AAI URI is: ' + aai_uri, isDebugEnabled)
78 String path = aai + "${aai_uri}/${aicCloudRegion}/volume-groups/volume-group/" + volumeGroupId
80 APIResponse queryAAIForVolumeGroupResponse = aaiUriUtil.executeAAIGetCall(execution, path)
82 def responseCode = queryAAIForVolumeGroupResponse.getStatusCode()
83 execution.setVariable("queryVolumeGroupResponseCode", responseCode)
84 String response = queryAAIForVolumeGroupResponse.getResponseBodyAsString()
85 response = StringEscapeUtils.unescapeXml(response)
87 if(responseCode == 200 && response != null){
88 execution.setVariable("queryAAIVolumeGroupResponse", response)
89 utils.log("DEBUG", "QueryAAIForVolumeGroup Received a Good REST Response is: \n" + response, isDebugEnabled)
91 String volumeGroupTenantId = ""
92 InputSource source = new InputSource(new StringReader(response));
93 DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
94 docFactory.setNamespaceAware(true)
95 DocumentBuilder docBuilder = docFactory.newDocumentBuilder()
96 Document createVCERequestXml = docBuilder.parse(source)
97 NodeList nodeList = createVCERequestXml.getElementsByTagNameNS("*", "relationship")
98 for (int x = 0; x < nodeList.getLength(); x++) {
99 Node node = nodeList.item(x)
100 if (node.getNodeType() == Node.ELEMENT_NODE) {
101 Element eElement = (Element) node
102 String e = eElement.getElementsByTagNameNS("*", "related-to").item(0).getTextContent()
103 if(e.equals("tenant")){
104 NodeList relationDataList = eElement.getElementsByTagNameNS("*", "relationship-data")
105 for (int d = 0; d < relationDataList.getLength(); d++) {
106 Node dataNode = relationDataList.item(d)
107 if (dataNode.getNodeType() == Node.ELEMENT_NODE) {
108 Element dElement = (Element) dataNode
109 String key = dElement.getElementsByTagNameNS("*", "relationship-key").item(0).getTextContent()
110 if(key.equals("tenant.tenant-id")){
111 volumeGroupTenantId = dElement.getElementsByTagNameNS("*", "relationship-value").item(0).getTextContent()
119 //Determine if Tenant Ids match
120 if(incomingTenantId.equals(volumeGroupTenantId)){
121 utils.log("DEBUG", "Tenant Ids Match", isDebugEnabled)
122 execution.setVariable("tenantIdsMatch", true)
124 utils.log("DEBUG", "Tenant Ids DO NOT Match", isDebugEnabled)
125 execution.setVariable("tenantIdsMatch", false)
128 //Determine if Volume Group Names match
129 String volumeGroupName = utils.getNodeText1(response, "volume-group-name")
130 if(incomingGroupName == null || incomingGroupName.length() < 1){
131 utils.log("DEBUG", "Incoming Volume Group Name is NOT Provided.", isDebugEnabled)
132 execution.setVariable("groupNamesMatch", true)
134 utils.log("DEBUG", "Incoming Volume Group Name is: " + incomingGroupName, isDebugEnabled)
135 if(volumeGroupName.equals(incomingGroupName)){
136 utils.log("DEBUG", "Volume Group Names Match.", isDebugEnabled)
137 execution.setVariable("groupNamesMatch", true)
139 utils.log("DEBUG", "Volume Group Names DO NOT Match.", isDebugEnabled)
140 execution.setVariable("groupNamesMatch", false)
144 utils.log("DEBUG", "QueryAAIForVolumeGroup Bad REST Response!", isDebugEnabled)
145 WorkflowException exception = new WorkflowException(processKey, 1, "Error Searching AAI for Volume Group. Received a Bad Response.")
146 execution.setVariable("WorkflowException", exception)
147 throw new BpmnError("MSOWorkflowException")
151 utils.log("ERROR", "Exception Occured Processing queryAAIForVolumeGroup. Exception is:\n" + e, isDebugEnabled)
152 throw new BpmnError("MSOWorkflowException")
154 utils.log("DEBUG", "=== COMPLETED queryAAIForVolumeGroup Process === ", isDebugEnabled)
157 public void assignVolumeHeatId(Execution execution){
158 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
159 execution.setVariable("prefix", Prefix)
161 utils.log("DEBUG", " === Started assignVolumeHeatId Process === ", isDebugEnabled)
163 String response = execution.getVariable("queryAAIVolumeGroupResponse")
164 String heatStackId = utils.getNodeText1(response, "heat-stack-id")
165 execution.setVariable("volumeHeatStackId", heatStackId)
166 execution.setVariable("ConfirmVolumeGroupTenantResponse", heatStackId)
167 // TODO: Should deprecate use of processKey+Response variable for the response. Will use "WorkflowResponse" instead
168 execution.setVariable("WorkflowResponse", heatStackId)
169 utils.log("DEBUG", "Volume Heat Stack Id is: " + heatStackId, isDebugEnabled)
172 utils.log("ERROR", "Exception Occured Processing assignVolumeHeatId. Exception is:\n" + e, isDebugEnabled)
173 throw new BpmnError("MSOWorkflowException")
175 utils.log("DEBUG", "=== COMPLETED assignVolumeHeatId Process === ", isDebugEnabled)
176 utils.log("DEBUG", "======== COMPLETED Confirm Volume Group Tenant Subflow ======== ", isDebugEnabled)
179 public void assignWorkflowException(Execution execution, String message){
180 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
181 execution.setVariable("prefix", Prefix)
182 String processKey = getProcessKey(execution);
183 utils.log("DEBUG", " === STARTED Assign Workflow Exception === ", isDebugEnabled)
185 String volumeGroupId = execution.getVariable("volumeGroupId")
187 String errorMessage = "Volume Group " + volumeGroupId + " " + message
189 WorkflowException exception = new WorkflowException(processKey, errorCode, errorMessage)
190 execution.setVariable("WorkflowException", exception)
191 execution.setVariable("CVGT_ErrorResponse", "") // Setting for Unit Testing Purposes
194 utils.log("ERROR", "Exception Occured Processing assignWorkflowException. Exception is:\n" + e, isDebugEnabled)
196 utils.log("DEBUG", "=== COMPLETED Assign Workflow Exception ==== ", isDebugEnabled)