aboutsummaryrefslogtreecommitdiffstats
path: root/bpmn/so-bpmn-infrastructure-common/src/main/groovy/org/onap/so/bpmn/infrastructure/scripts/DoCloudLeasedLineModify.groovy
blob: 5c9877840dd40541ded5c96eff9f6d7e2f231568 (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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
/*-
 * ============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 org.camunda.bpm.engine.delegate.BpmnError
import org.camunda.bpm.engine.delegate.DelegateExecution
import org.onap.aai.domain.yang.AllottedResource
import org.onap.aai.domain.yang.AllottedResources
import org.onap.aai.domain.yang.NetworkPolicy
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
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.json.JsonUtils
import org.onap.so.db.request.beans.ResourceOperationStatus
import org.slf4j.Logger
import org.slf4j.LoggerFactory

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

public class DoCloudLeasedLineModify extends AbstractServiceTaskProcessor {
    String Prefix = "MCLL_"

    ExceptionUtil exceptionUtil = new ExceptionUtil()
    JsonUtils jsonUtil = new JsonUtils()
    RequestDBUtil requestDBUtil = new RequestDBUtil()
    ServiceIntentUtils serviceIntentUtils = new ServiceIntentUtils()
    private static final Logger logger = LoggerFactory.getLogger(DoCloudLeasedLineModify.class)


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

        try {
            execution.setVariable("startTime", System.currentTimeMillis())
            msg = serviceIntentUtils.getExecutionInputParams(execution)
            logger.debug("Modify CLL input parameters: " + msg)

            execution.setVariable("prefix", Prefix)

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

            String additionalPropJsonStr = execution.getVariable("serviceIntentParams")
            if (isBlank(additionalPropJsonStr)) {
                msg = "ERROR: preProcessRequest: additionalPropJsonStr is null"
                logger.error(msg)
                exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
            }

            String cllId = execution.getVariable("serviceInstanceID")
            if (isBlank(cllId)) {
                msg = "ERROR: cllId is null"
                logger.debug(msg)
                exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
            }
            execution.setVariable("cllId", cllId)

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

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

            String modelInvariantUuid = execution.getVariable("modelInvariantUuid")
            String modelUuid = execution.getVariable("modelUuid")
            if (isEmpty(modelUuid)) {
                modelUuid = serviceIntentUtils.getModelUuidFromServiceInstance(execution.getVariable("serviceInstanceID"))
            }

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


            serviceIntentUtils.setExecVarFromJsonStr(execution, additionalPropJsonStr,
                    "transportNetworks", "transportNetworks", true)
            logger.debug("transportNetworks: " + execution.getVariable("transportNetworks"))

            if (isBlank(serviceIntentUtils.setExecVarFromJsonIfExists(execution, additionalPropJsonStr,
                    "enableSdnc", "enableSdnc"))) {
                serviceIntentUtils.setEnableSdncConfig(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 deleteServiceInstance(DelegateExecution execution) {
        try {
            AAIResourcesClient client = getAAIClient()
            AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer(execution.getVariable("globalSubscriberId")).serviceSubscription(execution.getVariable("subscriptionServiceType")).serviceInstance(execution.getVariable("serviceInstanceID")))
            client.delete(uri)
        } catch (BpmnError e) {
            throw e
        } catch (Exception ex) {
            String msg = "Exception in DoDeallocateTnNssi.deleteServiceInstance. " + ex.getMessage()
            logger.info(msg)
            exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
        }
    }


    void getExistingServiceInstance(DelegateExecution execution) {
        String serviceInstanceId = execution.getVariable("cllId")

        AAIResourcesClient resourceClient = getAAIClient()
        AAIResourceUri ssServiceuri = AAIUriFactory.createResourceUri(Types.SERVICE_INSTANCE.getFragment(serviceInstanceId))

        try {
            Optional<ServiceInstance> ssOpt = resourceClient.get(ServiceInstance.class, ssServiceuri)
            if (ssOpt.isPresent()) {
                ServiceInstance ss = ssOpt.get()
                AllottedResources ars = serviceIntentUtils.getAllottedResourcesFromAai(execution, serviceInstanceId, true)
                if (ars != null) {
                    List<AllottedResource> arList = ars.getAllottedResource()
                    List<String> arIdList = new ArrayList<>()
                    Map<String, String> policyMap = new HashMap<>()
                    Map<String, List<String>> logicalLinksMap = new HashMap<>()
                    for (AllottedResource ar : arList) {
                        String arId = ar.getId()
                        arIdList.add(arId)
                        String policyId = serviceIntentUtils.getPolicyIdFromAr(execution, serviceInstanceId, arId, true)
                        policyMap.put(arId, policyId)
                        List<String> logicalLinkList = serviceIntentUtils.getLogicalLinkNamesFromAr(execution,
                                serviceInstanceId, arId, true)
                        logicalLinksMap.put(arId, logicalLinkList)
                    }
                    execution.setVariable("arIdList", arIdList)
                    execution.setVariable("arPolicyMap", policyMap)
                    execution.setVariable("arLogicalLinkMap", logicalLinksMap)
                } else {
                    logger.error("ERROR: getExistingServiceInstance: getAllottedResources() returned null. ss=" + ss
                            .toString())
                }
            } else {
                exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Service instance was not found in aai to " +
                        "associate allotted resource for service :" + serviceInstanceId)
            }
        } catch (BpmnError e) {
            throw e;
        } catch (Exception ex) {
            String msg = "Exception in getExistingServiceInstance. " + ex.getMessage()
            logger.debug(msg)
            exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
        }
    }

    void updateServiceInstanceInAAI(DelegateExecution execution) {
        getExistingServiceInstance(execution)
        updateTsciNetworks(execution)
    }

    void updateServiceInstance(DelegateExecution execution) {
        String cllId = execution.getVariable("cllId")
        try {
            ServiceInstance ss = new ServiceInstance()
            //ss.setServiceInstanceId(cllId)
            String serviceStatus = "modified"
            ss.setOrchestrationStatus(serviceStatus)
            ss.setEnvironmentContext("tn")
            AAIResourcesClient client = getAAIClient()
            AAIResourceUri uri = AAIUriFactory.createResourceUri(
                    AAIFluentTypeBuilder.business()
                            .customer(execution.getVariable("globalSubscriberId"))
                            .serviceSubscription(execution.getVariable("subscriptionServiceType"))
                            .serviceInstance(cllId))
            client.update(uri, ss)
        } catch (BpmnError e) {
            throw e
        } catch (Exception ex) {
            String msg = "Exception in DoModifyCllInstance.updateServiceInstance. " + ex.getMessage()
            logger.info(msg)
            exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
        }
    }


    String getValidArId(DelegateExecution execution, String arIdStr) {
        List<String> arIdList = execution.getVariable("arIdList")
        /*
         * If arId is not specified by the caller, then we assume the caller
         * wants to modify the first network (i.e., allotted resource) in the TSCi tree.
         */
        String arId = isBlank(arIdStr) ? arIdList.get(0) : arIdStr

        return arId
    }

    void updateLogicalLinksInAr(DelegateExecution execution, String arId, String linkArrayJsonStr) {
        try {
            String serviceInstanceId = execution.getVariable('cllId')

            /*
             * Each TSCi connection-link in linkArrayJsonStr is considered as an "ADD" new
             * link to allotted-resource. So, if the link already exists under AR, then do
             * nothing. Otherwise, create logical-link.
             */
            List<String> linkStrList = jsonUtil.StringArrayToList(linkArrayJsonStr)
            for (String linkStr : linkStrList) {
                if (logicalLinkExists(execution, arId, linkStr)) {
                    continue
                }

                createLogicalLinkForAllocatedResource(execution, linkStr, serviceInstanceId, arId)
            }
        } catch (BpmnError e) {
            throw e
        } catch (Exception ex) {
            exceptionUtil.buildAndThrowWorkflowException(execution, 7000,
                    "Exception in updateLogicalLinksInAr" + ex.getMessage())
        }
    }

    void updateLogicalLinksInNetwork(DelegateExecution execution, String networkJsonStr) {
        try {
            String arId = getValidArId(execution, jsonUtil.getJsonValue(networkJsonStr, "id"))
            String linkArrayStr = jsonUtil.getJsonValue(networkJsonStr, "connectionLinks")
            updateLogicalLinksInAr(execution, arId, linkArrayStr)
        } catch (BpmnError e) {
            throw e
        } catch (Exception ex) {
            String msg = String.format("ERROR: updateLogicalLinksInNetwork: exception: %s", ex.getMessage())
            logger.error(msg)
            exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg);
        }
    }

    void updateTsciNetworks(DelegateExecution execution) {
        try {
            List<String> networkStrList = jsonUtil.StringArrayToList(execution.getVariable("transportNetworks"))
            for (String networkStr : networkStrList) {
                updateLogicalLinksInNetwork(execution, networkStr)
                updateNetworkPolicy(execution, networkStr)
            }

        } catch (BpmnError e) {
            throw e
        } catch (Exception ex) {
            exceptionUtil.buildAndThrowWorkflowException(execution, 7000,
                    "Exception in updateTsciNetworks" + ex.getMessage())
        }
    }

    int getMaxBwFromNetworkJsonStr(DelegateExecution execution, String networkJsonStr) {
        int maxBw = 0
        try {
            if (isBlank(networkJsonStr)) {
                String msg = "ERROR: getMaxBw: networkJsonStr is null"
                logger.error(msg)
                exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
            }
            String slaStr = jsonUtil.getJsonValue(networkJsonStr, "sla")
            if (isBlank(slaStr)) {
                String msg = "ERROR: getMaxBw: slaStr is null"
                logger.error(msg)
                exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
            }

            String bwStr = jsonUtil.getJsonValue(slaStr, "maxBandwidth")
            if (isNotBlank(bwStr)) {
                maxBw = Integer.parseInt(bwStr)
            } else {
                logger.error("ERROR: getMaxBw: maxBandwidth is null")
            }
        } catch (BpmnError e) {
            throw e
        } catch (Exception ex) {
            exceptionUtil.buildAndThrowWorkflowException(execution, 7000,
                    "Exception in getMaxBw" + ex.getMessage())
        }

        return maxBw
    }

    void updatePolicyMaxBandwidthInAAI(DelegateExecution execution, String policyId, int maxBw) {
        try {
            NetworkPolicy networkPolicy = new NetworkPolicy()
            networkPolicy.setMaxBandwidth(maxBw)
            AAIResourceUri networkPolicyUri =
                    AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.network().networkPolicy(policyId))
            getAAIClient().update(networkPolicyUri, networkPolicy)
        } catch (BpmnError e) {
            throw e
        } catch (Exception ex) {
            String msg = "Exception in DoModifyCllInstance.updatePolicyMaxBandwidthInAAI. " + ex.getMessage()
            logger.info(msg)
            exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
        }
    }

    void updateNetworkPolicy(DelegateExecution execution, String networkJsonStr) {
        try {
            int maxBw = getMaxBwFromNetworkJsonStr(execution, networkJsonStr)

            String arId = getValidArId(execution, jsonUtil.getJsonValue(networkJsonStr, "id"))
            Map<String, String> policyMap = execution.getVariable("arPolicyMap")
            String policyId = policyMap.get(arId)
            if (isBlank(policyId)) {
                String msg = String.format("ERROR: updateNetworkPolicy: policyId not found. arId=%s", arId)
                logger.error(msg)
                exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
            }

            updatePolicyMaxBandwidthInAAI(execution, policyId, maxBw)

        } catch (BpmnError e) {
            throw e
        } catch (Exception ex) {
            String msg = String.format("ERROR: updateNetworkPolicy: exception: %s", ex.getMessage())
            logger.error(msg)
            exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg);
        }
    }


    void createLogicalLinkForAllocatedResource(DelegateExecution execution,
                                               String linkJsonStr, String cllId,
                                               String allottedResourceId) {
        try {
            AAIResourceUri allottedResourceUri = serviceIntentUtils.buildAllottedResourceUri(execution,
                    cllId, allottedResourceId)

            if (!getAAIClient().exists(allottedResourceUri)) {
                logger.info("ERROR: createLogicalLinksForAllocatedResource: allottedResource not exist: uri={}",
                        allottedResourceUri)
                return
            }

            String linkId = jsonUtil.getJsonValue(linkJsonStr, "id")
            if (isBlank(linkId)) {
                linkId = "cll-" + UUID.randomUUID().toString()
            }
            logger.debug("createLogicalLinkForAllocatedResource: linkId=" + linkId)

            String epA = jsonUtil.getJsonValue(linkJsonStr, "transportEndpointA")
            String epB = jsonUtil.getJsonValue(linkJsonStr, "transportEndpointB")
            String modelInvariantId = execution.getVariable("modelInvariantUuid")
            String modelVersionId = execution.getVariable("modelUuid")

            org.onap.aai.domain.yang.LogicalLink resource = new org.onap.aai.domain.yang.LogicalLink()
            resource.setLinkId(linkId)
            resource.setLinkName(epA)
            resource.setLinkName2(epB)
            resource.setLinkType("TsciConnectionLink")
            resource.setInMaint(false)

            //epA is link-name
            AAIResourceUri logicalLinkUri =
                    AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.network().logicalLink(epA))
            getAAIClient().create(logicalLinkUri, resource)

            serviceIntentUtils.attachLogicalLinkToAllottedResource(execution, serviceIntentUtils.AAI_VERSION,
                    allottedResourceUri, epA);
        } catch (BpmnError e) {
            throw e
        } catch (Exception ex) {
            String msg = "Exception in DoModifyCllInstance.createLogicalLinksForAllocatedResource: " + ex.getMessage()
            logger.error(msg)
            exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
        }
    }


    void preprocessSdncModifyCllRequest(DelegateExecution execution) {
        def method = getClass().getSimpleName() + '.preprocessSdncModifyCllRequest(' +
                'execution=' + execution.getId() + ')'
        def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
        logger.trace('Entered ' + method)

        try {
            String serviceInstanceId = execution.getVariable("cllId")

            String sdncRequest = serviceIntentUtils.buildSDNCRequest(execution, serviceInstanceId, "update")

            execution.setVariable("CLL_SDNCRequest", sdncRequest)
            logger.debug("Outgoing SDNCRequest is: \n" + sdncRequest)

        } catch (BpmnError e) {
            throw e
        } catch (Exception e) {
            logger.debug("Exception Occurred Processing preprocessSdncModifyCllRequest. Exception is:\n" + e)
            exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occurred during  preProcessSDNCActivateRequest Method:\n" + e.getMessage())
        }
        logger.trace("COMPLETED preprocessSdncModifyCllRequest Process")
    }


    void validateSDNCResponse(DelegateExecution execution, String response, String method) {
        serviceIntentUtils.validateSDNCResponse(execution, response, method)
    }


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

        try {
            ServiceInstance si = new ServiceInstance()
            si.setOrchestrationStatus(orchStatus)
            AAIResourcesClient client = getAAIClient()
            AAIResourceUri uri = AAIUriFactory.createResourceUri(Types.SERVICE_INSTANCE.getFragment(cllId))
            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 cllId = execution.getVariable("cllId")
        String modelUuid = execution.getVariable("modelUuid")
        String jobId = execution.getVariable("jobId")
        String nsiId = cllId
        String operType = "MODIFY"

        ResourceOperationStatus roStatus = serviceIntentUtils.buildRoStatus(modelUuid, cllId,
                jobId, nsiId, operType, status, progress, statusDescription)

        logger.debug("prepareUpdateJobStatus: roStatus={}", roStatus)
        requestDBUtil.prepareUpdateResourceOperationStatus(execution, roStatus)
    }

    boolean logicalLinkExists(DelegateExecution execution, String arIdStr, String linkJsonStr) {
        if (isBlank(arIdStr)) {
            logger.error("ERROR: logicalLinkExists: arIdStr is empty")
            return false
        }
        if (isBlank(linkJsonStr)) {
            logger.error("ERROR: logicalLinkExists: linkJsonStr is empty")
            return false
        }

        Map<String, List<String>> logicalLinksMap = execution.getVariable("arLogicalLinkMap")
        if (logicalLinksMap == null) {
            logger.error("ERROR: logicalLinkExists: logicalLinksMap is null")
            return false
        }

        List<String> logicalLinkNameList = logicalLinksMap.get(arIdStr)
        if (logicalLinksMap == null) {
            logger.error("ERROR: logicalLinkExists: logicalLinkNameList is null. arIdStr=" + arIdStr)
            return false
        }

        String linkName = jsonUtil.getJsonValue(linkJsonStr, "transportEndpointA")
        if (isBlank(linkName)) {
            logger.error("ERROR: logicalLinkExists: epA is empty")
            return false
        }

        return logicalLinkNameList.contains(linkName)
    }
}