45ffd5552fa8f282da95fbc5cff1ce45816ac580
[so.git] / bpmn / MSOGammaBPMN / src / main / groovy / com / att / bpm / scripts / ConfirmVolumeGroupTenant.groovy
1 /*-
2  * ============LICENSE_START=======================================================
3  * OPENECOMP - MSO
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package com.att.bpm.scripts
22
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
27
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
35
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.*
46
47
48 /**
49  * Vnf Module Subflow for confirming the volume group belongs
50  * to the tenant
51  *
52  * @param tenantId
53  * @param volumeGroupId
54  *
55  */
56 class ConfirmVolumeGroupTenant extends AbstractServiceTaskProcessor{
57
58         String Prefix="CVGT_"
59
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);
65                 try{
66                         utils.log("DEBUG", " === Started QueryAAIForVolumeGroup Process === ", isDebugEnabled)
67
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")
73
74                         AaiUtil aaiUriUtil = new AaiUtil(this)
75                         def aai_uri = aaiUriUtil.getCloudInfrastructureCloudRegionUri(execution)
76                         logDebug('AAI URI is: ' + aai_uri, isDebugEnabled)
77
78                         String path = aai + "${aai_uri}/${aicCloudRegion}/volume-groups/volume-group/" + volumeGroupId
79
80                         APIResponse queryAAIForVolumeGroupResponse = aaiUriUtil.executeAAIGetCall(execution, path)
81
82                         def responseCode = queryAAIForVolumeGroupResponse.getStatusCode()
83                         execution.setVariable("queryVolumeGroupResponseCode", responseCode)
84                         String response = queryAAIForVolumeGroupResponse.getResponseBodyAsString()
85                         response = StringEscapeUtils.unescapeXml(response)
86
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)
90
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()
112                                                                         }
113                                                                 }
114                                                         }
115                                                 }
116                                         }
117                                 }
118
119                                 //Determine if Tenant Ids match
120                                 if(incomingTenantId.equals(volumeGroupTenantId)){
121                                         utils.log("DEBUG", "Tenant Ids Match", isDebugEnabled)
122                                         execution.setVariable("tenantIdsMatch", true)
123                                 }else{
124                                         utils.log("DEBUG", "Tenant Ids DO NOT Match", isDebugEnabled)
125                                         execution.setVariable("tenantIdsMatch", false)
126                                 }
127
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)
133                                 }else{
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)
138                                         }else{
139                                                 utils.log("DEBUG", "Volume Group Names DO NOT Match.", isDebugEnabled)
140                                                 execution.setVariable("groupNamesMatch", false)
141                                         }
142                                 }
143                         }else{
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")
148                         }
149
150                 }catch(Exception e){
151                         utils.log("ERROR", "Exception Occured Processing queryAAIForVolumeGroup. Exception is:\n" + e, isDebugEnabled)
152                         throw new BpmnError("MSOWorkflowException")
153                 }
154                 utils.log("DEBUG", "=== COMPLETED queryAAIForVolumeGroup Process === ", isDebugEnabled)
155         }
156
157         public void assignVolumeHeatId(Execution execution){
158                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
159                 execution.setVariable("prefix", Prefix)
160                 try{
161                         utils.log("DEBUG", " === Started assignVolumeHeatId Process === ", isDebugEnabled)
162
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)
170
171                 }catch(Exception e){
172                 utils.log("ERROR", "Exception Occured Processing assignVolumeHeatId. Exception is:\n" + e, isDebugEnabled)
173                 throw new BpmnError("MSOWorkflowException")
174         }
175         utils.log("DEBUG", "=== COMPLETED assignVolumeHeatId Process === ", isDebugEnabled)
176         utils.log("DEBUG", "======== COMPLETED Confirm Volume Group Tenant Subflow ======== ", isDebugEnabled)
177 }
178
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)
184                 try{
185                         String volumeGroupId = execution.getVariable("volumeGroupId")
186                         int errorCode = 1
187                         String errorMessage = "Volume Group " + volumeGroupId + " " + message
188
189                         WorkflowException exception = new WorkflowException(processKey, errorCode, errorMessage)
190                         execution.setVariable("WorkflowException", exception)
191                         execution.setVariable("CVGT_ErrorResponse", "") // Setting for Unit Testing Purposes
192
193                 }catch(Exception e){
194                         utils.log("ERROR", "Exception Occured Processing assignWorkflowException. Exception is:\n" + e, isDebugEnabled)
195                 }
196                 utils.log("DEBUG", "=== COMPLETED Assign Workflow Exception ==== ", isDebugEnabled)
197         }
198
199
200
201 }
202