aboutsummaryrefslogtreecommitdiffstats
path: root/bpmn/so-bpmn-infrastructure-common/src/main/groovy/org/onap/so/bpmn/infrastructure/scripts/DoDeallocateNSSI.groovy
blob: 554f7a6392e94c5c65e50a80e0aadf73ddb7312d (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
/*-
 * ============LICENSE_START=======================================================
 * ONAP - SO
 * ================================================================================
 # Copyright (c) 2019, CMCC Technologies Co., Ltd.
 #
 # 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.SliceProfiles
import org.onap.aaiclient.client.aai.entities.AAIResultWrapper
import org.onap.aaiclient.client.aai.entities.uri.AAIPluralResourceUri
import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri
import org.onap.aaiclient.client.aai.entities.uri.AAISimpleUri
import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory
import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder
import org.onap.so.beans.nsmf.*
import org.onap.so.beans.nsmf.oof.SubnetType
import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
import org.onap.so.bpmn.common.scripts.ExceptionUtil
import org.onap.so.bpmn.common.scripts.NssmfAdapterUtils
import org.onap.so.bpmn.common.scripts.RequestDBUtil
import org.onap.so.bpmn.core.domain.ServiceDecomposition
import org.onap.so.bpmn.core.json.JsonUtils
import org.onap.so.db.request.beans.OperationStatus
import org.slf4j.Logger
import org.slf4j.LoggerFactory

import javax.ws.rs.NotFoundException

class DoDeallocateNSSI extends AbstractServiceTaskProcessor
{
    private final String PREFIX ="DoDeallocateNSSI"

    private ExceptionUtil exceptionUtil = new ExceptionUtil()
    private JsonUtils jsonUtil = new JsonUtils()
    ObjectMapper objectMapper = new ObjectMapper()
    private RequestDBUtil requestDBUtil = new RequestDBUtil()
    private NssmfAdapterUtils nssmfAdapterUtils = new NssmfAdapterUtils(httpClientFactory, jsonUtil)

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

    @Override
    void preProcessRequest(DelegateExecution execution) {
        LOGGER.trace(" ***** ${PREFIX} Start preProcessRequest *****")

        def currentNSSI = execution.getVariable("currentNSSI")
        if (!currentNSSI) {
            String msg = "currentNSSI is null"
            LOGGER.error(msg)
            exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
        }

        LOGGER.trace("***** ${PREFIX} Exit preProcessRequest *****")
    }

    /**
     *
     * @param execution
     */
    void prepareDecomposeService(DelegateExecution execution)
    {
        LOGGER.trace(" *****${PREFIX} Start prepareDecomposeService *****")
        try
        {
            def currentNSSI = execution.getVariable("currentNSSI")
            String modelInvariantUuid = currentNSSI['modelInvariantId']
            String modelVersionId = currentNSSI['modelVersionId']
            String serviceModelInfo = """{
            "modelInvariantUuid":"${modelInvariantUuid}",
            "modelUuid":"${modelVersionId}",
            "modelVersion":""
             }"""
            execution.setVariable("serviceModelInfo", serviceModelInfo)
        }
        catch (any)
        {
            String exceptionMessage = "Bpmn error encountered in deallocate nssi. Unexpected Error from method prepareDecomposeService() - " + any.getMessage()
            exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
        }
        LOGGER.debug(" ***** ${PREFIX} Exit prepareDecomposeService *****")
    }

    /**
     * get vendor Info
     * @param execution
     */
    void processDecomposition(DelegateExecution execution) {
        LOGGER.debug("*****${PREFIX} start processDecomposition *****")

        try {
            ServiceDecomposition serviceDecomposition = execution.getVariable("serviceDecomposition") as ServiceDecomposition
            String vendor = serviceDecomposition ?.getServiceRole()
            NetworkType domainType = convertServiceCategory(serviceDecomposition.getServiceCategory())

            def currentNSSI = execution.getVariable("currentNSSI")
            currentNSSI['vendor'] = vendor
            currentNSSI['domainType'] = domainType
            LOGGER.info("processDecomposition, current vendor-domainType:" +String.join("-", vendor, domainType.toString()))

        } catch (any) {
            String exceptionMessage = "Bpmn error encountered in deallocate nssi. processDecomposition() - " + any.getMessage()
            LOGGER.debug(exceptionMessage)
            exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
        }
        LOGGER.debug("*****${PREFIX} Exit processDecomposition *****")
    }


    /**
     * get subnetType from serviceCategory
     * @return
     */
    private NetworkType convertServiceCategory(String serviceCategory){
        if(serviceCategory ==~ /CN.*/){
            return SubnetType.CN.getNetworkType()
        }
        if (serviceCategory ==~ /AN.*/){
            return SubnetType.AN.getNetworkType()
        }
        if (serviceCategory ==~ /TN.*BH.*/){
            return SubnetType.TN_BH.getNetworkType()
        }
        if(serviceCategory ==~ /TN.*MH.*/){
            return SubnetType.TN_MH.getNetworkType()
        }
        return null
    }
    
    /**
     * send deallocate request to nssmf
     * @param execution
     */
    void sendRequestToNSSMF(DelegateExecution execution)
    {
        LOGGER.debug("*****${PREFIX} start sendRequestToNSSMF *****")
        def currentNSSI = execution.getVariable("currentNSSI")
        String snssai= currentNSSI['snssai']
        String profileId = currentNSSI['profileId']
        String nssiId = currentNSSI['nssiServiceInstanceId']
        String nsiId = currentNSSI['nsiServiceInstanceId']
        String scriptName = execution.getVariable("scriptName")

        String serviceInvariantUuid = currentNSSI['modelInvariantId']
        String serviceUuid = currentNSSI['modelVersionId']
        String globalSubscriberId = currentNSSI['globalSubscriberId']
        String subscriptionServiceType = currentNSSI['serviceType']
        
        DeAllocateNssi deAllocateNssi = new DeAllocateNssi()
        deAllocateNssi.setNsiId(nsiId)
        deAllocateNssi.setNssiId(nssiId)
        deAllocateNssi.setTerminateNssiOption(0)
        deAllocateNssi.setSnssaiList(Arrays.asList(snssai))
        deAllocateNssi.setScriptName(scriptName)
        deAllocateNssi.setSliceProfileId(profileId)
        
        ServiceInfo serviceInfo = new ServiceInfo()
        serviceInfo.setServiceInvariantUuid(serviceInvariantUuid)
        serviceInfo.setServiceUuid(serviceUuid)
        serviceInfo.setNsiId(nsiId)
        serviceInfo.setNssiId(nssiId)
        serviceInfo.setGlobalSubscriberId(globalSubscriberId)
        serviceInfo.setSubscriptionServiceType(subscriptionServiceType)
        String serviceInfoString = objectMapper.writeValueAsString(serviceInfo)
        
        EsrInfo esrInfo = getEsrInfo(currentNSSI)
        String esrInfoString = objectMapper.writeValueAsString(esrInfo)
        
        execution.setVariable("deAllocateNssi",deAllocateNssi)
        execution.setVariable("esrInfo", esrInfoString)
        execution.setVariable("serviceInfo", serviceInfoString)
        String nssmfRequest = """
                {
                  "deAllocateNssi": ${objectMapper.writeValueAsString(deAllocateNssi)},
                  "esrInfo":  ${esrInfoString},
                  "serviceInfo": ${serviceInfoString}
                }
              """

        String urlStr = String.format("/api/rest/provMns/v1/NSS/SliceProfiles/%s", profileId)

        NssiResponse nssmfResponse = nssmfAdapterUtils.sendPostRequestNSSMF(execution, urlStr, nssmfRequest, NssiResponse.class)
        if (nssmfResponse != null) {
            currentNSSI['jobId']= nssmfResponse.getJobId() ?: "" 
            currentNSSI['jobProgress'] = 0            
            execution.setVariable("currentNSSI", currentNSSI)    
        } else {
            exceptionUtil.buildAndThrowWorkflowException(execution, 7000, "Received a Bad Response from NSSMF.")
        }
        LOGGER.debug("*****${PREFIX} Exit sendRequestToNSSMF *****")
    }

/**
     * send to nssmf query progress
     * @param execution
     */
    void prepareJobStatusRequest(DelegateExecution execution)
    {
        def currentNSSI = execution.getVariable("currentNSSI")
        String jobId = currentNSSI['jobId']
        execution.setVariable("jobId", jobId)
    }

    
    /**
     * send to nssmf query progress
     * @param execution
     */
    void handleJobStatus(DelegateExecution execution)
    {
        try 
        {
        String jobStatusResponse = execution.getVariable("responseDescriptor")
        String status = jsonUtil.getJsonValue(jobStatusResponse,"status")
        def statusDescription = jsonUtil.getJsonValue(jobStatusResponse,"statusDescription")
        def progress = jsonUtil.getJsonValue(jobStatusResponse,"progress")
        if(!status.equalsIgnoreCase("failed"))
        {
            if(!progress)
            {
                LOGGER.error("job progress is null or empty!")
                exceptionUtil.buildAndThrowWorkflowException(execution, 7000, "Received a Bad Job progress from NSSMF.")
            }
            def currentNSSI = execution.getVariable("currentNSSI")
            int oldProgress = currentNSSI['jobProgress']
            int currentProgress = Integer.parseInt(progress)

            execution.setVariable("isNSSIDeAllocated", (currentProgress == 100))
            execution.setVariable("isNeedUpdateDB", (oldProgress != currentProgress))
            currentNSSI['jobProgress'] = currentProgress
            currentNSSI['status'] = status
            currentNSSI['statusDescription'] = statusDescription

            String nssiId = currentNSSI['nssiServiceInstanceId']
            String nsiId = currentNSSI['nsiServiceInstanceId']
            LOGGER.debug("job status result: nsiId = ${nsiId}, nssiId=${nssiId}, oldProgress=${oldProgress}, progress = ${currentProgress}" )
        }
          else {
            execution.setVariable("isNeedUpdateDB", "true")
            exceptionUtil.buildAndThrowWorkflowException(execution, 7000, "Received a Bad Response from NSSMF.")
        }
        }
        catch (any)
        {
            String msg = "Received a Bad Response from NSSMF. cause-"+any.getCause()
            LOGGER.error(any.printStackTrace())
            exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
        }
    }

    private EsrInfo getEsrInfo(def currentNSSI)
    {
        NetworkType domainType = currentNSSI['domainType']
        String vendor = currentNSSI['vendor']
        
        EsrInfo info = new EsrInfo()
        info.setNetworkType(domainType)
        info.setVendor(vendor)
        return info
    }

 /**
     * handle job status
     * prepare update requestdb
     * @param execution
     */
    void prepareUpdateOperationStatus(DelegateExecution execution)
    {
        def currentNSSI = execution.getVariable("currentNSSI")
        int currentProgress = currentNSSI["jobProgress"]
        def proportion = currentNSSI['proportion']
        int progress = (currentProgress as int) == 0 ? 0 : (currentProgress as int) / 100 * (proportion as int)
        def status = currentNSSI['status']


        OperationStatus operationStatus = new OperationStatus()
        operationStatus.setServiceId(currentNSSI['e2eServiceInstanceId'] as String)
        operationStatus.setOperationId(currentNSSI['operationId'] as String)
        operationStatus.setOperation("DELETE")
        operationStatus.setResult("processing")
        operationStatus.setProgress(progress as String)
        operationStatus.setOperationContent(currentNSSI['domainType'].toString() + " " + status.toString())
        requestDBUtil.prepareUpdateOperationStatus(execution, operationStatus)
        LOGGER.debug("update operation, currentProgress=${currentProgress}, proportion=${proportion}, progress = ${progress}" )
    }
    
    /**
     * delete slice profile from aai
     * @param execution
     */
    void delSliceProfileServiceFromAAI(DelegateExecution execution)
    {
        LOGGER.debug("*****${PREFIX} start delSliceProfileFromAAI *****")
        def currentNSSI = execution.getVariable("currentNSSI")
        String nssiServiceInstanceId = currentNSSI['nssiServiceInstanceId']
        String profileId = currentNSSI['profileId']
        String globalSubscriberId = currentNSSI["globalSubscriberId"]
        String serviceType = currentNSSI['serviceType']

        try
        {
            LOGGER.debug("delete nssiServiceInstanceId:${nssiServiceInstanceId}, profileId:${profileId}")
            AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(
                AAIFluentTypeBuilder.business().customer(globalSubscriberId).serviceSubscription(serviceType).serviceInstance(profileId))
            if (!getAAIClient().exists(resourceUri)) {
                exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Service Instance was not found in aai")
            }
            getAAIClient().delete(resourceUri)
        }
        catch (any)
        {
            String msg = "delete slice profile from aai failed! cause-"+any.getCause()
            LOGGER.error(any.printStackTrace())
            exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
        }
        LOGGER.debug("*****${PREFIX} Exist delSliceProfileFromAAI *****")
    }

    void delSliceProfileFromAAI(DelegateExecution execution){

        LOGGER.debug("*****${PREFIX} start delSliceProfileFromAAI *****")
        def currentNSSI = execution.getVariable("currentNSSI")
        String globalSubscriberId = currentNSSI["globalSubscriberId"]
        String serviceType = currentNSSI['serviceType']
        String sliceProfileInstId = currentNSSI['profileId']

        try
        {
            AAIPluralResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer(globalSubscriberId).serviceSubscription(serviceType).serviceInstance(sliceProfileInstId).sliceProfiles())
            AAIResultWrapper wrapper = getAAIClient().get(resourceUri, NotFoundException.class)
            Optional<SliceProfiles> sliceProfilesOpt =wrapper.asBean(SliceProfiles.class)
            SliceProfiles sliceProfiles
            String profileId
            if(sliceProfilesOpt.isPresent()){
                sliceProfiles = sliceProfilesOpt.get()
                org.onap.aai.domain.yang.SliceProfile sliceProfile = sliceProfiles.getSliceProfile().get(0)
                profileId = sliceProfile ? sliceProfile.getProfileId() : ""
            }
            if (profileId){
                AAISimpleUri profileUri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer(globalSubscriberId).serviceSubscription(serviceType).serviceInstance(sliceProfileInstId).sliceProfile(profileId))
                if (!getAAIClient().exists(profileUri)) {
                    exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Service Instance was not found in aai")
                }
                getAAIClient().delete(profileUri)
            }

        }
        catch (any)
        {
            String msg = "delete service profile from aai failed! cause-"+any.getCause()
            LOGGER.error(any.printStackTrace())
            exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg);
        }
    }
}