Fixes for bugs in Allocate TN NSSI WF found in integration test
[so.git] / bpmn / so-bpmn-infrastructure-common / src / main / groovy / org / onap / so / bpmn / infrastructure / scripts / DoActivateTnNssi.groovy
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2020 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 com.fasterxml.jackson.databind.ObjectMapper
24 import groovy.json.JsonSlurper
25 import org.camunda.bpm.engine.delegate.BpmnError
26 import org.camunda.bpm.engine.delegate.DelegateExecution
27 import org.onap.aai.domain.yang.ServiceInstance
28 import org.onap.aaiclient.client.aai.AAIObjectType
29 import org.onap.aaiclient.client.aai.AAIResourcesClient
30 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri
31 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory
32 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder
33 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder.Types
34 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
35 import org.onap.so.bpmn.common.scripts.ExceptionUtil
36 import org.onap.so.bpmn.common.scripts.RequestDBUtil
37 import org.onap.so.bpmn.core.json.JsonUtils
38 import org.onap.so.db.request.beans.ResourceOperationStatus
39 import org.slf4j.Logger
40 import org.slf4j.LoggerFactory
41
42
43 public class DoActivateTnNssi extends AbstractServiceTaskProcessor {
44     String Prefix = "TNACT_"
45
46     ExceptionUtil exceptionUtil = new ExceptionUtil()
47     JsonUtils jsonUtil = new JsonUtils()
48     RequestDBUtil requestDBUtil = new RequestDBUtil()
49     TnNssmfUtils tnNssmfUtils = new TnNssmfUtils()
50     JsonSlurper jsonSlurper = new JsonSlurper()
51     ObjectMapper objectMapper = new ObjectMapper()
52     private static final Logger logger = LoggerFactory.getLogger(DoActivateTnNssi.class)
53
54
55     public void preProcessRequest(DelegateExecution execution) {
56         logger.debug("Start preProcessRequest")
57
58         execution.setVariable("startTime", System.currentTimeMillis())
59         String msg = tnNssmfUtils.getExecutionInputParams(execution)
60         logger.debug("Activate TN NSSI input parameters: " + msg)
61
62         execution.setVariable("prefix", Prefix)
63
64         tnNssmfUtils.setSdncCallbackUrl(execution, true)
65         logger.debug("SDNC Callback URL: " + execution.getVariable("sdncCallbackUrl"))
66
67         String modelInvariantUuid = execution.getVariable("modelInvariantUuid")
68         String modelUuid = execution.getVariable("modelUuid")
69         //here modelVersion is not set, we use modelUuid to decompose the service.
70         def isDebugLogEnabled = true
71         execution.setVariable("isDebugLogEnabled", isDebugLogEnabled)
72         String serviceModelInfo = """{
73             "modelInvariantUuid":"${modelInvariantUuid}",
74             "modelUuid":"${modelUuid}",
75             "modelVersion":""
76              }"""
77         execution.setVariable("serviceModelInfo", serviceModelInfo)
78
79         String sliceServiceInstanceId = execution.getVariable("serviceInstanceID")
80         execution.setVariable("sliceServiceInstanceId", sliceServiceInstanceId)
81
82         String sliceServiceInstanceName = execution.getVariable("servicename")
83         execution.setVariable("sliceServiceInstanceName", sliceServiceInstanceName)
84
85         String operationType = execution.getVariable("operationType")
86         String actionType = operationType.equals("activateInstance") ? "activate" : "deactivate"
87         execution.setVariable("actionType", actionType)
88
89
90         logger.debug("Finish preProcessRequest")
91     }
92
93     void preprocessSdncActOrDeactTnNssiRequest(DelegateExecution execution) {
94         def method = getClass().getSimpleName() + '.preprocessSdncActivateTnNssiRequest(' +
95                 'execution=' + execution.getId() + ')'
96         def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
97         logger.trace('Entered ' + method)
98
99         try {
100             String serviceInstanceId = execution.getVariable("sliceServiceInstanceId")
101             String actionType = execution.getVariable("actionType")
102
103             String sdncRequest = tnNssmfUtils.buildSDNCRequest(execution, serviceInstanceId, actionType)
104
105             execution.setVariable("TNNSSMF_SDNCRequest", sdncRequest)
106             logger.debug("Outgoing SDNCRequest is: \n" + sdncRequest)
107
108         } catch (Exception e) {
109             logger.debug("Exception Occured Processing preprocessSdncDeallocateTnNssiRequest. Exception is:\n" + e)
110             exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occured during  preProcessSDNCActivateRequest Method:\n" + e.getMessage())
111         }
112         logger.trace("COMPLETED preprocessSdncActivateTnNssiRequest Process")
113     }
114
115
116     void validateSDNCResponse(DelegateExecution execution, String response) {
117         tnNssmfUtils.validateSDNCResponse(execution, response, method)
118     }
119
120
121     void updateAAIOrchStatus(DelegateExecution execution) {
122         logger.debug("Start updateAAIOrchStatus")
123         String tnNssiId = execution.getVariable("tnNssiId")
124         String orchStatus = execution.getVariable("orchestrationStatus")
125
126         try {
127             ServiceInstance si = new ServiceInstance()
128             si.setOrchestrationStatus(orchStatus)
129             AAIResourcesClient client = getAAIClient()
130             AAIResourceUri uri = AAIUriFactory.createResourceUri(Types.SERVICE_INSTANCE.getFragment(tnNssiId))
131             client.update(uri, si)
132         } catch (BpmnError e) {
133             throw e
134         } catch (Exception ex) {
135             String msg = "Exception in CreateSliceService.updateAAIOrchStatus " + ex.getMessage()
136             logger.info(msg)
137             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
138         }
139
140         logger.debug("Finish updateAAIOrchStatus")
141     }
142
143     void prepareUpdateJobStatus(DelegateExecution execution,
144                                 String status,
145                                 String progress,
146                                 String statusDescription) {
147         String serviceId = execution.getVariable("serviceInstanceID")
148         String jobId = execution.getVariable("jobId")
149         String nsiId = execution.getVariable("nsiId")
150         String operType = execution.getVariable("actionType")
151
152
153         ResourceOperationStatus roStatus = new ResourceOperationStatus()
154         roStatus.setServiceId(serviceId)
155         roStatus.setOperationId(jobId)
156         roStatus.setResourceTemplateUUID(nsiId)
157         roStatus.setOperType(operType)
158         roStatus.setProgress(progress)
159         roStatus.setStatus(status)
160         roStatus.setStatusDescription(statusDescription)
161         requestDBUtil.prepareUpdateResourceOperationStatus(execution, roStatus)
162     }
163
164 }
165