Fixes for bugs found in Transport Slicing integration
[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 public class DoActivateTnNssi extends AbstractServiceTaskProcessor {
41     String Prefix = "TNACT_"
42
43     ExceptionUtil exceptionUtil = new ExceptionUtil()
44     JsonUtils jsonUtil = new JsonUtils()
45     RequestDBUtil requestDBUtil = new RequestDBUtil()
46     TnNssmfUtils tnNssmfUtils = new TnNssmfUtils()
47     JsonSlurper jsonSlurper = new JsonSlurper()
48     ObjectMapper objectMapper = new ObjectMapper()
49     private static final Logger logger = LoggerFactory.getLogger(DoActivateTnNssi.class)
50
51
52     public void preProcessRequest(DelegateExecution execution) {
53         logger.debug("Start preProcessRequest")
54
55         execution.setVariable("startTime", System.currentTimeMillis())
56         String msg = tnNssmfUtils.getExecutionInputParams(execution)
57         logger.debug("Activate TN NSSI input parameters: " + msg)
58
59         execution.setVariable("prefix", Prefix)
60
61         tnNssmfUtils.setSdncCallbackUrl(execution, true)
62         logger.debug("SDNC Callback URL: " + execution.getVariable("sdncCallbackUrl"))
63
64         String modelInvariantUuid = execution.getVariable("modelInvariantUuid")
65         String modelUuid = execution.getVariable("modelUuid")
66         //here modelVersion is not set, we use modelUuid to decompose the service.
67         def isDebugLogEnabled = true
68         execution.setVariable("isDebugLogEnabled", isDebugLogEnabled)
69         String serviceModelInfo = """{
70             "modelInvariantUuid":"${modelInvariantUuid}",
71             "modelUuid":"${modelUuid}",
72             "modelVersion":""
73              }"""
74         execution.setVariable("serviceModelInfo", serviceModelInfo)
75
76         String sliceServiceInstanceId = execution.getVariable("serviceInstanceID")
77         execution.setVariable("sliceServiceInstanceId", sliceServiceInstanceId)
78
79         String sliceServiceInstanceName = execution.getVariable("servicename")
80         execution.setVariable("sliceServiceInstanceName", sliceServiceInstanceName)
81
82         String operationType = execution.getVariable("operationType")
83         String actionType = operationType.equals("activateInstance") ? "activate" : "deactivate"
84         execution.setVariable("actionType", actionType)
85
86         tnNssmfUtils.setEnableSdncConfig(execution)
87
88         logger.debug("Finish preProcessRequest")
89     }
90
91     void preprocessSdncActOrDeactTnNssiRequest(DelegateExecution execution) {
92         def method = getClass().getSimpleName() + '.preprocessSdncActivateTnNssiRequest(' +
93                 'execution=' + execution.getId() + ')'
94         def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
95         logger.trace('Entered ' + method)
96
97         try {
98             String serviceInstanceId = execution.getVariable("sliceServiceInstanceId")
99             String actionType = execution.getVariable("actionType")
100
101             String sdncRequest = tnNssmfUtils.buildSDNCRequest(execution, serviceInstanceId, actionType)
102
103             execution.setVariable("TNNSSMF_SDNCRequest", sdncRequest)
104             logger.debug("Outgoing SDNCRequest is: \n" + sdncRequest)
105
106         } catch (Exception e) {
107             logger.debug("Exception Occured Processing preprocessSdncDeallocateTnNssiRequest. Exception is:\n" + e)
108             exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occured during  preProcessSDNCActivateRequest Method:\n" + e.getMessage())
109         }
110         logger.trace("COMPLETED preprocessSdncActivateTnNssiRequest Process")
111     }
112
113
114     void validateSDNCResponse(DelegateExecution execution, String response) {
115         String actionType = execution.getVariable("actionType")
116         tnNssmfUtils.validateSDNCResponse(execution, response, actionType)
117     }
118
119
120     String getOrchStatusBasedOnActionType(String actionType) {
121         String res = "unknown"
122         if (actionType.equals("activate")) {
123             res = "activated"
124         } else if (actionType.equals("deactivate")) {
125             res = "deactivated"
126         } else {
127             logger.error("ERROR: getOrchStatusBasedOnActionType bad actionType= \n" + actionType)
128         }
129
130         return res
131     }
132
133     void updateAAIOrchStatus(DelegateExecution execution) {
134         logger.debug("Start updateAAIOrchStatus")
135         String tnNssiId = execution.getVariable("sliceServiceInstanceId")
136         String orchStatus = execution.getVariable("orchestrationStatus")
137
138         try {
139             ServiceInstance si = new ServiceInstance()
140             si.setOrchestrationStatus(orchStatus)
141             AAIResourcesClient client = getAAIClient()
142             AAIResourceUri uri = AAIUriFactory.createResourceUri(Types.SERVICE_INSTANCE.getFragment(tnNssiId))
143             client.update(uri, si)
144         } catch (BpmnError e) {
145             throw e
146         } catch (Exception ex) {
147             String msg = "Exception in CreateSliceService.updateAAIOrchStatus " + ex.getMessage()
148             logger.info(msg)
149             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
150         }
151
152         logger.debug("Finish updateAAIOrchStatus")
153     }
154
155     void prepareUpdateJobStatus(DelegateExecution execution,
156                                 String status,
157                                 String progress,
158                                 String statusDescription) {
159         String ssInstanceId = execution.getVariable("sliceServiceInstanceId")
160         String modelUuid = execution.getVariable("modelUuid")
161         String jobId = execution.getVariable("jobId")
162         String nsiId = execution.getVariable("nsiId")
163         String operType = execution.getVariable("actionType")
164         operType = operType.toUpperCase()
165
166
167         ResourceOperationStatus roStatus = tnNssmfUtils.buildRoStatus(modelUuid, ssInstanceId,
168                 jobId, nsiId, operType, status, progress, statusDescription)
169
170         requestDBUtil.prepareUpdateResourceOperationStatus(execution, roStatus)
171     }
172
173 }
174