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