Merge "Addition of Dockerfile/logback for SO-Monitoring"
[so.git] / bpmn / so-bpmn-infrastructure-common / src / main / groovy / org / onap / so / bpmn / infrastructure / scripts / DeleteDeviceResource.groovy
1 /*-
2  * ============LICENSE_START=======================================================
3  * OPENECOMP - SO
4  * ================================================================================
5  * Copyright (C) 2018 Huawei Technologies Co., Ltd. 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.infrastructure.scripts
22
23 import org.json.JSONObject
24 import org.json.XML;
25
26 import static org.apache.commons.lang3.StringUtils.*;
27 import groovy.xml.XmlUtil
28 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
29 import org.onap.so.bpmn.common.scripts.ExceptionUtil
30 import org.onap.so.bpmn.common.recipe.ResourceInput;
31 import org.onap.so.bpmn.common.resource.ResourceRequestBuilder
32 import org.onap.so.bpmn.core.WorkflowException
33 import org.onap.so.bpmn.core.json.JsonUtils
34 import org.onap.so.bpmn.infrastructure.workflow.serviceTask.client.builder.AbstractBuilder
35 import org.onap.so.logger.MsoLogger
36 import org.onap.so.rest.APIResponse
37 import org.onap.so.bpmn.common.scripts.SDNCAdapterUtils
38
39 import java.util.UUID;
40
41 import org.camunda.bpm.engine.delegate.BpmnError
42 import org.camunda.bpm.engine.delegate.DelegateExecution
43 import org.apache.commons.lang3.*
44 import org.apache.commons.codec.binary.Base64;
45 import org.springframework.web.util.UriUtils
46 import org.onap.so.rest.RESTClient
47 import org.onap.so.rest.RESTConfig
48 import org.onap.so.rest.APIResponse;
49 import org.onap.so.bpmn.common.scripts.AaiUtil
50
51 /**
52  * This groovy class supports the <class>DeleteDeviceResource.bpmn</class> process.
53  * flow for Device Resource Delete
54  */
55 public class DeleteDeviceResource extends AbstractServiceTaskProcessor {
56
57     String Prefix="DELDEVRES_"
58
59     ExceptionUtil exceptionUtil = new ExceptionUtil()
60
61     JsonUtils jsonUtil = new JsonUtils()
62
63     private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, DeleteDeviceResource.class)
64
65     public void preProcessRequest(DelegateExecution execution){
66         msoLogger.info(" ***** Started preProcessRequest *****")
67         try {
68
69             //get bpmn inputs from resource request.
70             String requestId = execution.getVariable("mso-request-id")
71             String requestAction = execution.getVariable("requestAction")
72             msoLogger.info("The requestAction is: " + requestAction)
73             String recipeParamsFromRequest = execution.getVariable("recipeParams")
74             msoLogger.info("The recipeParams is: " + recipeParamsFromRequest)
75             String resourceInput = execution.getVariable("resourceInput")
76             msoLogger.info("The resourceInput is: " + resourceInput)
77             //Get ResourceInput Object
78             ResourceInput resourceInputObj = ResourceRequestBuilder.getJsonObject(resourceInput, ResourceInput.class)
79             execution.setVariable(Prefix + "ResourceInput", resourceInputObj)
80             String resourceInputPrameters = resourceInputObj.getResourceParameters()
81             String inputParametersJson = jsonUtil.getJsonValue(resourceInputPrameters, "requestInputs")
82             JSONObject inputParameters = new JSONObject(inputParametersJson)
83             execution.setVariable(Prefix + "ResourceRequestInputs", inputParameters)
84
85             //Deal with recipeParams
86             String recipeParamsFromWf = execution.getVariable("recipeParamXsd")
87             String resourceName = resourceInputObj.getResourceInstanceName()
88
89             String resourceInstanceId = resourceInputObj.getResourceInstancenUuid()
90             String deviceId = resourceInstanceId
91             execution.setVariable(Prefix + "DeviceId", deviceId)
92
93             getDeviceInAAI(execution)
94
95             execution.setVariable(Prefix + "serviceInstanceId", resourceInputObj.getServiceInstanceId())
96             execution.setVariable("mso-request-id", requestId)
97
98         } catch (BpmnError e) {
99             throw e;
100         } catch (Exception ex){
101             String msg = "Exception in preProcessRequest " + ex.getMessage()
102             msoLogger.debug(msg)
103             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
104         }
105     }
106
107         private void getDeviceInAAI(DelegateExecution execution) {
108                 msoLogger.info(" ***** Started getDeviceInAAI *****")
109                 
110                 String deviceId = execution.getVariable(Prefix + "DeviceId")
111                 AaiUtil aaiUriUtil = new AaiUtil()
112                 String aai_uri = aaiUriUtil.getNetworkDeviceUri(execution)
113                 String aai_endpoint = execution.getVariable("URN_aai_endpoint")
114                 String serviceAaiPath = "${aai_endpoint}${aai_uri}/" + UriUtils.encode(deviceId,"UTF-8")
115                 execution.setVariable(Prefix + "ServiceAaiPath", serviceAaiPath)
116                 
117                 APIResponse response = aaiUriUtil.executeAAIGetCall(execution, serviceAaiPath)
118                 int responseCode = response.getStatusCode()
119                 execution.setVariable(Prefix + "GetDeviceResponseCode", responseCode)
120                 msoLogger.debug("  Get device response code is: " + responseCode)
121
122                 String aaiResponse = response.getResponseBodyAsString()
123                 aaiResponse = StringEscapeUtils.unescapeXml(aaiResponse)
124                 aaiResponse = aaiResponse.replaceAll("&", "&amp;")
125                 execution.setVariable(Prefix + "GetDeviceResponse", aaiResponse)
126
127                 //Process Response
128                 if(responseCode == 200 || responseCode == 201 || responseCode == 202 )
129                         //200 OK 201 CREATED 202 ACCEPTED
130                 {
131                         msoLogger.debug("GET Device Received a Good Response")
132                         execution.setVariable(Prefix + "SuccessIndicator", true)
133                         execution.setVariable(Prefix + "FoundIndicator", true)
134                         
135                         String devClass = utils.getNodeText1(aaiResponse, "device_class")
136                         execution.setVariable(Prefix + "DeviceClass", devClass)
137                         msoLogger.debug(" DeviceClass is: " + devClass)
138
139                 }
140                 else
141                 {
142                         msoLogger.debug("Get DeviceInAAI Received a Bad Response Code. Response Code is: " + responseCode)
143
144                 }
145
146                 msoLogger.info(" ***** Exit getDeviceInAAI *****")
147         }
148
149     public void checkDevType(DelegateExecution execution){
150         msoLogger.info(" ***** Started checkDevType *****")
151         try {
152
153             String devType = execution.getVariable(Prefix + "DeviceClass")
154
155             if(StringUtils.isBlank(devType)) {
156                 devType = "OTHER"
157             }
158
159             execution.setVariable("device_class", devType)
160
161         } catch (Exception ex){
162             String msg = "Exception in checkDevType " + ex.getMessage()
163             msoLogger.debug( msg)
164             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
165         }
166     }
167
168         private void setProgressUpdateVariables(DelegateExecution execution, String body) {
169                 def dbAdapterEndpoint = execution.getVariable("URN_mso_adapters_openecomp_db_endpoint")
170                 execution.setVariable("CVFMI_dbAdapterEndpoint", dbAdapterEndpoint)
171                 execution.setVariable("CVFMI_updateResOperStatusRequest", body)
172         }
173
174         public void prepareUpdateProgress(DelegateExecution execution) {
175                 msoLogger.info(" ***** Started prepareUpdateProgress *****")
176                 ResourceInput resourceInputObj = execution.getVariable(Prefix + "ResourceInput")
177                 String operType = resourceInputObj.getOperationType()
178                 String resourceCustomizationUuid = resourceInputObj.getResourceModelInfo().getModelCustomizationUuid()
179                 String ServiceInstanceId = resourceInputObj.getServiceInstanceId()
180                 String modelName = resourceInputObj.getResourceModelInfo().getModelName()
181                 String operationId = resourceInputObj.getOperationId()
182                 String progress = execution.getVariable("progress")
183                 String status = execution.getVariable("status")
184                 String statusDescription = execution.getVariable("statusDescription")
185
186                 String body = """
187                 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
188                         xmlns:ns="http://org.openecomp.mso/requestsdb">
189                         <soapenv:Header/>
190                 <soapenv:Body>
191                     <ns:updateResourceOperationStatus>
192                                <operType>${operType}</operType>
193                                <operationId>${operationId}</operationId>
194                                <progress>${progress}</progress>
195                                <resourceTemplateUUID>${resourceCustomizationUuid}</resourceTemplateUUID>
196                                <serviceId>${ServiceInstanceId}</serviceId>
197                                <status>${status}</status>
198                                <statusDescription>${statusDescription}</statusDescription>
199                     </ns:updateResourceOperationStatus>
200                 </soapenv:Body>
201                 </soapenv:Envelope>"""
202
203                 setProgressUpdateVariables(execution, body)
204                 msoLogger.info(" ***** Exit prepareUpdateProgress *****")
205         }
206
207     public void getVNFTemplatefromSDC(DelegateExecution execution){
208         msoLogger.info(" ***** Started getVNFTemplatefromSDC *****")
209         try {
210             // To do
211
212
213         } catch (Exception ex){
214             String msg = "Exception in getVNFTemplatefromSDC " + ex.getMessage()
215             msoLogger.debug( msg)
216             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
217         }
218     }
219
220     public void postVNFInfoProcess(DelegateExecution execution){
221         msoLogger.info(" ***** Started postVNFInfoProcess *****")
222         try {
223             // To do
224
225
226         } catch (Exception ex){
227             String msg = "Exception in postVNFInfoProcess " + ex.getMessage()
228             msoLogger.debug( msg)
229             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
230         }
231     }
232
233     public void sendSyncResponse (DelegateExecution execution) {
234         msoLogger.debug( " *** sendSyncResponse *** ")
235
236         try {
237             String operationStatus = "finished"
238             // RESTResponse for main flow
239             String resourceOperationResp = """{"operationStatus":"${operationStatus}"}""".trim()
240             msoLogger.debug( " sendSyncResponse to APIH:" + "\n" + resourceOperationResp)
241             sendWorkflowResponse(execution, 202, resourceOperationResp)
242             execution.setVariable("sentSyncResponse", true)
243
244         } catch (Exception ex) {
245             String msg = "Exceptuion in sendSyncResponse:" + ex.getMessage()
246             msoLogger.debug( msg)
247             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
248         }
249         msoLogger.debug(" ***** Exit sendSyncResopnse *****")
250     }
251 }