aboutsummaryrefslogtreecommitdiffstats
path: root/bpmn/so-bpmn-infrastructure-common/src/main/groovy/org/onap/so/bpmn/infrastructure/scripts/DeleteSliceService.groovy
blob: 4c008a2eb93269a527f50dd37f6489616312a9fb (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
/*-
 * ============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 static org.apache.commons.lang3.StringUtils.isBlank
import javax.ws.rs.NotFoundException
import org.camunda.bpm.engine.delegate.BpmnError
import org.camunda.bpm.engine.delegate.DelegateExecution
import org.onap.aai.domain.yang.ServiceProfile
import org.onap.aai.domain.yang.ServiceProfiles
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.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.WorkflowException
import org.onap.so.bpmn.core.json.JsonUtils
import org.onap.so.db.request.beans.OperationStatus
import org.slf4j.Logger
import org.slf4j.LoggerFactory

class DeleteSliceService extends AbstractServiceTaskProcessor {

    private final String PREFIX ="DeleteSliceService"
    ExceptionUtil exceptionUtil = new ExceptionUtil()
    JsonUtils jsonUtil = new JsonUtils()
    private RequestDBUtil requestDBUtil = new RequestDBUtil()

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

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

        LOGGER.debug("*****${PREFIX} preProcessRequest *****")

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

            //e2eslice-service instance id
            String serviceInstanceId = execution.getVariable("serviceInstanceId")
            if (isBlank(serviceInstanceId)) {
                msg = "e2eslice-service id is null"
                exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
            }
            LOGGER.info("Input Request: ${siRequest}, reqId: ${requestId}, e2eslice-service: ${serviceInstanceId}")

            //subscriberInfo
            checkAndSetRequestParam(siRequest,"globalSubscriberId",false, execution)
            checkAndSetRequestParam(siRequest,"serviceType",false, execution)
            checkAndSetRequestParam(siRequest,"operationId",false, execution)
            checkAndSetRequestParam(siRequest,"scriptName",false, execution)
            //prepare init operation status
            execution.setVariable("progress", "0")
            execution.setVariable("result", "processing")
            execution.setVariable("operationType", "DELETE")
            execution.setVariable("operationContent", "Delete Slice service operation start")
            updateServiceOperationStatus(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("*****${PREFIX} Exit preProcessRequest *****")
    }

    /**
     * send asynchronous response
     * @param execution
     */
    void sendAsyncResponse(DelegateExecution execution) {
        LOGGER.trace("${PREFIX} Start sendSyncResponse ")

        try {
            String operationId = execution.getVariable("operationId")
            String syncResponse = """{"operationId":"${operationId}"}""".trim()
            LOGGER.info("sendSynchResponse: xmlSyncResponse - " + "\n" + syncResponse)
            sendWorkflowResponse(execution, 202, syncResponse)

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

    /**
     * Deletes the slice service instance in aai
     */
    void deleteSliceServiceInstance(DelegateExecution execution) {
        LOGGER.trace("${PREFIX} Start deleteSliceServiceInstance")
        try {

            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", "NSMF completes slicing service termination.")
            updateServiceOperationStatus(execution)

            LOGGER.trace("${PREFIX} Exited deleteSliceServiceInstance")
        }catch(Exception e){
            LOGGER.debug("Error occured within deleteSliceServiceInstance method: " + e)
            exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Error occured during deleteSliceServiceInstance from aai")
        }
    }

    /**
     * update operation status
     * @param execution
     */
    private void updateServiceOperationStatus(DelegateExecution execution){

        OperationStatus operationStatus = new OperationStatus()
        operationStatus.setServiceId(execution.getVariable("serviceInstanceId"))
        operationStatus.setOperationId(execution.getVariable("operationId"))
        operationStatus.setUserId(execution.getVariable("globalSubscriberId"))
        operationStatus.setResult(execution.getVariable("result"))
        operationStatus.setProgress(execution.getVariable("progress"))
        operationStatus.setOperationContent(execution.getVariable("operationContent"))
        operationStatus.setReason(execution.getVariable("reason"))
        operationStatus.setOperation(execution.getVariable("operationType"))

        requestDBUtil.prepareUpdateOperationStatus(execution, operationStatus)
    }

    /**
     * delete service profile from aai
     * @param execution
     */
    void delServiceProfileFromAAI(DelegateExecution execution)
    {
        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).serviceProfiles())
            AAIResultWrapper wrapper = getAAIClient().get(resourceUri, NotFoundException.class)
            Optional<ServiceProfiles> serviceProfilesOpt =wrapper.asBean(ServiceProfiles.class)
            if(serviceProfilesOpt.isPresent()){
                ServiceProfiles serviceProfiles = serviceProfilesOpt.get()
                ServiceProfile serviceProfile = serviceProfiles.getServiceProfile().get(0)
                profileId = serviceProfile ? serviceProfile.getProfileId() : ""
            }
            resourceUri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer(globalSubscriberId).serviceSubscription(serviceType).serviceInstance(serviceInstanceId).serviceProfile(profileId))
            if (!getAAIClient().exists(resourceUri)) {
                exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Service Instance was not found in aai")
            }
            getAAIClient().delete(resourceUri)
        }
        catch (any)
        {
            String msg = "delete service profile from aai failed! cause-"+any.getCause()
            LOGGER.error(any.printStackTrace())
            exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg);
        }
    }

     void sendSyncError(DelegateExecution execution) {
        LOGGER.debug("${PREFIX} Start 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())
        }

    }

    void prepareEndOperationStatus(DelegateExecution execution){
        LOGGER.debug(" ======== ${PREFIX} STARTED prepareEndOperationStatus Process ======== ")

        execution.setVariable("progress", "100")
        execution.setVariable("result", "error")
        execution.setVariable("operationContent", "NSSMF 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)
        updateServiceOperationStatus(execution)

        LOGGER.debug("======== ${PREFIX} COMPLETED prepareEndOperationStatus Process ======== ")
    }

    /**
     * check parameters from request body
     * set 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)
        }
    }
}