Transport Slicing Fixes
[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.AAIResourcesClient
29 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri
30 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory
31 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder.Types
32 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
33 import org.onap.so.bpmn.common.scripts.ExceptionUtil
34 import org.onap.so.bpmn.common.scripts.RequestDBUtil
35 import org.onap.so.bpmn.core.json.JsonUtils
36 import org.onap.so.db.request.beans.ResourceOperationStatus
37 import org.slf4j.Logger
38 import org.slf4j.LoggerFactory
39
40 import static org.apache.commons.lang3.StringUtils.isBlank
41 import static org.apache.commons.lang3.StringUtils.isEmpty
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         if (isEmpty(modelUuid)) {
70             modelUuid = tnNssmfUtils.getModelUuidFromServiceInstance(execution.getVariable("serviceInstanceID"))
71         }
72         def isDebugLogEnabled = true
73         execution.setVariable("isDebugLogEnabled", isDebugLogEnabled)
74         String serviceModelInfo = """{
75             "modelInvariantUuid":"${modelInvariantUuid}",
76             "modelUuid":"${modelUuid}",
77             "modelVersion":""
78              }"""
79         execution.setVariable("serviceModelInfo", serviceModelInfo)
80
81         String sliceServiceInstanceId = execution.getVariable("serviceInstanceID")
82         execution.setVariable("sliceServiceInstanceId", sliceServiceInstanceId)
83
84         String sliceServiceInstanceName = execution.getVariable("servicename")
85         execution.setVariable("sliceServiceInstanceName", sliceServiceInstanceName)
86
87         String operationType = execution.getVariable("operationType")
88         String actionType = operationType.equals("activateInstance") ? "activate" : "deactivate"
89         execution.setVariable("actionType", actionType)
90
91         String additionalPropJsonStr = execution.getVariable("sliceParams")
92         if (isBlank(additionalPropJsonStr) ||
93                 isBlank(tnNssmfUtils.setExecVarFromJsonIfExists(execution,
94                         additionalPropJsonStr,
95                         "enableSdnc", "enableSdnc"))) {
96             tnNssmfUtils.setEnableSdncConfig(execution)
97         }
98
99         logger.debug("Finish preProcessRequest")
100     }
101
102     void preprocessSdncActOrDeactTnNssiRequest(DelegateExecution execution) {
103         def method = getClass().getSimpleName() + '.preprocessSdncActivateTnNssiRequest(' +
104                 'execution=' + execution.getId() + ')'
105         def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
106         logger.trace('Entered ' + method)
107
108         try {
109             String serviceInstanceId = execution.getVariable("sliceServiceInstanceId")
110             String actionType = execution.getVariable("actionType")
111
112             String sdncRequest = tnNssmfUtils.buildSDNCRequest(execution, serviceInstanceId, actionType)
113
114             execution.setVariable("TNNSSMF_SDNCRequest", sdncRequest)
115             logger.debug("Outgoing SDNCRequest is: \n" + sdncRequest)
116
117         } catch (Exception e) {
118             logger.debug("Exception Occured Processing preprocessSdncDeallocateTnNssiRequest. Exception is:\n" + e)
119             exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occured during  preProcessSDNCActivateRequest Method:\n" + e.getMessage())
120         }
121         logger.trace("COMPLETED preprocessSdncActivateTnNssiRequest Process")
122     }
123
124
125     void validateSDNCResponse(DelegateExecution execution, String response) {
126         String actionType = execution.getVariable("actionType")
127         tnNssmfUtils.validateSDNCResponse(execution, response, actionType)
128     }
129
130
131     String getOrchStatusBasedOnActionType(String actionType) {
132         String res = "unknown"
133         if (actionType.equals("activate")) {
134             res = "activated"
135         } else if (actionType.equals("deactivate")) {
136             res = "deactivated"
137         } else {
138             logger.error("ERROR: getOrchStatusBasedOnActionType bad actionType= \n" + actionType)
139         }
140
141         return res
142     }
143
144     void updateAAIOrchStatus(DelegateExecution execution) {
145         logger.debug("Start updateAAIOrchStatus")
146         String tnNssiId = execution.getVariable("sliceServiceInstanceId")
147         String orchStatus = execution.getVariable("orchestrationStatus")
148
149         try {
150             ServiceInstance si = new ServiceInstance()
151             si.setOrchestrationStatus(orchStatus)
152             AAIResourcesClient client = getAAIClient()
153             AAIResourceUri uri = AAIUriFactory.createResourceUri(Types.SERVICE_INSTANCE.getFragment(tnNssiId))
154             client.update(uri, si)
155         } catch (BpmnError e) {
156             throw e
157         } catch (Exception ex) {
158             String msg = "Exception in CreateSliceService.updateAAIOrchStatus " + ex.getMessage()
159             logger.info(msg)
160             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
161         }
162
163         logger.debug("Finish updateAAIOrchStatus")
164     }
165
166     void prepareUpdateJobStatus(DelegateExecution execution,
167                                 String status,
168                                 String progress,
169                                 String statusDescription) {
170         String ssInstanceId = execution.getVariable("sliceServiceInstanceId")
171         String modelUuid = execution.getVariable("modelUuid")
172         String jobId = execution.getVariable("jobId")
173         String nsiId = execution.getVariable("nsiId")
174         String operType = execution.getVariable("actionType")
175         operType = operType.toUpperCase()
176
177
178         ResourceOperationStatus roStatus = tnNssmfUtils.buildRoStatus(modelUuid, ssInstanceId,
179                 jobId, nsiId, operType, status, progress, statusDescription)
180
181         requestDBUtil.prepareUpdateResourceOperationStatus(execution, roStatus)
182     }
183
184 }
185