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