2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 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 com.fasterxml.jackson.databind.ObjectMapper
24 import org.onap.aai.domain.yang.ServiceInstance
25 import org.onap.so.client.HttpClient
26 import org.onap.so.client.HttpClientFactory
27 import org.onap.aaiclient.client.aai.AAIObjectType
28 import org.onap.aaiclient.client.aai.AAIResourcesClient
29 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri
30 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory
32 import javax.ws.rs.core.Response
34 import static org.apache.commons.lang3.StringUtils.*
35 import org.springframework.web.util.UriUtils
36 import groovy.json.JsonSlurper
37 import org.camunda.bpm.engine.delegate.BpmnError
38 import org.camunda.bpm.engine.delegate.DelegateExecution
39 import org.onap.logging.filter.base.ONAPComponents
40 import org.onap.so.beans.nsmf.SliceTaskParams
41 import org.onap.so.db.request.beans.OrchestrationTask
42 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
43 import org.onap.so.bpmn.common.scripts.ExceptionUtil
44 import org.onap.so.bpmn.common.scripts.OofUtils
45 import org.onap.so.bpmn.core.UrnPropertiesReader
46 import org.onap.so.bpmn.common.scripts.MsoUtils
47 import org.onap.so.bpmn.core.json.JsonUtils
48 import org.onap.logging.filter.base.ErrorCode
49 import org.onap.so.logger.LoggingAnchor
50 import org.onap.so.logger.MessageEnum
51 import org.slf4j.Logger
52 import org.slf4j.LoggerFactory
54 public class CreateSliceService extends AbstractServiceTaskProcessor {
55 String Prefix = "CRESS_"
57 ExceptionUtil exceptionUtil = new ExceptionUtil()
58 JsonUtils jsonUtil = new JsonUtils()
59 JsonSlurper jsonSlurper = new JsonSlurper()
60 ObjectMapper objectMapper = new ObjectMapper()
61 OofUtils oofUtils = new OofUtils()
62 private static final Logger logger = LoggerFactory.getLogger(CreateSliceService.class)
65 public void preProcessRequest(DelegateExecution execution) {
66 logger.debug("Start preProcessRequest")
67 execution.setVariable("prefix", Prefix)
71 String ssRequest = execution.getVariable("bpmnRequest")
72 logger.debug(ssRequest)
74 String requestId = execution.getVariable("mso-request-id")
75 execution.setVariable("msoRequestId", requestId)
76 logger.debug("Input Request:" + ssRequest + " reqId:" + requestId)
78 String serviceInstanceId = execution.getVariable("serviceInstanceId")
79 if (isBlank(serviceInstanceId)) {
80 serviceInstanceId = UUID.randomUUID().toString()
83 String operationId = UUID.randomUUID().toString()
84 execution.setVariable("operationId", operationId)
86 logger.debug("Generated new Service Instance:" + serviceInstanceId)
87 serviceInstanceId = UriUtils.encode(serviceInstanceId, "UTF-8")
88 execution.setVariable("serviceInstanceId", serviceInstanceId)
91 String globalSubscriberId = jsonUtil.getJsonValue(ssRequest, "requestDetails.subscriberInfo.globalSubscriberId")
92 if (isBlank(globalSubscriberId)) {
93 msg = "Input globalSubscriberId' is null"
94 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
96 execution.setVariable("globalSubscriberId", globalSubscriberId)
100 execution.setVariable("source", jsonUtil.getJsonValue(ssRequest, "requestDetails.requestInfo.source"))
101 execution.setVariable("serviceInstanceName", jsonUtil.getJsonValue(ssRequest, "requestDetails.requestInfo.instanceName"))
102 execution.setVariable("disableRollback", jsonUtil.getJsonValue(ssRequest, "requestDetails.requestInfo.suppressRollback"))
103 String productFamilyId = jsonUtil.getJsonValue(ssRequest, "requestDetails.requestInfo.productFamilyId")
104 if (isBlank(productFamilyId)) {
105 msg = "Input productFamilyId is null"
107 //exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
109 execution.setVariable("productFamilyId", productFamilyId)
113 String serviceModelInfo = jsonUtil.getJsonValue(ssRequest, "requestDetails.modelInfo")
114 if (isBlank(serviceModelInfo)) {
115 msg = "Input serviceModelInfo is null"
117 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
119 execution.setVariable("serviceModelInfo", serviceModelInfo)
122 logger.debug("modelInfo: " + serviceModelInfo)
125 String subscriptionServiceType = jsonUtil.getJsonValue(ssRequest, "requestDetails.requestParameters.subscriptionServiceType")
126 if (isBlank(subscriptionServiceType)) {
127 msg = "Input subscriptionServiceType is null"
129 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
131 execution.setVariable("subscriptionServiceType", subscriptionServiceType)
133 logger.debug("subscriptionServiceType: " + subscriptionServiceType)
136 * Extracting User Parameters from incoming Request and converting into a Map
138 Map reqMap = jsonSlurper.parseText(ssRequest)
141 def userParamsList = reqMap.requestDetails?.requestParameters?.userParams
143 Map<String, String> inputMap = [:]
144 if (userParamsList) {
145 for (def i = 0; i < userParamsList.size(); i++) {
146 def userParams1 = userParamsList.get(i)
147 userParams1.each { param -> inputMap.put(param.key, param.value) }
151 logger.debug("User Input Parameters map: " + inputMap.toString())
152 String uuiRequest = inputMap.get("UUIRequest")
153 Map uuiReqMap = jsonSlurper.parseText(uuiRequest)
154 Map<String, Object> serviceObject = (Map<String, Object>) uuiReqMap.get("service")
155 Map<String, Object> parameterObject = (Map<String, Object>) serviceObject.get("parameters")
156 Map<String, Object> requestInputs = (Map<String, Object>) parameterObject.get("requestInputs")
158 def serviceProfile = [:]
159 for(entry in requestInputs) {
160 serviceProfile[entry.key] = entry.value
163 execution.setVariable("serviceInputParams", inputMap)
164 execution.setVariable("uuiRequest", uuiRequest)
165 execution.setVariable("serviceProfile", serviceProfile)
168 //execution.setVariable("serviceInputParams", jsonUtil.getJsonValue(siRequest, "requestDetails.requestParameters.userParams"))
169 //execution.setVariable("failExists", true)
171 } catch (BpmnError e) {
173 } catch (Exception ex) {
174 msg = "Exception in preProcessRequest " + ex.getMessage()
176 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
178 logger.debug("Finish preProcessRequest")
181 public void prepareSelectNSTRequest(DelegateExecution execution) {
182 logger.debug("Start prepareSelectNSTRequest")
183 String requestId = execution.getVariable("msoRequestId")
184 String messageType = "NSTSelectionResponse"
185 Map<String, Object> serviceProfile = execution.getVariable("serviceProfile")
186 execution.setVariable("nstSelectionUrl", "/api/oof/v1/selection/nst")
187 execution.setVariable("nstSelection_messageType",messageType)
188 execution.setVariable("nstSelection_correlator",requestId)
189 String timeout = UrnPropertiesReader.getVariable("mso.adapters.oof.timeout", execution);
190 execution.setVariable("nstSelection_timeout",timeout)
191 String oofRequest = oofUtils.buildSelectNSTRequest(requestId,messageType, serviceProfile)
192 execution.setVariable("nstSelection_oofRequest",oofRequest)
193 logger.debug("Finish prepareSelectNSTRequest")
197 public void processNSTSolutions(DelegateExecution execution) {
198 Map<String, Object> nstSolution
200 logger.debug("Start processing NSTSolutions")
201 Map<String, Object> resMap = objectMapper.readValue(execution.getVariable("nstSelection_oofResponse"),Map.class)
202 List<Map<String, Object>> nstSolutions = (List<Map<String, Object>>) resMap.get("solutions")
203 nstSolution = nstSolutions.get(0)
204 execution.setVariable("nstSolution", nstSolution)
205 } catch (Exception ex) {
206 logger.debug( "Failed to get NST solution suggested by OOF.")
207 exceptionUtil.buildAndThrowWorkflowException(execution, 401, "Failed to get NST solution suggested by OOF.")
212 public void prepareDecomposeService(DelegateExecution execution) {
213 logger.debug("Start prepareDecomposeService")
214 String uuiRequest = execution.getVariable("uuiRequest")
215 String ssModelInvariantUuid = jsonUtil.getJsonValue(uuiRequest, "service.serviceInvariantUuid")
216 String ssModelUuid = jsonUtil.getJsonValue(uuiRequest, "service.serviceUuid")
217 String ssServiceModelInfo = """{
218 "modelInvariantUuid":"${ssModelInvariantUuid}",
219 "modelUuid":"${ssModelUuid}",
222 execution.setVariable("ssServiceModelInfo", ssServiceModelInfo)
224 logger.debug("Finish prepareDecomposeService")
227 public void processDecomposition(DelegateExecution execution) {
228 logger.debug("Start processDecomposition")
229 String uuiRequest = execution.getVariable("uuiRequest")
230 Map<String, Object> serviceProfile = execution.getVariable("serviceProfile")
231 Map<String, Object> nstSolution = execution.getVariable("nstSolution")
233 Map uuiReqMap = jsonSlurper.parseText(uuiRequest)
234 Map<String, Object> serviceObject = (Map<String, Object>) uuiReqMap.get("service")
235 String subscriptionServiceType = serviceObject.get("serviceType")
237 String serviceType = (String) serviceProfile.get("sST")
238 String resourceSharingLevel = (String) serviceProfile.get("resourceSharingLevel")
239 String nstModelUuid = (String) nstSolution.get("UUID")
240 String nstModelInvariantUuid = (String) nstSolution.get("invariantUUID")
242 execution.setVariable("subscriptionServiceType", subscriptionServiceType)
243 execution.setVariable("serviceType", serviceType)
244 execution.setVariable("resourceSharingLevel", resourceSharingLevel)
245 execution.setVariable("nstModelUuid", nstModelUuid)
246 execution.setVariable("nstModelInvariantUuid", nstModelInvariantUuid)
248 logger.debug("Finish processDecomposition")
251 public void prepareCreateOrchestrationTask(DelegateExecution execution) {
252 logger.debug("Start createOrchestrationTask")
253 String taskId = execution.getBusinessKey()
254 execution.setVariable("orchestrationTaskId", taskId)
255 logger.debug("BusinessKey: " + taskId)
256 String serviceInstanceId = execution.getVariable("serviceInstanceId")
257 String serviceInstanceName = execution.getVariable("serviceInstanceName")
258 String taskName = "SliceServiceTask"
259 String taskStatus = "Planning"
260 String isManual = "false"
261 String requestMethod = "POST"
262 execution.setVariable("CSSOT_taskId", taskId)
263 execution.setVariable("CSSOT_name", taskName)
264 execution.setVariable("CSSOT_status", taskStatus)
265 execution.setVariable("CSSOT_isManual", isManual)
266 execution.setVariable("CSSOT_requestMethod", requestMethod)
268 Map<String, Object> serviceProfile = execution.getVariable("serviceProfile")
270 SliceTaskParams sliceTaskParams = new SliceTaskParams()
271 sliceTaskParams.setServiceId(serviceInstanceId)
272 sliceTaskParams.setServiceName(serviceInstanceName)
273 sliceTaskParams.setServiceProfile(serviceProfile)
274 execution.setVariable("sliceTaskParams", sliceTaskParams)
276 String paramJson = sliceTaskParams.convertToJson()
277 execution.setVariable("CSSOT_paramJson", paramJson)
278 logger.debug("CSSOT_paramJson: " + paramJson)
280 logger.debug("Finish createOrchestrationTask")
283 public void prepareUpdateOrchestrationTask(DelegateExecution execution) {
284 logger.debug("Start prepareUpdateOrchestrationTask")
285 String requestMethod = "PUT"
286 String taskStatus = execution.getVariable("taskStatus")
287 SliceTaskParams sliceTaskParams = execution.getVariable("sliceTaskParams")
288 String paramJson = sliceTaskParams.convertToJson()
289 execution.setVariable("CSSOT_status", taskStatus)
290 execution.setVariable("CSSOT_paramJson", paramJson)
291 execution.setVariable("CSSOT_requestMethod", requestMethod)
292 logger.debug("Finish prepareUpdateOrchestrationTask")
295 public void prepareGetUserOptions(DelegateExecution execution) {
296 logger.debug("Start prepareGetUserOptions")
297 String requestMethod = "GET"
298 execution.setVariable("taskAction", "commit")
299 String taskAction = execution.getVariable("taskAction")
300 logger.debug("task action is: " + taskAction)
301 if (!"commit".equals(taskAction) && !"abort".equals(taskAction)) {
302 String msg = "Unknown task action: " + taskAction
304 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
306 execution.setVariable("CSSOT_requestMethod", requestMethod)
307 logger.debug("Finish prepareGetUserOptions")
310 public void processUserOptions(DelegateExecution execution) {
311 logger.debug("Start processUserOptions")
312 String response = execution.getVariable("CSSOT_dbResponse")
313 OrchestrationTask orchestrationTask = objectMapper.readValue(response, OrchestrationTask.class)
314 String paramJson = orchestrationTask.getParams()
315 logger.debug("paramJson: " + paramJson)
316 SliceTaskParams sliceTaskParams = new SliceTaskParams()
317 sliceTaskParams.convertFromJson(paramJson)
318 execution.setVariable("sliceTaskParams", sliceTaskParams)
319 logger.debug("Finish processUserOptions")
322 public void updateAAIOrchStatus(DelegateExecution execution) {
323 logger.debug("Start updateAAIOrchStatus")
324 String serviceInstanceId = execution.getVariable("serviceInstanceId")
325 String orchStatus = execution.getVariable("orchestrationStatus")
328 ServiceInstance si = ServiceInstance si = new ServiceInstance()
329 si.setOrchestrationStatus(orchStatus)
330 AAIResourcesClient client = new AAIResourcesClient()
331 AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, serviceInstanceId)
332 client.update(uri, si)
333 } catch (BpmnError e) {
335 } catch (Exception ex) {
336 String msg = "Exception in CreateSliceService.updateAAIOrchStatus " + ex.getMessage()
338 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
341 logger.debug("Finish updateAAIOrchStatus")
344 public void prepareInitServiceOperationStatus(DelegateExecution execution) {
345 logger.debug("Start prepareInitServiceOperationStatus")
347 String serviceId = execution.getVariable("serviceInstanceId")
348 String operationId = execution.getVariable("operationId")
349 String operationType = "CREATE"
350 String userId = execution.getVariable("globalSubscriberId")
351 String result = "processing"
352 String progress = "0"
354 String operationContent = "Prepare service creation"
355 logger.debug("Generated new operation for Service Instance serviceId:" + serviceId + " operationId:" + operationId)
356 serviceId = UriUtils.encode(serviceId,"UTF-8")
357 execution.setVariable("serviceInstanceId", serviceId)
358 execution.setVariable("operationType", operationType)
360 def dbAdapterEndpoint = UrnPropertiesReader.getVariable("mso.adapters.openecomp.db.endpoint",execution)
361 execution.setVariable("CSSOS_dbAdapterEndpoint", dbAdapterEndpoint)
362 logger.debug("DB Adapter Endpoint is: " + dbAdapterEndpoint)
363 def dbAdapterAuth = UrnPropertiesReader.getVariable("mso.requestDb.auth")
364 Map<String, String> CSSOS_headerMap = [:]
365 CSSOS_headerMap.put("content-type", "application/soap+xml")
366 CSSOS_headerMap.put("Authorization", dbAdapterAuth)
367 execution.setVariable("CSSOS_headerMap", CSSOS_headerMap)
368 logger.debug("DB Adapter Header is: " + CSSOS_headerMap)
371 """<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
372 xmlns:ns="http://org.onap.so/requestsdb">
375 <ns:initServiceOperationStatus xmlns:ns="http://org.onap.so/requestsdb">
376 <serviceId>${MsoUtils.xmlEscape(serviceId)}</serviceId>
377 <operationId>${MsoUtils.xmlEscape(operationId)}</operationId>
378 <operationType>${MsoUtils.xmlEscape(operationType)}</operationType>
379 <userId>${MsoUtils.xmlEscape(userId)}</userId>
380 <result>${MsoUtils.xmlEscape(result)}</result>
381 <operationContent>${MsoUtils.xmlEscape(operationContent)}</operationContent>
382 <progress>${MsoUtils.xmlEscape(progress)}</progress>
383 <reason>${MsoUtils.xmlEscape(reason)}</reason>
384 </ns:initServiceOperationStatus>
386 </soapenv:Envelope>"""
388 payload = utils.formatXml(payload)
389 execution.setVariable("CSSOS_updateServiceOperStatusRequest", payload)
390 logger.debug("Outgoing updateServiceOperStatusRequest: \n" + payload)
392 logger.error(LoggingAnchor.FIVE, MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
393 "Exception Occured Processing prepareInitServiceOperationStatus.", "BPMN",
394 ErrorCode.UnknownError.getValue(), "Exception is:\n" + e)
396 logger.debug("Finish prepareInitServiceOperationStatus")
399 public void prepareUpdateServiceOperationStatus(DelegateExecution execution) {
400 logger.debug("Start preUpdateServiceOperationStatus")
402 String serviceId = execution.getVariable("serviceInstanceId")
403 String operationId = execution.getVariable("operationId")
404 String operationType = execution.getVariable("operationType")
405 String userId = execution.getVariable("globalSubscriberId")
406 String result = execution.getVariable("operationResult")
407 String progress = execution.getVariable("operationProgress")
408 String reason = execution.getVariable("operationReason")
409 String operationContent = "service: " + result + " progress: " + progress
412 """<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
413 xmlns:ns="http://org.onap.so/requestsdb">
416 <ns:initServiceOperationStatus xmlns:ns="http://org.onap.so/requestsdb">
417 <serviceId>${MsoUtils.xmlEscape(serviceId)}</serviceId>
418 <operationId>${MsoUtils.xmlEscape(operationId)}</operationId>
419 <operationType>${MsoUtils.xmlEscape(operationType)}</operationType>
420 <userId>${MsoUtils.xmlEscape(userId)}</userId>
421 <result>${MsoUtils.xmlEscape(result)}</result>
422 <operationContent>${MsoUtils.xmlEscape(operationContent)}</operationContent>
423 <progress>${MsoUtils.xmlEscape(progress)}</progress>
424 <reason>${MsoUtils.xmlEscape(reason)}</reason>
425 </ns:initServiceOperationStatus>
427 </soapenv:Envelope>"""
429 payload = utils.formatXml(payload)
430 execution.setVariable("CSSOS_updateServiceOperStatusRequest", payload)
431 logger.debug("Outgoing updateServiceOperStatusRequest: \n" + payload)
434 logger.error(LoggingAnchor.FIVE, MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
435 "Exception Occured Processing preUpdateServiceOperationStatus.", "BPMN",
436 ErrorCode.UnknownError.getValue(), "Exception is:\n" + e.getMessage())
438 logger.debug("Finish preUpdateServiceOperationStatus")
441 public void sendSyncResponse(DelegateExecution execution) {
442 logger.debug("Start sendSyncResponse")
444 String operationId = execution.getVariable("operationId")
445 String serviceInstanceId = execution.getVariable("serviceInstanceId")
446 // RESTResponse for API Handler (APIH) Reply Task
447 String createServiceRestRequest = """{"service":{"serviceId":"${serviceInstanceId}","operationId":"${operationId}"}}""".trim()
448 logger.debug("sendSyncResponse to APIH:" + "\n" + createServiceRestRequest)
449 sendWorkflowResponse(execution, 202, createServiceRestRequest)
450 execution.setVariable("sentSyncResponse", true)
451 } catch (Exception e) {
452 String msg = "Exceptuion in sendSyncResponse:" + e.getMessage()
454 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
456 logger.debug("Finish sendSyncResponse")
459 public void prepareCompletionRequest (DelegateExecution execution) {
460 logger.trace("Start prepareCompletionRequest")
462 String requestId = execution.getVariable("msoRequestId")
463 String serviceInstanceId = execution.getVariable("serviceInstanceId")
464 String source = execution.getVariable("source")
466 String msoCompletionRequest =
467 """<aetgt:MsoCompletionRequest xmlns:aetgt="http://org.onap/so/workflow/schema/v1"
468 xmlns:ns="http://org.onap/so/request/types/v1">
469 <request-info xmlns="http://org.onap/so/infra/vnf-request/v1">
470 <request-id>${MsoUtils.xmlEscape(requestId)}</request-id>
471 <action>CREATE</action>
472 <source>${MsoUtils.xmlEscape(source)}</source>
474 <status-message>Service Instance was created successfully.</status-message>
475 <serviceInstanceId>${MsoUtils.xmlEscape(serviceInstanceId)}</serviceInstanceId>
476 <mso-bpel-name>CreateGenericALaCarteServiceInstance</mso-bpel-name>
477 </aetgt:MsoCompletionRequest>"""
480 String xmlMsoCompletionRequest = utils.formatXml(msoCompletionRequest)
482 execution.setVariable("completionRequest", xmlMsoCompletionRequest)
483 logger.debug("Overall SUCCESS Response going to CompleteMsoProcess - " + "\n" + xmlMsoCompletionRequest)
485 } catch (Exception ex) {
486 String msg = " Exception in prepareCompletion:" + ex.getMessage()
488 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
490 logger.trace("Finish prepareCompletionRequest")