b078aed6acef39765acbd0f03b1be7e79f6e1b9c
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
14  * 
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  * 
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=========================================================
23  */
24
25 package org.onap.so.apihandlerinfra;
26
27
28 import static org.onap.so.logger.HttpHeadersConstants.REQUESTOR_ID;
29 import java.io.IOException;
30 import java.net.URL;
31 import java.sql.Timestamp;
32 import java.util.ArrayList;
33 import java.util.HashMap;
34 import java.util.List;
35 import java.util.Map;
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;
98 import org.slf4j.MDC;
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;
106
107 @Component
108 public class RequestHandlerUtils extends AbstractRestHandler {
109
110     private static Logger logger = LoggerFactory.getLogger(RequestHandlerUtils.class);
111
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";
115
116     @Autowired
117     private Environment env;
118
119     @Autowired
120     private RequestClientFactory reqClientFactory;
121
122     @Autowired
123     private ResponseBuilder builder;
124
125     @Autowired
126     private MsoRequest msoRequest;
127
128     @Autowired
129     private CamundaRequestHandler camundaRequestHandler;
130
131     @Autowired
132     private CatalogDbClient catalogDbClient;
133
134     public Response postBPELRequest(InfraActiveRequests currentActiveReq, RequestClientParameter requestClientParameter,
135             String orchestrationUri, String requestScope) throws ApiException {
136         HttpResponse response = null;
137         RequestClient requestClient = null;
138
139         try {
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;
153         }
154
155         if (response == null) {
156
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;
164         }
165
166         ResponseHandler respHandler = null;
167         int bpelStatus = 500;
168         try {
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;
182         }
183
184         // BPEL accepted the request, the request is in progress
185         if (bpelStatus == HttpStatus.SC_ACCEPTED) {
186             ServiceInstancesResponse jsonResponse;
187             CamundaResponse camundaResp = respHandler.getResponse();
188
189             if ("Success".equalsIgnoreCase(camundaResp.getMessage())) {
190                 try {
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());
198                     } else {
199                         jsonResponse.getRequestReferences().setRequestSelfLink(null);
200                     }
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;
212                 }
213                 return builder.buildResponse(HttpStatus.SC_ACCEPTED, requestClientParameter.getRequestId(),
214                         jsonResponse, requestClientParameter.getApiVersion());
215             }
216         }
217
218         List<String> variables = new ArrayList<>();
219         variables.add(bpelStatus + "");
220         String camundaJSONResponseBody = respHandler.getResponseBody();
221         if (camundaJSONResponseBody != null && !camundaJSONResponseBody.isEmpty()) {
222
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();
229
230             updateStatus(currentActiveReq, Status.FAILED, bpmnException.getMessage());
231
232             throw bpmnException;
233         } else {
234
235             ErrorLoggerInfo errorLoggerInfo =
236                     new ErrorLoggerInfo.Builder(MessageEnum.APIH_BPEL_RESPONSE_ERROR, ErrorCode.BusinessProcessError)
237                             .errorSource(requestClient.getUrl()).build();
238
239
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());
243
244             throw servException;
245         }
246     }
247
248
249
250     @Override
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);
259             try {
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();
268             }
269         }
270     }
271
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());
277         } else {
278             String requestScope;
279             if (sir.getRequestDetails().getModelInfo().getModelType() == null) {
280                 requestScope = requestScopeFromUri(requestUri);
281             } else {
282                 requestScope = sir.getRequestDetails().getModelInfo().getModelType().name();
283             }
284             return requestScope;
285         }
286     }
287
288
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);
293         }
294         if (!headers.containsKey(ONAPLogConstants.Headers.PARTNER_NAME)) {
295             throw new ValidationException(ONAPLogConstants.Headers.PARTNER_NAME + " header", true);
296         }
297         if (!headers.containsKey(REQUESTOR_ID)) {
298             throw new ValidationException(REQUESTOR_ID + " header", true);
299         }
300     }
301
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());
307         return requestUri;
308     }
309
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;
314
315         dup = duplicateCheck(action, instanceIdMap, instanceName, requestScope, currentActiveReq);
316
317         if (dup != null) {
318             inProgress = camundaHistoryCheck(dup, currentActiveReq);
319         }
320
321         if (dup != null && inProgress) {
322             buildErrorOnDuplicateRecord(currentActiveReq, action, instanceIdMap, instanceName, requestScope, dup);
323         }
324     }
325
326     public InfraActiveRequests duplicateCheck(Actions action, HashMap<String, String> instanceIdMap,
327             String instanceName, String requestScope, InfraActiveRequests currentActiveReq) throws ApiException {
328         InfraActiveRequests dup = null;
329         try {
330             if (!(instanceName == null && "service".equals(requestScope) && (action == Action.createInstance
331                     || action == Action.activateInstance || action == Action.assignInstance))) {
332                 dup = infraActiveRequestsClient.checkInstanceNameDuplicate(instanceIdMap, instanceName, requestScope);
333             }
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;
344         }
345         return dup;
346     }
347
348     public boolean camundaHistoryCheck(InfraActiveRequests duplicateRecord, InfraActiveRequests currentActiveReq)
349             throws RequestDbFailureException, ContactCamundaException {
350         String requestId = duplicateRecord.getRequestId();
351         ResponseEntity<List<HistoricProcessInstanceEntity>> response = null;
352         try {
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)
360                                     .build();
361             updateStatus(currentActiveReq, Status.FAILED, contactCamundaException.getMessage());
362             throw contactCamundaException;
363         }
364
365         if (response.getBody().isEmpty()) {
366             updateStatus(duplicateRecord, Status.COMPLETE, "Request Completed");
367         }
368         for (HistoricProcessInstance instance : response.getBody()) {
369             if (("ACTIVE").equals(instance.getState())) {
370                 return true;
371             } else {
372                 updateStatus(duplicateRecord, Status.COMPLETE, "Request Completed");
373             }
374         }
375         return false;
376     }
377
378     public ServiceInstancesRequest convertJsonToServiceInstanceRequest(String requestJSON, Actions action,
379             String requestId, String requestUri) throws ApiException {
380         try {
381             ObjectMapper mapper = new ObjectMapper();
382             return mapper.readValue(requestJSON, ServiceInstancesRequest.class);
383
384         } catch (IOException e) {
385
386             ErrorLoggerInfo errorLoggerInfo =
387                     new ErrorLoggerInfo.Builder(MessageEnum.APIH_REQUEST_VALIDATION_ERROR, ErrorCode.SchemaError)
388                             .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
389
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);
394
395             msoRequest.createErrorRequestRecord(Status.FAILED, requestId, validateException.getMessage(), action,
396                     requestScope, requestJSON);
397
398             throw validateException;
399         }
400     }
401
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));
406         try {
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();
416
417             updateStatus(currentActiveReq, Status.FAILED, validateException.getMessage());
418
419             throw validateException;
420         }
421     }
422
423     public void buildErrorOnDuplicateRecord(InfraActiveRequests currentActiveReq, Actions action,
424             HashMap<String, String> instanceIdMap, String instanceName, String requestScope, InfraActiveRequests dup)
425             throws ApiException {
426
427         String instance = null;
428         if (instanceName != null) {
429             instance = instanceName;
430         } else {
431             instance = instanceIdMap.get(requestScope + "InstanceId");
432         }
433         ErrorLoggerInfo errorLoggerInfo =
434                 new ErrorLoggerInfo.Builder(MessageEnum.APIH_DUPLICATE_FOUND, ErrorCode.SchemaError)
435                         .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
436
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();
441
442         updateStatus(currentActiveReq, Status.FAILED, dupException.getMessage());
443
444         throw dupException;
445     }
446
447     @Override
448     public String getRequestId(ContainerRequestContext requestContext) throws ValidateException {
449         String requestId = null;
450         if (requestContext.getProperty("requestId") != null) {
451             requestId = requestContext.getProperty("requestId").toString();
452         }
453         if (UUIDChecker.isValidUUID(requestId)) {
454             return requestId;
455         } else {
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();
463
464             throw validateException;
465         }
466     }
467
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);
485             }
486         } else if (instanceIdMap != null && !instanceIdMap.isEmpty()) {
487             if (instanceIdMap.get("serviceInstanceId") != null) {
488                 currentActiveReq.setServiceInstanceId(instanceIdMap.get("serviceInstanceId"));
489             }
490             if (instanceIdMap.get("vnfInstanceId") != null) {
491                 currentActiveReq.setVnfId(instanceIdMap.get("vnfInstanceId"));
492             }
493             if (instanceIdMap.get("vfModuleInstanceId") != null) {
494                 currentActiveReq.setVfModuleId(instanceIdMap.get("vfModuleInstanceId"));
495             }
496             if (instanceIdMap.get("volumeGroupInstanceId") != null) {
497                 currentActiveReq.setVolumeGroupId(instanceIdMap.get("volumeGroupInstanceId"));
498             }
499             if (instanceIdMap.get("networkInstanceId") != null) {
500                 currentActiveReq.setNetworkId(instanceIdMap.get("networkInstanceId"));
501             }
502             if (instanceIdMap.get("configurationInstanceId") != null) {
503                 currentActiveReq.setConfigurationId(instanceIdMap.get("configurationInstanceId"));
504             }
505             if (instanceIdMap.get(CommonConstants.INSTANCE_GROUP_INSTANCE_ID) != null) {
506                 currentActiveReq.setInstanceGroupId(instanceIdMap.get(CommonConstants.INSTANCE_GROUP_INSTANCE_ID));
507             }
508         }
509     }
510
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);
517         } else {
518             return msoRawRequest;
519         }
520     }
521
522     public Optional<String> retrieveModelName(RequestParameters requestParams) {
523         String requestTestApi = null;
524         TestApi testApi = null;
525
526         if (requestParams != null) {
527             requestTestApi = requestParams.getTestApi();
528         }
529
530         if (requestTestApi == null) {
531             if (requestParams != null && requestParams.getALaCarte() != null && !requestParams.getALaCarte()) {
532                 requestTestApi = env.getProperty(CommonConstants.MACRO_TEST_API);
533             } else {
534                 requestTestApi = env.getProperty(CommonConstants.ALACARTE_TEST_API);
535             }
536         }
537
538         try {
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);
544         }
545     }
546
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();
552         }
553         return defaultModel;
554     }
555
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;
561             if (aLaCarteFlag) {
562                 serviceRecord = catalogDbClient.getFirstByModelNameOrderByModelVersionDesc(defaultServiceModelName);
563                 if (serviceRecord != null) {
564                     serviceType = serviceRecord.getServiceType();
565                 }
566             } else {
567                 serviceRecord =
568                         catalogDbClient.getServiceByID(sir.getRequestDetails().getModelInfo().getModelVersionId());
569                 if (serviceRecord != null) {
570                     serviceType = serviceRecord.getServiceType();
571                 } else {
572                     serviceRecord = catalogDbClient.getFirstByModelNameOrderByModelVersionDesc(defaultServiceModelName);
573                     if (serviceRecord != null) {
574                         serviceType = serviceRecord.getServiceType();
575                     }
576                 }
577             }
578         } else {
579             serviceType = msoRequest.getServiceInstanceType(sir, requestScope);
580         }
581         return serviceType;
582     }
583
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();
594                     }
595                 }
596             }
597         }
598         return null;
599     }
600
601     private String requestScopeFromUri(String requestUri) {
602         String requestScope;
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();
613         } else {
614             requestScope = ModelType.service.name();
615         }
616         return requestScope;
617     }
618
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);
639         }
640         return request;
641     }
642
643     protected void setInstanceIdAndName(InfraActiveRequests infraActiveRequest,
644             InfraActiveRequests currentActiveRequest) throws ApiException {
645         String requestScope = infraActiveRequest.getRequestScope();
646         try {
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;
658             }
659             if (instanceName != null) {
660                 type.setName(currentActiveRequest, instanceName);
661             }
662             type.setId(currentActiveRequest, type.getId(infraActiveRequest));
663         } catch (IllegalArgumentException e) {
664             logger.error(
665                     "requestScope \"{}\" does not match a ModelType enum. Unable to set instanceId and instanceName from the original request.",
666                     requestScope);
667         }
668     }
669
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
673         VfModule vfm = null;
674         String modelVersionId = modelInfo.getModelVersionId();
675         Boolean isBaseVfModule = false;
676
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());
682         }
683
684         if (vfm != null) {
685             if (vfm.getIsBase()) {
686                 isBaseVfModule = true;
687             }
688         } else if (action == Action.createInstance || action == Action.updateInstance) {
689             String serviceVersionText = "";
690             if (sdcServiceModelVersion != null && !sdcServiceModelVersion.isEmpty()) {
691                 serviceVersionText = " with version " + sdcServiceModelVersion;
692             }
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;
702         }
703         return isBaseVfModule;
704     }
705
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;
711         } else {
712             return modelInfo.getModelType();
713         }
714     }
715
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");
721     }
722
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
729
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)) {
735             try {
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();
741
742
743                 ValidateException validateException =
744                         new ValidateException.Builder(e.getMessage(), HttpStatus.SC_BAD_REQUEST,
745                                 ErrorNumbers.SVC_BAD_PARAMETER).cause(e).errorInfo(errorLoggerInfo).build();
746
747                 updateStatus(currentActiveReq, Status.FAILED, validateException.getMessage());
748
749                 throw validateException;
750             }
751         } else if (modelInfo.getModelType().equals(ModelType.vfModule)
752                 || modelInfo.getModelType().equals(ModelType.volumeGroup)
753                 || modelInfo.getModelType().equals(ModelType.vnf)) {
754             try {
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();
760
761
762                 ValidateException validateException =
763                         new ValidateException.Builder(e.getMessage(), HttpStatus.SC_BAD_REQUEST,
764                                 ErrorNumbers.SVC_BAD_PARAMETER).cause(e).errorInfo(errorLoggerInfo).build();
765
766                 updateStatus(currentActiveReq, Status.FAILED, validateException.getMessage());
767
768                 throw validateException;
769             }
770         } else if (modelInfo.getModelType().equals(ModelType.network)) {
771             try {
772                 recipeLookupResult = getNetworkUri(sir, action);
773             } catch (ValidationException e) {
774
775                 ErrorLoggerInfo errorLoggerInfo =
776                         new ErrorLoggerInfo.Builder(MessageEnum.APIH_REQUEST_VALIDATION_ERROR, ErrorCode.SchemaError)
777                                 .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
778
779
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());
784
785                 throw validateException;
786             }
787         } else if (modelInfo.getModelType().equals(ModelType.instanceGroup)) {
788             recipeLookupResult = new RecipeLookupResult("/mso/async/services/WorkflowActionBB", 180);
789         }
790
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();
795
796
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)
800                                     .build();
801
802             updateStatus(currentActiveReq, Status.FAILED, recipeNotFoundExceptionException.getMessage());
803             throw recipeNotFoundExceptionException;
804         }
805         return recipeLookupResult;
806     }
807
808     protected RecipeLookupResult getServiceURI(ServiceInstancesRequest servInstReq, Actions action,
809             boolean alaCarteFlag) throws IOException {
810         // SERVICE REQUEST
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;
819
820         if (alaCarteFlag) {
821             serviceRecord = catalogDbClient.getFirstByModelNameOrderByModelVersionDesc(defaultServiceModelName);
822             if (serviceRecord != null) {
823                 recipe = catalogDbClient.getFirstByServiceModelUUIDAndAction(serviceRecord.getModelUUID(),
824                         action.toString());
825             }
826         } else {
827             serviceRecord = catalogDbClient.getServiceByID(modelInfo.getModelVersionId());
828             recipe = catalogDbClient.getFirstByServiceModelUUIDAndAction(modelInfo.getModelVersionId(),
829                     action.toString());
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(),
836                                 action.toString());
837                         if (recipe != null) {
838                             break;
839                         }
840                     }
841                 }
842             }
843         }
844
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) {
848             return 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(),
854                     action.toString());
855         }
856         if (modelInfo.getModelVersionId() == null) {
857             modelInfo.setModelVersionId(serviceRecord.getModelUUID());
858         }
859         if (recipe == null) {
860             return null;
861         }
862         return new RecipeLookupResult(recipe.getOrchestrationUri(), recipe.getRecipeTimeout());
863     }
864
865     protected void mapToLegacyRequest(RequestDetails requestDetails) throws IOException {
866         RequestParameters reqParam;
867         if (requestDetails.getRequestParameters() == null) {
868             reqParam = new RequestParameters();
869         } else {
870             reqParam = requestDetails.getRequestParameters();
871         }
872         if (requestDetails.getCloudConfiguration() == null) {
873             CloudConfiguration cloudConfig = configureCloudConfig(reqParam);
874             if (cloudConfig != null) {
875                 requestDetails.setCloudConfiguration(cloudConfig);
876             }
877         }
878
879         List<Map<String, Object>> userParams = configureUserParams(reqParam);
880         if (!userParams.isEmpty()) {
881             if (reqParam == null) {
882                 requestDetails.setRequestParameters(new RequestParameters());
883             }
884             requestDetails.getRequestParameters().setUserParams(userParams);
885         }
886     }
887
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);
892     }
893
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());
898             }
899         }
900     }
901
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<>();
905
906         for (Map<String, Object> params : reqParams.getUserParams()) {
907             if (params.containsKey("service")) {
908                 Service service = serviceMapper(params);
909
910                 addUserParams(userParams, service.getInstanceParams());
911
912                 for (Networks network : service.getResources().getNetworks()) {
913                     addUserParams(userParams, network.getInstanceParams());
914                 }
915
916                 for (Vnfs vnf : service.getResources().getVnfs()) {
917                     addUserParams(userParams, vnf.getInstanceParams());
918
919                     for (VfModules vfModule : vnf.getVfModules()) {
920                         addUserParams(userParams, vfModule.getInstanceParams());
921                     }
922                 }
923             }
924         }
925
926         return mapFlatMapToNameValue(userParams);
927     }
928
929     protected List<Map<String, Object>> mapFlatMapToNameValue(Map<String, Object> flatMap) {
930         List<Map<String, Object>> targetUserParams = new ArrayList<>();
931
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);
937         }
938         return targetUserParams;
939     }
940
941     protected CloudConfiguration configureCloudConfig(RequestParameters reqParams) throws IOException {
942
943         for (Map<String, Object> params : reqParams.getUserParams()) {
944             if (params.containsKey("service")) {
945                 Service service = serviceMapper(params);
946
947                 Optional<CloudConfiguration> targetConfiguration = addCloudConfig(service.getCloudConfiguration());
948
949                 if (targetConfiguration.isPresent()) {
950                     return targetConfiguration.get();
951                 } else {
952                     for (Networks network : service.getResources().getNetworks()) {
953                         targetConfiguration = addCloudConfig(network.getCloudConfiguration());
954                         if (targetConfiguration.isPresent()) {
955                             return targetConfiguration.get();
956                         }
957                     }
958
959                     for (Vnfs vnf : service.getResources().getVnfs()) {
960                         targetConfiguration = addCloudConfig(vnf.getCloudConfiguration());
961
962                         if (targetConfiguration.isPresent()) {
963                             return targetConfiguration.get();
964                         }
965
966                         for (VfModules vfModule : vnf.getVfModules()) {
967                             targetConfiguration = addCloudConfig(vfModule.getCloudConfiguration());
968
969                             if (targetConfiguration.isPresent()) {
970                                 return targetConfiguration.get();
971                             }
972                         }
973                     }
974                 }
975             }
976         }
977
978         return null;
979     }
980
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) {
985             return null;
986         }
987         return new RecipeLookupResult(vnfRecipe.getOrchestrationUri(), vnfRecipe.getRecipeTimeout());
988     }
989
990
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;
996
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());
1005                     }
1006                     recipe = catalogDbClient.getFirstNetworkRecipeByModelNameAndAction(networkResource.getModelName(),
1007                             action.toString());
1008                 } else {
1009                     throw new ValidationException("no catalog entry found");
1010                 }
1011             } else if (action != Action.deleteInstance) {
1012                 throw new ValidationException("modelCustomizationId for networkResourceCustomization lookup", true);
1013             }
1014         } else {
1015             // ok for version < 3 and action delete
1016             if (modelName != null) {
1017                 recipe = catalogDbClient.getFirstNetworkRecipeByModelNameAndAction(modelName, action.toString());
1018             }
1019         }
1020
1021         if (recipe == null) {
1022             recipe = catalogDbClient.getFirstNetworkRecipeByModelNameAndAction(defaultNetworkType, action.toString());
1023         }
1024
1025         return recipe != null ? new RecipeLookupResult(recipe.getOrchestrationUri(), recipe.getRecipeTimeout()) : null;
1026     }
1027
1028
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);
1037         }
1038         return Optional.empty();
1039     }
1040
1041     private RecipeLookupResult getVnfOrVfModuleUri(ServiceInstancesRequest servInstReq, Actions action)
1042             throws ValidationException {
1043
1044         ModelInfo modelInfo = servInstReq.getRequestDetails().getModelInfo();
1045         String vnfComponentType = modelInfo.getModelType().name();
1046
1047         RelatedInstanceList[] instanceList = null;
1048         if (servInstReq.getRequestDetails() != null) {
1049             instanceList = servInstReq.getRequestDetails().getRelatedInstanceList();
1050         }
1051
1052         Recipe recipe;
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;
1060
1061         if (instanceList != null) {
1062
1063             for (RelatedInstanceList relatedInstanceList : instanceList) {
1064
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();
1070                 }
1071
1072                 if (relatedInstanceModelInfo.getModelType().equals(ModelType.vnf)) {
1073                     relatedInstanceModelVersionId = relatedInstanceModelInfo.getModelVersionId();
1074                     relatedInstanceModelInvariantId = relatedInstanceModelInfo.getModelInvariantId();
1075                     relatedInstanceVersion = relatedInstanceModelInfo.getModelVersion();
1076                     relatedInstanceModelCustomizationName = relatedInstanceModelInfo.getModelCustomizationName();
1077                 }
1078             }
1079
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
1097                 // in A&AI).
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.
1100
1101                 VnfResource vnfResource = null;
1102                 VnfResourceCustomization vrc = null;
1103                 // Validation for vnfResource
1104
1105                 if (modelCustomizationId != null) {
1106                     vrc = catalogDbClient.getVnfResourceCustomizationByModelCustomizationUUID(modelCustomizationId);
1107                     if (vrc != null) {
1108                         vnfResource = vrc.getVnfResources();
1109                     }
1110                 } else {
1111                     org.onap.so.db.catalog.beans.Service service =
1112                             catalogDbClient.getServiceByID(relatedInstanceModelVersionId);
1113                     if (service == null) {
1114                         service = catalogDbClient.getServiceByModelVersionAndModelInvariantUUID(relatedInstanceVersion,
1115                                 relatedInstanceModelInvariantId);
1116                     }
1117
1118                     if (service == null) {
1119                         throw new ValidationException("service in relatedInstance");
1120                     }
1121                     for (VnfResourceCustomization vnfResourceCustom : service.getVnfCustomizations()) {
1122                         if (vnfResourceCustom.getModelInstanceName().equals(modelCustomizationName)) {
1123                             vrc = vnfResourceCustom;
1124                         }
1125                     }
1126
1127                     if (vrc != null) {
1128                         vnfResource = vrc.getVnfResources();
1129                         modelInfo.setModelCustomizationId(vrc.getModelCustomizationUUID());
1130                         modelInfo.setModelCustomizationUuid(vrc.getModelCustomizationUUID());
1131                     }
1132                 }
1133
1134                 if (vnfResource == null) {
1135                     throw new ValidationException("vnfResource");
1136                 } else {
1137                     if (modelInfo.getModelVersionId() == null) {
1138                         modelInfo.setModelVersionId(vnfResource.getModelUUID());
1139                     }
1140                 }
1141
1142                 VnfRecipe vnfRecipe = null;
1143
1144                 if (vrc != null) {
1145                     String nfRole = vrc.getNfRole();
1146                     if (nfRole != null) {
1147                         vnfRecipe =
1148                                 catalogDbClient.getFirstVnfRecipeByNfRoleAndAction(vrc.getNfRole(), action.toString());
1149                     }
1150                 }
1151
1152                 if (vnfRecipe == null) {
1153                     vnfRecipe = catalogDbClient.getFirstVnfRecipeByNfRoleAndAction(defaultSource, action.toString());
1154                 }
1155
1156                 if (vnfRecipe == null) {
1157                     return null;
1158                 }
1159
1160                 return new RecipeLookupResult(vnfRecipe.getOrchestrationUri(), vnfRecipe.getRecipeTimeout());
1161             } else {
1162                 /*
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).
1176                  */
1177                 VfModuleCustomization vfmc = null;
1178                 VnfResource vnfr;
1179                 VnfResourceCustomization vnfrc;
1180                 VfModule vfModule = null;
1181
1182                 if (modelInfo.getModelCustomizationId() != null) {
1183                     vfmc = catalogDbClient
1184                             .getVfModuleCustomizationByModelCuztomizationUUID(modelInfo.getModelCustomizationId());
1185                 } else {
1186                     vnfr = catalogDbClient.getVnfResourceByModelUUID(relatedInstanceModelVersionId);
1187                     if (vnfr == null) {
1188                         vnfr = catalogDbClient.getFirstVnfResourceByModelInvariantUUIDAndModelVersion(
1189                                 relatedInstanceModelInvariantId, relatedInstanceVersion);
1190                     }
1191                     vnfrc = catalogDbClient.getFirstVnfResourceCustomizationByModelInstanceNameAndVnfResources(
1192                             relatedInstanceModelCustomizationName, vnfr);
1193
1194                     List<VfModuleCustomization> list = vnfrc.getVfModuleCustomizations();
1195
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();
1205                             }
1206                         } else {
1207                             vfmCustom = catalogDbClient
1208                                     .getVfModuleCustomizationByModelCuztomizationUUID(vf.getModelCustomizationUUID());
1209                             if (vfmCustom != null) {
1210                                 vfModule = vfmCustom.getVfModule();
1211                             } else {
1212                                 vfModule = catalogDbClient.getVfModuleByModelInvariantUUIDAndModelVersion(
1213                                         relatedInstanceModelInvariantId, relatedInstanceVersion);
1214                             }
1215                         }
1216
1217                         if (vfModule != null) {
1218                             modelInfo.setModelCustomizationId(vf.getModelCustomizationUUID());
1219                             modelInfo.setModelCustomizationUuid(vf.getModelCustomizationUUID());
1220                             break;
1221                         }
1222                     }
1223                 }
1224
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
1230                 }
1231
1232                 if (modelInfo.getModelVersionId() == null) {
1233                     modelInfo.setModelVersionId(vfModule.getModelUUID());
1234                 }
1235
1236
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) {
1248                                 break;
1249                             }
1250                         }
1251                     }
1252                 }
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());
1259                     }
1260
1261                     if (recipe == null) {
1262                         return null;
1263                     }
1264                 }
1265             }
1266         } else {
1267
1268             if (modelInfo.getModelType().equals(ModelType.vnf)) {
1269                 recipe = catalogDbClient.getFirstVnfRecipeByNfRoleAndAction(defaultSource, action.toString());
1270                 if (recipe == null) {
1271                     return null;
1272                 }
1273             } else {
1274                 recipe = catalogDbClient.getFirstVnfComponentsRecipeByVfModuleModelUUIDAndVnfComponentTypeAndAction(
1275                         defaultSource, vnfComponentType, action.toString());
1276
1277                 if (recipe == null) {
1278                     return null;
1279                 }
1280             }
1281         }
1282
1283         return new RecipeLookupResult(recipe.getOrchestrationUri(), recipe.getRecipeTimeout());
1284     }
1285 }