[MSO-8] Update the maven dependency
[so.git] / bpmn / MSOCommonBPMN / src / main / groovy / org / openecomp / mso / bpmn / common / 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 org.openecomp.mso.bpmn.common.scripts
22
23 import javax.xml.parsers.DocumentBuilder
24 import javax.xml.parsers.DocumentBuilderFactory
25
26 import org.apache.commons.lang3.*
27 import org.camunda.bpm.engine.delegate.BpmnError
28 import org.camunda.bpm.engine.runtime.Execution
29 import org.openecomp.mso.bpmn.core.WorkflowException
30 import org.openecomp.mso.rest.APIResponse
31 import org.w3c.dom.Document
32 import org.w3c.dom.Element
33 import org.w3c.dom.Node
34 import org.w3c.dom.NodeList
35 import org.xml.sax.InputSource
36
37
38 /**
39  * Vnf Module Subflow for confirming the volume group belongs
40  * to the tenant
41  *
42  * @param tenantId
43  * @param volumeGroupId
44  *
45  */
46 class ConfirmVolumeGroupTenant extends AbstractServiceTaskProcessor{
47
48         String Prefix="CVGT_"
49
50         public void preProcessRequest(Execution execution){
51                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
52                 execution.setVariable("prefix", Prefix)
53                 utils.log("DEBUG", " ======== STARTED Confirm Volume Group Tenant Subflow ======== ", isDebugEnabled)
54                 String processKey = getProcessKey(execution);
55                 try{
56                         utils.log("DEBUG", " === Started QueryAAIForVolumeGroup Process === ", isDebugEnabled)
57
58                         String volumeGroupId = execution.getVariable("volumeGroupId")
59                         String incomingGroupName = execution.getVariable("volumeGroupName")
60                         String incomingTenantId = execution.getVariable("tenantId")
61                         def aicCloudRegion = execution.getVariable("aicCloudRegion")
62                         String aai = execution.getVariable("URN_aai_endpoint")
63
64                         AaiUtil aaiUriUtil = new AaiUtil(this)
65                         def aai_uri = aaiUriUtil.getCloudInfrastructureCloudRegionUri(execution)
66                         logDebug('AAI URI is: ' + aai_uri, isDebugEnabled)
67
68                         String path = aai + "${aai_uri}/${aicCloudRegion}/volume-groups/volume-group/" + volumeGroupId
69
70                         APIResponse queryAAIForVolumeGroupResponse = aaiUriUtil.executeAAIGetCall(execution, path)
71
72                         def responseCode = queryAAIForVolumeGroupResponse.getStatusCode()
73                         execution.setVariable("queryVolumeGroupResponseCode", responseCode)
74                         String response = queryAAIForVolumeGroupResponse.getResponseBodyAsString()
75                         response = StringEscapeUtils.unescapeXml(response)
76
77                         utils.logAudit("ConfirmVolumeGroup Response: " + response)
78                         utils.logAudit("ConfirmVolumeGroup Response Code: " + responseCode)
79
80                         if(responseCode == 200 && response != null){
81                                 execution.setVariable("queryAAIVolumeGroupResponse", response)
82                                 utils.log("DEBUG", "QueryAAIForVolumeGroup Received a Good REST Response is: \n" + response, isDebugEnabled)
83
84                                 String volumeGroupTenantId = ""
85                                 InputSource source = new InputSource(new StringReader(response));
86                                 DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
87                                 docFactory.setNamespaceAware(true)
88                                 DocumentBuilder docBuilder = docFactory.newDocumentBuilder()
89                                 Document createVCERequestXml = docBuilder.parse(source)
90                                 NodeList nodeList = createVCERequestXml.getElementsByTagNameNS("*", "relationship")
91                                 for (int x = 0; x < nodeList.getLength(); x++) {
92                                         Node node = nodeList.item(x)
93                                         if (node.getNodeType() == Node.ELEMENT_NODE) {
94                                                 Element eElement = (Element) node
95                                                 String e = eElement.getElementsByTagNameNS("*", "related-to").item(0).getTextContent()
96                                                 if(e.equals("tenant")){
97                                                         NodeList relationDataList = eElement.getElementsByTagNameNS("*", "relationship-data")
98                                                         for (int d = 0; d < relationDataList.getLength(); d++) {
99                                                                 Node dataNode = relationDataList.item(d)
100                                                                 if (dataNode.getNodeType() == Node.ELEMENT_NODE) {
101                                                                         Element dElement = (Element) dataNode
102                                                                         String key = dElement.getElementsByTagNameNS("*", "relationship-key").item(0).getTextContent()
103                                                                         if(key.equals("tenant.tenant-id")){
104                                                                                 volumeGroupTenantId = dElement.getElementsByTagNameNS("*", "relationship-value").item(0).getTextContent()
105                                                                         }
106                                                                 }
107                                                         }
108                                                 }
109                                         }
110                                 }
111
112                                 //Determine if Tenant Ids match
113                                 if(incomingTenantId.equals(volumeGroupTenantId)){
114                                         utils.log("DEBUG", "Tenant Ids Match", isDebugEnabled)
115                                         execution.setVariable("tenantIdsMatch", true)
116                                 }else{
117                                         utils.log("DEBUG", "Tenant Ids DO NOT Match", isDebugEnabled)
118                                         execution.setVariable("tenantIdsMatch", false)
119                                 }
120
121                                 //Determine if Volume Group Names match
122                                 String volumeGroupName = utils.getNodeText1(response, "volume-group-name")
123                                 if(incomingGroupName == null || incomingGroupName.length() < 1){
124                                         utils.log("DEBUG", "Incoming Volume Group Name is NOT Provided.", isDebugEnabled)
125                                         execution.setVariable("groupNamesMatch", true)
126                                 }else{
127                                         utils.log("DEBUG", "Incoming Volume Group Name is: " + incomingGroupName, isDebugEnabled)
128                                         if(volumeGroupName.equals(incomingGroupName)){
129                                                 utils.log("DEBUG", "Volume Group Names Match.", isDebugEnabled)
130                                                 execution.setVariable("groupNamesMatch", true)
131                                         }else{
132                                                 utils.log("DEBUG", "Volume Group Names DO NOT Match.", isDebugEnabled)
133                                                 execution.setVariable("groupNamesMatch", false)
134                                         }
135                                 }
136                         }else{
137                                 utils.log("DEBUG", "QueryAAIForVolumeGroup Bad REST Response!", isDebugEnabled)
138                                 WorkflowException exception = new WorkflowException(processKey, 1, "Error Searching AAI for Volume Group. Received a Bad Response.")
139                                 execution.setVariable("WorkflowException", exception)
140                                 throw new BpmnError("MSOWorkflowException")
141                         }
142
143                 }catch(Exception e){
144                         utils.log("ERROR", "Exception Occured Processing queryAAIForVolumeGroup. Exception is:\n" + e, isDebugEnabled)
145                         throw new BpmnError("MSOWorkflowException")
146                 }
147                 utils.log("DEBUG", "=== COMPLETED queryAAIForVolumeGroup Process === ", isDebugEnabled)
148         }
149
150         public void assignVolumeHeatId(Execution execution){
151                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
152                 execution.setVariable("prefix", Prefix)
153                 try{
154                         utils.log("DEBUG", " === Started assignVolumeHeatId Process === ", isDebugEnabled)
155
156                         String response = execution.getVariable("queryAAIVolumeGroupResponse")
157                         String heatStackId = utils.getNodeText1(response, "heat-stack-id")
158                         execution.setVariable("volumeHeatStackId", heatStackId)
159                         execution.setVariable("ConfirmVolumeGroupTenantResponse", heatStackId)
160                         // TODO: Should deprecate use of processKey+Response variable for the response. Will use "WorkflowResponse" instead
161                         execution.setVariable("WorkflowResponse", heatStackId)
162                         utils.log("DEBUG", "Volume Heat Stack Id is: " + heatStackId, isDebugEnabled)
163
164                 }catch(Exception e){
165                 utils.log("ERROR", "Exception Occured Processing assignVolumeHeatId. Exception is:\n" + e, isDebugEnabled)
166                 throw new BpmnError("MSOWorkflowException")
167         }
168         utils.log("DEBUG", "=== COMPLETED assignVolumeHeatId Process === ", isDebugEnabled)
169         utils.log("DEBUG", "======== COMPLETED Confirm Volume Group Tenant Subflow ======== ", isDebugEnabled)
170 }
171
172         public void assignWorkflowException(Execution execution, String message){
173                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
174                 execution.setVariable("prefix", Prefix)
175                 String processKey = getProcessKey(execution);
176                 utils.log("DEBUG", " === STARTED Assign Workflow Exception === ", isDebugEnabled)
177                 try{
178                         String volumeGroupId = execution.getVariable("volumeGroupId")
179                         int errorCode = 1
180                         String errorMessage = "Volume Group " + volumeGroupId + " " + message
181
182                         WorkflowException exception = new WorkflowException(processKey, errorCode, errorMessage)
183                         execution.setVariable("WorkflowException", exception)
184                         execution.setVariable("CVGT_ErrorResponse", "") // Setting for Unit Testing Purposes
185
186                 }catch(Exception e){
187                         utils.log("ERROR", "Exception Occured Processing assignWorkflowException. Exception is:\n" + e, isDebugEnabled)
188                 }
189                 utils.log("DEBUG", "=== COMPLETED Assign Workflow Exception ==== ", isDebugEnabled)
190         }
191
192
193
194 }
195