aboutsummaryrefslogtreecommitdiffstats
path: root/bpmn/so-bpmn-infrastructure-common/src/main/groovy/org/onap/so/bpmn/infrastructure/scripts/DoModifyCoreNSSI.groovy
blob: ff16184f0296acaa36c5bd081c3cc78e8a621ebd (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
/*-
 * ============LICENSE_START=======================================================
 * ONAP - SO
 * ================================================================================
 * Copyright (C) 2020  Telecom Italia
 * ================================================================================
 * 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.databind.ObjectMapper
import org.camunda.bpm.engine.delegate.DelegateExecution
import org.onap.aai.domain.yang.v19.AllottedResource
import org.onap.aai.domain.yang.v19.AllottedResources
import org.onap.aai.domain.yang.v19.ServiceInstance
import org.onap.aai.domain.yang.v19.SliceProfile
import org.onap.aai.domain.yang.v19.SliceProfiles
import org.onap.aaiclient.client.aai.AAIResourcesClient
import org.onap.aaiclient.client.aai.entities.AAIEdgeLabel
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.ExceptionUtil
import org.onap.so.bpmn.common.scripts.MsoUtils
import org.onap.so.bpmn.common.scripts.RequestDBUtil
import org.onap.so.bpmn.core.json.JsonUtils
import org.slf4j.Logger
import org.slf4j.LoggerFactory

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

class DoModifyCoreNSSI extends DoCommonCoreNSSI {

    private final String PREFIX ="DoModifyCoreNSSI"
    private final String ACTION = "Modify"

    private ExceptionUtil exceptionUtil = new ExceptionUtil()
    private RequestDBUtil requestDBUtil = new RequestDBUtil()
    private MsoUtils utils = new MsoUtils()
    private JsonUtils jsonUtil = new JsonUtils()

    private static final Logger LOGGER = LoggerFactory.getLogger( DoModifyCoreNSSI.class)


    /**
     * Prepares Slice Profile
     * @param execution
     * @return SLice Profile
     */
    SliceProfile prepareSliceProfile(DelegateExecution execution) {
        def currentNSSI = execution.getVariable("currentNSSI")

        String sliceProfileID = currentNSSI['sliceProfileId']
        Map<String,Object> sliceProfileMap = new ObjectMapper().readValue(currentNSSI['sliceProfile'], Map.class)

        SliceProfile sliceProfile = new SliceProfile()
        sliceProfile.setServiceAreaDimension("")
        sliceProfile.setPayloadSize(0)
        sliceProfile.setJitter(0)
        sliceProfile.setSurvivalTime(0)
        sliceProfile.setExpDataRate(0)
        sliceProfile.setTrafficDensity(0)
        sliceProfile.setConnDensity(0)
        sliceProfile.setSNssai(currentNSSI['S-NSSAI'])

        if(!isBlank(sliceProfileMap.get("expDataRateUL"))) {
            sliceProfile.setExpDataRateUL(Integer.parseInt(sliceProfileMap.get("expDataRateUL").toString()))
        }

        if(!isBlank(sliceProfileMap.get("expDataRateDL"))) {
            sliceProfile.setExpDataRateDL(Integer.parseInt(sliceProfileMap.get("expDataRateDL").toString()))
        }

        if(!isBlank(sliceProfileMap.get("activityFactor"))) {
            sliceProfile.setActivityFactor(Integer.parseInt(sliceProfileMap.get("activityFactor").toString()))
        }

        if(!isBlank(sliceProfileMap.get("resourceSharingLevel"))) {
            sliceProfile.setResourceSharingLevel(sliceProfileMap.get("resourceSharingLevel").toString())
        }

        if(!isBlank(sliceProfileMap.get("uEMobilityLevel"))) {
            sliceProfile.setUeMobilityLevel(sliceProfileMap.get("uEMobilityLevel").toString())
        }

        if(!isBlank(sliceProfileMap.get("coverageAreaTAList"))) {
            sliceProfile.setCoverageAreaTAList(sliceProfileMap.get("coverageAreaTAList").toString())
        }

        if(!isBlank(sliceProfileMap.get("maxNumberofUEs"))) {
            sliceProfile.setMaxNumberOfUEs(Integer.parseInt(sliceProfileMap.get("maxNumberofUEs").toString()))
        }

        if(!isBlank(sliceProfileMap.get("latency"))) {
            sliceProfile.setLatency(Integer.parseInt(sliceProfileMap.get("latency").toString()))
        }

        sliceProfile.setProfileId(sliceProfileID)
        sliceProfile.setE2ELatency(0)

        return sliceProfile
    }


    /**
     * Prepares Slice Profile Instance
     * @param execution
     * @return Slice Profile Instance
     */
    ServiceInstance prepareSliceProfileInstance(DelegateExecution execution) {

        def currentNSSI = execution.getVariable("currentNSSI")

        ServiceInstance sliceProfileInstance = new ServiceInstance()
        String sliceProfileInstanceId = UUID.randomUUID().toString()
        sliceProfileInstance.setServiceInstanceId(sliceProfileInstanceId)


        String sliceInstanceName = "sliceprofile_" + sliceProfileInstanceId
        sliceProfileInstance.setServiceInstanceName(sliceInstanceName)

        String serviceType = jsonUtil.getJsonValue(currentNSSI['sliceProfile'], "sST")
        sliceProfileInstance.setServiceType(serviceType)

        String serviceStatus = "deactivated"
        sliceProfileInstance.setOrchestrationStatus(serviceStatus)

        String serviceInstanceLocationid = jsonUtil.getJsonValue(currentNSSI['sliceProfile'], "plmnIdList")
        sliceProfileInstance.setServiceInstanceLocationId(serviceInstanceLocationid)

        String serviceRole = "slice-profile-instance"
        sliceProfileInstance.setServiceRole(serviceRole)
        List<String> snssaiList = (List<String>)currentNSSI['S-NSSAIs']
        String snssai = snssaiList.get(0)

        sliceProfileInstance.setEnvironmentContext(snssai)
        sliceProfileInstance.setWorkloadContext("CN-NF")

        // TO DO: Model info

        return sliceProfileInstance
    }



    /**
     * Creates Slice Profile Instance
     * @param execution
     */
    void createSliceProfileInstance(DelegateExecution execution) {
        LOGGER.trace("${PREFIX} Start createSliceProfileInstance")

        def currentNSSI = execution.getVariable("currentNSSI")

        String globalSubscriberId = execution.getVariable("globalSubscriberId")
        String subscriptionServiceType = execution.getVariable("subscriptionServiceType")

        SliceProfile sliceProfile = prepareSliceProfile(execution)

        ServiceInstance sliceProfileInstance = prepareSliceProfileInstance(execution)

        SliceProfiles sliceProfiles = new SliceProfiles()
        sliceProfiles.getSliceProfile().add(sliceProfile)
        sliceProfileInstance.setSliceProfiles(sliceProfiles)

        try {
            AAIResourcesClient client = getAAIClient()
            AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer(globalSubscriberId).serviceSubscription(subscriptionServiceType).
                                                                serviceInstance(sliceProfileInstance.getServiceInstanceId()))
            client.create(uri, sliceProfileInstance)

            currentNSSI['createdSliceProfileInstanceId'] = sliceProfileInstance.getServiceInstanceId()
        } catch (Exception ex) {
            exceptionUtil.buildAndThrowWorkflowException(execution, 25000, "Exception occurred while Slice Profile create call:" + ex.getMessage())
        }

        LOGGER.trace("${PREFIX} Exit createSliceProfileInstance")
    }


    /**
     * Creates Allotted Resource
     * @param execution
     * @return AllottedResource
     */
    AllottedResource createAllottedResource(DelegateExecution execution) {
        def currentNSSI = execution.getVariable("currentNSSI")

        String globalSubscriberId = execution.getVariable("globalSubscriberId")
        String subscriptionServiceType = execution.getVariable("subscriptionServiceType")
        String sliceProfileInstanceId = currentNSSI['createdSliceProfileInstanceId']

        AllottedResource allottedResource = new AllottedResource()

        String allottedResourceId = UUID.randomUUID().toString()

        allottedResource.setId(allottedResourceId)

        // TO DO: No other info

        try {
            AAIResourcesClient client = getAAIClient()
            AAIResourceUri allottedResourceUri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer(globalSubscriberId).serviceSubscription(subscriptionServiceType).serviceInstance(sliceProfileInstanceId).allottedResource(allottedResourceId))

            client.create(allottedResourceUri, allottedResource)

            currentNSSI['allottedResourceUri'] = allottedResourceUri
        } catch (Exception ex) {
            exceptionUtil.buildAndThrowWorkflowException(execution, 25000, "Exception occurred while Allotted Resource create call:" + ex.getMessage())
        }

        return allottedResource
    }



    /**
     * Creates Slice Profile association with NSSI
     * @param execution
     */
    void associateSliceProfileInstanceWithNSSI(DelegateExecution execution) {
        LOGGER.trace("${PREFIX} Start associateSliceProfileInstanceWithNSSI")

        def currentNSSI = execution.getVariable("currentNSSI")

        String nssiId = currentNSSI['nssiId']

        String sliceProfileInstanceId = currentNSSI['createdSliceProfileInstanceId']

        AAIResourcesClient client = getAAIClient()

        // Creates Allotted Resource
        AllottedResource allottedResource = createAllottedResource(execution)
        AAIResourceUri allottedResourceUri = (AAIResourceUri)currentNSSI['allottedResourceUri']

        // Updates Slice Profile Instance with Allotted Resource
        try {
            AAIResourceUri sliceProfileInstanceUri = AAIUriFactory.createResourceUri(Types.SERVICE_INSTANCE.getFragment(sliceProfileInstanceId))
            Optional<ServiceInstance> sliceProfileInstanceOpt = client.get(ServiceInstance.class, sliceProfileInstanceUri)
            if (sliceProfileInstanceOpt.isPresent()) {
                ServiceInstance sliceProfileInstance = sliceProfileInstanceOpt.get()

                AllottedResources allottedResources = sliceProfileInstance.getAllottedResources()
                if(allottedResources == null) {
                    allottedResources = new AllottedResources()
                }

                allottedResources.getAllottedResource().add(allottedResource)
                sliceProfileInstance.setAllottedResources(allottedResources)

                client.update(sliceProfileInstanceUri, sliceProfileInstance)
            }
            else {
                exceptionUtil.buildAndThrowWorkflowException(execution, 500, "No slice profile instance found with id = " + sliceProfileInstanceId)
            }

        } catch(Exception e){
            exceptionUtil.buildAndThrowWorkflowException(execution, 25000, "Exception occured while Slice Profile Instance update call: " + e.getMessage())
        }


        // Associates NSSI with Allotted Resource
        try {
            AAIResourceUri nssiUri = AAIUriFactory.createResourceUri(Types.SERVICE_INSTANCE.getFragment(nssiId))
            client.connect(nssiUri, allottedResourceUri, AAIEdgeLabel.USES)
        } catch(Exception e){
            exceptionUtil.buildAndThrowWorkflowException(execution, 25000, "Exception occured while NSSI with Allotted Resource connect call: " + e.getMessage())
        }

        LOGGER.trace("${PREFIX} Exit associateSliceProfileInstanceWithNSSI")
    }


    @Override
    String getPrefix() {
        return PREFIX
    }


    @Override
    String getAction() {
        return ACTION
    }
}