2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 
   6  * Copyright (C) 2017 Huawei Technologies Co., Ltd. All rights reserved.
 
   7  * Modifications Copyright (C) 2019 IBM.
 
   8  * ================================================================================
 
   9  * Modifications Copyright (c) 2019 Samsung
 
  10  * ================================================================================
 
  11  * Licensed under the Apache License, Version 2.0 (the "License");
 
  12  * you may not use this file except in compliance with the License.
 
  13  * You may obtain a copy of the License at
 
  15  *      http://www.apache.org/licenses/LICENSE-2.0
 
  17  * Unless required by applicable law or agreed to in writing, software
 
  18  * distributed under the License is distributed on an "AS IS" BASIS,
 
  19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  20  * See the License for the specific language governing permissions and
 
  21  * limitations under the License.
 
  22  * ============LICENSE_END=========================================================
 
  25 package org.onap.so.apihandlerinfra;
 
  28 import static org.onap.so.logger.HttpHeadersConstants.REQUESTOR_ID;
 
  29 import java.io.IOException;
 
  31 import java.sql.Timestamp;
 
  32 import java.util.ArrayList;
 
  33 import java.util.HashMap;
 
  34 import java.util.List;
 
  36 import java.util.Optional;
 
  37 import javax.ws.rs.container.ContainerRequestContext;
 
  38 import javax.ws.rs.core.MultivaluedMap;
 
  39 import javax.ws.rs.core.Response;
 
  40 import org.apache.commons.lang.StringUtils;
 
  41 import org.apache.http.HttpResponse;
 
  42 import org.apache.http.HttpStatus;
 
  43 import org.camunda.bpm.engine.history.HistoricProcessInstance;
 
  44 import org.camunda.bpm.engine.impl.persistence.entity.HistoricProcessInstanceEntity;
 
  45 import org.onap.logging.ref.slf4j.ONAPLogConstants;
 
  46 import org.onap.so.apihandler.camundabeans.CamundaResponse;
 
  47 import org.onap.so.apihandler.common.CommonConstants;
 
  48 import org.onap.so.apihandler.common.ErrorNumbers;
 
  49 import org.onap.so.apihandler.common.RequestClient;
 
  50 import org.onap.so.apihandler.common.RequestClientFactory;
 
  51 import org.onap.so.apihandler.common.RequestClientParameter;
 
  52 import org.onap.so.apihandler.common.ResponseBuilder;
 
  53 import org.onap.so.apihandler.common.ResponseHandler;
 
  54 import org.onap.so.apihandlerinfra.exceptions.ApiException;
 
  55 import org.onap.so.apihandlerinfra.exceptions.BPMNFailureException;
 
  56 import org.onap.so.apihandlerinfra.exceptions.ClientConnectionException;
 
  57 import org.onap.so.apihandlerinfra.exceptions.ContactCamundaException;
 
  58 import org.onap.so.apihandlerinfra.exceptions.DuplicateRequestException;
 
  59 import org.onap.so.apihandlerinfra.exceptions.RecipeNotFoundException;
 
  60 import org.onap.so.apihandlerinfra.exceptions.RequestDbFailureException;
 
  61 import org.onap.so.apihandlerinfra.exceptions.ValidateException;
 
  62 import org.onap.so.apihandlerinfra.exceptions.VfModuleNotFoundException;
 
  63 import org.onap.so.apihandlerinfra.infra.rest.handler.AbstractRestHandler;
 
  64 import org.onap.so.apihandlerinfra.logging.ErrorLoggerInfo;
 
  65 import org.onap.so.constants.Status;
 
  66 import org.onap.so.db.catalog.beans.NetworkResource;
 
  67 import org.onap.so.db.catalog.beans.NetworkResourceCustomization;
 
  68 import org.onap.so.db.catalog.beans.Recipe;
 
  69 import org.onap.so.db.catalog.beans.ServiceRecipe;
 
  70 import org.onap.so.db.catalog.beans.VfModule;
 
  71 import org.onap.so.db.catalog.beans.VfModuleCustomization;
 
  72 import org.onap.so.db.catalog.beans.VnfRecipe;
 
  73 import org.onap.so.db.catalog.beans.VnfResource;
 
  74 import org.onap.so.db.catalog.beans.VnfResourceCustomization;
 
  75 import org.onap.so.db.catalog.client.CatalogDbClient;
 
  76 import org.onap.so.db.request.beans.InfraActiveRequests;
 
  77 import org.onap.so.db.request.client.RequestsDbClient;
 
  78 import org.onap.so.exceptions.ValidationException;
 
  79 import org.onap.so.logger.ErrorCode;
 
  80 import org.onap.so.logger.LogConstants;
 
  81 import org.onap.so.logger.MessageEnum;
 
  82 import org.onap.so.serviceinstancebeans.CloudConfiguration;
 
  83 import org.onap.so.serviceinstancebeans.ModelInfo;
 
  84 import org.onap.so.serviceinstancebeans.ModelType;
 
  85 import org.onap.so.serviceinstancebeans.Networks;
 
  86 import org.onap.so.serviceinstancebeans.RelatedInstance;
 
  87 import org.onap.so.serviceinstancebeans.RelatedInstanceList;
 
  88 import org.onap.so.serviceinstancebeans.RequestDetails;
 
  89 import org.onap.so.serviceinstancebeans.RequestParameters;
 
  90 import org.onap.so.serviceinstancebeans.Service;
 
  91 import org.onap.so.serviceinstancebeans.ServiceInstancesRequest;
 
  92 import org.onap.so.serviceinstancebeans.ServiceInstancesResponse;
 
  93 import org.onap.so.serviceinstancebeans.VfModules;
 
  94 import org.onap.so.serviceinstancebeans.Vnfs;
 
  95 import org.onap.so.utils.UUIDChecker;
 
  96 import org.slf4j.Logger;
 
  97 import org.slf4j.LoggerFactory;
 
  99 import org.springframework.beans.factory.annotation.Autowired;
 
 100 import org.springframework.core.env.Environment;
 
 101 import org.springframework.http.ResponseEntity;
 
 102 import org.springframework.stereotype.Component;
 
 103 import org.springframework.web.client.RestClientException;
 
 104 import com.fasterxml.jackson.annotation.JsonInclude.Include;
 
 105 import com.fasterxml.jackson.databind.ObjectMapper;
 
 108 public class RequestHandlerUtils extends AbstractRestHandler {
 
 110     private static Logger logger = LoggerFactory.getLogger(RequestHandlerUtils.class);
 
 112     protected static final String SAVE_TO_DB = "save instance to db";
 
 113     private static final String NAME = "name";
 
 114     private static final String VALUE = "value";
 
 117     private Environment env;
 
 120     private RequestClientFactory reqClientFactory;
 
 123     private ResponseBuilder builder;
 
 126     private MsoRequest msoRequest;
 
 129     private CamundaRequestHandler camundaRequestHandler;
 
 132     private CatalogDbClient catalogDbClient;
 
 134     public Response postBPELRequest(InfraActiveRequests currentActiveReq, RequestClientParameter requestClientParameter,
 
 135             String orchestrationUri, String requestScope) throws ApiException {
 
 136         HttpResponse response = null;
 
 137         RequestClient requestClient = null;
 
 140             requestClient = reqClientFactory.getRequestClient(orchestrationUri);
 
 141             response = requestClient.post(requestClientParameter);
 
 142         } catch (Exception e) {
 
 143             logger.error("Error posting request to BPMN", e);
 
 144             ErrorLoggerInfo errorLoggerInfo =
 
 145                     new ErrorLoggerInfo.Builder(MessageEnum.APIH_BPEL_COMMUNICATE_ERROR, ErrorCode.AvailabilityError)
 
 146                             .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
 
 147             String url = requestClient != null ? requestClient.getUrl() : "";
 
 148             ClientConnectionException clientException =
 
 149                     new ClientConnectionException.Builder(url, HttpStatus.SC_BAD_GATEWAY,
 
 150                             ErrorNumbers.SVC_NO_SERVER_RESOURCES).cause(e).errorInfo(errorLoggerInfo).build();
 
 151             updateStatus(currentActiveReq, Status.FAILED, clientException.getMessage());
 
 152             throw clientException;
 
 155         if (response == null) {
 
 157             ErrorLoggerInfo errorLoggerInfo =
 
 158                     new ErrorLoggerInfo.Builder(MessageEnum.APIH_BPEL_COMMUNICATE_ERROR, ErrorCode.BusinessProcessError)
 
 159                             .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
 
 160             ClientConnectionException clientException = new ClientConnectionException.Builder(requestClient.getUrl(),
 
 161                     HttpStatus.SC_BAD_GATEWAY, ErrorNumbers.SVC_NO_SERVER_RESOURCES).errorInfo(errorLoggerInfo).build();
 
 162             updateStatus(currentActiveReq, Status.FAILED, clientException.getMessage());
 
 163             throw clientException;
 
 166         ResponseHandler respHandler = null;
 
 167         int bpelStatus = 500;
 
 169             respHandler = new ResponseHandler(response, requestClient.getType());
 
 170             bpelStatus = respHandler.getStatus();
 
 171         } catch (ApiException e) {
 
 172             logger.error("Exception occurred", e);
 
 173             ErrorLoggerInfo errorLoggerInfo =
 
 174                     new ErrorLoggerInfo.Builder(MessageEnum.APIH_BPEL_RESPONSE_ERROR, ErrorCode.SchemaError)
 
 175                             .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
 
 176             ValidateException validateException =
 
 177                     new ValidateException.Builder("Exception caught mapping Camunda JSON response to object",
 
 178                             HttpStatus.SC_INTERNAL_SERVER_ERROR, ErrorNumbers.SVC_BAD_PARAMETER).cause(e)
 
 179                                     .errorInfo(errorLoggerInfo).build();
 
 180             updateStatus(currentActiveReq, Status.FAILED, validateException.getMessage());
 
 181             throw validateException;
 
 184         // BPEL accepted the request, the request is in progress
 
 185         if (bpelStatus == HttpStatus.SC_ACCEPTED) {
 
 186             ServiceInstancesResponse jsonResponse;
 
 187             CamundaResponse camundaResp = respHandler.getResponse();
 
 189             if ("Success".equalsIgnoreCase(camundaResp.getMessage())) {
 
 191                     ObjectMapper mapper = new ObjectMapper();
 
 192                     jsonResponse = mapper.readValue(camundaResp.getResponse(), ServiceInstancesResponse.class);
 
 193                     jsonResponse.getRequestReferences().setRequestId(requestClientParameter.getRequestId());
 
 194                     Optional<URL> selfLinkUrl =
 
 195                             buildSelfLinkUrl(currentActiveReq.getRequestUrl(), requestClientParameter.getRequestId());
 
 196                     if (selfLinkUrl.isPresent()) {
 
 197                         jsonResponse.getRequestReferences().setRequestSelfLink(selfLinkUrl.get());
 
 199                         jsonResponse.getRequestReferences().setRequestSelfLink(null);
 
 201                 } catch (IOException e) {
 
 202                     logger.error("Exception occurred", e);
 
 203                     ErrorLoggerInfo errorLoggerInfo =
 
 204                             new ErrorLoggerInfo.Builder(MessageEnum.APIH_BPEL_RESPONSE_ERROR, ErrorCode.SchemaError)
 
 205                                     .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
 
 206                     ValidateException validateException =
 
 207                             new ValidateException.Builder("Exception caught mapping Camunda JSON response to object",
 
 208                                     HttpStatus.SC_NOT_ACCEPTABLE, ErrorNumbers.SVC_BAD_PARAMETER).cause(e)
 
 209                                             .errorInfo(errorLoggerInfo).build();
 
 210                     updateStatus(currentActiveReq, Status.FAILED, validateException.getMessage());
 
 211                     throw validateException;
 
 213                 return builder.buildResponse(HttpStatus.SC_ACCEPTED, requestClientParameter.getRequestId(),
 
 214                         jsonResponse, requestClientParameter.getApiVersion());
 
 218         List<String> variables = new ArrayList<>();
 
 219         variables.add(bpelStatus + "");
 
 220         String camundaJSONResponseBody = respHandler.getResponseBody();
 
 221         if (camundaJSONResponseBody != null && !camundaJSONResponseBody.isEmpty()) {
 
 223             ErrorLoggerInfo errorLoggerInfo =
 
 224                     new ErrorLoggerInfo.Builder(MessageEnum.APIH_BPEL_RESPONSE_ERROR, ErrorCode.BusinessProcessError)
 
 225                             .errorSource(requestClient.getUrl()).build();
 
 226             BPMNFailureException bpmnException =
 
 227                     new BPMNFailureException.Builder(String.valueOf(bpelStatus) + camundaJSONResponseBody, bpelStatus,
 
 228                             ErrorNumbers.SVC_DETAILED_SERVICE_ERROR).errorInfo(errorLoggerInfo).build();
 
 230             updateStatus(currentActiveReq, Status.FAILED, bpmnException.getMessage());
 
 235             ErrorLoggerInfo errorLoggerInfo =
 
 236                     new ErrorLoggerInfo.Builder(MessageEnum.APIH_BPEL_RESPONSE_ERROR, ErrorCode.BusinessProcessError)
 
 237                             .errorSource(requestClient.getUrl()).build();
 
 240             BPMNFailureException servException = new BPMNFailureException.Builder(String.valueOf(bpelStatus),
 
 241                     bpelStatus, ErrorNumbers.SVC_DETAILED_SERVICE_ERROR).errorInfo(errorLoggerInfo).build();
 
 242             updateStatus(currentActiveReq, Status.FAILED, servException.getMessage());
 
 251     public void updateStatus(InfraActiveRequests aq, Status status, String errorMessage)
 
 252             throws RequestDbFailureException {
 
 253         if ((status == Status.FAILED) || (status == Status.COMPLETE)) {
 
 254             aq.setStatusMessage(errorMessage);
 
 255             aq.setProgress(new Long(100));
 
 256             aq.setRequestStatus(status.toString());
 
 257             Timestamp endTimeStamp = new Timestamp(System.currentTimeMillis());
 
 258             aq.setEndTime(endTimeStamp);
 
 260                 infraActiveRequestsClient.save(aq);
 
 261             } catch (Exception e) {
 
 262                 ErrorLoggerInfo errorLoggerInfo =
 
 263                         new ErrorLoggerInfo.Builder(MessageEnum.APIH_DB_ACCESS_EXC, ErrorCode.DataError)
 
 264                                 .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
 
 265                 throw new RequestDbFailureException.Builder(SAVE_TO_DB, e.toString(),
 
 266                         HttpStatus.SC_INTERNAL_SERVER_ERROR, ErrorNumbers.SVC_DETAILED_SERVICE_ERROR).cause(e)
 
 267                                 .errorInfo(errorLoggerInfo).build();
 
 272     public String deriveRequestScope(Actions action, ServiceInstancesRequest sir, String requestUri) {
 
 273         if (action == Action.inPlaceSoftwareUpdate || action == Action.applyUpdatedConfig) {
 
 274             return (ModelType.vnf.name());
 
 275         } else if (action == Action.addMembers || action == Action.removeMembers) {
 
 276             return (ModelType.instanceGroup.toString());
 
 279             if (sir.getRequestDetails().getModelInfo().getModelType() == null) {
 
 280                 requestScope = requestScopeFromUri(requestUri);
 
 282                 requestScope = sir.getRequestDetails().getModelInfo().getModelType().name();
 
 289     public void validateHeaders(ContainerRequestContext context) throws ValidationException {
 
 290         MultivaluedMap<String, String> headers = context.getHeaders();
 
 291         if (!headers.containsKey(ONAPLogConstants.Headers.REQUEST_ID)) {
 
 292             throw new ValidationException(ONAPLogConstants.Headers.REQUEST_ID + " header", true);
 
 294         if (!headers.containsKey(ONAPLogConstants.Headers.PARTNER_NAME)) {
 
 295             throw new ValidationException(ONAPLogConstants.Headers.PARTNER_NAME + " header", true);
 
 297         if (!headers.containsKey(REQUESTOR_ID)) {
 
 298             throw new ValidationException(REQUESTOR_ID + " header", true);
 
 302     public String getRequestUri(ContainerRequestContext context, String uriPrefix) {
 
 303         String requestUri = context.getUriInfo().getPath();
 
 304         String httpUrl = MDC.get(LogConstants.URI_BASE).concat(requestUri);
 
 305         MDC.put(LogConstants.HTTP_URL, httpUrl);
 
 306         requestUri = requestUri.substring(requestUri.indexOf(uriPrefix) + uriPrefix.length());
 
 310     public void checkForDuplicateRequests(Actions action, HashMap<String, String> instanceIdMap, String requestScope,
 
 311             InfraActiveRequests currentActiveReq, String instanceName) throws ApiException {
 
 312         InfraActiveRequests dup = null;
 
 313         boolean inProgress = false;
 
 315         dup = duplicateCheck(action, instanceIdMap, instanceName, requestScope, currentActiveReq);
 
 318             inProgress = camundaHistoryCheck(dup, currentActiveReq);
 
 321         if (dup != null && inProgress) {
 
 322             buildErrorOnDuplicateRecord(currentActiveReq, action, instanceIdMap, instanceName, requestScope, dup);
 
 326     public InfraActiveRequests duplicateCheck(Actions action, HashMap<String, String> instanceIdMap,
 
 327             String instanceName, String requestScope, InfraActiveRequests currentActiveReq) throws ApiException {
 
 328         InfraActiveRequests dup = null;
 
 330             if (!(instanceName == null && "service".equals(requestScope) && (action == Action.createInstance
 
 331                     || action == Action.activateInstance || action == Action.assignInstance))) {
 
 332                 dup = infraActiveRequestsClient.checkInstanceNameDuplicate(instanceIdMap, instanceName, requestScope);
 
 334         } catch (Exception e) {
 
 335             ErrorLoggerInfo errorLoggerInfo =
 
 336                     new ErrorLoggerInfo.Builder(MessageEnum.APIH_DUPLICATE_CHECK_EXC, ErrorCode.DataError)
 
 337                             .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
 
 338             RequestDbFailureException requestDbFailureException =
 
 339                     new RequestDbFailureException.Builder("check for duplicate instance", e.toString(),
 
 340                             HttpStatus.SC_INTERNAL_SERVER_ERROR, ErrorNumbers.SVC_DETAILED_SERVICE_ERROR).cause(e)
 
 341                                     .errorInfo(errorLoggerInfo).build();
 
 342             updateStatus(currentActiveReq, Status.FAILED, requestDbFailureException.getMessage());
 
 343             throw requestDbFailureException;
 
 348     public boolean camundaHistoryCheck(InfraActiveRequests duplicateRecord, InfraActiveRequests currentActiveReq)
 
 349             throws RequestDbFailureException, ContactCamundaException {
 
 350         String requestId = duplicateRecord.getRequestId();
 
 351         ResponseEntity<List<HistoricProcessInstanceEntity>> response = null;
 
 353             response = camundaRequestHandler.getCamundaProcessInstanceHistory(requestId, true);
 
 354         } catch (RestClientException e) {
 
 355             logger.error("Error querying Camunda for process-instance history for requestId: {}, exception: {}",
 
 356                     requestId, e.getMessage());
 
 357             ContactCamundaException contactCamundaException =
 
 358                     new ContactCamundaException.Builder("process-instance", requestId, e.toString(),
 
 359                             HttpStatus.SC_INTERNAL_SERVER_ERROR, ErrorNumbers.SVC_DETAILED_SERVICE_ERROR).cause(e)
 
 361             updateStatus(currentActiveReq, Status.FAILED, contactCamundaException.getMessage());
 
 362             throw contactCamundaException;
 
 365         if (response.getBody().isEmpty()) {
 
 366             updateStatus(duplicateRecord, Status.COMPLETE, "Request Completed");
 
 368         for (HistoricProcessInstance instance : response.getBody()) {
 
 369             if (("ACTIVE").equals(instance.getState())) {
 
 372                 updateStatus(duplicateRecord, Status.COMPLETE, "Request Completed");
 
 378     public ServiceInstancesRequest convertJsonToServiceInstanceRequest(String requestJSON, Actions action,
 
 379             String requestId, String requestUri) throws ApiException {
 
 381             ObjectMapper mapper = new ObjectMapper();
 
 382             return mapper.readValue(requestJSON, ServiceInstancesRequest.class);
 
 384         } catch (IOException e) {
 
 386             ErrorLoggerInfo errorLoggerInfo =
 
 387                     new ErrorLoggerInfo.Builder(MessageEnum.APIH_REQUEST_VALIDATION_ERROR, ErrorCode.SchemaError)
 
 388                             .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
 
 390             ValidateException validateException =
 
 391                     new ValidateException.Builder("Error mapping request: " + e.getMessage(), HttpStatus.SC_BAD_REQUEST,
 
 392                             ErrorNumbers.SVC_BAD_PARAMETER).cause(e).errorInfo(errorLoggerInfo).build();
 
 393             String requestScope = requestScopeFromUri(requestUri);
 
 395             msoRequest.createErrorRequestRecord(Status.FAILED, requestId, validateException.getMessage(), action,
 
 396                     requestScope, requestJSON);
 
 398             throw validateException;
 
 402     public void parseRequest(ServiceInstancesRequest sir, HashMap<String, String> instanceIdMap, Actions action,
 
 403             String version, String requestJSON, Boolean aLaCarte, String requestId,
 
 404             InfraActiveRequests currentActiveReq) throws ValidateException, RequestDbFailureException {
 
 405         int reqVersion = Integer.parseInt(version.substring(1));
 
 407             msoRequest.parse(sir, instanceIdMap, action, version, requestJSON, reqVersion, aLaCarte);
 
 408         } catch (Exception e) {
 
 409             logger.error("failed to parse request", e);
 
 410             ErrorLoggerInfo errorLoggerInfo =
 
 411                     new ErrorLoggerInfo.Builder(MessageEnum.APIH_REQUEST_VALIDATION_ERROR, ErrorCode.SchemaError)
 
 412                             .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
 
 413             ValidateException validateException =
 
 414                     new ValidateException.Builder("Error parsing request: " + e.getMessage(), HttpStatus.SC_BAD_REQUEST,
 
 415                             ErrorNumbers.SVC_BAD_PARAMETER).cause(e).errorInfo(errorLoggerInfo).build();
 
 417             updateStatus(currentActiveReq, Status.FAILED, validateException.getMessage());
 
 419             throw validateException;
 
 423     public void buildErrorOnDuplicateRecord(InfraActiveRequests currentActiveReq, Actions action,
 
 424             HashMap<String, String> instanceIdMap, String instanceName, String requestScope, InfraActiveRequests dup)
 
 425             throws ApiException {
 
 427         String instance = null;
 
 428         if (instanceName != null) {
 
 429             instance = instanceName;
 
 431             instance = instanceIdMap.get(requestScope + "InstanceId");
 
 433         ErrorLoggerInfo errorLoggerInfo =
 
 434                 new ErrorLoggerInfo.Builder(MessageEnum.APIH_DUPLICATE_FOUND, ErrorCode.SchemaError)
 
 435                         .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
 
 437         DuplicateRequestException dupException =
 
 438                 new DuplicateRequestException.Builder(requestScope, instance, dup.getRequestStatus(),
 
 439                         dup.getRequestId(), HttpStatus.SC_CONFLICT, ErrorNumbers.SVC_DETAILED_SERVICE_ERROR)
 
 440                                 .errorInfo(errorLoggerInfo).build();
 
 442         updateStatus(currentActiveReq, Status.FAILED, dupException.getMessage());
 
 448     public String getRequestId(ContainerRequestContext requestContext) throws ValidateException {
 
 449         String requestId = null;
 
 450         if (requestContext.getProperty("requestId") != null) {
 
 451             requestId = requestContext.getProperty("requestId").toString();
 
 453         if (UUIDChecker.isValidUUID(requestId)) {
 
 456             ErrorLoggerInfo errorLoggerInfo =
 
 457                     new ErrorLoggerInfo.Builder(MessageEnum.APIH_BPEL_RESPONSE_ERROR, ErrorCode.SchemaError)
 
 458                             .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
 
 459             ValidateException validateException =
 
 460                     new ValidateException.Builder("Request Id " + requestId + " is not a valid UUID",
 
 461                             HttpStatus.SC_INTERNAL_SERVER_ERROR, ErrorNumbers.SVC_BAD_PARAMETER)
 
 462                                     .errorInfo(errorLoggerInfo).build();
 
 464             throw validateException;
 
 468     public void setInstanceId(InfraActiveRequests currentActiveReq, String requestScope, String instanceId,
 
 469             Map<String, String> instanceIdMap) {
 
 470         if (StringUtils.isNotBlank(instanceId)) {
 
 471             if (ModelType.service.name().equalsIgnoreCase(requestScope)) {
 
 472                 currentActiveReq.setServiceInstanceId(instanceId);
 
 473             } else if (ModelType.vnf.name().equalsIgnoreCase(requestScope)) {
 
 474                 currentActiveReq.setVnfId(instanceId);
 
 475             } else if (ModelType.vfModule.name().equalsIgnoreCase(requestScope)) {
 
 476                 currentActiveReq.setVfModuleId(instanceId);
 
 477             } else if (ModelType.volumeGroup.name().equalsIgnoreCase(requestScope)) {
 
 478                 currentActiveReq.setVolumeGroupId(instanceId);
 
 479             } else if (ModelType.network.name().equalsIgnoreCase(requestScope)) {
 
 480                 currentActiveReq.setNetworkId(instanceId);
 
 481             } else if (ModelType.configuration.name().equalsIgnoreCase(requestScope)) {
 
 482                 currentActiveReq.setConfigurationId(instanceId);
 
 483             } else if (ModelType.instanceGroup.toString().equalsIgnoreCase(requestScope)) {
 
 484                 currentActiveReq.setInstanceGroupId(instanceId);
 
 486         } else if (instanceIdMap != null && !instanceIdMap.isEmpty()) {
 
 487             if (instanceIdMap.get("serviceInstanceId") != null) {
 
 488                 currentActiveReq.setServiceInstanceId(instanceIdMap.get("serviceInstanceId"));
 
 490             if (instanceIdMap.get("vnfInstanceId") != null) {
 
 491                 currentActiveReq.setVnfId(instanceIdMap.get("vnfInstanceId"));
 
 493             if (instanceIdMap.get("vfModuleInstanceId") != null) {
 
 494                 currentActiveReq.setVfModuleId(instanceIdMap.get("vfModuleInstanceId"));
 
 496             if (instanceIdMap.get("volumeGroupInstanceId") != null) {
 
 497                 currentActiveReq.setVolumeGroupId(instanceIdMap.get("volumeGroupInstanceId"));
 
 499             if (instanceIdMap.get("networkInstanceId") != null) {
 
 500                 currentActiveReq.setNetworkId(instanceIdMap.get("networkInstanceId"));
 
 502             if (instanceIdMap.get("configurationInstanceId") != null) {
 
 503                 currentActiveReq.setConfigurationId(instanceIdMap.get("configurationInstanceId"));
 
 505             if (instanceIdMap.get(CommonConstants.INSTANCE_GROUP_INSTANCE_ID) != null) {
 
 506                 currentActiveReq.setInstanceGroupId(instanceIdMap.get(CommonConstants.INSTANCE_GROUP_INSTANCE_ID));
 
 511     public String mapJSONtoMSOStyle(String msoRawRequest, ServiceInstancesRequest serviceInstRequest,
 
 512             boolean isAlaCarte, Actions action) throws IOException {
 
 513         ObjectMapper mapper = new ObjectMapper();
 
 514         mapper.setSerializationInclusion(Include.NON_NULL);
 
 515         if (serviceInstRequest != null) {
 
 516             return mapper.writeValueAsString(serviceInstRequest);
 
 518             return msoRawRequest;
 
 522     public Optional<String> retrieveModelName(RequestParameters requestParams) {
 
 523         String requestTestApi = null;
 
 524         TestApi testApi = null;
 
 526         if (requestParams != null) {
 
 527             requestTestApi = requestParams.getTestApi();
 
 530         if (requestTestApi == null) {
 
 531             if (requestParams != null && requestParams.getALaCarte() != null && !requestParams.getALaCarte()) {
 
 532                 requestTestApi = env.getProperty(CommonConstants.MACRO_TEST_API);
 
 534                 requestTestApi = env.getProperty(CommonConstants.ALACARTE_TEST_API);
 
 539             testApi = TestApi.valueOf(requestTestApi);
 
 540             return Optional.of(testApi.getModelName());
 
 541         } catch (Exception e) {
 
 542             logger.warn("Catching the exception on the valueOf enum call and continuing", e);
 
 543             throw new IllegalArgumentException("Invalid TestApi is provided", e);
 
 547     public String getDefaultModel(ServiceInstancesRequest sir) {
 
 548         String defaultModel = sir.getRequestDetails().getRequestInfo().getSource() + "_DEFAULT";
 
 549         Optional<String> oModelName = retrieveModelName(sir.getRequestDetails().getRequestParameters());
 
 550         if (oModelName.isPresent()) {
 
 551             defaultModel = oModelName.get();
 
 556     public String getServiceType(String requestScope, ServiceInstancesRequest sir, Boolean aLaCarteFlag) {
 
 557         String serviceType = null;
 
 558         if (requestScope.equalsIgnoreCase(ModelType.service.toString())) {
 
 559             String defaultServiceModelName = getDefaultModel(sir);
 
 560             org.onap.so.db.catalog.beans.Service serviceRecord;
 
 562                 serviceRecord = catalogDbClient.getFirstByModelNameOrderByModelVersionDesc(defaultServiceModelName);
 
 563                 if (serviceRecord != null) {
 
 564                     serviceType = serviceRecord.getServiceType();
 
 568                         catalogDbClient.getServiceByID(sir.getRequestDetails().getModelInfo().getModelVersionId());
 
 569                 if (serviceRecord != null) {
 
 570                     serviceType = serviceRecord.getServiceType();
 
 572                     serviceRecord = catalogDbClient.getFirstByModelNameOrderByModelVersionDesc(defaultServiceModelName);
 
 573                     if (serviceRecord != null) {
 
 574                         serviceType = serviceRecord.getServiceType();
 
 579             serviceType = msoRequest.getServiceInstanceType(sir, requestScope);
 
 584     protected String setServiceInstanceId(String requestScope, ServiceInstancesRequest sir) {
 
 585         if (sir.getServiceInstanceId() != null) {
 
 586             return sir.getServiceInstanceId();
 
 587         } else if (requestScope.equalsIgnoreCase(ModelType.instanceGroup.toString())) {
 
 588             RelatedInstanceList[] relatedInstances = sir.getRequestDetails().getRelatedInstanceList();
 
 589             if (relatedInstances != null) {
 
 590                 for (RelatedInstanceList relatedInstanceList : relatedInstances) {
 
 591                     RelatedInstance relatedInstance = relatedInstanceList.getRelatedInstance();
 
 592                     if (relatedInstance.getModelInfo().getModelType() == ModelType.service) {
 
 593                         return relatedInstance.getInstanceId();
 
 601     private String requestScopeFromUri(String requestUri) {
 
 603         if (requestUri.contains(ModelType.network.name())) {
 
 604             requestScope = ModelType.network.name();
 
 605         } else if (requestUri.contains(ModelType.vfModule.name())) {
 
 606             requestScope = ModelType.vfModule.name();
 
 607         } else if (requestUri.contains(ModelType.volumeGroup.name())) {
 
 608             requestScope = ModelType.volumeGroup.name();
 
 609         } else if (requestUri.contains(ModelType.configuration.name())) {
 
 610             requestScope = ModelType.configuration.name();
 
 611         } else if (requestUri.contains(ModelType.vnf.name())) {
 
 612             requestScope = ModelType.vnf.name();
 
 614             requestScope = ModelType.service.name();
 
 619     protected InfraActiveRequests createNewRecordCopyFromInfraActiveRequest(InfraActiveRequests infraActiveRequest,
 
 620             String requestId, Timestamp startTimeStamp, String source, String requestUri, String requestorId,
 
 621             String originalRequestId) throws ApiException {
 
 622         InfraActiveRequests request = new InfraActiveRequests();
 
 623         request.setRequestId(requestId);
 
 624         request.setStartTime(startTimeStamp);
 
 625         request.setSource(source);
 
 626         request.setRequestUrl(requestUri);
 
 627         request.setProgress(new Long(5));
 
 628         request.setRequestorId(requestorId);
 
 629         request.setRequestStatus(Status.IN_PROGRESS.toString());
 
 630         request.setOriginalRequestId(originalRequestId);
 
 631         request.setLastModifiedBy(Constants.MODIFIED_BY_APIHANDLER);
 
 632         if (infraActiveRequest != null) {
 
 633             request.setTenantId(infraActiveRequest.getTenantId());
 
 634             request.setRequestBody(updateRequestorIdInRequestBody(infraActiveRequest, requestorId));
 
 635             request.setAicCloudRegion(infraActiveRequest.getAicCloudRegion());
 
 636             request.setRequestScope(infraActiveRequest.getRequestScope());
 
 637             request.setRequestAction(infraActiveRequest.getRequestAction());
 
 638             setInstanceIdAndName(infraActiveRequest, request);
 
 643     protected void setInstanceIdAndName(InfraActiveRequests infraActiveRequest,
 
 644             InfraActiveRequests currentActiveRequest) throws ApiException {
 
 645         String requestScope = infraActiveRequest.getRequestScope();
 
 647             ModelType type = ModelType.valueOf(requestScope);
 
 648             String instanceName = type.getName(infraActiveRequest);
 
 649             if (instanceName == null && type.equals(ModelType.vfModule)) {
 
 650                 logger.error("vfModule for requestId: {} being resumed does not have an instanceName.",
 
 651                         infraActiveRequest.getRequestId());
 
 652                 ValidateException validateException = new ValidateException.Builder(
 
 653                         "vfModule for requestId: " + infraActiveRequest.getRequestId()
 
 654                                 + " being resumed does not have an instanceName.",
 
 655                         HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_BAD_PARAMETER).build();
 
 656                 updateStatus(currentActiveRequest, Status.FAILED, validateException.getMessage());
 
 657                 throw validateException;
 
 659             if (instanceName != null) {
 
 660                 type.setName(currentActiveRequest, instanceName);
 
 662             type.setId(currentActiveRequest, type.getId(infraActiveRequest));
 
 663         } catch (IllegalArgumentException e) {
 
 665                     "requestScope \"{}\" does not match a ModelType enum. Unable to set instanceId and instanceName from the original request.",
 
 670     protected Boolean getIsBaseVfModule(ModelInfo modelInfo, Actions action, String vnfType,
 
 671             String sdcServiceModelVersion, InfraActiveRequests currentActiveReq) throws ApiException {
 
 672         // Get VF Module-specific base module indicator
 
 674         String modelVersionId = modelInfo.getModelVersionId();
 
 675         Boolean isBaseVfModule = false;
 
 677         if (modelVersionId != null) {
 
 678             vfm = catalogDbClient.getVfModuleByModelUUID(modelVersionId);
 
 679         } else if (modelInfo.getModelInvariantId() != null && modelInfo.getModelVersion() != null) {
 
 680             vfm = catalogDbClient.getVfModuleByModelInvariantUUIDAndModelVersion(modelInfo.getModelInvariantId(),
 
 681                     modelInfo.getModelVersion());
 
 685             if (vfm.getIsBase()) {
 
 686                 isBaseVfModule = true;
 
 688         } else if (action == Action.createInstance || action == Action.updateInstance) {
 
 689             String serviceVersionText = "";
 
 690             if (sdcServiceModelVersion != null && !sdcServiceModelVersion.isEmpty()) {
 
 691                 serviceVersionText = " with version " + sdcServiceModelVersion;
 
 693             String errorMessage = "VnfType " + vnfType + " and VF Module Model Name " + modelInfo.getModelName()
 
 694                     + serviceVersionText + " not found in MSO Catalog DB";
 
 695             ErrorLoggerInfo errorLoggerInfo =
 
 696                     new ErrorLoggerInfo.Builder(MessageEnum.APIH_DB_ATTRIBUTE_NOT_FOUND, ErrorCode.DataError)
 
 697                             .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
 
 698             VfModuleNotFoundException vfModuleException = new VfModuleNotFoundException.Builder(errorMessage,
 
 699                     HttpStatus.SC_NOT_FOUND, ErrorNumbers.SVC_BAD_PARAMETER).errorInfo(errorLoggerInfo).build();
 
 700             updateStatus(currentActiveReq, Status.FAILED, vfModuleException.getMessage());
 
 701             throw vfModuleException;
 
 703         return isBaseVfModule;
 
 706     protected ModelType getModelType(Actions action, ModelInfo modelInfo) {
 
 707         if (action == Action.applyUpdatedConfig || action == Action.inPlaceSoftwareUpdate) {
 
 708             return ModelType.vnf;
 
 709         } else if (action == Action.addMembers || action == Action.removeMembers) {
 
 710             return ModelType.instanceGroup;
 
 712             return modelInfo.getModelType();
 
 716     protected String updateRequestorIdInRequestBody(InfraActiveRequests infraActiveRequest, String newRequestorId) {
 
 717         String requestBody = infraActiveRequest.getRequestBody();
 
 718         return requestBody.replaceAll(
 
 719                 "(?s)(\"requestInfo\"\\s*?:\\s*?\\{.*?\"requestorId\"\\s*?:\\s*?\")(.*?)(\"[ ]*(?:,|\\R|\\}))",
 
 720                 "$1" + newRequestorId + "$3");
 
 723     public RecipeLookupResult getServiceInstanceOrchestrationURI(ServiceInstancesRequest sir, Actions action,
 
 724             boolean alaCarteFlag, InfraActiveRequests currentActiveReq) throws ApiException {
 
 725         RecipeLookupResult recipeLookupResult = null;
 
 726         // if the aLaCarte flag is set to TRUE, the API-H should choose the VID_DEFAULT recipe for the requested action
 
 727         ModelInfo modelInfo = sir.getRequestDetails().getModelInfo();
 
 728         // Query MSO Catalog DB
 
 730         if (action == Action.applyUpdatedConfig || action == Action.inPlaceSoftwareUpdate) {
 
 731             recipeLookupResult = getDefaultVnfUri(sir, action);
 
 732         } else if (action == Action.addMembers || action == Action.removeMembers) {
 
 733             recipeLookupResult = new RecipeLookupResult("/mso/async/services/WorkflowActionBB", 180);
 
 734         } else if (modelInfo.getModelType().equals(ModelType.service)) {
 
 736                 recipeLookupResult = getServiceURI(sir, action, alaCarteFlag);
 
 737             } catch (IOException e) {
 
 738                 ErrorLoggerInfo errorLoggerInfo =
 
 739                         new ErrorLoggerInfo.Builder(MessageEnum.APIH_REQUEST_VALIDATION_ERROR, ErrorCode.SchemaError)
 
 740                                 .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
 
 743                 ValidateException validateException =
 
 744                         new ValidateException.Builder(e.getMessage(), HttpStatus.SC_BAD_REQUEST,
 
 745                                 ErrorNumbers.SVC_BAD_PARAMETER).cause(e).errorInfo(errorLoggerInfo).build();
 
 747                 updateStatus(currentActiveReq, Status.FAILED, validateException.getMessage());
 
 749                 throw validateException;
 
 751         } else if (modelInfo.getModelType().equals(ModelType.vfModule)
 
 752                 || modelInfo.getModelType().equals(ModelType.volumeGroup)
 
 753                 || modelInfo.getModelType().equals(ModelType.vnf)) {
 
 755                 recipeLookupResult = getVnfOrVfModuleUri(sir, action);
 
 756             } catch (ValidationException e) {
 
 757                 ErrorLoggerInfo errorLoggerInfo =
 
 758                         new ErrorLoggerInfo.Builder(MessageEnum.APIH_REQUEST_VALIDATION_ERROR, ErrorCode.SchemaError)
 
 759                                 .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
 
 762                 ValidateException validateException =
 
 763                         new ValidateException.Builder(e.getMessage(), HttpStatus.SC_BAD_REQUEST,
 
 764                                 ErrorNumbers.SVC_BAD_PARAMETER).cause(e).errorInfo(errorLoggerInfo).build();
 
 766                 updateStatus(currentActiveReq, Status.FAILED, validateException.getMessage());
 
 768                 throw validateException;
 
 770         } else if (modelInfo.getModelType().equals(ModelType.network)) {
 
 772                 recipeLookupResult = getNetworkUri(sir, action);
 
 773             } catch (ValidationException e) {
 
 775                 ErrorLoggerInfo errorLoggerInfo =
 
 776                         new ErrorLoggerInfo.Builder(MessageEnum.APIH_REQUEST_VALIDATION_ERROR, ErrorCode.SchemaError)
 
 777                                 .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
 
 780                 ValidateException validateException =
 
 781                         new ValidateException.Builder(e.getMessage(), HttpStatus.SC_BAD_REQUEST,
 
 782                                 ErrorNumbers.SVC_BAD_PARAMETER).cause(e).errorInfo(errorLoggerInfo).build();
 
 783                 updateStatus(currentActiveReq, Status.FAILED, validateException.getMessage());
 
 785                 throw validateException;
 
 787         } else if (modelInfo.getModelType().equals(ModelType.instanceGroup)) {
 
 788             recipeLookupResult = new RecipeLookupResult("/mso/async/services/WorkflowActionBB", 180);
 
 791         if (recipeLookupResult == null) {
 
 792             ErrorLoggerInfo errorLoggerInfo =
 
 793                     new ErrorLoggerInfo.Builder(MessageEnum.APIH_DB_ACCESS_EXC, ErrorCode.DataError)
 
 794                             .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
 
 797             RecipeNotFoundException recipeNotFoundExceptionException =
 
 798                     new RecipeNotFoundException.Builder("Recipe could not be retrieved from catalog DB.",
 
 799                             HttpStatus.SC_NOT_FOUND, ErrorNumbers.SVC_GENERAL_SERVICE_ERROR).errorInfo(errorLoggerInfo)
 
 802             updateStatus(currentActiveReq, Status.FAILED, recipeNotFoundExceptionException.getMessage());
 
 803             throw recipeNotFoundExceptionException;
 
 805         return recipeLookupResult;
 
 808     protected RecipeLookupResult getServiceURI(ServiceInstancesRequest servInstReq, Actions action,
 
 809             boolean alaCarteFlag) throws IOException {
 
 811         // Construct the default service name
 
 812         // TODO need to make this a configurable property
 
 813         String defaultServiceModelName = getDefaultModel(servInstReq);
 
 814         RequestDetails requestDetails = servInstReq.getRequestDetails();
 
 815         ModelInfo modelInfo = requestDetails.getModelInfo();
 
 816         org.onap.so.db.catalog.beans.Service serviceRecord;
 
 817         List<org.onap.so.db.catalog.beans.Service> serviceRecordList;
 
 818         ServiceRecipe recipe = null;
 
 821             serviceRecord = catalogDbClient.getFirstByModelNameOrderByModelVersionDesc(defaultServiceModelName);
 
 822             if (serviceRecord != null) {
 
 823                 recipe = catalogDbClient.getFirstByServiceModelUUIDAndAction(serviceRecord.getModelUUID(),
 
 827             serviceRecord = catalogDbClient.getServiceByID(modelInfo.getModelVersionId());
 
 828             recipe = catalogDbClient.getFirstByServiceModelUUIDAndAction(modelInfo.getModelVersionId(),
 
 830             if (recipe == null) {
 
 831                 serviceRecordList = catalogDbClient
 
 832                         .getServiceByModelInvariantUUIDOrderByModelVersionDesc(modelInfo.getModelInvariantId());
 
 833                 if (!serviceRecordList.isEmpty()) {
 
 834                     for (org.onap.so.db.catalog.beans.Service record : serviceRecordList) {
 
 835                         recipe = catalogDbClient.getFirstByServiceModelUUIDAndAction(record.getModelUUID(),
 
 837                         if (recipe != null) {
 
 845         // if an aLaCarte flag was sent in the request, throw an error if the recipe was not found
 
 846         RequestParameters reqParam = requestDetails.getRequestParameters();
 
 847         if (reqParam != null && alaCarteFlag && recipe == null) {
 
 849         } else if (!alaCarteFlag && recipe != null && Action.createInstance.equals(action)) {
 
 850             mapToLegacyRequest(requestDetails);
 
 851         } else if (recipe == null) { // aLaCarte wasn't sent, so we'll try the default
 
 852             serviceRecord = catalogDbClient.getFirstByModelNameOrderByModelVersionDesc(defaultServiceModelName);
 
 853             recipe = catalogDbClient.getFirstByServiceModelUUIDAndAction(serviceRecord.getModelUUID(),
 
 856         if (modelInfo.getModelVersionId() == null) {
 
 857             modelInfo.setModelVersionId(serviceRecord.getModelUUID());
 
 859         if (recipe == null) {
 
 862         return new RecipeLookupResult(recipe.getOrchestrationUri(), recipe.getRecipeTimeout());
 
 865     protected void mapToLegacyRequest(RequestDetails requestDetails) throws IOException {
 
 866         RequestParameters reqParam;
 
 867         if (requestDetails.getRequestParameters() == null) {
 
 868             reqParam = new RequestParameters();
 
 870             reqParam = requestDetails.getRequestParameters();
 
 872         if (requestDetails.getCloudConfiguration() == null) {
 
 873             CloudConfiguration cloudConfig = configureCloudConfig(reqParam);
 
 874             if (cloudConfig != null) {
 
 875                 requestDetails.setCloudConfiguration(cloudConfig);
 
 879         List<Map<String, Object>> userParams = configureUserParams(reqParam);
 
 880         if (!userParams.isEmpty()) {
 
 881             if (reqParam == null) {
 
 882                 requestDetails.setRequestParameters(new RequestParameters());
 
 884             requestDetails.getRequestParameters().setUserParams(userParams);
 
 888     private Service serviceMapper(Map<String, Object> params) throws IOException {
 
 889         ObjectMapper obj = new ObjectMapper();
 
 890         String input = obj.writeValueAsString(params.get("service"));
 
 891         return obj.readValue(input, Service.class);
 
 894     private void addUserParams(Map<String, Object> targetUserParams, List<Map<String, String>> sourceUserParams) {
 
 895         for (Map<String, String> map : sourceUserParams) {
 
 896             for (Map.Entry<String, String> entry : map.entrySet()) {
 
 897                 targetUserParams.put(entry.getKey(), entry.getValue());
 
 902     protected List<Map<String, Object>> configureUserParams(RequestParameters reqParams) throws IOException {
 
 903         logger.debug("Configuring UserParams for Macro Request");
 
 904         Map<String, Object> userParams = new HashMap<>();
 
 906         for (Map<String, Object> params : reqParams.getUserParams()) {
 
 907             if (params.containsKey("service")) {
 
 908                 Service service = serviceMapper(params);
 
 910                 addUserParams(userParams, service.getInstanceParams());
 
 912                 for (Networks network : service.getResources().getNetworks()) {
 
 913                     addUserParams(userParams, network.getInstanceParams());
 
 916                 for (Vnfs vnf : service.getResources().getVnfs()) {
 
 917                     addUserParams(userParams, vnf.getInstanceParams());
 
 919                     for (VfModules vfModule : vnf.getVfModules()) {
 
 920                         addUserParams(userParams, vfModule.getInstanceParams());
 
 926         return mapFlatMapToNameValue(userParams);
 
 929     protected List<Map<String, Object>> mapFlatMapToNameValue(Map<String, Object> flatMap) {
 
 930         List<Map<String, Object>> targetUserParams = new ArrayList<>();
 
 932         for (Map.Entry<String, Object> map : flatMap.entrySet()) {
 
 933             Map<String, Object> targetMap = new HashMap<>();
 
 934             targetMap.put(NAME, map.getKey());
 
 935             targetMap.put(VALUE, map.getValue());
 
 936             targetUserParams.add(targetMap);
 
 938         return targetUserParams;
 
 941     protected CloudConfiguration configureCloudConfig(RequestParameters reqParams) throws IOException {
 
 943         for (Map<String, Object> params : reqParams.getUserParams()) {
 
 944             if (params.containsKey("service")) {
 
 945                 Service service = serviceMapper(params);
 
 947                 Optional<CloudConfiguration> targetConfiguration = addCloudConfig(service.getCloudConfiguration());
 
 949                 if (targetConfiguration.isPresent()) {
 
 950                     return targetConfiguration.get();
 
 952                     for (Networks network : service.getResources().getNetworks()) {
 
 953                         targetConfiguration = addCloudConfig(network.getCloudConfiguration());
 
 954                         if (targetConfiguration.isPresent()) {
 
 955                             return targetConfiguration.get();
 
 959                     for (Vnfs vnf : service.getResources().getVnfs()) {
 
 960                         targetConfiguration = addCloudConfig(vnf.getCloudConfiguration());
 
 962                         if (targetConfiguration.isPresent()) {
 
 963                             return targetConfiguration.get();
 
 966                         for (VfModules vfModule : vnf.getVfModules()) {
 
 967                             targetConfiguration = addCloudConfig(vfModule.getCloudConfiguration());
 
 969                             if (targetConfiguration.isPresent()) {
 
 970                                 return targetConfiguration.get();
 
 981     private RecipeLookupResult getDefaultVnfUri(ServiceInstancesRequest sir, Actions action) {
 
 982         String defaultSource = getDefaultModel(sir);
 
 983         VnfRecipe vnfRecipe = catalogDbClient.getFirstVnfRecipeByNfRoleAndAction(defaultSource, action.toString());
 
 984         if (vnfRecipe == null) {
 
 987         return new RecipeLookupResult(vnfRecipe.getOrchestrationUri(), vnfRecipe.getRecipeTimeout());
 
 991     private RecipeLookupResult getNetworkUri(ServiceInstancesRequest sir, Actions action) throws ValidationException {
 
 992         String defaultNetworkType = getDefaultModel(sir);
 
 993         ModelInfo modelInfo = sir.getRequestDetails().getModelInfo();
 
 994         String modelName = modelInfo.getModelName();
 
 995         Recipe recipe = null;
 
 997         if (modelInfo.getModelCustomizationId() != null) {
 
 998             NetworkResourceCustomization networkResourceCustomization = catalogDbClient
 
 999                     .getNetworkResourceCustomizationByModelCustomizationUUID(modelInfo.getModelCustomizationId());
 
1000             if (networkResourceCustomization != null) {
 
1001                 NetworkResource networkResource = networkResourceCustomization.getNetworkResource();
 
1002                 if (networkResource != null) {
 
1003                     if (modelInfo.getModelVersionId() == null) {
 
1004                         modelInfo.setModelVersionId(networkResource.getModelUUID());
 
1006                     recipe = catalogDbClient.getFirstNetworkRecipeByModelNameAndAction(networkResource.getModelName(),
 
1009                     throw new ValidationException("no catalog entry found");
 
1011             } else if (action != Action.deleteInstance) {
 
1012                 throw new ValidationException("modelCustomizationId for networkResourceCustomization lookup", true);
 
1015             // ok for version < 3 and action delete
 
1016             if (modelName != null) {
 
1017                 recipe = catalogDbClient.getFirstNetworkRecipeByModelNameAndAction(modelName, action.toString());
 
1021         if (recipe == null) {
 
1022             recipe = catalogDbClient.getFirstNetworkRecipeByModelNameAndAction(defaultNetworkType, action.toString());
 
1025         return recipe != null ? new RecipeLookupResult(recipe.getOrchestrationUri(), recipe.getRecipeTimeout()) : null;
 
1029     private Optional<CloudConfiguration> addCloudConfig(CloudConfiguration sourceCloudConfiguration) {
 
1030         CloudConfiguration targetConfiguration = new CloudConfiguration();
 
1031         if (sourceCloudConfiguration != null) {
 
1032             targetConfiguration.setAicNodeClli(sourceCloudConfiguration.getAicNodeClli());
 
1033             targetConfiguration.setTenantId(sourceCloudConfiguration.getTenantId());
 
1034             targetConfiguration.setLcpCloudRegionId(sourceCloudConfiguration.getLcpCloudRegionId());
 
1035             targetConfiguration.setCloudOwner(sourceCloudConfiguration.getCloudOwner());
 
1036             return Optional.of(targetConfiguration);
 
1038         return Optional.empty();
 
1041     private RecipeLookupResult getVnfOrVfModuleUri(ServiceInstancesRequest servInstReq, Actions action)
 
1042             throws ValidationException {
 
1044         ModelInfo modelInfo = servInstReq.getRequestDetails().getModelInfo();
 
1045         String vnfComponentType = modelInfo.getModelType().name();
 
1047         RelatedInstanceList[] instanceList = null;
 
1048         if (servInstReq.getRequestDetails() != null) {
 
1049             instanceList = servInstReq.getRequestDetails().getRelatedInstanceList();
 
1053         String defaultSource = getDefaultModel(servInstReq);
 
1054         String modelCustomizationId = modelInfo.getModelCustomizationId();
 
1055         String modelCustomizationName = modelInfo.getModelCustomizationName();
 
1056         String relatedInstanceModelVersionId = null;
 
1057         String relatedInstanceModelInvariantId = null;
 
1058         String relatedInstanceVersion = null;
 
1059         String relatedInstanceModelCustomizationName = null;
 
1061         if (instanceList != null) {
 
1063             for (RelatedInstanceList relatedInstanceList : instanceList) {
 
1065                 RelatedInstance relatedInstance = relatedInstanceList.getRelatedInstance();
 
1066                 ModelInfo relatedInstanceModelInfo = relatedInstance.getModelInfo();
 
1067                 if (relatedInstanceModelInfo.getModelType().equals(ModelType.service)) {
 
1068                     relatedInstanceModelVersionId = relatedInstanceModelInfo.getModelVersionId();
 
1069                     relatedInstanceVersion = relatedInstanceModelInfo.getModelVersion();
 
1072                 if (relatedInstanceModelInfo.getModelType().equals(ModelType.vnf)) {
 
1073                     relatedInstanceModelVersionId = relatedInstanceModelInfo.getModelVersionId();
 
1074                     relatedInstanceModelInvariantId = relatedInstanceModelInfo.getModelInvariantId();
 
1075                     relatedInstanceVersion = relatedInstanceModelInfo.getModelVersion();
 
1076                     relatedInstanceModelCustomizationName = relatedInstanceModelInfo.getModelCustomizationName();
 
1080             if (modelInfo.getModelType().equals(ModelType.vnf)) {
 
1081                 // a. For a vnf request (only create, no update currently):
 
1082                 // i. (v3-v4) If modelInfo.modelCustomizationId is provided, use it to validate catalog DB has record in
 
1083                 // vnf_resource_customization.model_customization_uuid.
 
1084                 // ii. (v2-v4) If modelInfo.modelCustomizationId is NOT provided (because it is a pre-1702 ASDC model or
 
1085                 // pre-v3), then modelInfo.modelCustomizationName must have
 
1086                 // been provided (else create request should be rejected). APIH should use the
 
1087                 // relatedInstance.modelInfo[service].modelVersionId** + modelInfo[vnf].modelCustomizationName
 
1088                 // to â€œjoinâ€�? service_to_resource_customizations with vnf_resource_customization to confirm a
 
1089                 // vnf_resource_customization.model_customization_uuid record exists.
 
1090                 // **If relatedInstance.modelInfo[service].modelVersionId was not provided, use
 
1091                 // relatedInstance.modelInfo[service].modelInvariantId + modelVersion instead to lookup modelVersionId
 
1092                 // (MODEL_UUID) in SERVICE table.
 
1093                 // iii. Regardless of how the value was provided/obtained above, APIH must always populate
 
1094                 // vnfModelCustomizationId in bpmnRequest. It would be assumed it was MSO generated
 
1095                 // during 1707 data migration if VID did not provide it originally on request.
 
1096                 // iv. Note: continue to construct the â€œvnf-typeâ€�? value and pass to BPMN (must still be populated
 
1098                 // 1. If modelCustomizationName is NOT provided on a vnf/vfModule request, use modelCustomizationId to
 
1099                 // look it up in our catalog to construct vnf-type value to pass to BPMN.
 
1101                 VnfResource vnfResource = null;
 
1102                 VnfResourceCustomization vrc = null;
 
1103                 // Validation for vnfResource
 
1105                 if (modelCustomizationId != null) {
 
1106                     vrc = catalogDbClient.getVnfResourceCustomizationByModelCustomizationUUID(modelCustomizationId);
 
1108                         vnfResource = vrc.getVnfResources();
 
1111                     org.onap.so.db.catalog.beans.Service service =
 
1112                             catalogDbClient.getServiceByID(relatedInstanceModelVersionId);
 
1113                     if (service == null) {
 
1114                         service = catalogDbClient.getServiceByModelVersionAndModelInvariantUUID(relatedInstanceVersion,
 
1115                                 relatedInstanceModelInvariantId);
 
1118                     if (service == null) {
 
1119                         throw new ValidationException("service in relatedInstance");
 
1121                     for (VnfResourceCustomization vnfResourceCustom : service.getVnfCustomizations()) {
 
1122                         if (vnfResourceCustom.getModelInstanceName().equals(modelCustomizationName)) {
 
1123                             vrc = vnfResourceCustom;
 
1128                         vnfResource = vrc.getVnfResources();
 
1129                         modelInfo.setModelCustomizationId(vrc.getModelCustomizationUUID());
 
1130                         modelInfo.setModelCustomizationUuid(vrc.getModelCustomizationUUID());
 
1134                 if (vnfResource == null) {
 
1135                     throw new ValidationException("vnfResource");
 
1137                     if (modelInfo.getModelVersionId() == null) {
 
1138                         modelInfo.setModelVersionId(vnfResource.getModelUUID());
 
1142                 VnfRecipe vnfRecipe = null;
 
1145                     String nfRole = vrc.getNfRole();
 
1146                     if (nfRole != null) {
 
1148                                 catalogDbClient.getFirstVnfRecipeByNfRoleAndAction(vrc.getNfRole(), action.toString());
 
1152                 if (vnfRecipe == null) {
 
1153                     vnfRecipe = catalogDbClient.getFirstVnfRecipeByNfRoleAndAction(defaultSource, action.toString());
 
1156                 if (vnfRecipe == null) {
 
1160                 return new RecipeLookupResult(vnfRecipe.getOrchestrationUri(), vnfRecipe.getRecipeTimeout());
 
1163                  * (v5-v7) If modelInfo.modelCustomizationId is NOT provided (because it is a pre-1702 ASDC model or
 
1164                  * pre-v3), then modelInfo.modelCustomizationName must have // been provided (else create request should
 
1165                  * be rejected). APIH should use the relatedInstance.modelInfo[vnf].modelVersionId +
 
1166                  * modelInfo[vnf].modelCustomizationName // to join vnf_to_resource_customizations with
 
1167                  * vf_resource_customization to confirm a vf_resource_customization.model_customization_uuid record
 
1168                  * exists. // Once the vnfs model_customization_uuid has been obtained, use it to find all vfModule
 
1169                  * customizations for that vnf customization in the vnf_res_custom_to_vf_module_custom join table. //
 
1170                  * For each vf_module_cust_model_customization_uuid value returned, use that UUID to query
 
1171                  * vf_module_customization table along with modelInfo[vfModule|volumeGroup].modelVersionId to // confirm
 
1172                  * record matches request data (and to identify the modelCustomizationId associated with the vfModule in
 
1173                  * the request). This means taking each record found // in vf_module_customization and looking up in
 
1174                  * vf_module (using vf_module_customization’s FK into vf_module) to find a match on
 
1175                  * MODEL_INVARIANT_UUID (modelInvariantId) // and MODEL_VERSION (modelVersion).
 
1177                 VfModuleCustomization vfmc = null;
 
1179                 VnfResourceCustomization vnfrc;
 
1180                 VfModule vfModule = null;
 
1182                 if (modelInfo.getModelCustomizationId() != null) {
 
1183                     vfmc = catalogDbClient
 
1184                             .getVfModuleCustomizationByModelCuztomizationUUID(modelInfo.getModelCustomizationId());
 
1186                     vnfr = catalogDbClient.getVnfResourceByModelUUID(relatedInstanceModelVersionId);
 
1188                         vnfr = catalogDbClient.getFirstVnfResourceByModelInvariantUUIDAndModelVersion(
 
1189                                 relatedInstanceModelInvariantId, relatedInstanceVersion);
 
1191                     vnfrc = catalogDbClient.getFirstVnfResourceCustomizationByModelInstanceNameAndVnfResources(
 
1192                             relatedInstanceModelCustomizationName, vnfr);
 
1194                     List<VfModuleCustomization> list = vnfrc.getVfModuleCustomizations();
 
1196                     String vfModuleModelUUID = modelInfo.getModelVersionId();
 
1197                     for (VfModuleCustomization vf : list) {
 
1198                         VfModuleCustomization vfmCustom;
 
1199                         if (vfModuleModelUUID != null) {
 
1200                             vfmCustom = catalogDbClient
 
1201                                     .getVfModuleCustomizationByModelCustomizationUUIDAndVfModuleModelUUID(
 
1202                                             vf.getModelCustomizationUUID(), vfModuleModelUUID);
 
1203                             if (vfmCustom != null) {
 
1204                                 vfModule = vfmCustom.getVfModule();
 
1207                             vfmCustom = catalogDbClient
 
1208                                     .getVfModuleCustomizationByModelCuztomizationUUID(vf.getModelCustomizationUUID());
 
1209                             if (vfmCustom != null) {
 
1210                                 vfModule = vfmCustom.getVfModule();
 
1212                                 vfModule = catalogDbClient.getVfModuleByModelInvariantUUIDAndModelVersion(
 
1213                                         relatedInstanceModelInvariantId, relatedInstanceVersion);
 
1217                         if (vfModule != null) {
 
1218                             modelInfo.setModelCustomizationId(vf.getModelCustomizationUUID());
 
1219                             modelInfo.setModelCustomizationUuid(vf.getModelCustomizationUUID());
 
1225                 if (vfmc == null && vfModule == null) {
 
1226                     throw new ValidationException("vfModuleCustomization");
 
1227                 } else if (vfModule == null && vfmc != null) {
 
1228                     vfModule = vfmc.getVfModule(); // can't be null as vfModuleModelUUID is not-null property in
 
1229                                                    // VfModuleCustomization table
 
1232                 if (modelInfo.getModelVersionId() == null) {
 
1233                     modelInfo.setModelVersionId(vfModule.getModelUUID());
 
1237                 recipe = catalogDbClient.getFirstVnfComponentsRecipeByVfModuleModelUUIDAndVnfComponentTypeAndAction(
 
1238                         vfModule.getModelUUID(), vnfComponentType, action.toString());
 
1239                 if (recipe == null) {
 
1240                     List<VfModule> vfModuleRecords = catalogDbClient
 
1241                             .getVfModuleByModelInvariantUUIDOrderByModelVersionDesc(vfModule.getModelInvariantUUID());
 
1242                     if (!vfModuleRecords.isEmpty()) {
 
1243                         for (VfModule record : vfModuleRecords) {
 
1244                             recipe = catalogDbClient
 
1245                                     .getFirstVnfComponentsRecipeByVfModuleModelUUIDAndVnfComponentTypeAndAction(
 
1246                                             record.getModelUUID(), vnfComponentType, action.toString());
 
1247                             if (recipe != null) {
 
1253                 if (recipe == null) {
 
1254                     recipe = catalogDbClient.getFirstVnfComponentsRecipeByVfModuleModelUUIDAndVnfComponentTypeAndAction(
 
1255                             defaultSource, vnfComponentType, action.toString());
 
1256                     if (recipe == null) {
 
1257                         recipe = catalogDbClient.getFirstVnfComponentsRecipeByVnfComponentTypeAndAction(
 
1258                                 vnfComponentType, action.toString());
 
1261                     if (recipe == null) {
 
1268             if (modelInfo.getModelType().equals(ModelType.vnf)) {
 
1269                 recipe = catalogDbClient.getFirstVnfRecipeByNfRoleAndAction(defaultSource, action.toString());
 
1270                 if (recipe == null) {
 
1274                 recipe = catalogDbClient.getFirstVnfComponentsRecipeByVfModuleModelUUIDAndVnfComponentTypeAndAction(
 
1275                         defaultSource, vnfComponentType, action.toString());
 
1277                 if (recipe == null) {
 
1283         return new RecipeLookupResult(recipe.getOrchestrationUri(), recipe.getRecipeTimeout());