aboutsummaryrefslogtreecommitdiffstats
path: root/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/ManagedObject3gppServiceInstances.java
blob: 605e2a83864fee635dde1a5ce6089076c878b59f (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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
/*-
 * ============LICENSE_START=======================================================
 * ONAP - SO
 * ================================================================================
 * Copyright (c) 2022 Deutsche telekom
 * ================================================================================
 * 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.apihandlerinfra;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.v3.oas.annotations.OpenAPIDefinition;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.info.Info;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import org.apache.http.HttpStatus;
import org.onap.aai.domain.yang.Relationship;
import org.onap.aai.domain.yang.RelationshipData;
import org.onap.aai.domain.yang.ServiceInstance;
import org.onap.aai.domain.yang.SliceProfile;
import org.onap.aaiclient.client.aai.AAIRestClientImpl;
import org.onap.logging.filter.base.ErrorCode;
import org.onap.so.apihandler.camundabeans.CamundaResponse;
import org.onap.so.apihandler.common.*;
import org.onap.so.apihandlerinfra.exceptions.ApiException;
import org.onap.so.apihandlerinfra.exceptions.BPMNFailureException;
import org.onap.so.apihandlerinfra.exceptions.RequestDbFailureException;
import org.onap.so.apihandlerinfra.exceptions.ValidateException;
import org.onap.so.apihandlerinfra.logging.ErrorLoggerInfo;
import org.onap.so.moi.*;
import org.onap.so.constants.Status;
import org.onap.so.db.catalog.beans.Service;
import org.onap.so.db.catalog.beans.ServiceRecipe;
import org.onap.so.db.catalog.client.CatalogDbClient;
import org.onap.so.db.request.beans.InfraActiveRequests;
import org.onap.so.db.request.client.RequestsDbClient;
import org.onap.so.logger.LogConstants;
import org.onap.so.logger.LoggingAnchor;
import org.onap.so.logger.MessageEnum;
import org.onap.so.rest.catalog.beans.Vnf;
import org.onap.so.serviceinstancebeans.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import javax.ws.rs.*;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.sql.Timestamp;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

@Component
@Path("/onap/so/infra/moi/api/rest/")
@OpenAPIDefinition(
        info = @Info(title = "/onap/so/infra/moi/api/rest/", description = "API Requests for 3gpp MO Instances"))
public class ManagedObject3gppServiceInstances {

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

    private static final String MSO_PROP_APIHANDLER_INFRA = "MSO_PROP_APIHANDLER_INFRA";

    private static final String END_OF_THE_TRANSACTION = "End of the transaction, the final response is: ";

    private static final String SAVE_TO_DB = "save instance to db";

    private static final String URI_PREFIX = "/moi/api/rest/";

    @Autowired
    private MsoRequest msoRequest;

    @Autowired
    private CatalogDbClient catalogDbClient;

    @Autowired
    private RequestsDbClient requestsDbClient;

    @Autowired
    private RequestHandlerUtils requestHandlerUtils;

    @Autowired
    private ResponseBuilder builder;

    @Autowired
    private CamundaClient camundaClient;

    @Autowired
    private ResponseHandler responseHandler;

    private AAIRestClientImpl aaiRestClient = new AAIRestClientImpl();

    /**
     * POST Requests for 3GPP MOI Service create nssi on a version provided
     *
     * @throws ApiException
     */

    @PUT
    @Path("/{version:[vV][1]}/NetworkSliceSubnet/{id}")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @Operation(description = "Create a 3GPP MOI NSSI on a version provided", responses = @ApiResponse(
            content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))))
    public Response allocateNssi(MoiAllocateRequest request, @PathParam("version") String version,
            @PathParam("id") String id, @Context ContainerRequestContext requestContext) throws ApiException {
        String requestId = requestHandlerUtils.getRequestId(requestContext);
        HashMap<String, String> instanceIdMap = new HashMap<>();
        instanceIdMap.put("serviceInstanceId", id);
        return processAllocateRequest(request, Action.createRanSlice, version, requestId, instanceIdMap,
                requestHandlerUtils.getRequestUri(requestContext, URI_PREFIX));
    }

    @PATCH
    @Path("/{version:[vV][1]}/NetworkSliceSubnet/{id}")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @Operation(description = "Modify a 3GPP MOI NSSI on a version provided", responses = @ApiResponse(
            content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))))
    public Response modifyNssi(MoiAllocateRequest request, @PathParam("version") String version,
            @PathParam("id") String id, @Context ContainerRequestContext requestContext) throws ApiException {
        String requestId = requestHandlerUtils.getRequestId(requestContext);
        HashMap<String, String> instanceIdMap = new HashMap<>();
        instanceIdMap.put("serviceInstanceId", id);
        return processAllocateRequest(request, Action.modifyRanSlice, version, requestId, instanceIdMap,
                requestHandlerUtils.getRequestUri(requestContext, URI_PREFIX));
    }

    /**
     * DELETE Requests to delete 3GPP MOI Service instance on a version provided
     *
     * @throws ApiException
     */
    @DELETE
    @Path("/{version:[vV][1]}/NetworkSliceSubnet/{id}")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @Operation(description = "Delete a 3GPP MOI NSSI on a version provided", responses = @ApiResponse(
            content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))))
    public Response deleteNssi(MoiAllocateRequest request, @PathParam("version") String version,
            @PathParam("id") String id, @Context ContainerRequestContext requestContext) throws ApiException {
        String requestId = requestHandlerUtils.getRequestId(requestContext);
        HashMap<String, String> instanceIdMap = new HashMap<>();
        instanceIdMap.put("serviceInstanceId", id);
        return processAllocateRequest(request, Action.deleteRanSlice, version, requestId, instanceIdMap,
                requestHandlerUtils.getRequestUri(requestContext, URI_PREFIX));
    }

    /**
     * GET Requests for 3GPP MOI Service get nssi on a version provided
     *
     * @throws ApiException
     */
    @GET
    @Operation(description = "Find slice Profile for nssi id", responses = @ApiResponse(
            content = @Content(array = @ArraySchema(schema = @Schema(implementation = Vnf.class)))))
    @Path("/{version:[vV][1]}/NetworkSliceSubnet/{id}")
    @Produces({MediaType.APPLICATION_JSON})
    @Transactional(readOnly = true)
    public Response getMOIAttributes(@PathParam("version") String version, @PathParam("id") String id)
            throws ApiException {
        GETMoiResponse mOIResponse = new GETMoiResponse();
        String response = null;

        Optional<ServiceInstance> serviceInstance = aaiRestClient.getServiceInstanceById(id, "5G", "5GCustomer");
        if (serviceInstance.isPresent()) {
            LOGGER.info("Id from request {}", id);
            ServiceInstance serviceInstanceObj = serviceInstance.get();
            mOIResponse.setId(serviceInstanceObj.getServiceInstanceId());
            mOIResponse.setAdministrativeState(serviceInstanceObj.getOperationalStatus());
            mOIResponse.setOperationalState(serviceInstanceObj.getOrchestrationStatus());


            Attributes attributes = new Attributes();
            List<org.onap.so.moi.SliceProfile> sliceProfileList = new ArrayList<org.onap.so.moi.SliceProfile>();

            List<Relationship> listOfNssiRelationship = serviceInstanceObj.getRelationshipList().getRelationship();

            List<Relationship> listOfNssiRelationshipAR = listOfNssiRelationship.stream()
                    .filter(relationship -> relationship.getRelatedTo().equalsIgnoreCase("allotted-resource"))
                    .collect(Collectors.toList());

            for (Relationship relationship : listOfNssiRelationshipAR) {
                org.onap.so.moi.SliceProfile sliceProfile = new org.onap.so.moi.SliceProfile();
                for (RelationshipData relationshipData : relationship.getRelationshipData()) {
                    if (relationshipData.getRelationshipKey()
                            .equalsIgnoreCase("service-instance.service-instance-id")) {
                        String sliceProfileInstanceId = relationshipData.getRelationshipValue();
                        LOGGER.debug(">>> sliceProfileInstance: {}", sliceProfileInstanceId);

                        Optional<ServiceInstance> sliceProfileServiceInstance = aaiRestClient
                                .getServiceInstanceByIdWithDepth(sliceProfileInstanceId, "5G", "5GCustomer");
                        if (sliceProfileServiceInstance.isPresent()) {
                            ServiceInstance sliceProfileServiceInstanceObj = sliceProfileServiceInstance.get();
                            SliceProfile sliceProfile1 =
                                    sliceProfileServiceInstanceObj.getSliceProfiles().getSliceProfile().get(0);
                            try {
                                sliceProfileList
                                        .add(AAISliceProfileToMOISlice(sliceProfile1, sliceProfileServiceInstanceObj));
                            } catch (Exception e) {
                                LOGGER.info("Can not construct responce {}", e);
                            }

                        }


                    }
                }

            }

            attributes.setSliceProfileList(sliceProfileList);
            mOIResponse.setAttributes(attributes);
            ObjectMapper mapper = new ObjectMapper();
            try {
                response = mapper.writeValueAsString(mOIResponse);
            } catch (Exception exception) {
                LOGGER.error("Error while creating MOIResponse JSON" + exception.getMessage());
            }

            return builder.buildResponse(HttpStatus.SC_OK, null, response, version);


        }
        return builder.buildResponse(HttpStatus.SC_NOT_FOUND, null, null, version);
    }

    private org.onap.so.moi.SliceProfile AAISliceProfileToMOISlice(SliceProfile sliceProfile,
            ServiceInstance serviceInstance) {
        org.onap.so.moi.SliceProfile sliceProfileMoi = new org.onap.so.moi.SliceProfile();

        sliceProfileMoi.setSliceProfileId(sliceProfile.getProfileId());

        String serviceType = serviceInstance.getServiceType();

        // rANSliceSubnetProfile
        RANSliceSubnetProfile ranSliceSubnetProfile = new RANSliceSubnetProfile();
        ranSliceSubnetProfile.setServiceType(serviceType);
        ranSliceSubnetProfile.setdLLatency(sliceProfile.getLatency());
        ranSliceSubnetProfile.setCoverageAreaTAList(Integer.valueOf(sliceProfile.getCoverageAreaTAList()));
        ranSliceSubnetProfile.setMaxNumberofUEs(sliceProfile.getMaxNumberOfUEs());
        ranSliceSubnetProfile.setResourceSharingLevel(sliceProfile.getResourceSharingLevel());
        ranSliceSubnetProfile.setAreaTrafficCapDL(sliceProfile.getAreaTrafficCapDL());

        // plmnId
        String[] plmnIdData = serviceInstance.getServiceInstanceLocationId().split("-");
        PlmnId plmnId = new PlmnId();
        plmnId.setMcc(Integer.valueOf(plmnIdData[0]));
        plmnId.setMnc(Integer.valueOf(plmnIdData[1]));

        // snssai
        String[] snssaiData = serviceInstance.getEnvironmentContext().split("-");
        Snssai snssai = new Snssai();
        snssai.setSst(snssaiData[0]);
        snssai.setSd(snssaiData[1]);

        // Plmninfo
        PlmnInfo plmnInfo = new PlmnInfo();
        plmnInfo.setPlmnId(plmnId);
        plmnInfo.setSnssai(snssai);

        List<PlmnInfo> plmnInfoList = new ArrayList<PlmnInfo>();
        plmnInfoList.add(plmnInfo);

        sliceProfileMoi.setrANSliceSubnetProfile(ranSliceSubnetProfile);
        sliceProfileMoi.setPlmnInfoList(plmnInfoList);

        return sliceProfileMoi;

    }

    /**
     * Process allocate service request and send request to corresponding workflow
     *
     * @param request
     * @param action
     * @param version
     * @return
     * @throws ApiException
     */
    private Response processAllocateRequest(MoiAllocateRequest request, Action action, String version, String requestId,
            HashMap<String, String> instanceIdMap, String requestUri) throws ApiException {
        String defaultServiceModelName = "UUI_DEFAULT";
        String requestScope = ModelType.service.name();
        String apiVersion = version.substring(1);
        String serviceRequestJson = toString.apply(request);

        ServiceInstancesRequest sir = createServiceInstanceRequest(request, requestId);
        String requestDetails = null;
        try {
            requestDetails = new ObjectMapper().writeValueAsString(sir);
            LOGGER.debug(">>> sir: {}", sir);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        if (serviceRequestJson != null) {
            InfraActiveRequests currentActiveReq = createRequestObject(request, action, requestId, Status.IN_PROGRESS,
                    requestScope, serviceRequestJson);
            // instanceName ???
            String instanceId = instanceIdMap.get("serviceInstanceId");
            String instanceName = sir.getRequestDetails().getRequestInfo().getInstanceName();
            requestHandlerUtils.checkForDuplicateRequests(action, instanceIdMap, requestScope, currentActiveReq,
                    instanceName);

            requestHandlerUtils.setInstanceId(currentActiveReq, requestScope, instanceId, instanceIdMap);
            try {
                requestsDbClient.save(currentActiveReq);
            } catch (Exception e) {
                LOGGER.error("Exception occurred", e);
                ErrorLoggerInfo errorLoggerInfo =
                        new ErrorLoggerInfo.Builder(MessageEnum.APIH_DB_ACCESS_EXC, ErrorCode.DataError)
                                .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
                throw new RequestDbFailureException.Builder(SAVE_TO_DB, e.toString(),
                        HttpStatus.SC_INTERNAL_SERVER_ERROR, ErrorNumbers.SVC_DETAILED_SERVICE_ERROR).cause(e)
                                .errorInfo(errorLoggerInfo).build();
            }

            RecipeLookupResult recipeLookupResult;
            try {
                recipeLookupResult = getServiceInstanceOrchestrationURI("123", action, defaultServiceModelName);

                LOGGER.debug("recipeLookupResult: {}", recipeLookupResult);
            } catch (Exception e) {
                LOGGER.error(LoggingAnchor.FOUR, MessageEnum.APIH_DB_ACCESS_EXC.toString(), MSO_PROP_APIHANDLER_INFRA,
                        ErrorCode.AvailabilityError.getValue(), "Exception while communciate with Catalog DB", e);
                Response response = msoRequest.buildServiceErrorResponse(HttpStatus.SC_NOT_FOUND,
                        MsoException.ServiceException, "No communication to catalog DB " + e.getMessage(),
                        ErrorNumbers.SVC_NO_SERVER_RESOURCES, null, version);
                LOGGER.debug(END_OF_THE_TRANSACTION + response.getEntity());
                return response;
            }

            if (recipeLookupResult == null) {
                LOGGER.error(LoggingAnchor.FOUR, MessageEnum.APIH_DB_ATTRIBUTE_NOT_FOUND.toString(),
                        MSO_PROP_APIHANDLER_INFRA, ErrorCode.DataError.getValue(), "No recipe found in DB");
                Response response = msoRequest.buildServiceErrorResponse(HttpStatus.SC_NOT_FOUND,
                        MsoException.ServiceException, "Recipe does not exist in catalog DB",
                        ErrorNumbers.SVC_GENERAL_SERVICE_ERROR, null, version);
                LOGGER.debug(END_OF_THE_TRANSACTION + response.getEntity());
                return response;
            }

            String serviceInstanceType = sir.getRequestDetails().getRequestParameters().getSubscriptionServiceType();
            RequestClientParameter parameter;
            try {
                parameter = new RequestClientParameter.Builder().setRequestId(requestId).setBaseVfModule(false)
                        .setRecipeTimeout(recipeLookupResult.getRecipeTimeout()).setRequestAction(action.name())
                        .setServiceInstanceId(instanceId).setServiceType(serviceInstanceType)
                        .setRequestDetails(requestDetails).setApiVersion(version).setALaCarte(false)
                        .setRecipeParamXsd(recipeLookupResult.getRecipeParamXsd()).setApiVersion(apiVersion)
                        .setRequestUri(requestUri).build();
            } catch (Exception e) {
                LOGGER.error("Exception occurred", e);
                ErrorLoggerInfo errorLoggerInfo =
                        new ErrorLoggerInfo.Builder(MessageEnum.APIH_BPEL_RESPONSE_ERROR, ErrorCode.SchemaError)
                                .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
                throw new ValidateException.Builder("Unable to generate RequestClientParamter object" + e.getMessage(),
                        HttpStatus.SC_INTERNAL_SERVER_ERROR, ErrorNumbers.SVC_BAD_PARAMETER).errorInfo(errorLoggerInfo)
                                .build();
            }
            return postBPELRequest(currentActiveReq, parameter, recipeLookupResult.getOrchestrationURI(), requestScope);
        } else {
            Response response = msoRequest.buildServiceErrorResponse(HttpStatus.SC_INTERNAL_SERVER_ERROR,
                    MsoException.ServiceException, "JsonProcessingException occurred - serviceRequestJson is null",
                    ErrorNumbers.SVC_BAD_PARAMETER, null, version);
            return response;
        }
    }


    private ServiceInstancesRequest createServiceInstanceRequest(MoiAllocateRequest request, String id) {

        RequestDetails requestDetails = new RequestDetails();
        RequestInfo requestInfo = new RequestInfo();
        requestInfo.setInstanceName("ran_nssi_" + id);
        requestInfo.setRequestorId("MO");
        requestInfo.setProductFamilyId("5G");
        requestInfo.setSource("MO");

        requestDetails.setRequestInfo(requestInfo);

        SubscriberInfo subscriberInfo = new SubscriberInfo();
        subscriberInfo.setGlobalSubscriberId("5GCustomer");
        requestDetails.setSubscriberInfo(subscriberInfo);

        Project project = new Project();
        project.setProjectName("basicnw-project");
        requestDetails.setProject(project);

        OwningEntity owningEntity = new OwningEntity();
        owningEntity.setOwningEntityId("67f2e84c-734d-4e90-a1e4-d2ffa2e75849");
        owningEntity.setOwningEntityName("OE-5GCustomer");
        requestDetails.setOwningEntity(owningEntity);

        RequestParameters requestParameters = new RequestParameters();
        requestParameters.setaLaCarte(false);
        requestParameters.setSubscriptionServiceType("5G");

        Map<String, Object> nssiUserParams = new HashMap<>();
        Attributes attributes = request.getAttributes();
        nssiUserParams.put("nssi", attributes);

        Map<String, Object> homingSolution = new HashMap<>();
        homingSolution.put("Homing_Solution", "true");

        List<Map<String, Object>> userParams = new ArrayList<>();

        userParams.add(nssiUserParams);
        userParams.add(homingSolution);

        requestParameters.setUserParams(userParams);

        requestDetails.setRequestParameters(requestParameters);

        ServiceInstancesRequest sir = new ServiceInstancesRequest();
        sir.setServiceInstanceId(id);
        sir.setRequestDetails(requestDetails);

        return sir;
    }

    public InfraActiveRequests createRequestObject(Object request, Action action, String requestId, Status status,
            String requestScope, String requestJson) {
        InfraActiveRequests aq = new InfraActiveRequests();
        try {
            String serviceInstanceName = null;
            String serviceInstanceId = null;
            if (action.name().equals("ranSlice")) {
                serviceInstanceName = "ran_nssi_" + requestId;
                aq.setServiceInstanceName(serviceInstanceName);
            } else if (action.name().equals("updateInstance")) {
                LOGGER.debug(">>>>> updateInstance");
            }

            aq.setRequestId(requestId);
            aq.setRequestAction(action.toString());
            aq.setRequestUrl(MDC.get(LogConstants.HTTP_URL));
            Timestamp startTimeStamp = new Timestamp(System.currentTimeMillis());
            aq.setStartTime(startTimeStamp);
            aq.setRequestScope(requestScope);
            aq.setRequestBody(requestJson);
            aq.setRequestStatus(status.toString());
            aq.setLastModifiedBy(Constants.MODIFIED_BY_APIHANDLER);
        } catch (Exception e) {
            LOGGER.error("Exception when creation record request", e);

            if (!status.equals(Status.FAILED)) {
                throw e;
            }
        }
        return aq;
    }

    /**
     * Getting recipes from catalogDb
     *
     * @param serviceModelUUID the service model version uuid
     * @param action the action for the service
     * @param defaultServiceModelName default service name
     * @return the service recipe result
     */
    private RecipeLookupResult getServiceInstanceOrchestrationURI(String serviceModelUUID, Action action,
            String defaultServiceModelName) {

        RecipeLookupResult recipeLookupResult = getServiceURI(serviceModelUUID, action, defaultServiceModelName);

        if (recipeLookupResult != null) {
            LOGGER.debug("Orchestration URI is: " + recipeLookupResult.getOrchestrationURI() + ", recipe Timeout is: "
                    + Integer.toString(recipeLookupResult.getRecipeTimeout()));
        } else {
            LOGGER.debug("No matching recipe record found");
        }
        return recipeLookupResult;
    }

    Function<Object, String> toString = serviceRequest -> {
        ObjectMapper mapper = new ObjectMapper();
        String requestAsString = null;
        try {
            requestAsString = mapper.writeValueAsString(serviceRequest);
        } catch (JsonProcessingException e) {
            LOGGER.debug("Exception while converting service request object to String {}", e);
        }
        return requestAsString;
    };

    /**
     * Getting recipes from catalogDb If Service recipe is not set, use default recipe, if set , use special recipe.
     *
     * @param serviceModelUUID the service version uuid
     * @param action the action of the service.
     * @param defaultServiceModelName default service name
     * @return the service recipe result.
     */
    private RecipeLookupResult getServiceURI(String serviceModelUUID, Action action, String defaultServiceModelName) {

        Service defaultServiceRecord =
                catalogDbClient.getFirstByModelNameOrderByModelVersionDesc(defaultServiceModelName);
        // set recipe as default generic recipe
        ServiceRecipe recipe =
                catalogDbClient.getFirstByServiceModelUUIDAndAction(defaultServiceRecord.getModelUUID(), action.name());
        // check the service special recipe
        if (null != serviceModelUUID && !serviceModelUUID.isEmpty()) {
            ServiceRecipe serviceSpecialRecipe =
                    catalogDbClient.getFirstByServiceModelUUIDAndAction(serviceModelUUID, action.name());
            if (null != serviceSpecialRecipe) {
                // set service special recipe.
                recipe = serviceSpecialRecipe;
            }
        }

        if (recipe == null) {
            return null;
        }
        return new RecipeLookupResult(recipe.getOrchestrationUri(), recipe.getRecipeTimeout(), recipe.getParamXsd());

    }

    private Response postBPELRequest(InfraActiveRequests currentActiveReq, RequestClientParameter parameter,
            String orchestrationURI, String requestScope) throws ApiException {
        ResponseEntity<String> response =
                requestHandlerUtils.postRequest(currentActiveReq, parameter, orchestrationURI);
        LOGGER.debug("BPEL response : " + response);
        int bpelStatus = responseHandler.setStatus(response.getStatusCodeValue());
        String jsonResponse;
        try {
            responseHandler.acceptedResponse(response);
            CamundaResponse camundaResponse = responseHandler.getCamundaResponse(response);
            String responseBody = camundaResponse.getResponse();
            if ("Success".equalsIgnoreCase(camundaResponse.getMessage())) {
                jsonResponse = responseBody;
            } else {
                BPMNFailureException bpmnException =
                        new BPMNFailureException.Builder(String.valueOf(bpelStatus) + responseBody, bpelStatus,
                                ErrorNumbers.SVC_DETAILED_SERVICE_ERROR).build();
                requestHandlerUtils.updateStatus(currentActiveReq, Status.FAILED, bpmnException.getMessage());
                throw bpmnException;
            }
        } catch (ApiException e) {
            requestHandlerUtils.updateStatus(currentActiveReq, Status.FAILED, e.getMessage());
            throw e;
        }
        return builder.buildResponse(HttpStatus.SC_ACCEPTED, parameter.getRequestId(), jsonResponse,
                parameter.getApiVersion());
    }
}