aboutsummaryrefslogtreecommitdiffstats
path: root/bpmn/so-bpmn-infrastructure-common/src/main/groovy/org/onap/so/bpmn/infrastructure/scripts/DeleteCommunicationService.groovy
blob: d00f349690cbc64cf8a9a6484156c9798bd0f96c (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
/*-
 * ============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 org.onap.aaiclient.client.aai.entities.uri.AAISimpleUri

import static org.apache.commons.lang3.StringUtils.isBlank
import javax.ws.rs.NotFoundException
import javax.ws.rs.core.Response
import org.camunda.bpm.engine.delegate.BpmnError
import org.camunda.bpm.engine.delegate.DelegateExecution
import org.onap.aai.domain.yang.CommunicationServiceProfile
import org.onap.aai.domain.yang.CommunicationServiceProfiles
import org.onap.aai.domain.yang.Relationship
import org.onap.aai.domain.yang.ServiceInstance
import org.onap.aaiclient.client.aai.AAIObjectName
import org.onap.aaiclient.client.aai.AAIObjectType
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.AAIUriFactory
import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder
import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder.Types
import org.onap.logging.filter.base.ONAPComponents
import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
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.UrnPropertiesReader
import org.onap.so.bpmn.core.WorkflowException
import org.onap.so.bpmn.core.json.JsonUtils
import org.onap.so.client.HttpClient
import org.onap.so.db.request.beans.OperationStatus
import org.slf4j.Logger
import org.slf4j.LoggerFactory

class DeleteCommunicationService extends AbstractServiceTaskProcessor {
    private final String PREFIX ="DeleteCommunicationService"
    private final Long TIMEOUT = 60 * 60 * 1000

    ExceptionUtil exceptionUtil = new ExceptionUtil()
    JsonUtils jsonUtil = new JsonUtils()
    private RequestDBUtil requestDBUtil = new RequestDBUtil()
    private static final Logger LOGGER = LoggerFactory.getLogger(DeleteCommunicationService.class)

    @Override
    void preProcessRequest(DelegateExecution execution) {
        execution.setVariable("prefix",PREFIX)
        String msg = ""

        LOGGER.trace("Starting preProcessRequest")

        try {
            // check for incoming json message/input
            String siRequest = execution.getVariable("bpmnRequest")
            String requestId = execution.getVariable("mso-request-id")
            execution.setVariable("msoRequestId", requestId)
            execution.setVariable("operationType", "DELETE")

            //communication service id
            String serviceInstanceId = execution.getVariable("serviceInstanceId")
            if (isBlank(serviceInstanceId)) {
                msg = "communication-service id is null"
                exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
            }

            LOGGER.info("Input Request: ${siRequest}, reqId: ${requestId}, serviceInstanceId: ${serviceInstanceId}")

            //requestParameters
            checkAndSetRequestParam(siRequest,"globalSubscriberId",false,execution)
            checkAndSetRequestParam(siRequest,"serviceType",false,execution)
            checkAndSetRequestParam(siRequest,"operationId",false,execution)

        } catch (BpmnError e) {
            throw e
        } catch (any) {
            msg = "Exception in preProcessRequest " + any.getCause()
            LOGGER.debug(msg)
            exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
        }
        LOGGER.trace("Exit preProcessRequest")
    }


    /**
     * prepare update operation status
     * @param execution
     */
    void preInitUpdateOperationStatus(DelegateExecution execution){
        LOGGER.trace(" ======== STARTED initUpdateOperationStatus Process ======== ")
        try{
            execution.setVariable("result","processing")
            execution.setVariable("progress","0")
            execution.setVariable("operationContent","delete communication service operation start")
            setOperationStatus(execution)

        }catch(Exception e){
            LOGGER.error("Exception Occured Processing initUpdateOperationStatus. Exception is:\n" + e)
            execution.setVariable("CVFMI_ErrorResponse", "Error Occurred during initUpdateOperationStatus Method:\n" + e.getMessage())
        }
        LOGGER.trace("======== COMPLETED initUpdateOperationStatus Process ======== ")
    }

    /**
     * send sync response
     * @param execution
     */
    void sendSyncResponse(DelegateExecution execution) {
        LOGGER.debug("Begin sendSyncResponse")

        try {
            String operationId = execution.getVariable("operationId")
            String syncResponse = """{"operationId":"${operationId}"}""".trim()
            sendWorkflowResponse(execution, 202, syncResponse)

        } catch (Exception ex) {
            String msg  = "Exception in sendSyncResponse: " + ex.getMessage()
            exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
        }
        LOGGER.debug("Exit sendSyncResponse")
    }

    /**
     * query CommunicationSerive from AAI
     * save e2eslice-service instance id and service name
     * @param execution
     */
    void queryCommunicationSeriveFromAAI(DelegateExecution execution)
    {
        LOGGER.trace(" ***** begin queryCommunicationSeriveFromAAI *****")
        String serviceInstanceId = execution.getVariable("serviceInstanceId")

        String errorMsg = "query communication service from aai failed"
        AAIResultWrapper wrapper = queryAAI(execution, Types.SERVICE_INSTANCE, serviceInstanceId, errorMsg)
        Optional<ServiceInstance> si = wrapper.asBean(ServiceInstance.class)
        if(si.isPresent())
        {
            String serviceInstName = si.get()?.getServiceInstanceName()
            String e2eSliceServiceInstId
            if(si.isPresent())
            {
                List<Relationship> relationshipList = si.get().getRelationshipList()?.getRelationship()
                for (Relationship relationship : relationshipList)
                {
                    String relatedTo = relationship.getRelatedTo()
                    if (relatedTo == "service-instance")
                    {
                        String relatedLink = relationship.getRelatedLink()?:""
                        e2eSliceServiceInstId = relatedLink ? relatedLink.substring(relatedLink.lastIndexOf("/") + 1,relatedLink.length()) : ""
                        break
                    }
                }
            }
            execution.setVariable("e2eSliceServiceInstanceId", e2eSliceServiceInstId)
            execution.setVariable("serviceInstanceName", serviceInstName ?: "")
            LOGGER.info("communication-service Id: ${serviceInstanceId}, e2eslice-service Id: ${e2eSliceServiceInstId}, serviceName: ${serviceInstName}")
        }
        LOGGER.debug(" ***** Exit queryCommunicationSeriveFromAAI *****")
    }

    /**
     * query AAI
     * @param execution
     * @param aaiObjectName
     * @param instanceId
     * @return AAIResultWrapper
     */
    private AAIResultWrapper queryAAI(DelegateExecution execution, AAIObjectName aaiObjectName, String instanceId, String errorMsg)
    {
        String globalSubscriberId = execution.getVariable("globalSubscriberId")
        String serviceType = execution.getVariable("serviceType")

        AAIResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer(globalSubscriberId).serviceSubscription(serviceType).serviceInstance(instanceId))
        if (!getAAIClient().exists(resourceUri)) {
            exceptionUtil.buildAndThrowWorkflowException(execution, 2500, errorMsg)
        }
        AAIResultWrapper wrapper = getAAIClient().get(resourceUri, NotFoundException.class)
        return wrapper
    }


    /**
     * 再次调用deleteE2EServiceInstance接口,然后获取到operationid,
     */
    void sendRequest2NSMFWF(DelegateExecution execution) {
        LOGGER.debug("begin preRequestSend2NSMF")
        try {
            //url:/onap/so/infra/e2eServiceInstances/v3/{serviceInstanceId}"
            def NSMF_endpoint = UrnPropertiesReader.getVariable("mso.infra.endpoint.url", execution)
            String url = "${NSMF_endpoint}/e2eServiceInstances/v3/${execution.getVariable("e2eSliceServiceInstanceId")}"

            String requestBody = """
                {
                    "globalSubscriberId": "${execution.getVariable("globalSubscriberId")}",
                    "serviceType": "${execution.getVariable("serviceType")}"
                }
            """
            requestBody.replaceAll("\\s+", "")

            String msoKey = UrnPropertiesReader.getVariable("mso.msoKey", execution)
            String basicAuth =  UrnPropertiesReader.getVariable("mso.infra.endpoint.auth", execution)
//            String basicAuthValue = utils.encrypt(basicAuth, msoKey)
//            String encodeString = utils.getBasicAuth(basicAuthValue, msoKey)

            HttpClient httpClient = getHttpClientFactory().newJsonClient(new URL(url), ONAPComponents.SO)
//            httpClient.addAdditionalHeader("Authorization", encodeString)
            httpClient.addAdditionalHeader("Authorization", basicAuth)
            httpClient.addAdditionalHeader("Accept", "application/json")
            Response httpResponse = httpClient.delete(requestBody)
            handleNSSMFWFResponse(httpResponse, execution)

        } catch (BpmnError e) {
            throw e
        } catch (any) {
            String msg = "Exception in DeleteCommunicationService.preRequestSend2NSMF. " + any.getCause()
            LOGGER.error(msg)
            exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
        }

        LOGGER.debug("exit preRequestSend2NSMF")
    }

    /**
     * prepare update operation status
     * @param execution
     */
    private void handleNSSMFWFResponse(Response httpResponse, DelegateExecution execution){
        LOGGER.debug(" ======== STARTED prepareUpdateOperationStatus Process ======== ")

        int nsmfResponseCode = httpResponse.getStatus()
        LOGGER.debug("nsmfResponseCode${nsmfResponseCode}")

        if (nsmfResponseCode >= 200 && nsmfResponseCode < 204 && httpResponse.hasEntity()) {
            String nsmfResponse = httpResponse.readEntity(String.class)
            def e2eOperationId = jsonUtil.getJsonValue(nsmfResponse, "operationId")
            execution.setVariable("e2eOperationId", e2eOperationId)
            execution.setVariable("progress","20")
            execution.setVariable("operationContent","waiting nsmf service delete finished")

            execution.setVariable("currentCycle",0)
            execution.setVariable("isNSMFTimeOut", "no")
            execution.setVariable("isNSMFWFRspSucceed","yes")
        }
        else
        {
            String serviceName = execution.getVariable("serviceInstanceName")
            execution.setVariable("progress", "100")
            execution.setVariable("result", "error")
            execution.setVariable("operationContent", "terminate service failure.")
            execution.setVariable("reason","NSMF WF asynchronous response failed, status Code:${nsmfResponseCode}")
            execution.setVariable("isNSMFWFRspSucceed","no")
            LOGGER.error("nsmf async response error,nsmfResponseCode:${nsmfResponseCode},serivceName:${serviceName}")
        }
        setOperationStatus(execution)
        LOGGER.debug("======== COMPLETED prepareUpdateOperationStatus Process ======== ")
    }

    /**
     * prepare to call sub process
     * @param execution
     */
    void prepareCallCheckProcessStatus(DelegateExecution execution)
    {
        LOGGER.debug(PREFIX + "prepareCallCheckProcessStatus Start")

        def successConditions = new ArrayList<>()
        successConditions.add("finished")
        execution.setVariable("successConditions", successConditions)

        def errorConditions = new ArrayList<>()
        errorConditions.add("error")
        execution.setVariable("errorConditions", errorConditions)

        execution.setVariable("processServiceType", "communication service")
        execution.setVariable("subOperationType", "DELETE")
        execution.setVariable("initProgress", 20)
        execution.setVariable("endProgress",90)

        execution.setVariable("timeOut", TIMEOUT)

        LOGGER.debug(PREFIX + "prepareCallCheckProcessStatus Exit")
    }

    /**
     * delete communication profile from AAI
     * @param execution
     */
    void delCSProfileFromAAI(DelegateExecution execution)
    {
        LOGGER.debug("start delete communication service profile from AAI")
        String globalSubscriberId = execution.getVariable("globalSubscriberId")
        String serviceType = execution.getVariable("serviceType")
        String serviceInstanceId = execution.getVariable("serviceInstanceId")

        String profileId
        try
        {
            AAIPluralResourceUri resourceUri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer(globalSubscriberId).serviceSubscription(serviceType).serviceInstance(serviceInstanceId).communicationServiceProfiles())
            AAIResultWrapper wrapper = getAAIClient().get(resourceUri, NotFoundException.class)
            Optional<CommunicationServiceProfiles> csProfilesOpt = wrapper.asBean(CommunicationServiceProfiles.class)
            if(csProfilesOpt.isPresent()){
                CommunicationServiceProfiles csProfiles = csProfilesOpt.get()
                CommunicationServiceProfile csProfile = csProfiles.getCommunicationServiceProfile().get(0)
                profileId = csProfile ? csProfile.getProfileId() : ""
            }
            AAISimpleUri profileUri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer(globalSubscriberId).serviceSubscription(serviceType).serviceInstance(serviceInstanceId).communicationServiceProfile(profileId))
            if (!getAAIClient().exists(profileUri)) {
                exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "communication service profile was not found in aai")
            }

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

    }

    /**
     * delete communication service from AAI
     * @param execution
     */
    void delCSFromAAI(DelegateExecution execution)
    {
        try
        {
            LOGGER.debug("start delete communication service from AAI")
            AAIResourceUri serviceInstanceUri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer(execution.getVariable("globalSubscriberId")).serviceSubscription(execution.getVariable("serviceType")).serviceInstance(execution.getVariable("serviceInstanceId")))
            getAAIClient().delete(serviceInstanceUri)

            execution.setVariable("progress", "100")
            execution.setVariable("result", "finished")
            execution.setVariable("operationContent", "CSMF completes service terminated.")
            setOperationStatus(execution)
            LOGGER.debug("end delete communication service from AAI")
        }
        catch (any)
        {
            LOGGER.error("Error occured within delCSFromAAI method, cause: ${any.getCause()} ")
            exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Error occured during delete communication service from aai")
        }
    }

    void sendSyncError(DelegateExecution execution)
    {
        LOGGER.debug("Starting sendSyncError")

        try {
            String errorMessage = "Sending Sync Error."
            if (execution.getVariable("WorkflowException") instanceof WorkflowException) {
                WorkflowException wfe = execution.getVariable("WorkflowException")
                errorMessage = wfe.getErrorMessage()
            }

            String buildworkflowException =
                    """<aetgt:WorkflowException xmlns:aetgt="http://org.onap/so/workflow/schema/v1">
					<aetgt:ErrorMessage>${MsoUtils.xmlEscape(errorMessage)}</aetgt:ErrorMessage>
					<aetgt:ErrorCode>7000</aetgt:ErrorCode>
				   </aetgt:WorkflowException>"""

            LOGGER.debug(buildworkflowException)
            sendWorkflowResponse(execution, 500, buildworkflowException)

        } catch (Exception ex) {
            LOGGER.error("Sending Sync Error Activity Failed. " + "\n" + ex.getMessage())
        }
    }

    /**
     * prepare update operation status
     * @param execution
     */
    void preFailedOperationStatus(DelegateExecution execution)
    {
        LOGGER.debug(" ======== STARTED preFailedOperationStatus Process ======== ")

        execution.setVariable("progress", "100")
        execution.setVariable("result", "error")
        execution.setVariable("operationContent", "terminate service failure")

        WorkflowException wfex = execution.getVariable("WorkflowException") as WorkflowException
        String errorMessage = wfex.getErrorMessage()
        errorMessage = errorMessage.length() > 200 ? errorMessage.substring(0,200) + "......" : errorMessage
        execution.setVariable("reason", errorMessage)
        setOperationStatus(execution)

        LOGGER.debug("======== COMPLETED prepareEndOperationStatus Process ======== ")
    }

    /**
     * prepare Operation status
     * @param execution
     * @param operationType
     */
    private void setOperationStatus(DelegateExecution execution)
    {
        OperationStatus operationStatus = new OperationStatus()
        operationStatus.setServiceId(execution.getVariable("serviceInstanceId"))
        operationStatus.setOperationId(execution.getVariable("operationId"))
        operationStatus.setUserId(execution.getVariable("globalSubscriberId"))
        //interface not support update
        operationStatus.setServiceName(execution.getVariable("serviceInstanceName"))
        operationStatus.setResult(execution.getVariable("result"))
        operationStatus.setProgress(execution.getVariable("progress"))
        operationStatus.setOperationContent(execution.getVariable("operationContent"))
        operationStatus.setReason(execution.getVariable("reason")?:"")
        operationStatus.setOperation("DELETE")

        requestDBUtil.prepareUpdateOperationStatus(execution, operationStatus)
    }

    void prepareFailureStatus(DelegateExecution execution)
    {
        execution.setVariable("result", "finished")
        execution.setVariable("progress", "100")
        execution.setVariable("operationContent", "terminate service failure.")
        setOperationStatus(execution)
        LOGGER.debug("${PREFIX}-prepareFailureStatus,result:${execution.getVariable("result")}, reason: ${execution.getVariable("reason")}")
    }

    /**
     * check request json and save parameter to execution
     * @param siRequest
     * @param paraName
     * @param isErrorException
     * @param execution
     */
    private void checkAndSetRequestParam(String siRequest, String paraName, boolean isErrorException, DelegateExecution execution)
    {
        String msg = ""
        String paramValue = jsonUtil.getJsonValue(siRequest, paraName)
        if (isBlank(paramValue)) {
            msg = "Input ${paraName} is null"
            LOGGER.error(msg)
            if(isErrorException)
            {
                exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
            }

        } else {
            execution.setVariable(paraName, paramValue)
        }
    }

}