aboutsummaryrefslogtreecommitdiffstats
path: root/bpmn/so-bpmn-infrastructure-common/src/main/groovy/org/onap/so/bpmn/infrastructure/scripts/TnAllocateNssi.groovy
blob: 9debd4011ba8e46eac06cce1ec89ba85b69660b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/*-
 * ============LICENSE_START=======================================================
 * ONAP - SO
 * ================================================================================
 * Copyright (C) 2020 Huawei Technologies Co., Ltd. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License")
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.so.bpmn.infrastructure.scripts

import com.fasterxml.jackson.core.type.TypeReference
import com.fasterxml.jackson.databind.ObjectMapper
import org.json.JSONArray
import com.google.gson.JsonArray
import com.google.gson.GsonBuilder
import com.google.gson.JsonObject
import com.google.gson.JsonParser
import groovy.json.JsonSlurper
import org.onap.so.bpmn.common.scripts.OofUtils
import com.google.gson.Gson
import org.camunda.bpm.engine.delegate.BpmnError
import org.camunda.bpm.engine.delegate.DelegateExecution
import org.onap.aai.domain.yang.ServiceInstance
import org.onap.aaiclient.client.aai.AAIResourcesClient
import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri
import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory
import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder.Types
import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
import org.onap.so.bpmn.common.scripts.ExceptionUtil
import org.onap.so.bpmn.common.scripts.RequestDBUtil
import org.onap.so.bpmn.core.UrnPropertiesReader
import org.onap.so.bpmn.core.domain.ServiceDecomposition
import org.onap.so.bpmn.core.domain.ServiceProxy
import org.onap.so.bpmn.core.json.JsonUtils
import org.onap.so.db.request.beans.ResourceOperationStatus
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.web.util.UriUtils

import static org.apache.commons.lang3.StringUtils.isBlank

class TnAllocateNssi extends AbstractServiceTaskProcessor {
    String Prefix = "TNALLOC_"

    ExceptionUtil exceptionUtil = new ExceptionUtil()
    JsonUtils jsonUtil = new JsonUtils()
    RequestDBUtil requestDBUtil = new RequestDBUtil()
    JsonSlurper jsonSlurper = new JsonSlurper()
    ObjectMapper objectMapper = new ObjectMapper()
    OofUtils oofUtils = new OofUtils()
    TnNssmfUtils tnNssmfUtils = new TnNssmfUtils()
    private static final Logger logger = LoggerFactory.getLogger(TnAllocateNssi.class)

    void preProcessRequest(DelegateExecution execution) {
        logger.debug("Start preProcessRequest")
        execution.setVariable("prefix", Prefix)
        String msg = ""

        try {
            execution.setVariable("startTime", System.currentTimeMillis())

            msg = tnNssmfUtils.getExecutionInputParams(execution)
            logger.debug("Allocate TN NSSI input parameters: " + msg)

            tnNssmfUtils.setSdncCallbackUrl(execution, true)
            logger.debug("SDNC Callback URL: " + execution.getVariable("sdncCallbackUrl"))

            String additionalPropJsonStr = execution.getVariable("sliceParams")

            String tnNssiId = jsonUtil.getJsonValue(additionalPropJsonStr, "serviceInstanceID") //for debug
            if (isBlank(tnNssiId)) {
                tnNssiId = UUID.randomUUID().toString()
            }

            String operationId = UUID.randomUUID().toString()
            execution.setVariable("operationId", operationId)

            logger.debug("Generate new TN NSSI ID:" + tnNssiId)
            tnNssiId = UriUtils.encode(tnNssiId, "UTF-8")
            execution.setVariable("sliceServiceInstanceId", tnNssiId)

            String sliceServiceInstanceName = execution.getVariable("servicename")
            execution.setVariable("sliceServiceInstanceName", sliceServiceInstanceName)

            String sst = execution.getVariable("sst")
            execution.setVariable("sst", sst)

            //additional properties
            String sliceProfile = jsonUtil.getJsonValue(additionalPropJsonStr, "sliceProfile")
            if (isBlank(sliceProfile)) {
                msg = "Input sliceProfile is null"
                logger.debug(msg)
                exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
            }
            else {
                execution.setVariable("sliceProfile", sliceProfile)
            }

            String transportSliceNetworks = jsonUtil.getJsonValue(additionalPropJsonStr, "transportSliceNetworks")
            if (isBlank(transportSliceNetworks)) {
                msg = "Input transportSliceNetworks is null"
                logger.debug(msg)
                exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
            }
            else {
                execution.setVariable("transportSliceNetworks", transportSliceNetworks)
            }
            logger.debug("transportSliceNetworks: " + transportSliceNetworks)

            String nsiInfoStr = jsonUtil.getJsonValue(additionalPropJsonStr, "nsiInfo")
            if (isBlank(nsiInfoStr)) {
                msg = "Input nsiInfo is null"
                logger.debug(msg)
                exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
            }
            else {
                execution.setVariable("nsiInfo", nsiInfoStr)
            }

            if (isBlank(tnNssmfUtils.setExecVarFromJsonIfExists(execution, additionalPropJsonStr,
                    "enableSdnc", "enableSdnc"))) {
                tnNssmfUtils.setEnableSdncConfig(execution)
            }

            if (isBlank(additionalPropJsonStr) ||
                    isBlank(tnNssmfUtils.setExecVarFromJsonIfExists(execution,
                            additionalPropJsonStr,
                            "enableOof", "enableOof"))) {
                tnNssmfUtils.setEnableOofConfig(execution)
            }

        } catch (BpmnError e) {
            throw e
        } catch (Exception ex) {
            msg = "Exception in preProcessRequest " + ex.getMessage()
            logger.debug(msg)
            exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
        }
        logger.debug("Finish preProcessRequest")
    }


    void prepareDecomposeService(DelegateExecution execution) {
        logger.debug("Start prepareDecomposeService")
        String msg = ""
        String modelUuid = execution.getVariable("modelUuid")
        if (isBlank(modelUuid)) {
            msg = "Input modelUuid is null"
            logger.debug(msg)
            exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
        }

        String modelInvariantUuid = execution.getVariable("modelInvariantUuid")
        if (isBlank(modelInvariantUuid)) {
            msg = "Input modelInvariantUuid is null"
            logger.debug(msg)
            exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
        }

        String serviceModelInfo = """{
            "modelInvariantUuid":"${modelInvariantUuid}",
            "modelUuid":"${modelUuid}",
            "modelVersion":""
             }"""
        execution.setVariable("serviceModelInfo", serviceModelInfo)

        logger.debug("Finish prepareDecomposeService")
    }

    void processDecomposition(DelegateExecution execution) {
        logger.debug("Start processDecomposition")

        ServiceDecomposition tnNsstServiceDecomposition = execution.getVariable("tnNsstServiceDecomposition")
        logger.debug("tnNsstServiceDecomposition : " + tnNsstServiceDecomposition.toString())
        //TN NSST decomposition
        String tnModelVersion = tnNsstServiceDecomposition.getModelInfo().getModelVersion()
        String tnModelName = tnNsstServiceDecomposition.getModelInfo().getModelName()

        List<ServiceProxy> serviceProxyList = tnNsstServiceDecomposition.getServiceProxy()
        List<String> nsstInfoList = new ArrayList<>()
        for (ServiceProxy serviceProxy : serviceProxyList) {
            String nsstModelUuid = serviceProxy.getModelInfo().getModelUuid()
            String nsstModelInvariantUuid = serviceProxy.getModelInfo().getModelInvariantUuid()
            String name = serviceProxy.getModelInfo().getModelName()
            String nsstServiceModelInfo = """{
            "UUID":"${nsstModelUuid}",
            "invariantUUID":"${nsstModelInvariantUuid}",
            "name":"${name}"
             }"""
            nsstInfoList.add(nsstServiceModelInfo)
        }
        int currentIndex = 0
        int maxIndex = nsstInfoList.size()
        if (maxIndex < 1) {
            String msg = "Exception in TN NSST processDecomposition. There is no NSST associated with TN NSST "
            logger.info(msg)
            //exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
        }
        else {
            execution.setVariable("tnNsstInfoList", nsstInfoList)
            execution.setVariable("tnModelVersion", tnModelVersion)
            execution.setVariable("tnModelName", tnModelName)
            execution.setVariable("currentIndex", currentIndex)
            execution.setVariable("maxIndex", maxIndex)
        }

        execution.setVariable("tnNfSliceProfile", execution.getVariable("sliceProfile"))
        execution.setVariable("tnModelName", tnModelName)
        logger.debug("End processDecomposition")
    }

    void prepareOofSelection(DelegateExecution execution) {
        logger.debug("Start prepareOofSelection")

        String urlString = UrnPropertiesReader.getVariable("mso.oof.endpoint", execution)
        logger.debug("get NSSI option OOF Url: " + urlString)
        //build oof request body

        String requestId = execution.getVariable("msoRequestId")
        String messageType = "NSSISelectionResponse"
        Map<String, Object> profileInfo = (Map<String, Object>) objectMapper.readValue(execution.getVariable("tnNfSliceProfile"), Map.class)
        String modelUuid = execution.getVariable("modelUuid")
        String modelInvariantUuid = execution.getVariable("modelInvariantUuid")
        String modelName = execution.getVariable("tnModelName")
        String timeout = UrnPropertiesReader.getVariable("mso.adapters.oof.timeout", execution);

        execution.setVariable("nssiSelection_Url", "/api/oof/selection/nssi/v1")
        execution.setVariable("nssiSelection_messageType", messageType)
        execution.setVariable("nssiSelection_correlator", requestId)
        execution.setVariable("nssiSelection_timeout", timeout)
        String oofRequest = oofUtils.buildSelectNSSIRequest(requestId, messageType, modelUuid, modelInvariantUuid, modelName, profileInfo)
        execution.setVariable("nssiSelection_oofRequest", oofRequest)

        logger.debug("Finish prepareOofSelection")
    }

    void processOofSelection(DelegateExecution execution) {
        logger.debug(Prefix + "processOofSelection method start")
        String oofResponse = execution.getVariable("nssiSelection_asyncCallbackResponse")
        String requestStatus = jsonUtil.getJsonValue(oofResponse, "requestStatus")
        if (requestStatus.equals("completed")) {
            String solutions = jsonUtil.getJsonValue(oofResponse, "solutions")
            JsonParser parser = new JsonParser()
            JsonArray solution = parser.parse(solutions) as JsonArray
            if (solution.size() >= 1) {
                JsonObject sol = solution.get(0) as JsonObject
                String tnNfNssiId = sol.get("NSSIId").getAsString()
                String invariantUuid = sol.get("invariantUUID").getAsString()
                String uuid = sol.get("UUID").getAsString()
                String nssiName = sol.get("NSSIName").getAsString()
                execution.setVariable("TnServiceInstanceId", tnNfNssiId)
                execution.setVariable("TNNFInvariantUUID", invariantUuid)
                execution.setVariable("TNNFUUID", uuid)
                execution.setVariable("servicename", nssiName)
                logger.debug("TnServiceInstanceId from OOF " + tnNfNssiId)
                execution.setVariable("isOofTnNssiSelected", true)
            }
            else {
                logger.debug("No solutions returned from OOF .. Create new TN NF NSSI")
                execution.setVariable("isOofTnNssiSelected", false)
            }
        }
        else {
            String statusMessage = jsonUtil.getJsonValue(oofResponse, "statusMessage")
            logger.error("received failed status from oof " + statusMessage)
            exceptionUtil.buildAndThrowWorkflowException(execution, 7000, "Received a failed Async Response from OOF : " + statusMessage)
        }

        logger.debug(Prefix + "processOofSelection method finished")
    }

    void prepareModifyTnNssiInputs(DelegateExecution execution) {
        logger.debug(Prefix + "prepareModifyTnNssiInputs method start")
        String additionalPropJsonStr = execution.getVariable("sliceParams")
        String sliceProfile = execution.getVariable("sliceProfile")
        String snssaiList = jsonUtil.getJsonValue(sliceProfile, "snssaiList")
        String sliceProfileId = jsonUtil.getJsonValue(sliceProfile, "sliceProfileId")
        String nsiInfo = jsonUtil.getJsonValue(additionalPropJsonStr, "nsiInfo")
        String scriptName = jsonUtil.getJsonValue(additionalPropJsonStr, "scriptName")
        String modelInvariantUuid = execution.getVariable("modelInvariantUuid")
        String modelUuid = execution.getVariable("modelUuid")
        String serviceInstanceID = execution.getVariable("TnServiceInstanceId")
        String servicename = execution.getVariable("servicename")
        String transportSliceNetworks = jsonUtil.getJsonValue(additionalPropJsonStr, "transportSliceNetworks")

        JsonObject modifySliceParams = new JsonObject()
        modifySliceParams.addProperty("nsiInfo", nsiInfo)
        modifySliceParams.addProperty("serviceInstanceID", serviceInstanceID)
        modifySliceParams.addProperty("servicename", servicename)
        modifySliceParams.addProperty("sliceProfile", sliceProfile)
        modifySliceParams.addProperty("sliceProfileId", sliceProfileId)
        modifySliceParams.addProperty("modelInvariantUuid", modelInvariantUuid)
        modifySliceParams.addProperty("modelUuid", modelUuid)
        modifySliceParams.addProperty("scriptName", scriptName)
        modifySliceParams.addProperty("snssaiList", snssaiList)
        modifySliceParams.addProperty("transportSliceNetworks", transportSliceNetworks)

        execution.setVariable("modifySliceParams", modifySliceParams.toString())

        logger.debug(Prefix + "prepareModifyTnNssiInputs method finished")
    }

    def createModifyNssiQueryJobStatus = { DelegateExecution execution ->
        logger.debug(Prefix + "createModifyNssiQueryJobStatus method start")
        JsonObject esrInfo = new JsonObject()
        esrInfo.addProperty("networkType", "tn")
        esrInfo.addProperty("vendor", "ONAP_internal")

        execution.setVariable("esrInfo", esrInfo.toString())

        JsonObject serviceInfo = new JsonObject()
        serviceInfo.addProperty("nssiId", execution.getVariable("TnServiceInstanceId") as String)
        serviceInfo.addProperty("nsiId", execution.getVariable("nsiId") as String)
        serviceInfo.addProperty("nssiName", execution.getVariable("servicename") as String)
        serviceInfo.addProperty("sST", execution.getVariable("sst") as String)
        serviceInfo.addProperty("PLMNIdList", objectMapper.writeValueAsString(execution.getVariable("pLMNIdList")))
        serviceInfo.addProperty("globalSubscriberId", execution.getVariable("globalSubscriberId") as String)
        serviceInfo.addProperty("subscriptionServiceType", execution.getVariable("subscriptionServiceType") as String)
        serviceInfo.addProperty("serviceInvariantUuid", execution.getVariable("modelInvariantUuid") as String)
        serviceInfo.addProperty("serviceUuid", execution.getVariable("modelUuid") as String)
        execution.setVariable("serviceInfo", serviceInfo.toString())
        execution.setVariable("responseId", "")
    }

    def processModifyJobStatusRsp = { DelegateExecution execution ->
        logger.debug(Prefix + "processJobStatusRsp method start")
        String jobResponse = execution.getVariable("jobResponse")
        logger.debug("Job status response " + jobResponse)
        String status = jsonUtil.getJsonValue(jobResponse, "status")
        String nssi = jsonUtil.getJsonValue(jobResponse, "nssiId")
        if (status.equalsIgnoreCase("finished")) {
            logger.debug("Job successfully completed ... proceeding with flow for nssi : " + nssi)
        }
        else {
            String statusDescription = jsonUtil.getJsonValue(jobResponse, "statusDescription")
            logger.error("received failed status from job status query for nssi : " + nssi + " with status description : " + statusDescription)
            exceptionUtil.buildAndThrowWorkflowException(execution, 7000, "received failed status from job status query for nssi : " + nssi + " with status description : " + statusDescription)
        }
    }

    void updateAAIOrchStatus(DelegateExecution execution) {
        logger.debug("Start updateAAIOrchStatus")
        String sliceServiceInstanceId = execution.getVariable("sliceServiceInstanceId")
        String orchStatus = execution.getVariable("orchestrationStatus")

        try {
            ServiceInstance si = new ServiceInstance()
            si.setOrchestrationStatus(orchStatus)
            AAIResourcesClient client = new AAIResourcesClient()
            AAIResourceUri uri = AAIUriFactory.createResourceUri(Types.SERVICE_INSTANCE.getFragment(sliceServiceInstanceId))
            client.update(uri, si)
        } catch (BpmnError e) {
            throw e
        } catch (Exception ex) {
            String msg = "Exception in CreateSliceService.updateAAIOrchStatus " + ex.getMessage()
            logger.info(msg)
            exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
        }

        logger.debug("Finish updateAAIOrchStatus")
    }


    void prepareUpdateJobStatus(DelegateExecution execution,
                                String status,
                                String progress,
                                String statusDescription) {
        String modelUuid = execution.getVariable("modelUuid")
        String ssInstanceId = execution.getVariable("sliceServiceInstanceId")
        String jobId = execution.getVariable("jobId")
        String nsiId = execution.getVariable("nsiId")

        ResourceOperationStatus roStatus = new ResourceOperationStatus()
        roStatus.setServiceId(nsiId)
        roStatus.setOperationId(jobId)
        roStatus.setResourceTemplateUUID(modelUuid)
        roStatus.setResourceInstanceID(ssInstanceId)
        roStatus.setOperType("ALLOCATE")
        roStatus.setProgress(progress)
        roStatus.setStatus(status)
        roStatus.setStatusDescription(statusDescription)
        requestDBUtil.prepareUpdateResourceOperationStatus(execution, roStatus)
    }

}