2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.so.bpmn.infrastructure.scripts;
23 import static org.apache.commons.lang3.StringUtils.*;
25 import javax.ws.rs.NotFoundException
27 import org.apache.commons.lang3.*
28 import org.camunda.bpm.engine.delegate.BpmnError
29 import org.camunda.bpm.engine.delegate.DelegateExecution
30 import org.json.JSONArray
31 import org.json.JSONObject
32 import org.onap.aai.domain.yang.ServiceInstance
33 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
34 import org.onap.so.bpmn.common.scripts.ExceptionUtil
35 import org.onap.so.bpmn.common.scripts.MsoUtils
36 import org.onap.so.bpmn.core.WorkflowException
37 import org.onap.so.bpmn.core.domain.Resource
38 import org.onap.so.bpmn.core.json.JsonUtils
39 import org.onap.so.bpmn.core.UrnPropertiesReader
40 import org.onap.so.client.aai.AAIObjectType
41 import org.onap.so.client.aai.AAIResourcesClient
42 import org.onap.so.client.aai.entities.AAIResultWrapper
43 import org.onap.so.client.aai.entities.uri.AAIResourceUri
44 import org.onap.so.client.aai.entities.uri.AAIUriFactory
45 import org.springframework.web.util.UriUtils
50 * This groovy class supports the <class>UpdateCustomE2EServiceInstance.bpmn</class> process.
51 * AlaCarte flow for 1702 ServiceInstance Update
54 public class UpdateCustomE2EServiceInstance extends AbstractServiceTaskProcessor {
55 String Prefix="UPDSI_"
56 ExceptionUtil exceptionUtil = new ExceptionUtil()
57 JsonUtils jsonUtil = new JsonUtils()
60 public void preProcessRequest (DelegateExecution execution) {
61 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
62 execution.setVariable("prefix",Prefix)
64 utils.log("INFO", " *** preProcessRequest() *** ", isDebugEnabled)
68 String siRequest = execution.getVariable("bpmnRequest")
69 utils.logAudit(siRequest)
71 String requestId = execution.getVariable("mso-request-id")
72 execution.setVariable("msoRequestId", requestId)
73 utils.log("INFO", "Input Request:" + siRequest + " reqId:" + requestId, isDebugEnabled)
75 String serviceInstanceId = execution.getVariable("serviceInstanceId")
76 if (isBlank(serviceInstanceId)) {
77 msg = "Input serviceInstanceId' is null"
78 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
81 //subscriberInfo for aai
82 String globalSubscriberId = jsonUtil.getJsonValue(siRequest, "requestDetails.subscriberInfo.globalSubscriberId")
83 if (isBlank(globalSubscriberId)) {
84 msg = "Input globalSubscriberId' is null"
85 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
87 execution.setVariable("globalSubscriberId", globalSubscriberId)
91 execution.setVariable("source", jsonUtil.getJsonValue(siRequest, "requestDetails.requestInfo.source"))
92 execution.setVariable("serviceInstanceName", jsonUtil.getJsonValue(siRequest, "requestDetails.requestInfo.instanceName"))
93 execution.setVariable("disableRollback", jsonUtil.getJsonValue(siRequest, "requestDetails.requestInfo.suppressRollback"))
94 String productFamilyId = jsonUtil.getJsonValue(siRequest, "requestDetails.requestInfo.productFamilyId")
95 if (isBlank(productFamilyId))
97 msg = "Input productFamilyId is null"
98 utils.log("INFO", msg, isDebugEnabled)
100 execution.setVariable("productFamilyId", productFamilyId)
104 String userParams = jsonUtil.getJsonValue(siRequest, "requestDetails.requestParameters.userParams")
105 utils.log("INFO", "userParams:" + userParams, isDebugEnabled)
106 List<String> paramList = jsonUtil.StringArrayToList(execution, userParams)
107 String uuiRequest = jsonUtil.getJsonValue(paramList.get(0), "UUIRequest")
108 if (isBlank(uuiRequest)) {
109 msg = "Input uuiRequest is null"
110 utils.log("INFO", msg, isDebugEnabled)
111 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
114 execution.setVariable("uuiRequest", uuiRequest)
117 utils.log("INFO", "uuiRequest:\n" + uuiRequest, isDebugEnabled)
119 //serviceType for aai
120 String serviceType = jsonUtil.getJsonValue(uuiRequest, "service.serviceType")
121 if (isBlank(serviceType)) {
122 msg = "Input serviceType is null"
123 utils.log("INFO", msg, isDebugEnabled)
124 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
126 execution.setVariable("serviceType", serviceType)
130 String modelInvariantUuid = jsonUtil.getJsonValue(uuiRequest, "service.serviceInvariantUuid")
131 utils.log("INFO","modelInvariantUuid: " + modelInvariantUuid, isDebugEnabled)
132 execution.setVariable("modelInvariantUuid", modelInvariantUuid)
133 execution.setVariable("model-invariant-id-target", modelInvariantUuid)
135 String modelUuid = jsonUtil.getJsonValue(uuiRequest, "service.serviceUuid")
136 utils.log("INFO","modelUuid: " + modelUuid, isDebugEnabled)
137 execution.setVariable("modelUuid", modelUuid)
138 execution.setVariable("model-version-id-target", modelUuid)
140 String serviceModelName = jsonUtil.getJsonValue(uuiRequest, "service.parameters.templateName")
141 utils.log("INFO","serviceModelName: " + serviceModelName, isDebugEnabled)
142 if(serviceModelName == null) {
143 serviceModelName = ""
145 execution.setVariable("serviceModelName", serviceModelName)
148 String operationId = jsonUtil.getJsonValue(siRequest, "operationId")
149 if (isBlank(operationId)) {
150 operationId = UUID.randomUUID().toString()
152 execution.setVariable("operationId", operationId)
153 execution.setVariable("operationType", "update")
154 execution.setVariable("hasResourcetoUpdate", false)
156 } catch (BpmnError e) {
158 } catch (Exception ex){
159 msg = "Exception in preProcessRequest " + ex.getMessage()
160 utils.log("INFO", msg, isDebugEnabled)
161 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
163 utils.log("INFO"," ***** Exit preProcessRequest *****", isDebugEnabled)
167 * Gets the service instance and its relationships from aai
169 public void getServiceInstance(DelegateExecution execution) {
171 String serviceInstanceId = execution.getVariable('serviceInstanceId')
172 String globalSubscriberId = execution.getVariable('globalSubscriberId')
173 String serviceType = execution.getVariable('serviceType')
175 AAIResourcesClient resourceClient = new AAIResourcesClient()
176 AAIResourceUri serviceInstanceUri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, globalSubscriberId, serviceType, serviceInstanceId)
177 AAIResultWrapper wrapper = resourceClient.get(serviceInstanceUri, NotFoundException.class)
179 Optional<ServiceInstance> si = wrapper.asBean(ServiceInstance.class)
180 execution.setVariable("serviceInstanceName", si.get().getServiceInstanceName())
181 execution.setVariable("model-invariant-id-original", si.get().getModelInvariantId())
182 execution.setVariable("model-version-id-original", si.get().getModelVersionId())
184 JSONObject ob = new JSONObject(wrapper.getJson())
185 JSONArray ar = ob.getJSONObject("relationship-list").getJSONArray("relationship")
187 execution.setVariable("serviceRelationShip", ar.toString())
190 }catch(BpmnError e) {
192 }catch(NotFoundException e) {
193 exceptionUtil.buildAndThrowWorkflowException(execution, 404, "Service-instance does not exist AAI")
194 }catch(Exception ex) {
195 String msg = "Internal Error in getServiceInstance: " + ex.getMessage()
196 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
200 public void preCompareModelVersions(DelegateExecution execution) {
201 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
204 public void postCompareModelVersions(DelegateExecution execution) {
205 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
206 utils.log("DEBUG", " ======== STARTED postCompareModelVersions Process ======== ", isDebugEnabled)
208 def hasResourcetoUpdate = false
209 def hasResourcetoAdd = false
210 def hasResourcetoDelete = false
211 List<Resource> addResourceList = execution.getVariable("addResourceList")
212 List<Resource> delResourceList = execution.getVariable("delResourceList")
214 if(addResourceList != null && !addResourceList.isEmpty()) {
215 hasResourcetoAdd = true
218 if(delResourceList != null && !delResourceList.isEmpty()) {
219 hasResourcetoDelete = true
222 hasResourcetoUpdate = hasResourcetoAdd || hasResourcetoDelete
223 execution.setVariable("hasResourcetoUpdate", hasResourcetoUpdate)
225 utils.log("DEBUG", "======== COMPLETED postCompareModelVersions Process ======== ", isDebugEnabled)
229 * Init the service Operation Status
231 public void prepareInitServiceOperationStatus(DelegateExecution execution){
232 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
233 utils.log("DEBUG", " ======== STARTED prepareInitServiceOperationStatus Process ======== ", isDebugEnabled)
235 String serviceId = execution.getVariable("serviceInstanceId")
236 String operationId = execution.getVariable("operationId")
237 String operationType = execution.getVariable("operationType")
239 String result = "processing"
240 String progress = "0"
242 String operationContent = "Prepare service updating"
243 utils.log("DEBUG", "Generated new operation for Service Instance serviceId:" + serviceId + " operationId:" + operationId, isDebugEnabled)
244 serviceId = UriUtils.encode(serviceId,"UTF-8")
245 execution.setVariable("serviceInstanceId", serviceId)
246 execution.setVariable("operationId", operationId)
247 execution.setVariable("operationType", operationType)
249 def dbAdapterEndpoint = UrnPropertiesReader.getVariable("mso.adapters.openecomp.db.endpoint", execution)
250 execution.setVariable("CVFMI_dbAdapterEndpoint", dbAdapterEndpoint)
251 utils.log("DEBUG", "DB Adapter Endpoint is: " + dbAdapterEndpoint, isDebugEnabled)
254 """<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
255 xmlns:ns="http://org.onap.so/requestsdb">
258 <ns:updateServiceOperationStatus xmlns:ns="http://org.onap.so/requestsdb">
259 <serviceId>${MsoUtils.xmlEscape(serviceId)}</serviceId>
260 <operationId>${MsoUtils.xmlEscape(operationId)}</operationId>
261 <operationType>${MsoUtils.xmlEscape(operationType)}</operationType>
262 <userId>${MsoUtils.xmlEscape(userId)}</userId>
263 <result>${MsoUtils.xmlEscape(result)}</result>
264 <operationContent>${MsoUtils.xmlEscape(operationContent)}</operationContent>
265 <progress>${MsoUtils.xmlEscape(progress)}</progress>
266 <reason>${MsoUtils.xmlEscape(reason)}</reason>
267 </ns:updateServiceOperationStatus>
269 </soapenv:Envelope>"""
271 payload = utils.formatXml(payload)
272 execution.setVariable("CVFMI_updateServiceOperStatusRequest", payload)
273 utils.log("DEBUG", "Outgoing updateServiceOperStatusRequest: \n" + payload, isDebugEnabled)
274 utils.logAudit("CreateVfModuleInfra Outgoing updateServiceOperStatusRequest Request: " + payload)
277 utils.log("ERROR", "Exception Occured Processing prepareInitServiceOperationStatus. Exception is:\n" + e, isDebugEnabled)
278 execution.setVariable("CVFMI_ErrorResponse", "Error Occurred during prepareInitServiceOperationStatus Method:\n" + e.getMessage())
280 utils.log("DEBUG", "======== COMPLETED prepareInitServiceOperationStatus Process ======== ", isDebugEnabled)
284 * Update the service Operation Status
286 public void preUpdateServiceOperationStatus(DelegateExecution execution){
287 def method = getClass().getSimpleName() + '.preUpdateServiceOperationStatus(' +'execution=' + execution.getId() +')'
288 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
289 utils.log("INFO","Entered " + method, isDebugEnabled)
292 String serviceId = execution.getVariable("serviceInstanceId")
293 String operationId = execution.getVariable("operationId")
294 String operationType = execution.getVariable("operationType")
295 String serviceName = execution.getVariable("serviceInstanceName")
296 String result = execution.getVariable("operationResult")
297 String progress = execution.getVariable("progress")
298 String reason = execution.getVariable("operationReason")
300 utils.log("INFO", "progress: " + progress , isDebugEnabled)
302 String operationContent = "Prepare service : " + execution.getVariable("operationStatus")
304 utils.log("INFO", "Generated new operation for Service Instance serviceId:" + serviceId + " operationId:" + operationId, isDebugEnabled)
305 serviceId = UriUtils.encode(serviceId,"UTF-8")
306 execution.setVariable("serviceInstanceId", serviceId)
307 execution.setVariable("operationId", operationId)
308 execution.setVariable("operationType", operationType)
310 def dbAdapterEndpoint = UrnPropertiesReader.getVariable("mso.adapters.openecomp.db.endpoint", execution)
311 execution.setVariable("CVFMI_dbAdapterEndpoint", dbAdapterEndpoint)
312 utils.log("INFO", "DB Adapter Endpoint is: " + dbAdapterEndpoint, isDebugEnabled)
315 """<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
316 xmlns:ns="http://org.onap.so/requestsdb">
319 <ns:updateServiceOperationStatus xmlns:ns="http://org.onap.so/requestsdb">
320 <serviceId>${MsoUtils.xmlEscape(serviceId)}</serviceId>
321 <operationId>${MsoUtils.xmlEscape(operationId)}</operationId>
322 <operationType>${MsoUtils.xmlEscape(operationType)}</operationType>
323 <userId>${MsoUtils.xmlEscape(userId)}</userId>
324 <result>${MsoUtils.xmlEscape(result)}</result>
325 <operationContent>${MsoUtils.xmlEscape(operationContent)}</operationContent>
326 <progress>${MsoUtils.xmlEscape(progress)}</progress>
327 <reason>${MsoUtils.xmlEscape(reason)}</reason>
328 </ns:updateServiceOperationStatus>
330 </soapenv:Envelope>"""
332 payload = utils.formatXml(payload)
333 execution.setVariable("CVFMI_updateServiceOperStatusRequest", payload)
334 utils.log("INFO", "Outgoing preUpdateServiceOperationStatus: \n" + payload, isDebugEnabled)
338 utils.log("ERROR", "Exception Occured Processing preUpdateServiceOperationStatus. Exception is:\n" + e, isDebugEnabled)
339 execution.setVariable("CVFMI_ErrorResponse", "Error Occurred during preUpdateServiceOperationStatus Method:\n" + e.getMessage())
341 utils.log("INFO", "======== COMPLETED preUpdateServiceOperationStatus Process ======== ", isDebugEnabled)
342 utils.log("INFO", "Exited " + method, isDebugEnabled)
345 public void sendSyncResponse (DelegateExecution execution) {
346 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
347 utils.log("INFO", " *** sendSyncResponse *** ", isDebugEnabled)
350 String operationId = execution.getVariable("operationId")
351 def hasResourcetoUpdate = execution.getVariable("hasResourcetoUpdate")
353 String updateServiceResp = ""
354 if(hasResourcetoUpdate) {
355 // RESTResponse for API Handler (APIH) Reply Task
356 updateServiceResp = """{"operationId":"${operationId}"}""".trim()
359 updateServiceResp = """{"OperationResult":"No Resource to Add or Delete or Service Instance not found in AAI."}"""
362 utils.log("INFO", " sendSyncResponse to APIH:" + "\n" + updateServiceResp, isDebugEnabled)
363 sendWorkflowResponse(execution, 202, updateServiceResp)
364 execution.setVariable("sentSyncResponse", true)
366 } catch (Exception ex) {
367 String msg = "Exceptuion in sendSyncResponse:" + ex.getMessage()
368 utils.log("INFO", msg, isDebugEnabled)
369 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
371 utils.log("INFO"," ***** Exit sendSyncResopnse *****", isDebugEnabled)
374 public void sendSyncError (DelegateExecution execution) {
375 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
376 utils.log("INFO", " *** sendSyncError *** ", isDebugEnabled)
379 String errorMessage = ""
381 if (execution.getVariable("WorkflowException") instanceof WorkflowException) {
382 WorkflowException wfe = execution.getVariable("WorkflowException")
383 errorMessage = wfe.getErrorMessage()
384 errorCode = wfe.getErrorCode()
386 errorMessage = "Sending Sync Error."
389 String buildworkflowException =
390 """<aetgt:WorkflowException xmlns:aetgt="http://org.onap/so/workflow/schema/v1">
391 <aetgt:ErrorMessage>${MsoUtils.xmlEscape(errorMessage)}</aetgt:ErrorMessage>
392 <aetgt:ErrorCode>${MsoUtils.xmlEscape(errorCode)}</aetgt:ErrorCode>
393 </aetgt:WorkflowException>"""
395 utils.logAudit(buildworkflowException)
396 sendWorkflowResponse(execution, 500, buildworkflowException)
398 } catch (Exception ex) {
399 utils.log("INFO", " Sending Sync Error Activity Failed. " + "\n" + ex.getMessage(), isDebugEnabled)
404 public void prepareCompletionRequest (DelegateExecution execution) {
405 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
406 utils.log("INFO", " *** prepareCompletion *** ", isDebugEnabled)
409 String requestId = execution.getVariable("msoRequestId")
410 String serviceInstanceId = execution.getVariable("serviceInstanceId")
411 String source = execution.getVariable("source")
413 String msoCompletionRequest =
414 """<aetgt:MsoCompletionRequest xmlns:aetgt="http://org.onap/so/workflow/schema/v1"
415 xmlns:ns="http://org.onap/so/request/types/v1">
416 <request-info xmlns="http://org.onap/so/infra/vnf-request/v1">
417 <request-id>${MsoUtils.xmlEscape(requestId)}</request-id>
418 <action>UPDATE</action>
419 <source>${MsoUtils.xmlEscape(source)}</source>
421 <status-message>Service Instance was updated successfully.</status-message>
422 <serviceInstanceId>${MsoUtils.xmlEscape(serviceInstanceId)}</serviceInstanceId>
423 <mso-bpel-name>UpdateCustomE2EServiceInstance</mso-bpel-name>
424 </aetgt:MsoCompletionRequest>"""
427 String xmlMsoCompletionRequest = utils.formatXml(msoCompletionRequest)
429 execution.setVariable("completionRequest", xmlMsoCompletionRequest)
430 utils.log("INFO", " Overall SUCCESS Response going to CompleteMsoProcess - " + "\n" + xmlMsoCompletionRequest, isDebugEnabled)
432 } catch (Exception ex) {
433 String msg = " Exception in prepareCompletion:" + ex.getMessage()
434 utils.log("INFO", msg, isDebugEnabled)
435 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
437 utils.log("INFO", "*** Exit prepareCompletionRequest ***", isDebugEnabled)
440 public void prepareFalloutRequest(DelegateExecution execution){
441 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
442 utils.log("INFO", " *** prepareFalloutRequest *** ", isDebugEnabled)
445 WorkflowException wfex = execution.getVariable("WorkflowException")
446 utils.log("INFO", " Input Workflow Exception: " + wfex.toString(), isDebugEnabled)
447 String requestId = execution.getVariable("msoRequestId")
448 String source = execution.getVariable("source")
450 """<request-info xmlns="http://org.onap/so/infra/vnf-request/v1">
451 <request-id>${MsoUtils.xmlEscape(requestId)}</request-id>
452 <action>UPDATE</action>
453 <source>${MsoUtils.xmlEscape(source)}</source>
456 String falloutRequest = exceptionUtil.processMainflowsBPMNException(execution, requestInfo)
457 execution.setVariable("falloutRequest", falloutRequest)
458 } catch (Exception ex) {
459 utils.log("INFO", "Exception prepareFalloutRequest:" + ex.getMessage(), isDebugEnabled)
460 String errorException = " Bpmn error encountered in UpdateCustomE2EServiceInstance flow. FalloutHandlerRequest, buildErrorResponse() - " + ex.getMessage()
461 String requestId = execution.getVariable("msoRequestId")
462 String falloutRequest =
463 """<aetgt:FalloutHandlerRequest xmlns:aetgt="http://org.onap/so/workflow/schema/v1"
464 xmlns:ns="http://org.onap/so/request/types/v1"
465 xmlns:wfsch="http://org.onap/so/workflow/schema/v1">
466 <request-info xmlns="http://org.onap/so/infra/vnf-request/v1">
467 <request-id>${MsoUtils.xmlEscape(requestId)}</request-id>
468 <action>UPDATE</action>
471 <aetgt:WorkflowException xmlns:aetgt="http://org.onap/so/workflow/schema/v1">
472 <aetgt:ErrorMessage>${MsoUtils.xmlEscape(errorException)}</aetgt:ErrorMessage>
473 <aetgt:ErrorCode>7000</aetgt:ErrorCode>
474 </aetgt:WorkflowException>
475 </aetgt:FalloutHandlerRequest>"""
477 execution.setVariable("falloutRequest", falloutRequest)
479 utils.log("INFO", "*** Exit prepareFalloutRequest ***", isDebugEnabled)