Get Csar file bug fix
[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.aai.domain.yang.Device
31 import org.onap.so.bpmn.common.recipe.ResourceInput;
32 import org.onap.so.bpmn.common.resource.ResourceRequestBuilder
33 import org.onap.so.bpmn.core.UrnPropertiesReader
34 import org.onap.so.bpmn.core.WorkflowException
35 import org.onap.so.bpmn.core.json.JsonUtils
36 import org.onap.so.bpmn.infrastructure.workflow.serviceTask.client.builder.AbstractBuilder
37 import org.onap.so.client.HttpClient
38 import org.onap.so.client.aai.AAIObjectType
39 import org.onap.so.client.aai.AAIResourcesClient
40 import org.onap.so.client.aai.entities.uri.AAIResourceUri
41 import org.onap.so.client.aai.entities.uri.AAIUriFactory
42 import org.onap.so.logger.MsoLogger
43 import org.onap.so.bpmn.common.scripts.SDNCAdapterUtils
44
45 import java.util.UUID;
46 import javax.ws.rs.core.Response
47 import org.camunda.bpm.engine.delegate.BpmnError
48 import org.camunda.bpm.engine.delegate.DelegateExecution
49 import org.apache.commons.lang3.*
50 import javax.ws.rs.core.MediaType
51 import org.apache.commons.codec.binary.Base64
52 import org.onap.so.utils.TargetEntity
53
54
55 /**
56  * This groovy class supports the <class>DeleteDeviceResource.bpmn</class> process.
57  * flow for Device Resource Delete
58  */
59 public class DeleteDeviceResource extends AbstractServiceTaskProcessor {
60
61     String Prefix="DELDEVRES_"
62
63     ExceptionUtil exceptionUtil = new ExceptionUtil()
64
65     JsonUtils jsonUtil = new JsonUtils()
66
67     private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, DeleteDeviceResource.class)
68
69     public void preProcessRequest(DelegateExecution execution){
70         msoLogger.info(" ***** Started preProcessRequest *****")
71         try {
72
73             //get bpmn inputs from resource request.
74             String requestId = execution.getVariable("mso-request-id")
75             String requestAction = execution.getVariable("requestAction")
76             msoLogger.info("The requestAction is: " + requestAction)
77             String recipeParamsFromRequest = execution.getVariable("recipeParams")
78             msoLogger.info("The recipeParams is: " + recipeParamsFromRequest)
79             String resourceInput = execution.getVariable("resourceInput")
80             msoLogger.info("The resourceInput is: " + resourceInput)
81             //Get ResourceInput Object
82             ResourceInput resourceInputObj = ResourceRequestBuilder.getJsonObject(resourceInput, ResourceInput.class)
83             execution.setVariable(Prefix + "ResourceInput", resourceInputObj)
84             String resourceInputPrameters = resourceInputObj.getResourceParameters()
85             String inputParametersJson = jsonUtil.getJsonValue(resourceInputPrameters, "requestInputs")
86             JSONObject inputParameters = new JSONObject(inputParametersJson)
87             execution.setVariable(Prefix + "ResourceRequestInputs", inputParameters)
88
89             //Deal with recipeParams
90             String recipeParamsFromWf = execution.getVariable("recipeParamXsd")
91             String resourceName = resourceInputObj.getResourceInstanceName()
92
93             String resourceInstanceId = resourceInputObj.getResourceInstancenUuid()
94             String deviceId = resourceInstanceId
95             execution.setVariable(Prefix + "DeviceId", deviceId)
96
97             getDeviceInAAI(execution)
98
99             execution.setVariable(Prefix + "serviceInstanceId", resourceInputObj.getServiceInstanceId())
100             execution.setVariable("mso-request-id", requestId)
101
102         } catch (Exception ex){
103             String msg = "Exception in preProcessRequest " + ex.getMessage()
104             msoLogger.debug(msg)
105 //            exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
106         }
107     }
108
109         private void getDeviceInAAI(DelegateExecution execution) {
110                 msoLogger.info(" ***** Started getDeviceInAAI *****")
111         try {
112                 String deviceId = execution.getVariable(Prefix + "DeviceId")
113         
114         AAIResourcesClient client = new AAIResourcesClient()
115         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.DEVICE, deviceId)
116         Device dev = client.get(uri).asBean(Device.class).get()
117         
118         String devClass = dev.getClass ()
119         execution.setVariable(Prefix + "DeviceClass", devClass)
120         msoLogger.debug(" DeviceClass is: " + devClass)
121
122         } catch (Exception ex){
123             String msg = "Exception in getDeviceInAAI " + ex.getMessage()
124             msoLogger.debug(msg)
125 //            exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
126         }
127
128                 msoLogger.info(" ***** Exit getDeviceInAAI *****")
129         }
130
131     public void checkDevType(DelegateExecution execution){
132         msoLogger.info(" ***** Started checkDevType *****")
133         try {
134
135             String devType = execution.getVariable(Prefix + "DeviceClass")
136
137             if(StringUtils.isBlank(devType)) {
138                 devType = "OTHER"
139             }
140
141             execution.setVariable("device_class", devType)
142
143         } catch (Exception ex){
144             String msg = "Exception in checkDevType " + ex.getMessage()
145             msoLogger.debug( msg)
146 //            exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
147         }
148     }
149
150         private void setProgressUpdateVariables(DelegateExecution execution, String body) {
151                 def dbAdapterEndpoint = UrnPropertiesReader.getVariable("mso.adapters.openecomp.db.endpoint", execution)
152                 execution.setVariable("CVFMI_dbAdapterEndpoint", dbAdapterEndpoint)
153                 execution.setVariable("CVFMI_updateResOperStatusRequest", body)
154         }
155
156         public void prepareUpdateProgress(DelegateExecution execution) {
157                 msoLogger.info(" ***** Started prepareUpdateProgress *****")
158                 ResourceInput resourceInputObj = execution.getVariable(Prefix + "ResourceInput")
159                 String operType = resourceInputObj.getOperationType()
160                 String resourceCustomizationUuid = resourceInputObj.getResourceModelInfo().getModelCustomizationUuid()
161                 String ServiceInstanceId = resourceInputObj.getServiceInstanceId()
162                 String modelName = resourceInputObj.getResourceModelInfo().getModelName()
163                 String operationId = resourceInputObj.getOperationId()
164                 String progress = execution.getVariable("progress")
165                 String status = execution.getVariable("status")
166                 String statusDescription = execution.getVariable("statusDescription")
167
168                 String body = """
169                 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
170                         xmlns:ns="http://org.openecomp.mso/requestsdb">
171                         <soapenv:Header/>
172                 <soapenv:Body>
173                     <ns:updateResourceOperationStatus>
174                                <operType>${operType}</operType>
175                                <operationId>${operationId}</operationId>
176                                <progress>${progress}</progress>
177                                <resourceTemplateUUID>${resourceCustomizationUuid}</resourceTemplateUUID>
178                                <serviceId>${ServiceInstanceId}</serviceId>
179                                <status>${status}</status>
180                                <statusDescription>${statusDescription}</statusDescription>
181                     </ns:updateResourceOperationStatus>
182                 </soapenv:Body>
183                 </soapenv:Envelope>"""
184
185                 setProgressUpdateVariables(execution, body)
186                 msoLogger.info(" ***** Exit prepareUpdateProgress *****")
187         }
188
189     public void getVNFTemplatefromSDC(DelegateExecution execution){
190         msoLogger.info(" ***** Started getVNFTemplatefromSDC *****")
191         try {
192             // To do
193
194
195         } catch (Exception ex){
196             String msg = "Exception in getVNFTemplatefromSDC " + ex.getMessage()
197             msoLogger.debug( msg)
198 //            exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
199         }
200     }
201
202     public void postVNFInfoProcess(DelegateExecution execution){
203         msoLogger.info(" ***** Started postVNFInfoProcess *****")
204         try {
205             // To do
206
207
208         } catch (Exception ex){
209             String msg = "Exception in postVNFInfoProcess " + ex.getMessage()
210             msoLogger.debug( msg)
211 //            exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
212         }
213     }
214
215     public void sendSyncResponse (DelegateExecution execution) {
216         msoLogger.debug( " *** sendSyncResponse *** ")
217
218         try {
219             String operationStatus = "finished"
220             // RESTResponse for main flow
221             String resourceOperationResp = """{"operationStatus":"${operationStatus}"}""".trim()
222             msoLogger.debug( " sendSyncResponse to APIH:" + "\n" + resourceOperationResp)
223             sendWorkflowResponse(execution, 202, resourceOperationResp)
224             execution.setVariable("sentSyncResponse", true)
225
226         } catch (Exception ex) {
227             String msg = "Exceptuion in sendSyncResponse:" + ex.getMessage()
228             msoLogger.debug( msg)
229 //            exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
230         }
231         msoLogger.debug(" ***** Exit sendSyncResopnse *****")
232     }
233 }