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