AT&T 1712 and 1802 release code
[so.git] / bpmn / MSOCommonBPMN / src / main / groovy / org / openecomp / mso / bpmn / common / scripts / ConfirmVolumeGroupTenant.groovy
1 /*-\r
2  * ============LICENSE_START=======================================================\r
3  * ONAP - SO\r
4  * ================================================================================\r
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.\r
6  * ================================================================================\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  * \r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * \r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  * ============LICENSE_END=========================================================\r
19  */\r
20 \r
21 package org.openecomp.mso.bpmn.common.scripts\r
22 \r
23 import javax.xml.parsers.DocumentBuilder\r
24 import javax.xml.parsers.DocumentBuilderFactory\r
25 \r
26 import org.apache.commons.lang3.*\r
27 import org.camunda.bpm.engine.delegate.BpmnError\r
28 import org.camunda.bpm.engine.delegate.DelegateExecution\r
29 import org.openecomp.mso.bpmn.core.WorkflowException\r
30 import org.openecomp.mso.rest.APIResponse\r
31 import org.w3c.dom.Document\r
32 import org.w3c.dom.Element\r
33 import org.w3c.dom.Node\r
34 import org.w3c.dom.NodeList\r
35 import org.xml.sax.InputSource\r
36 \r
37 \r
38 /**\r
39  * Vnf Module Subflow for confirming the volume group belongs\r
40  * to the tenant\r
41  *\r
42  * @param tenantId\r
43  * @param volumeGroupId\r
44  *\r
45  */\r
46 class ConfirmVolumeGroupTenant extends AbstractServiceTaskProcessor{\r
47 \r
48         String Prefix="CVGT_"\r
49         ExceptionUtil exceptionUtil = new ExceptionUtil()\r
50 \r
51         public void preProcessRequest(DelegateExecution execution){\r
52                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")\r
53                 execution.setVariable("prefix", Prefix)\r
54                 utils.log("DEBUG", " ======== STARTED Confirm Volume Group Tenant Subflow ======== ", isDebugEnabled)\r
55                 String processKey = getProcessKey(execution);\r
56                 try{\r
57                         utils.log("DEBUG", " === Started QueryAAIForVolumeGroup Process === ", isDebugEnabled)\r
58 \r
59                         String volumeGroupId = execution.getVariable("volumeGroupId")\r
60                         String incomingGroupName = execution.getVariable("volumeGroupName")\r
61                         String incomingTenantId = execution.getVariable("tenantId")\r
62                         def aicCloudRegion = execution.getVariable("aicCloudRegion")\r
63                         String aai = execution.getVariable("URN_aai_endpoint")\r
64 \r
65                         AaiUtil aaiUriUtil = new AaiUtil(this)\r
66                         def aai_uri = aaiUriUtil.getCloudInfrastructureCloudRegionUri(execution)\r
67                         logDebug('AAI URI is: ' + aai_uri, isDebugEnabled)\r
68 \r
69                         String path = aai + "${aai_uri}/${aicCloudRegion}/volume-groups/volume-group/" + volumeGroupId\r
70 \r
71                         APIResponse queryAAIForVolumeGroupResponse = aaiUriUtil.executeAAIGetCall(execution, path)\r
72 \r
73                         def responseCode = queryAAIForVolumeGroupResponse.getStatusCode()\r
74                         execution.setVariable("queryVolumeGroupResponseCode", responseCode)\r
75                         String response = queryAAIForVolumeGroupResponse.getResponseBodyAsString()\r
76                         response = StringEscapeUtils.unescapeXml(response)\r
77 \r
78                         utils.logAudit("ConfirmVolumeGroup Response: " + response)\r
79                         utils.logAudit("ConfirmVolumeGroup Response Code: " + responseCode)\r
80 \r
81                         if(responseCode == 200 && response != null){\r
82                                 execution.setVariable("queryAAIVolumeGroupResponse", response)\r
83                                 utils.log("DEBUG", "QueryAAIForVolumeGroup Received a Good REST Response is: \n" + response, isDebugEnabled)\r
84 \r
85                                 String volumeGroupTenantId = ""\r
86                                 InputSource source = new InputSource(new StringReader(response));\r
87                                 DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();\r
88                                 docFactory.setNamespaceAware(true)\r
89                                 DocumentBuilder docBuilder = docFactory.newDocumentBuilder()\r
90                                 Document createVCERequestXml = docBuilder.parse(source)\r
91                                 NodeList nodeList = createVCERequestXml.getElementsByTagNameNS("*", "relationship")\r
92                                 for (int x = 0; x < nodeList.getLength(); x++) {\r
93                                         Node node = nodeList.item(x)\r
94                                         if (node.getNodeType() == Node.ELEMENT_NODE) {\r
95                                                 Element eElement = (Element) node\r
96                                                 String e = eElement.getElementsByTagNameNS("*", "related-to").item(0).getTextContent()\r
97                                                 if(e.equals("tenant")){\r
98                                                         NodeList relationDataList = eElement.getElementsByTagNameNS("*", "relationship-data")\r
99                                                         for (int d = 0; d < relationDataList.getLength(); d++) {\r
100                                                                 Node dataNode = relationDataList.item(d)\r
101                                                                 if (dataNode.getNodeType() == Node.ELEMENT_NODE) {\r
102                                                                         Element dElement = (Element) dataNode\r
103                                                                         String key = dElement.getElementsByTagNameNS("*", "relationship-key").item(0).getTextContent()\r
104                                                                         if(key.equals("tenant.tenant-id")){\r
105                                                                                 volumeGroupTenantId = dElement.getElementsByTagNameNS("*", "relationship-value").item(0).getTextContent()\r
106                                                                         }\r
107                                                                 }\r
108                                                         }\r
109                                                 }\r
110                                         }\r
111                                 }\r
112 \r
113                                 //Determine if Tenant Ids match\r
114                                 if(incomingTenantId.equals(volumeGroupTenantId)){\r
115                                         utils.log("DEBUG", "Tenant Ids Match", isDebugEnabled)\r
116                                         execution.setVariable("tenantIdsMatch", true)\r
117                                 }else{\r
118                                         utils.log("DEBUG", "Tenant Ids DO NOT Match", isDebugEnabled)\r
119                                         execution.setVariable("tenantIdsMatch", false)\r
120                                 }\r
121 \r
122                                 //Determine if Volume Group Names match\r
123                                 String volumeGroupName = utils.getNodeText1(response, "volume-group-name")\r
124                                 if(incomingGroupName == null || incomingGroupName.length() < 1){\r
125                                         utils.log("DEBUG", "Incoming Volume Group Name is NOT Provided.", isDebugEnabled)\r
126                                         execution.setVariable("groupNamesMatch", true)\r
127                                 }else{\r
128                                         utils.log("DEBUG", "Incoming Volume Group Name is: " + incomingGroupName, isDebugEnabled)\r
129                                         if(volumeGroupName.equals(incomingGroupName)){\r
130                                                 utils.log("DEBUG", "Volume Group Names Match.", isDebugEnabled)\r
131                                                 execution.setVariable("groupNamesMatch", true)\r
132                                         }else{\r
133                                                 utils.log("DEBUG", "Volume Group Names DO NOT Match.", isDebugEnabled)\r
134                                                 execution.setVariable("groupNamesMatch", false)\r
135                                         }\r
136                                 }\r
137                         }else{\r
138                                 utils.log("DEBUG", "QueryAAIForVolumeGroup Bad REST Response!", isDebugEnabled)\r
139                                 exceptionUtil.buildAndThrowWorkflowException(execution, 1, "Error Searching AAI for Volume Group. Received a Bad Response.")\r
140                         }\r
141 \r
142                 }catch(BpmnError b){\r
143                         throw b\r
144                 }catch(Exception e){\r
145                         utils.log("ERROR", "Exception Occured Processing queryAAIForVolumeGroup. Exception is:\n" + e, isDebugEnabled)\r
146                         exceptionUtil.buildAndThrowWorkflowException(execution, 5000, "Internal Error - Occured in preProcessRequest.")\r
147                 }\r
148                 utils.log("DEBUG", "=== COMPLETED queryAAIForVolumeGroup Process === ", isDebugEnabled)\r
149         }\r
150 \r
151         public void assignVolumeHeatId(DelegateExecution execution){\r
152                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")\r
153                 execution.setVariable("prefix", Prefix)\r
154                 try{\r
155                         utils.log("DEBUG", " === Started assignVolumeHeatId Process === ", isDebugEnabled)\r
156 \r
157                         String response = execution.getVariable("queryAAIVolumeGroupResponse")\r
158                         String heatStackId = utils.getNodeText1(response, "heat-stack-id")\r
159                         execution.setVariable("volumeHeatStackId", heatStackId)\r
160                         execution.setVariable("ConfirmVolumeGroupTenantResponse", heatStackId)\r
161                         // TODO: Should deprecate use of processKey+Response variable for the response. Will use "WorkflowResponse" instead\r
162                         execution.setVariable("WorkflowResponse", heatStackId)\r
163                         utils.log("DEBUG", "Volume Heat Stack Id is: " + heatStackId, isDebugEnabled)\r
164 \r
165                 }catch(Exception e){\r
166                 utils.log("ERROR", "Exception Occured Processing assignVolumeHeatId. Exception is:\n" + e, isDebugEnabled)\r
167                 exceptionUtil.buildAndThrowWorkflowException(execution, 5000, "Internal Error - Occured in assignVolumeHeatId.")\r
168         }\r
169         utils.log("DEBUG", "=== COMPLETED assignVolumeHeatId Process === ", isDebugEnabled)\r
170         utils.log("DEBUG", "======== COMPLETED Confirm Volume Group Tenant Subflow ======== ", isDebugEnabled)\r
171 }\r
172 \r
173         public void assignWorkflowException(DelegateExecution execution, String message){\r
174                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")\r
175                 execution.setVariable("prefix", Prefix)\r
176                 String processKey = getProcessKey(execution);\r
177                 utils.log("DEBUG", " === STARTED Assign Workflow Exception === ", isDebugEnabled)\r
178                 try{\r
179                         String volumeGroupId = execution.getVariable("volumeGroupId")\r
180                         int errorCode = 1\r
181                         String errorMessage = "Volume Group " + volumeGroupId + " " + message\r
182 \r
183                         exceptionUtil.buildWorkflowException(execution, errorCode, errorMessage)\r
184                 }catch(Exception e){\r
185                         utils.log("ERROR", "Exception Occured Processing assignWorkflowException. Exception is:\n" + e, isDebugEnabled)\r
186                 }\r
187                 utils.log("DEBUG", "=== COMPLETED Assign Workflow Exception ==== ", isDebugEnabled)\r
188         }\r
189 \r
190 \r
191 \r
192 }\r
193 \r