aboutsummaryrefslogtreecommitdiffstats
path: root/bpmn/so-bpmn-infrastructure-common/src/main/groovy/org/onap/so/bpmn/infrastructure/scripts/DoCreateTnNssiInstance.groovy
blob: d2ba28256ff5ae95c629bccaa98f13f38ba51e17 (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
/*-
 * ============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.NetworkPolicy
import org.onap.aai.domain.yang.SliceProfile
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.so.bpmn.common.scripts.AbstractServiceTaskProcessor
import org.onap.so.bpmn.common.scripts.ExceptionUtil
import org.onap.so.bpmn.core.json.JsonUtils
import org.slf4j.Logger
import org.slf4j.LoggerFactory

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

class DoCreateTnNssiInstance extends AbstractServiceTaskProcessor {

    private static final Logger logger = LoggerFactory.getLogger(DoCreateTnNssiInstance.class);
    JsonUtils jsonUtil = new JsonUtils()
    TnNssmfUtils tnNssmfUtils = new TnNssmfUtils()
    ExceptionUtil exceptionUtil = new ExceptionUtil()
    String Prefix = "DCTN_"

    void preProcessRequest(DelegateExecution execution) {
        String msg = ""
        logger.trace("Enter preProcessRequest()")

        execution.setVariable("prefix", Prefix)

        String modelInvariantUuid = execution.getVariable("modelInvariantUuid")
        String modelUuid = execution.getVariable("modelUuid")
        //here modelVersion is not set, we use modelUuid to decompose the service.
        def isDebugLogEnabled = true
        execution.setVariable("isDebugLogEnabled", isDebugLogEnabled)
        String serviceModelInfo = """{
            "modelInvariantUuid":"${modelInvariantUuid}",
            "modelUuid":"${modelUuid}",
            "modelVersion":""
             }"""
        execution.setVariable("serviceModelInfo", serviceModelInfo)

        if (isBlank(execution.getVariable("enableSdnc"))) {
            tnNssmfUtils.setEnableSdncConfig(execution)
        }

        logger.trace("Exit preProcessRequest")
    }


    void createSliceProfile(DelegateExecution execution) {

        String ssInstanceId = execution.getVariable("sliceServiceInstanceId")
        String sliceProfileStr = execution.getVariable("sliceProfile")
        String sliceProfileId = UUID.randomUUID().toString()
        SliceProfile sliceProfile = new SliceProfile();
        sliceProfile.setProfileId(sliceProfileId)
        sliceProfile.setLatency(Integer.parseInt(jsonUtil.getJsonValue(sliceProfileStr, "latency")))
        sliceProfile.setResourceSharingLevel(jsonUtil.getJsonValue(sliceProfileStr, "resourceSharingLevel"))
        //sliceProfile.setSNssai(tnNssmfUtils.getFirstSnssaiFromSliceProfile(sliceProfileStr))

        sliceProfile.setMaxBandwidth(Integer.parseInt(jsonUtil.getJsonValue(sliceProfileStr, "maxBandwidth")))

        //sliceProfile.setReliability(new Object())
        try {
            AAIResourcesClient client = getAAIClient()
            AAIResourceUri uri =
                    AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business()
                            .customer(execution.getVariable("globalSubscriberId"))
                            .serviceSubscription(execution.getVariable("subscriptionServiceType"))
                            .serviceInstance(ssInstanceId)
                            .sliceProfile(sliceProfileId))
            client.create(uri, sliceProfile)

        } catch (BpmnError e) {
            throw e
        } catch (Exception ex) {
            String msg = "Exception in DoCreateSliceServiceInstance.instantiateSliceService. " + ex.getMessage()
            logger.info(msg)
            exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
        }
    }


    void createServiceInstance(DelegateExecution execution) {

        String serviceRole = "nssi"
        String serviceType = execution.getVariable("subscriptionServiceType")
        String ssInstanceId = execution.getVariable("sliceServiceInstanceId")
        String sliceProfileStr = execution.getVariable("sliceProfile")
        String sst = execution.getVariable("sst")
        try {
            if (sliceProfileStr == null || sliceProfileStr.isEmpty()) {
                String msg = "ERROR: createServiceInstance: sliceProfile is null"
                logger.error(msg)
                exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
            }

            org.onap.aai.domain.yang.ServiceInstance ss = new org.onap.aai.domain.yang.ServiceInstance()
            ss.setServiceInstanceId(ssInstanceId)
            String sliceInstanceName = execution.getVariable("sliceServiceInstanceName")
            if (isBlank(sliceInstanceName)) {
                logger.error("ERROR: createServiceInstance: sliceInstanceName is null")
                sliceInstanceName = ssInstanceId
            }
            ss.setServiceInstanceName(sliceInstanceName)
            ss.setServiceType(sst)
            String serviceStatus = "deactivated"
            ss.setOrchestrationStatus(serviceStatus)
            String modelInvariantUuid = execution.getVariable("modelInvariantUuid")
            String modelUuid = execution.getVariable("modelUuid")
            ss.setModelInvariantId(modelInvariantUuid)
            ss.setModelVersionId(modelUuid)
            String serviceInstanceLocationId = tnNssmfUtils.getFirstPlmnIdFromSliceProfile(sliceProfileStr)
            ss.setServiceInstanceLocationId(serviceInstanceLocationId)
            String snssai = tnNssmfUtils.getFirstSnssaiFromSliceProfile(sliceProfileStr)
            //ss.setEnvironmentContext(snssai)
            ss.setEnvironmentContext("tn")
            ss.setServiceRole(serviceRole)

            String domainTypeStr = jsonUtil.getJsonValue(sliceProfileStr, "domainType")
            if (isNotBlank(domainTypeStr)) {
                ss.setWorkloadContext(domainTypeStr)
            }

            String resourceSharingLevel = jsonUtil.getJsonValue(sliceProfileStr, "resourceSharingLevel")
            if (isNotBlank(resourceSharingLevel)) {
                ss.setServiceFunction(resourceSharingLevel)
            }

            AAIResourcesClient client = getAAIClient()
            AAIResourceUri uri =
                    AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business()
                            .customer(execution.getVariable("globalSubscriberId"))
                            .serviceSubscription(execution.getVariable("subscriptionServiceType"))
                            .serviceInstance(ssInstanceId))
            client.create(uri, ss)
        } catch (BpmnError e) {
            throw e
        } catch (Exception ex) {
            String msg = "Exception in DoCreateTnNssiInstance.createServiceInstance: " + ex.getMessage()
            logger.error(msg)
            exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
        }
    }


    void createAllottedResource(DelegateExecution execution) {
        String ssInstanceId = execution.getVariable('sliceServiceInstanceId')

        try {
            List<String> networkStrList = jsonUtil.StringArrayToList(execution.getVariable("transportSliceNetworks"))

            for (String networkStr : networkStrList) {
                String allottedResourceId = UUID.randomUUID().toString()
                AAIResourceUri allottedResourceUri =
                        AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business()
                                .customer(execution.getVariable("globalSubscriberId"))
                                .serviceSubscription(execution.getVariable("subscriptionServiceType"))
                                .serviceInstance(execution.getVariable("sliceServiceInstanceId"))
                                .allottedResource(allottedResourceId))
                execution.setVariable("allottedResourceUri", allottedResourceUri)
                String modelInvariantId = execution.getVariable("modelInvariantUuid")
                String modelVersionId = execution.getVariable("modelUuid")

                org.onap.aai.domain.yang.AllottedResource resource = new org.onap.aai.domain.yang.AllottedResource()
                resource.setId(allottedResourceId)
                resource.setType("TsciNetwork")
                resource.setAllottedResourceName("network_" + execution.getVariable("sliceServiceInstanceName"))
                getAAIClient().create(allottedResourceUri, resource)

                createNetworkPolicyForAllocatedResource(execution, ssInstanceId, allottedResourceId)

                String linkArrayStr = jsonUtil.getJsonValue(networkStr, "connectionLinks")
                createLogicalLinksForAllocatedResource(execution, linkArrayStr, ssInstanceId, allottedResourceId)
            }
        } catch (BpmnError e) {
            throw e
        } catch (Exception ex) {
            String msg = "Exception in DoCreateTnNssiInstance.createAllottedResource: " + ex.getMessage()
            logger.error(msg)
            exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
        }
    }

    void createNetworkPolicy(DelegateExecution execution, String ssInstanceId, String networkPolicyId) {
        try {

            String sliceProfileStr = execution.getVariable("sliceProfile")
            if (sliceProfileStr == null || sliceProfileStr.isEmpty()) {
                String msg = "ERROR: createNetworkPolicy: sliceProfile is null"
                logger.error(msg)
                exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
            }

            NetworkPolicy networkPolicy = new NetworkPolicy();
            networkPolicy.setNetworkPolicyId(networkPolicyId)
            networkPolicy.setName("TSCi policy")
            networkPolicy.setType("SLA")
            networkPolicy.setNetworkPolicyFqdn(ssInstanceId)

            String latencyStr = jsonUtil.getJsonValue(sliceProfileStr, "latency")
            if (latencyStr != null && !latencyStr.isEmpty()) {
                networkPolicy.setLatency(Integer.parseInt(latencyStr))
            }

            String bwStr = jsonUtil.getJsonValue(sliceProfileStr, "maxBandwidth")
            if (bwStr != null && !bwStr.isEmpty()) {
                networkPolicy.setMaxBandwidth(Integer.parseInt(bwStr))
            } else {
                logger.debug("ERROR: createNetworkPolicy: maxBandwidth is null")
            }

            //networkPolicy.setReliability(new Object())

            AAIResourceUri networkPolicyUri =
                    AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.network().networkPolicy(networkPolicyId))
            getAAIClient().create(networkPolicyUri, networkPolicy)

        } catch (BpmnError e) {
            throw e
        } catch (Exception ex) {
            String msg = "Exception in DoCreateSliceServiceInstance.instantiateSliceService. " + ex.getMessage()
            logger.error(msg)
            exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
        }
    }

    void createNetworkPolicyForAllocatedResource(DelegateExecution execution,
                                                 String ssInstanceId,
                                                 String allottedResourceId) {
        try {
            AAIResourceUri allottedResourceUri =
                    AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business()
                            .customer(execution.getVariable("globalSubscriberId"))
                            .serviceSubscription(execution.getVariable("subscriptionServiceType"))
                            .serviceInstance(ssInstanceId)
                            .allottedResource(allottedResourceId))

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

            String networkPolicyId = UUID.randomUUID().toString()
            createNetworkPolicy(execution, ssInstanceId, networkPolicyId)

            tnNssmfUtils.attachNetworkPolicyToAllottedResource(execution, tnNssmfUtils.AAI_VERSION,
                    allottedResourceUri,
                    networkPolicyId);

        } catch (BpmnError e) {
            throw e
        } catch (Exception ex) {
            String msg = "Exception in DoCreateSliceServiceInstance.instantiateSliceService. " + ex.getMessage()
            logger.error(msg)
            exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
        }
    }

    void createLogicalLinksForAllocatedResource(DelegateExecution execution,
                                                String linkArrayStr, String ssInstanceId,
                                                String allottedResourceId) {
        try {
            AAIResourceUri allottedResourceUri =
                    AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business()
                            .customer(execution.getVariable("globalSubscriberId"))
                            .serviceSubscription(execution.getVariable("subscriptionServiceType"))
                            .serviceInstance(ssInstanceId)
                            .allottedResource(allottedResourceId))

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

            List<String> linkStrList = jsonUtil.StringArrayToList(linkArrayStr)

            for (String linkStr : linkStrList) {
                String linkId = jsonUtil.getJsonValue(linkStr, "name")
                if (isBlank(linkId)) {
                    linkId = "tn-nssmf-" + UUID.randomUUID().toString()
                }
                logger.debug("createLogicalLinksForAllocatedResource: linkId=" + linkId)

                String epA = jsonUtil.getJsonValue(linkStr, "transportEndpointA")
                String epB = jsonUtil.getJsonValue(linkStr, "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)

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

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

        logger.trace("STARTED preProcessSDNCActivateRequest Process")
        try {
            String serviceInstanceId = execution.getVariable("sliceServiceInstanceId")

            String createSDNCRequest = tnNssmfUtils.buildSDNCRequest(execution, serviceInstanceId, "create")

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

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


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