82be3e6a0c63757025119f151d2f63249398871d
[so.git] /
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.UrnPropertiesReader
33 import org.onap.so.bpmn.core.WorkflowException
34 import org.onap.so.bpmn.core.json.JsonUtils
35 import org.onap.so.bpmn.infrastructure.workflow.serviceTask.client.builder.AbstractBuilder
36 import org.onap.so.client.HttpClient
37 import org.onap.so.logger.MsoLogger
38 import org.onap.so.bpmn.common.scripts.SDNCAdapterUtils
39
40 import java.util.UUID;
41 import javax.ws.rs.core.Response
42 import org.camunda.bpm.engine.delegate.BpmnError
43 import org.camunda.bpm.engine.delegate.DelegateExecution
44 import org.apache.commons.lang3.*
45 import javax.ws.rs.core.MediaType
46 import org.apache.commons.codec.binary.Base64;
47 import org.springframework.web.util.UriUtils
48 import org.onap.so.utils.TargetEntity
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                 URL url = new URL(serviceAaiPath)
118                 HttpClient client = new HttpClient(url, MediaType.APPLICATION_XML, TargetEntity.AAI)
119                 client.addBasicAuthHeader(UrnPropertiesReader.getVariable("aai.auth", execution), UrnPropertiesReader.getVariable("mso.msoKey", execution))
120                 client.addAdditionalHeader("X-FromAppId", "MSO")
121                 client.addAdditionalHeader("X-TransactionId", utils.getRequestID())
122                 client.addAdditionalHeader("Accept", MediaType.APPLICATION_XML)
123                 Response response = client.get()
124
125
126
127                 int responseCode = response.getStatus()
128                 execution.setVariable(Prefix + "GetDeviceResponseCode", responseCode)
129                 msoLogger.debug("  Get device response code is: " + responseCode)
130
131                 String aaiResponse = response.readEntity(String.class)
132                 aaiResponse = StringEscapeUtils.unescapeXml(aaiResponse)
133                 aaiResponse = aaiResponse.replaceAll("&", "&amp;")
134                 execution.setVariable(Prefix + "GetDeviceResponse", aaiResponse)
135
136                 //Process Response
137                 if(responseCode == 200 || responseCode == 201 || responseCode == 202 )
138                         //200 OK 201 CREATED 202 ACCEPTED
139                 {
140                         msoLogger.debug("GET Device Received a Good Response")
141                         execution.setVariable(Prefix + "SuccessIndicator", true)
142                         execution.setVariable(Prefix + "FoundIndicator", true)
143
144                         String devClass = utils.getNodeText(aaiResponse, "device_class")
145                         execution.setVariable(Prefix + "DeviceClass", devClass)
146                         msoLogger.debug(" DeviceClass is: " + devClass)
147
148                 }
149                 else
150                 {
151                         msoLogger.debug("Get DeviceInAAI Received a Bad Response Code. Response Code is: " + responseCode)
152
153                 }
154
155                 msoLogger.info(" ***** Exit getDeviceInAAI *****")
156         }
157
158     public void checkDevType(DelegateExecution execution){
159         msoLogger.info(" ***** Started checkDevType *****")
160         try {
161
162             String devType = execution.getVariable(Prefix + "DeviceClass")
163
164             if(StringUtils.isBlank(devType)) {
165                 devType = "OTHER"
166             }
167
168             execution.setVariable("device_class", devType)
169
170         } catch (Exception ex){
171             String msg = "Exception in checkDevType " + ex.getMessage()
172             msoLogger.debug( msg)
173             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
174         }
175     }
176
177         private void setProgressUpdateVariables(DelegateExecution execution, String body) {
178                 def dbAdapterEndpoint = execution.getVariable("URN_mso_adapters_openecomp_db_endpoint")
179                 execution.setVariable("CVFMI_dbAdapterEndpoint", dbAdapterEndpoint)
180                 execution.setVariable("CVFMI_updateResOperStatusRequest", body)
181         }
182
183         public void prepareUpdateProgress(DelegateExecution execution) {
184                 msoLogger.info(" ***** Started prepareUpdateProgress *****")
185                 ResourceInput resourceInputObj = execution.getVariable(Prefix + "ResourceInput")
186                 String operType = resourceInputObj.getOperationType()
187                 String resourceCustomizationUuid = resourceInputObj.getResourceModelInfo().getModelCustomizationUuid()
188                 String ServiceInstanceId = resourceInputObj.getServiceInstanceId()
189                 String modelName = resourceInputObj.getResourceModelInfo().getModelName()
190                 String operationId = resourceInputObj.getOperationId()
191                 String progress = execution.getVariable("progress")
192                 String status = execution.getVariable("status")
193                 String statusDescription = execution.getVariable("statusDescription")
194
195                 String body = """
196                 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
197                         xmlns:ns="http://org.openecomp.mso/requestsdb">
198                         <soapenv:Header/>
199                 <soapenv:Body>
200                     <ns:updateResourceOperationStatus>
201                                <operType>${operType}</operType>
202                                <operationId>${operationId}</operationId>
203                                <progress>${progress}</progress>
204                                <resourceTemplateUUID>${resourceCustomizationUuid}</resourceTemplateUUID>
205                                <serviceId>${ServiceInstanceId}</serviceId>
206                                <status>${status}</status>
207                                <statusDescription>${statusDescription}</statusDescription>
208                     </ns:updateResourceOperationStatus>
209                 </soapenv:Body>
210                 </soapenv:Envelope>"""
211
212                 setProgressUpdateVariables(execution, body)
213                 msoLogger.info(" ***** Exit prepareUpdateProgress *****")
214         }
215
216     public void getVNFTemplatefromSDC(DelegateExecution execution){
217         msoLogger.info(" ***** Started getVNFTemplatefromSDC *****")
218         try {
219             // To do
220
221
222         } catch (Exception ex){
223             String msg = "Exception in getVNFTemplatefromSDC " + ex.getMessage()
224             msoLogger.debug( msg)
225             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
226         }
227     }
228
229     public void postVNFInfoProcess(DelegateExecution execution){
230         msoLogger.info(" ***** Started postVNFInfoProcess *****")
231         try {
232             // To do
233
234
235         } catch (Exception ex){
236             String msg = "Exception in postVNFInfoProcess " + ex.getMessage()
237             msoLogger.debug( msg)
238             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
239         }
240     }
241
242     public void sendSyncResponse (DelegateExecution execution) {
243         msoLogger.debug( " *** sendSyncResponse *** ")
244
245         try {
246             String operationStatus = "finished"
247             // RESTResponse for main flow
248             String resourceOperationResp = """{"operationStatus":"${operationStatus}"}""".trim()
249             msoLogger.debug( " sendSyncResponse to APIH:" + "\n" + resourceOperationResp)
250             sendWorkflowResponse(execution, 202, resourceOperationResp)
251             execution.setVariable("sentSyncResponse", true)
252
253         } catch (Exception ex) {
254             String msg = "Exceptuion in sendSyncResponse:" + ex.getMessage()
255             msoLogger.debug( msg)
256             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
257         }
258         msoLogger.debug(" ***** Exit sendSyncResopnse *****")
259     }
260 }