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