Merge "Pass error message in the exception when thrown from CDS-Adapter"
[so.git] / mso-api-handlers / mso-api-handler-infra / src / main / java / org / onap / so / apihandlerinfra / RequestHandlerUtils.java
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 = requestScopeFromUri(requestUri);;
279
280             if (sir.getRequestDetails() == null) {
281                 return requestScope;
282             }
283             if (sir.getRequestDetails().getModelInfo() == null) {
284                 return requestScope;
285             }
286             if (sir.getRequestDetails().getModelInfo().getModelType() == null) {
287                 return requestScope;
288             }
289             requestScope = sir.getRequestDetails().getModelInfo().getModelType().name();
290             return requestScope;
291         }
292     }
293
294
295     public void validateHeaders(ContainerRequestContext context) throws ValidationException {
296         MultivaluedMap<String, String> headers = context.getHeaders();
297         if (!headers.containsKey(ONAPLogConstants.Headers.REQUEST_ID)) {
298             throw new ValidationException(ONAPLogConstants.Headers.REQUEST_ID + " header", true);
299         }
300         if (!headers.containsKey(ONAPLogConstants.Headers.PARTNER_NAME)) {
301             throw new ValidationException(ONAPLogConstants.Headers.PARTNER_NAME + " header", true);
302         }
303         if (!headers.containsKey(REQUESTOR_ID)) {
304             throw new ValidationException(REQUESTOR_ID + " header", true);
305         }
306     }
307
308     public String getRequestUri(ContainerRequestContext context, String uriPrefix) {
309         String requestUri = context.getUriInfo().getPath();
310         String httpUrl = MDC.get(LogConstants.URI_BASE).concat(requestUri);
311         MDC.put(LogConstants.HTTP_URL, httpUrl);
312         requestUri = requestUri.substring(requestUri.indexOf(uriPrefix) + uriPrefix.length());
313         return requestUri;
314     }
315
316     public void checkForDuplicateRequests(Actions action, HashMap<String, String> instanceIdMap, String requestScope,
317             InfraActiveRequests currentActiveReq, String instanceName) throws ApiException {
318         InfraActiveRequests dup = null;
319         boolean inProgress = false;
320
321         dup = duplicateCheck(action, instanceIdMap, instanceName, requestScope, currentActiveReq);
322
323         if (dup != null) {
324             inProgress = camundaHistoryCheck(dup, currentActiveReq);
325         }
326
327         if (dup != null && inProgress) {
328             buildErrorOnDuplicateRecord(currentActiveReq, action, instanceIdMap, instanceName, requestScope, dup);
329         }
330     }
331
332     public InfraActiveRequests duplicateCheck(Actions action, HashMap<String, String> instanceIdMap,
333             String instanceName, String requestScope, InfraActiveRequests currentActiveReq) throws ApiException {
334         InfraActiveRequests dup = null;
335         try {
336             if (!(instanceName == null && "service".equals(requestScope) && (action == Action.createInstance
337                     || action == Action.activateInstance || action == Action.assignInstance))) {
338                 dup = infraActiveRequestsClient.checkInstanceNameDuplicate(instanceIdMap, instanceName, requestScope);
339             }
340         } catch (Exception e) {
341             ErrorLoggerInfo errorLoggerInfo =
342                     new ErrorLoggerInfo.Builder(MessageEnum.APIH_DUPLICATE_CHECK_EXC, ErrorCode.DataError)
343                             .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
344             RequestDbFailureException requestDbFailureException =
345                     new RequestDbFailureException.Builder("check for duplicate instance", e.toString(),
346                             HttpStatus.SC_INTERNAL_SERVER_ERROR, ErrorNumbers.SVC_DETAILED_SERVICE_ERROR).cause(e)
347                                     .errorInfo(errorLoggerInfo).build();
348             updateStatus(currentActiveReq, Status.FAILED, requestDbFailureException.getMessage());
349             throw requestDbFailureException;
350         }
351         return dup;
352     }
353
354     public boolean camundaHistoryCheck(InfraActiveRequests duplicateRecord, InfraActiveRequests currentActiveReq)
355             throws RequestDbFailureException, ContactCamundaException {
356         String requestId = duplicateRecord.getRequestId();
357         ResponseEntity<List<HistoricProcessInstanceEntity>> response = null;
358         try {
359             response = camundaRequestHandler.getCamundaProcessInstanceHistory(requestId, true);
360         } catch (RestClientException e) {
361             logger.error("Error querying Camunda for process-instance history for requestId: {}, exception: {}",
362                     requestId, e.getMessage());
363             ContactCamundaException contactCamundaException =
364                     new ContactCamundaException.Builder("process-instance", requestId, e.toString(),
365                             HttpStatus.SC_INTERNAL_SERVER_ERROR, ErrorNumbers.SVC_DETAILED_SERVICE_ERROR).cause(e)
366                                     .build();
367             updateStatus(currentActiveReq, Status.FAILED, contactCamundaException.getMessage());
368             throw contactCamundaException;
369         }
370
371         if (response.getBody().isEmpty()) {
372             updateStatus(duplicateRecord, Status.COMPLETE, "Request Completed");
373         }
374         for (HistoricProcessInstance instance : response.getBody()) {
375             if (("ACTIVE").equals(instance.getState())) {
376                 return true;
377             } else {
378                 updateStatus(duplicateRecord, Status.COMPLETE, "Request Completed");
379             }
380         }
381         return false;
382     }
383
384     public ServiceInstancesRequest convertJsonToServiceInstanceRequest(String requestJSON, Actions action,
385             String requestId, String requestUri) throws ApiException {
386         try {
387             ObjectMapper mapper = new ObjectMapper();
388             return mapper.readValue(requestJSON, ServiceInstancesRequest.class);
389
390         } catch (IOException e) {
391
392             ErrorLoggerInfo errorLoggerInfo =
393                     new ErrorLoggerInfo.Builder(MessageEnum.APIH_REQUEST_VALIDATION_ERROR, ErrorCode.SchemaError)
394                             .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
395
396             ValidateException validateException =
397                     new ValidateException.Builder("Error mapping request: " + e.getMessage(), HttpStatus.SC_BAD_REQUEST,
398                             ErrorNumbers.SVC_BAD_PARAMETER).cause(e).errorInfo(errorLoggerInfo).build();
399             String requestScope = requestScopeFromUri(requestUri);
400
401             msoRequest.createErrorRequestRecord(Status.FAILED, requestId, validateException.getMessage(), action,
402                     requestScope, requestJSON);
403
404             throw validateException;
405         }
406     }
407
408     public void parseRequest(ServiceInstancesRequest sir, HashMap<String, String> instanceIdMap, Actions action,
409             String version, String requestJSON, Boolean aLaCarte, String requestId,
410             InfraActiveRequests currentActiveReq) throws ValidateException, RequestDbFailureException {
411         int reqVersion = Integer.parseInt(version.substring(1));
412         try {
413             msoRequest.parse(sir, instanceIdMap, action, version, requestJSON, reqVersion, aLaCarte);
414         } catch (Exception e) {
415             logger.error("failed to parse request", e);
416             ErrorLoggerInfo errorLoggerInfo =
417                     new ErrorLoggerInfo.Builder(MessageEnum.APIH_REQUEST_VALIDATION_ERROR, ErrorCode.SchemaError)
418                             .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
419             ValidateException validateException =
420                     new ValidateException.Builder("Error parsing request: " + e.getMessage(), HttpStatus.SC_BAD_REQUEST,
421                             ErrorNumbers.SVC_BAD_PARAMETER).cause(e).errorInfo(errorLoggerInfo).build();
422
423             updateStatus(currentActiveReq, Status.FAILED, validateException.getMessage());
424
425             throw validateException;
426         }
427     }
428
429     public void buildErrorOnDuplicateRecord(InfraActiveRequests currentActiveReq, Actions action,
430             HashMap<String, String> instanceIdMap, String instanceName, String requestScope, InfraActiveRequests dup)
431             throws ApiException {
432
433         String instance = null;
434         if (instanceName != null) {
435             instance = instanceName;
436         } else {
437             instance = instanceIdMap.get(requestScope + "InstanceId");
438         }
439         ErrorLoggerInfo errorLoggerInfo =
440                 new ErrorLoggerInfo.Builder(MessageEnum.APIH_DUPLICATE_FOUND, ErrorCode.SchemaError)
441                         .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
442
443         DuplicateRequestException dupException =
444                 new DuplicateRequestException.Builder(requestScope, instance, dup.getRequestStatus(),
445                         dup.getRequestId(), HttpStatus.SC_CONFLICT, ErrorNumbers.SVC_DETAILED_SERVICE_ERROR)
446                                 .errorInfo(errorLoggerInfo).build();
447
448         updateStatus(currentActiveReq, Status.FAILED, dupException.getMessage());
449
450         throw dupException;
451     }
452
453     @Override
454     public String getRequestId(ContainerRequestContext requestContext) throws ValidateException {
455         String requestId = null;
456         if (requestContext.getProperty("requestId") != null) {
457             requestId = requestContext.getProperty("requestId").toString();
458         }
459         if (UUIDChecker.isValidUUID(requestId)) {
460             return requestId;
461         } else {
462             ErrorLoggerInfo errorLoggerInfo =
463                     new ErrorLoggerInfo.Builder(MessageEnum.APIH_BPEL_RESPONSE_ERROR, ErrorCode.SchemaError)
464                             .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
465             ValidateException validateException =
466                     new ValidateException.Builder("Request Id " + requestId + " is not a valid UUID",
467                             HttpStatus.SC_INTERNAL_SERVER_ERROR, ErrorNumbers.SVC_BAD_PARAMETER)
468                                     .errorInfo(errorLoggerInfo).build();
469
470             throw validateException;
471         }
472     }
473
474     public void setInstanceId(InfraActiveRequests currentActiveReq, String requestScope, String instanceId,
475             Map<String, String> instanceIdMap) {
476         if (StringUtils.isNotBlank(instanceId)) {
477             if (ModelType.service.name().equalsIgnoreCase(requestScope)) {
478                 currentActiveReq.setServiceInstanceId(instanceId);
479             } else if (ModelType.vnf.name().equalsIgnoreCase(requestScope)) {
480                 currentActiveReq.setVnfId(instanceId);
481             } else if (ModelType.vfModule.name().equalsIgnoreCase(requestScope)) {
482                 currentActiveReq.setVfModuleId(instanceId);
483             } else if (ModelType.volumeGroup.name().equalsIgnoreCase(requestScope)) {
484                 currentActiveReq.setVolumeGroupId(instanceId);
485             } else if (ModelType.network.name().equalsIgnoreCase(requestScope)) {
486                 currentActiveReq.setNetworkId(instanceId);
487             } else if (ModelType.configuration.name().equalsIgnoreCase(requestScope)) {
488                 currentActiveReq.setConfigurationId(instanceId);
489             } else if (ModelType.instanceGroup.toString().equalsIgnoreCase(requestScope)) {
490                 currentActiveReq.setInstanceGroupId(instanceId);
491             }
492         } else if (instanceIdMap != null && !instanceIdMap.isEmpty()) {
493             if (instanceIdMap.get("serviceInstanceId") != null) {
494                 currentActiveReq.setServiceInstanceId(instanceIdMap.get("serviceInstanceId"));
495             }
496             if (instanceIdMap.get("vnfInstanceId") != null) {
497                 currentActiveReq.setVnfId(instanceIdMap.get("vnfInstanceId"));
498             }
499             if (instanceIdMap.get("vfModuleInstanceId") != null) {
500                 currentActiveReq.setVfModuleId(instanceIdMap.get("vfModuleInstanceId"));
501             }
502             if (instanceIdMap.get("volumeGroupInstanceId") != null) {
503                 currentActiveReq.setVolumeGroupId(instanceIdMap.get("volumeGroupInstanceId"));
504             }
505             if (instanceIdMap.get("networkInstanceId") != null) {
506                 currentActiveReq.setNetworkId(instanceIdMap.get("networkInstanceId"));
507             }
508             if (instanceIdMap.get("configurationInstanceId") != null) {
509                 currentActiveReq.setConfigurationId(instanceIdMap.get("configurationInstanceId"));
510             }
511             if (instanceIdMap.get(CommonConstants.INSTANCE_GROUP_INSTANCE_ID) != null) {
512                 currentActiveReq.setInstanceGroupId(instanceIdMap.get(CommonConstants.INSTANCE_GROUP_INSTANCE_ID));
513             }
514             if (instanceIdMap.get("PnfId") != null) {
515                 currentActiveReq.setPnfId(instanceIdMap.get("PnfId"));
516             }
517         }
518     }
519
520     public String mapJSONtoMSOStyle(String msoRawRequest, ServiceInstancesRequest serviceInstRequest,
521             boolean isAlaCarte, Actions action) throws IOException {
522         ObjectMapper mapper = new ObjectMapper();
523         mapper.setSerializationInclusion(Include.NON_NULL);
524         if (serviceInstRequest != null) {
525             return mapper.writeValueAsString(serviceInstRequest);
526         } else {
527             return msoRawRequest;
528         }
529     }
530
531     public Optional<String> retrieveModelName(RequestParameters requestParams) {
532         String requestTestApi = null;
533         TestApi testApi = null;
534
535         if (requestParams != null) {
536             requestTestApi = requestParams.getTestApi();
537         }
538
539         if (requestTestApi == null) {
540             if (requestParams != null && requestParams.getALaCarte() != null && !requestParams.getALaCarte()) {
541                 requestTestApi = env.getProperty(CommonConstants.MACRO_TEST_API);
542             } else {
543                 requestTestApi = env.getProperty(CommonConstants.ALACARTE_TEST_API);
544             }
545         }
546
547         try {
548             testApi = TestApi.valueOf(requestTestApi);
549             return Optional.of(testApi.getModelName());
550         } catch (Exception e) {
551             logger.warn("Catching the exception on the valueOf enum call and continuing", e);
552             throw new IllegalArgumentException("Invalid TestApi is provided", e);
553         }
554     }
555
556     public String getDefaultModel(ServiceInstancesRequest sir) {
557         String defaultModel = sir.getRequestDetails().getRequestInfo().getSource() + "_DEFAULT";
558         Optional<String> oModelName = retrieveModelName(sir.getRequestDetails().getRequestParameters());
559         if (oModelName.isPresent()) {
560             defaultModel = oModelName.get();
561         }
562         return defaultModel;
563     }
564
565     public String getServiceType(String requestScope, ServiceInstancesRequest sir, Boolean aLaCarteFlag) {
566         String serviceType = null;
567         if (requestScope.equalsIgnoreCase(ModelType.service.toString())) {
568             String defaultServiceModelName = getDefaultModel(sir);
569             org.onap.so.db.catalog.beans.Service serviceRecord;
570             if (aLaCarteFlag) {
571                 serviceRecord = catalogDbClient.getFirstByModelNameOrderByModelVersionDesc(defaultServiceModelName);
572                 if (serviceRecord != null) {
573                     serviceType = serviceRecord.getServiceType();
574                 }
575             } else {
576                 serviceRecord =
577                         catalogDbClient.getServiceByID(sir.getRequestDetails().getModelInfo().getModelVersionId());
578                 if (serviceRecord != null) {
579                     serviceType = serviceRecord.getServiceType();
580                 } else {
581                     serviceRecord = catalogDbClient.getFirstByModelNameOrderByModelVersionDesc(defaultServiceModelName);
582                     if (serviceRecord != null) {
583                         serviceType = serviceRecord.getServiceType();
584                     }
585                 }
586             }
587         } else {
588             serviceType = msoRequest.getServiceInstanceType(sir, requestScope);
589         }
590         return serviceType;
591     }
592
593     protected String setServiceInstanceId(String requestScope, ServiceInstancesRequest sir) {
594         if (sir.getServiceInstanceId() != null) {
595             return sir.getServiceInstanceId();
596         } else if (requestScope.equalsIgnoreCase(ModelType.instanceGroup.toString())) {
597             RelatedInstanceList[] relatedInstances = sir.getRequestDetails().getRelatedInstanceList();
598             if (relatedInstances != null) {
599                 for (RelatedInstanceList relatedInstanceList : relatedInstances) {
600                     RelatedInstance relatedInstance = relatedInstanceList.getRelatedInstance();
601                     if (relatedInstance.getModelInfo().getModelType() == ModelType.service) {
602                         return relatedInstance.getInstanceId();
603                     }
604                 }
605             }
606         }
607         return null;
608     }
609
610     private String requestScopeFromUri(String requestUri) {
611         String requestScope;
612         if (requestUri.contains(ModelType.network.name())) {
613             requestScope = ModelType.network.name();
614         } else if (requestUri.contains(ModelType.vfModule.name())) {
615             requestScope = ModelType.vfModule.name();
616         } else if (requestUri.contains(ModelType.volumeGroup.name())) {
617             requestScope = ModelType.volumeGroup.name();
618         } else if (requestUri.contains(ModelType.configuration.name())) {
619             requestScope = ModelType.configuration.name();
620         } else if (requestUri.contains(ModelType.vnf.name())) {
621             requestScope = ModelType.vnf.name();
622         } else if (requestUri.contains(ModelType.pnf.name())) {
623             requestScope = ModelType.pnf.name();
624         } else {
625             requestScope = ModelType.service.name();
626         }
627         return requestScope;
628     }
629
630     protected InfraActiveRequests createNewRecordCopyFromInfraActiveRequest(InfraActiveRequests infraActiveRequest,
631             String requestId, Timestamp startTimeStamp, String source, String requestUri, String requestorId,
632             String originalRequestId) throws ApiException {
633         InfraActiveRequests request = new InfraActiveRequests();
634         request.setRequestId(requestId);
635         request.setStartTime(startTimeStamp);
636         request.setSource(source);
637         request.setRequestUrl(requestUri);
638         request.setProgress(new Long(5));
639         request.setRequestorId(requestorId);
640         request.setRequestStatus(Status.IN_PROGRESS.toString());
641         request.setOriginalRequestId(originalRequestId);
642         request.setLastModifiedBy(Constants.MODIFIED_BY_APIHANDLER);
643         if (infraActiveRequest != null) {
644             request.setTenantId(infraActiveRequest.getTenantId());
645             request.setRequestBody(updateRequestorIdInRequestBody(infraActiveRequest, requestorId));
646             request.setCloudRegion(infraActiveRequest.getCloudRegion());
647             request.setRequestScope(infraActiveRequest.getRequestScope());
648             request.setRequestAction(infraActiveRequest.getRequestAction());
649             setInstanceIdAndName(infraActiveRequest, request);
650         }
651         return request;
652     }
653
654     protected void setInstanceIdAndName(InfraActiveRequests infraActiveRequest,
655             InfraActiveRequests currentActiveRequest) throws ApiException {
656         String requestScope = infraActiveRequest.getRequestScope();
657         try {
658             ModelType type = ModelType.valueOf(requestScope);
659             String instanceName = type.getName(infraActiveRequest);
660             if (instanceName == null && type.equals(ModelType.vfModule)) {
661                 logger.error("vfModule for requestId: {} being resumed does not have an instanceName.",
662                         infraActiveRequest.getRequestId());
663                 ValidateException validateException = new ValidateException.Builder(
664                         "vfModule for requestId: " + infraActiveRequest.getRequestId()
665                                 + " being resumed does not have an instanceName.",
666                         HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_BAD_PARAMETER).build();
667                 updateStatus(currentActiveRequest, Status.FAILED, validateException.getMessage());
668                 throw validateException;
669             }
670             if (instanceName != null) {
671                 type.setName(currentActiveRequest, instanceName);
672             }
673             type.setId(currentActiveRequest, type.getId(infraActiveRequest));
674         } catch (IllegalArgumentException e) {
675             logger.error(
676                     "requestScope \"{}\" does not match a ModelType enum. Unable to set instanceId and instanceName from the original request.",
677                     requestScope);
678         }
679     }
680
681     protected Boolean getIsBaseVfModule(ModelInfo modelInfo, Actions action, String vnfType,
682             String sdcServiceModelVersion, InfraActiveRequests currentActiveReq) throws ApiException {
683         // Get VF Module-specific base module indicator
684         VfModule vfm = null;
685         String modelVersionId = modelInfo.getModelVersionId();
686         Boolean isBaseVfModule = false;
687
688         if (modelVersionId != null) {
689             vfm = catalogDbClient.getVfModuleByModelUUID(modelVersionId);
690         } else if (modelInfo.getModelInvariantId() != null && modelInfo.getModelVersion() != null) {
691             vfm = catalogDbClient.getVfModuleByModelInvariantUUIDAndModelVersion(modelInfo.getModelInvariantId(),
692                     modelInfo.getModelVersion());
693         }
694
695         if (vfm != null) {
696             if (vfm.getIsBase()) {
697                 isBaseVfModule = true;
698             }
699         } else if (action == Action.createInstance || action == Action.updateInstance) {
700             String serviceVersionText = "";
701             if (sdcServiceModelVersion != null && !sdcServiceModelVersion.isEmpty()) {
702                 serviceVersionText = " with version " + sdcServiceModelVersion;
703             }
704             String errorMessage = "VnfType " + vnfType + " and VF Module Model Name " + modelInfo.getModelName()
705                     + serviceVersionText + " not found in MSO Catalog DB";
706             ErrorLoggerInfo errorLoggerInfo =
707                     new ErrorLoggerInfo.Builder(MessageEnum.APIH_DB_ATTRIBUTE_NOT_FOUND, ErrorCode.DataError)
708                             .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
709             VfModuleNotFoundException vfModuleException = new VfModuleNotFoundException.Builder(errorMessage,
710                     HttpStatus.SC_NOT_FOUND, ErrorNumbers.SVC_BAD_PARAMETER).errorInfo(errorLoggerInfo).build();
711             updateStatus(currentActiveReq, Status.FAILED, vfModuleException.getMessage());
712             throw vfModuleException;
713         }
714         return isBaseVfModule;
715     }
716
717     protected ModelType getModelType(Actions action, ModelInfo modelInfo) {
718         if (action == Action.applyUpdatedConfig || action == Action.inPlaceSoftwareUpdate) {
719             return ModelType.vnf;
720         } else if (action == Action.addMembers || action == Action.removeMembers) {
721             return ModelType.instanceGroup;
722         } else {
723             return modelInfo.getModelType();
724         }
725     }
726
727     protected String updateRequestorIdInRequestBody(InfraActiveRequests infraActiveRequest, String newRequestorId) {
728         String requestBody = infraActiveRequest.getRequestBody();
729         return requestBody.replaceAll(
730                 "(?s)(\"requestInfo\"\\s*?:\\s*?\\{.*?\"requestorId\"\\s*?:\\s*?\")(.*?)(\"[ ]*(?:,|\\R|\\}))",
731                 "$1" + newRequestorId + "$3");
732     }
733
734     public RecipeLookupResult getServiceInstanceOrchestrationURI(ServiceInstancesRequest sir, Actions action,
735             boolean alaCarteFlag, InfraActiveRequests currentActiveReq) throws ApiException {
736         RecipeLookupResult recipeLookupResult = null;
737         // if the aLaCarte flag is set to TRUE, the API-H should choose the VID_DEFAULT recipe for the requested action
738         ModelInfo modelInfo = sir.getRequestDetails().getModelInfo();
739         // Query MSO Catalog DB
740
741         if (action == Action.applyUpdatedConfig || action == Action.inPlaceSoftwareUpdate) {
742             recipeLookupResult = getDefaultVnfUri(sir, action);
743         } else if (action == Action.addMembers || action == Action.removeMembers) {
744             recipeLookupResult = new RecipeLookupResult("/mso/async/services/WorkflowActionBB", 180);
745         } else if (modelInfo.getModelType().equals(ModelType.service)) {
746             try {
747                 recipeLookupResult = getServiceURI(sir, action, alaCarteFlag);
748             } catch (IOException e) {
749                 ErrorLoggerInfo errorLoggerInfo =
750                         new ErrorLoggerInfo.Builder(MessageEnum.APIH_REQUEST_VALIDATION_ERROR, ErrorCode.SchemaError)
751                                 .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
752
753
754                 ValidateException validateException =
755                         new ValidateException.Builder(e.getMessage(), HttpStatus.SC_BAD_REQUEST,
756                                 ErrorNumbers.SVC_BAD_PARAMETER).cause(e).errorInfo(errorLoggerInfo).build();
757
758                 updateStatus(currentActiveReq, Status.FAILED, validateException.getMessage());
759
760                 throw validateException;
761             }
762         } else if (modelInfo.getModelType().equals(ModelType.vfModule)
763                 || modelInfo.getModelType().equals(ModelType.volumeGroup)
764                 || modelInfo.getModelType().equals(ModelType.vnf)) {
765             try {
766                 recipeLookupResult = getVnfOrVfModuleUri(sir, action);
767             } catch (ValidationException e) {
768                 ErrorLoggerInfo errorLoggerInfo =
769                         new ErrorLoggerInfo.Builder(MessageEnum.APIH_REQUEST_VALIDATION_ERROR, ErrorCode.SchemaError)
770                                 .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
771
772
773                 ValidateException validateException =
774                         new ValidateException.Builder(e.getMessage(), HttpStatus.SC_BAD_REQUEST,
775                                 ErrorNumbers.SVC_BAD_PARAMETER).cause(e).errorInfo(errorLoggerInfo).build();
776
777                 updateStatus(currentActiveReq, Status.FAILED, validateException.getMessage());
778
779                 throw validateException;
780             }
781         } else if (modelInfo.getModelType().equals(ModelType.network)) {
782             try {
783                 recipeLookupResult = getNetworkUri(sir, action);
784             } catch (ValidationException e) {
785
786                 ErrorLoggerInfo errorLoggerInfo =
787                         new ErrorLoggerInfo.Builder(MessageEnum.APIH_REQUEST_VALIDATION_ERROR, ErrorCode.SchemaError)
788                                 .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
789
790
791                 ValidateException validateException =
792                         new ValidateException.Builder(e.getMessage(), HttpStatus.SC_BAD_REQUEST,
793                                 ErrorNumbers.SVC_BAD_PARAMETER).cause(e).errorInfo(errorLoggerInfo).build();
794                 updateStatus(currentActiveReq, Status.FAILED, validateException.getMessage());
795
796                 throw validateException;
797             }
798         } else if (modelInfo.getModelType().equals(ModelType.instanceGroup)) {
799             recipeLookupResult = new RecipeLookupResult("/mso/async/services/WorkflowActionBB", 180);
800         }
801
802         if (recipeLookupResult == null) {
803             ErrorLoggerInfo errorLoggerInfo =
804                     new ErrorLoggerInfo.Builder(MessageEnum.APIH_DB_ACCESS_EXC, ErrorCode.DataError)
805                             .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
806
807
808             RecipeNotFoundException recipeNotFoundExceptionException =
809                     new RecipeNotFoundException.Builder("Recipe could not be retrieved from catalog DB.",
810                             HttpStatus.SC_NOT_FOUND, ErrorNumbers.SVC_GENERAL_SERVICE_ERROR).errorInfo(errorLoggerInfo)
811                                     .build();
812
813             updateStatus(currentActiveReq, Status.FAILED, recipeNotFoundExceptionException.getMessage());
814             throw recipeNotFoundExceptionException;
815         }
816         return recipeLookupResult;
817     }
818
819     protected RecipeLookupResult getServiceURI(ServiceInstancesRequest servInstReq, Actions action,
820             boolean alaCarteFlag) throws IOException {
821         // SERVICE REQUEST
822         // Construct the default service name
823         // TODO need to make this a configurable property
824         String defaultServiceModelName = getDefaultModel(servInstReq);
825         RequestDetails requestDetails = servInstReq.getRequestDetails();
826         ModelInfo modelInfo = requestDetails.getModelInfo();
827         org.onap.so.db.catalog.beans.Service serviceRecord;
828         List<org.onap.so.db.catalog.beans.Service> serviceRecordList;
829         ServiceRecipe recipe = null;
830
831         if (alaCarteFlag) {
832             serviceRecord = catalogDbClient.getFirstByModelNameOrderByModelVersionDesc(defaultServiceModelName);
833             if (serviceRecord != null) {
834                 recipe = catalogDbClient.getFirstByServiceModelUUIDAndAction(serviceRecord.getModelUUID(),
835                         action.toString());
836             }
837         } else {
838             serviceRecord = catalogDbClient.getServiceByID(modelInfo.getModelVersionId());
839             recipe = catalogDbClient.getFirstByServiceModelUUIDAndAction(modelInfo.getModelVersionId(),
840                     action.toString());
841             if (recipe == null) {
842                 serviceRecordList = catalogDbClient
843                         .getServiceByModelInvariantUUIDOrderByModelVersionDesc(modelInfo.getModelInvariantId());
844                 if (!serviceRecordList.isEmpty()) {
845                     for (org.onap.so.db.catalog.beans.Service record : serviceRecordList) {
846                         recipe = catalogDbClient.getFirstByServiceModelUUIDAndAction(record.getModelUUID(),
847                                 action.toString());
848                         if (recipe != null) {
849                             break;
850                         }
851                     }
852                 }
853             }
854         }
855
856         // if an aLaCarte flag was sent in the request, throw an error if the recipe was not found
857         RequestParameters reqParam = requestDetails.getRequestParameters();
858         if (reqParam != null && alaCarteFlag && recipe == null) {
859             return null;
860         } else if (!alaCarteFlag && recipe != null && Action.createInstance.equals(action)) {
861             mapToLegacyRequest(requestDetails);
862         } else if (recipe == null) { // aLaCarte wasn't sent, so we'll try the default
863             serviceRecord = catalogDbClient.getFirstByModelNameOrderByModelVersionDesc(defaultServiceModelName);
864             recipe = catalogDbClient.getFirstByServiceModelUUIDAndAction(serviceRecord.getModelUUID(),
865                     action.toString());
866         }
867         if (modelInfo.getModelVersionId() == null) {
868             modelInfo.setModelVersionId(serviceRecord.getModelUUID());
869         }
870         if (recipe == null) {
871             return null;
872         }
873         return new RecipeLookupResult(recipe.getOrchestrationUri(), recipe.getRecipeTimeout());
874     }
875
876     protected void mapToLegacyRequest(RequestDetails requestDetails) throws IOException {
877         RequestParameters reqParam;
878         if (requestDetails.getRequestParameters() == null) {
879             reqParam = new RequestParameters();
880         } else {
881             reqParam = requestDetails.getRequestParameters();
882         }
883         if (requestDetails.getCloudConfiguration() == null) {
884             CloudConfiguration cloudConfig = configureCloudConfig(reqParam);
885             if (cloudConfig != null) {
886                 requestDetails.setCloudConfiguration(cloudConfig);
887             }
888         }
889
890         List<Map<String, Object>> userParams = configureUserParams(reqParam);
891         if (!userParams.isEmpty()) {
892             if (reqParam == null) {
893                 requestDetails.setRequestParameters(new RequestParameters());
894             }
895             requestDetails.getRequestParameters().setUserParams(userParams);
896         }
897     }
898
899     private Service serviceMapper(Map<String, Object> params) throws IOException {
900         ObjectMapper obj = new ObjectMapper();
901         String input = obj.writeValueAsString(params.get("service"));
902         return obj.readValue(input, Service.class);
903     }
904
905     private void addUserParams(Map<String, Object> targetUserParams, List<Map<String, String>> sourceUserParams) {
906         for (Map<String, String> map : sourceUserParams) {
907             for (Map.Entry<String, String> entry : map.entrySet()) {
908                 targetUserParams.put(entry.getKey(), entry.getValue());
909             }
910         }
911     }
912
913     protected List<Map<String, Object>> configureUserParams(RequestParameters reqParams) throws IOException {
914         logger.debug("Configuring UserParams for Macro Request");
915         Map<String, Object> userParams = new HashMap<>();
916
917         for (Map<String, Object> params : reqParams.getUserParams()) {
918             if (params.containsKey("service")) {
919                 Service service = serviceMapper(params);
920
921                 addUserParams(userParams, service.getInstanceParams());
922
923                 for (Networks network : service.getResources().getNetworks()) {
924                     addUserParams(userParams, network.getInstanceParams());
925                 }
926
927                 for (Vnfs vnf : service.getResources().getVnfs()) {
928                     addUserParams(userParams, vnf.getInstanceParams());
929
930                     for (VfModules vfModule : vnf.getVfModules()) {
931                         addUserParams(userParams, vfModule.getInstanceParams());
932                     }
933                 }
934             }
935         }
936
937         return mapFlatMapToNameValue(userParams);
938     }
939
940     protected List<Map<String, Object>> mapFlatMapToNameValue(Map<String, Object> flatMap) {
941         List<Map<String, Object>> targetUserParams = new ArrayList<>();
942
943         for (Map.Entry<String, Object> map : flatMap.entrySet()) {
944             Map<String, Object> targetMap = new HashMap<>();
945             targetMap.put(NAME, map.getKey());
946             targetMap.put(VALUE, map.getValue());
947             targetUserParams.add(targetMap);
948         }
949         return targetUserParams;
950     }
951
952     protected CloudConfiguration configureCloudConfig(RequestParameters reqParams) throws IOException {
953
954         for (Map<String, Object> params : reqParams.getUserParams()) {
955             if (params.containsKey("service")) {
956                 Service service = serviceMapper(params);
957
958                 Optional<CloudConfiguration> targetConfiguration = addCloudConfig(service.getCloudConfiguration());
959
960                 if (targetConfiguration.isPresent()) {
961                     return targetConfiguration.get();
962                 } else {
963                     for (Networks network : service.getResources().getNetworks()) {
964                         targetConfiguration = addCloudConfig(network.getCloudConfiguration());
965                         if (targetConfiguration.isPresent()) {
966                             return targetConfiguration.get();
967                         }
968                     }
969
970                     for (Vnfs vnf : service.getResources().getVnfs()) {
971                         targetConfiguration = addCloudConfig(vnf.getCloudConfiguration());
972
973                         if (targetConfiguration.isPresent()) {
974                             return targetConfiguration.get();
975                         }
976
977                         for (VfModules vfModule : vnf.getVfModules()) {
978                             targetConfiguration = addCloudConfig(vfModule.getCloudConfiguration());
979
980                             if (targetConfiguration.isPresent()) {
981                                 return targetConfiguration.get();
982                             }
983                         }
984                     }
985                 }
986             }
987         }
988
989         return null;
990     }
991
992     private RecipeLookupResult getDefaultVnfUri(ServiceInstancesRequest sir, Actions action) {
993         String defaultSource = getDefaultModel(sir);
994         VnfRecipe vnfRecipe = catalogDbClient.getFirstVnfRecipeByNfRoleAndAction(defaultSource, action.toString());
995         if (vnfRecipe == null) {
996             return null;
997         }
998         return new RecipeLookupResult(vnfRecipe.getOrchestrationUri(), vnfRecipe.getRecipeTimeout());
999     }
1000
1001
1002     private RecipeLookupResult getNetworkUri(ServiceInstancesRequest sir, Actions action) throws ValidationException {
1003         String defaultNetworkType = getDefaultModel(sir);
1004         ModelInfo modelInfo = sir.getRequestDetails().getModelInfo();
1005         String modelName = modelInfo.getModelName();
1006         Recipe recipe = null;
1007
1008         if (modelInfo.getModelCustomizationId() != null) {
1009             NetworkResourceCustomization networkResourceCustomization = catalogDbClient
1010                     .getNetworkResourceCustomizationByModelCustomizationUUID(modelInfo.getModelCustomizationId());
1011             if (networkResourceCustomization != null) {
1012                 NetworkResource networkResource = networkResourceCustomization.getNetworkResource();
1013                 if (networkResource != null) {
1014                     if (modelInfo.getModelVersionId() == null) {
1015                         modelInfo.setModelVersionId(networkResource.getModelUUID());
1016                     }
1017                     recipe = catalogDbClient.getFirstNetworkRecipeByModelNameAndAction(networkResource.getModelName(),
1018                             action.toString());
1019                 } else {
1020                     throw new ValidationException("no catalog entry found");
1021                 }
1022             } else if (action != Action.deleteInstance) {
1023                 throw new ValidationException("modelCustomizationId for networkResourceCustomization lookup", true);
1024             }
1025         } else {
1026             // ok for version < 3 and action delete
1027             if (modelName != null) {
1028                 recipe = catalogDbClient.getFirstNetworkRecipeByModelNameAndAction(modelName, action.toString());
1029             }
1030         }
1031
1032         if (recipe == null) {
1033             recipe = catalogDbClient.getFirstNetworkRecipeByModelNameAndAction(defaultNetworkType, action.toString());
1034         }
1035
1036         return recipe != null ? new RecipeLookupResult(recipe.getOrchestrationUri(), recipe.getRecipeTimeout()) : null;
1037     }
1038
1039
1040     private Optional<CloudConfiguration> addCloudConfig(CloudConfiguration sourceCloudConfiguration) {
1041         CloudConfiguration targetConfiguration = new CloudConfiguration();
1042         if (sourceCloudConfiguration != null) {
1043             targetConfiguration.setAicNodeClli(sourceCloudConfiguration.getAicNodeClli());
1044             targetConfiguration.setTenantId(sourceCloudConfiguration.getTenantId());
1045             targetConfiguration.setLcpCloudRegionId(sourceCloudConfiguration.getLcpCloudRegionId());
1046             targetConfiguration.setCloudOwner(sourceCloudConfiguration.getCloudOwner());
1047             return Optional.of(targetConfiguration);
1048         }
1049         return Optional.empty();
1050     }
1051
1052     private RecipeLookupResult getVnfOrVfModuleUri(ServiceInstancesRequest servInstReq, Actions action)
1053             throws ValidationException {
1054
1055         ModelInfo modelInfo = servInstReq.getRequestDetails().getModelInfo();
1056         String vnfComponentType = modelInfo.getModelType().name();
1057
1058         RelatedInstanceList[] instanceList = null;
1059         if (servInstReq.getRequestDetails() != null) {
1060             instanceList = servInstReq.getRequestDetails().getRelatedInstanceList();
1061         }
1062
1063         Recipe recipe;
1064         String defaultSource = getDefaultModel(servInstReq);
1065         String modelCustomizationId = modelInfo.getModelCustomizationId();
1066         String modelCustomizationName = modelInfo.getModelCustomizationName();
1067         String relatedInstanceModelVersionId = null;
1068         String relatedInstanceModelInvariantId = null;
1069         String relatedInstanceVersion = null;
1070         String relatedInstanceModelCustomizationName = null;
1071
1072         if (instanceList != null) {
1073
1074             for (RelatedInstanceList relatedInstanceList : instanceList) {
1075
1076                 RelatedInstance relatedInstance = relatedInstanceList.getRelatedInstance();
1077                 ModelInfo relatedInstanceModelInfo = relatedInstance.getModelInfo();
1078                 if (relatedInstanceModelInfo.getModelType().equals(ModelType.service)) {
1079                     relatedInstanceModelVersionId = relatedInstanceModelInfo.getModelVersionId();
1080                     relatedInstanceVersion = relatedInstanceModelInfo.getModelVersion();
1081                 }
1082
1083                 if (relatedInstanceModelInfo.getModelType().equals(ModelType.vnf)) {
1084                     relatedInstanceModelVersionId = relatedInstanceModelInfo.getModelVersionId();
1085                     relatedInstanceModelInvariantId = relatedInstanceModelInfo.getModelInvariantId();
1086                     relatedInstanceVersion = relatedInstanceModelInfo.getModelVersion();
1087                     relatedInstanceModelCustomizationName = relatedInstanceModelInfo.getModelCustomizationName();
1088                 }
1089             }
1090
1091             if (modelInfo.getModelType().equals(ModelType.vnf)) {
1092                 // a. For a vnf request (only create, no update currently):
1093                 // i. (v3-v4) If modelInfo.modelCustomizationId is provided, use it to validate catalog DB has record in
1094                 // vnf_resource_customization.model_customization_uuid.
1095                 // ii. (v2-v4) If modelInfo.modelCustomizationId is NOT provided (because it is a pre-1702 ASDC model or
1096                 // pre-v3), then modelInfo.modelCustomizationName must have
1097                 // been provided (else create request should be rejected). APIH should use the
1098                 // relatedInstance.modelInfo[service].modelVersionId** + modelInfo[vnf].modelCustomizationName
1099                 // to â€œjoinâ€�? service_to_resource_customizations with vnf_resource_customization to confirm a
1100                 // vnf_resource_customization.model_customization_uuid record exists.
1101                 // **If relatedInstance.modelInfo[service].modelVersionId was not provided, use
1102                 // relatedInstance.modelInfo[service].modelInvariantId + modelVersion instead to lookup modelVersionId
1103                 // (MODEL_UUID) in SERVICE table.
1104                 // iii. Regardless of how the value was provided/obtained above, APIH must always populate
1105                 // vnfModelCustomizationId in bpmnRequest. It would be assumed it was MSO generated
1106                 // during 1707 data migration if VID did not provide it originally on request.
1107                 // iv. Note: continue to construct the â€œvnf-typeâ€�? value and pass to BPMN (must still be populated
1108                 // in A&AI).
1109                 // 1. If modelCustomizationName is NOT provided on a vnf/vfModule request, use modelCustomizationId to
1110                 // look it up in our catalog to construct vnf-type value to pass to BPMN.
1111
1112                 VnfResource vnfResource = null;
1113                 VnfResourceCustomization vrc = null;
1114                 // Validation for vnfResource
1115
1116                 if (modelCustomizationId != null) {
1117                     vrc = catalogDbClient.getVnfResourceCustomizationByModelCustomizationUUID(modelCustomizationId);
1118                     if (vrc != null) {
1119                         vnfResource = vrc.getVnfResources();
1120                     }
1121                 } else {
1122                     org.onap.so.db.catalog.beans.Service service =
1123                             catalogDbClient.getServiceByID(relatedInstanceModelVersionId);
1124                     if (service == null) {
1125                         service = catalogDbClient.getServiceByModelVersionAndModelInvariantUUID(relatedInstanceVersion,
1126                                 relatedInstanceModelInvariantId);
1127                     }
1128
1129                     if (service == null) {
1130                         throw new ValidationException("service in relatedInstance");
1131                     }
1132                     for (VnfResourceCustomization vnfResourceCustom : service.getVnfCustomizations()) {
1133                         if (vnfResourceCustom.getModelInstanceName().equals(modelCustomizationName)) {
1134                             vrc = vnfResourceCustom;
1135                         }
1136                     }
1137
1138                     if (vrc != null) {
1139                         vnfResource = vrc.getVnfResources();
1140                         modelInfo.setModelCustomizationId(vrc.getModelCustomizationUUID());
1141                         modelInfo.setModelCustomizationUuid(vrc.getModelCustomizationUUID());
1142                     }
1143                 }
1144
1145                 if (vnfResource == null) {
1146                     throw new ValidationException("vnfResource");
1147                 } else {
1148                     if (modelInfo.getModelVersionId() == null) {
1149                         modelInfo.setModelVersionId(vnfResource.getModelUUID());
1150                     }
1151                 }
1152
1153                 VnfRecipe vnfRecipe = null;
1154
1155                 if (vrc != null) {
1156                     String nfRole = vrc.getNfRole();
1157                     if (nfRole != null) {
1158                         vnfRecipe =
1159                                 catalogDbClient.getFirstVnfRecipeByNfRoleAndAction(vrc.getNfRole(), action.toString());
1160                     }
1161                 }
1162
1163                 if (vnfRecipe == null) {
1164                     vnfRecipe = catalogDbClient.getFirstVnfRecipeByNfRoleAndAction(defaultSource, action.toString());
1165                 }
1166
1167                 if (vnfRecipe == null) {
1168                     return null;
1169                 }
1170
1171                 return new RecipeLookupResult(vnfRecipe.getOrchestrationUri(), vnfRecipe.getRecipeTimeout());
1172             } else {
1173                 /*
1174                  * (v5-v7) If modelInfo.modelCustomizationId is NOT provided (because it is a pre-1702 ASDC model or
1175                  * pre-v3), then modelInfo.modelCustomizationName must have // been provided (else create request should
1176                  * be rejected). APIH should use the relatedInstance.modelInfo[vnf].modelVersionId +
1177                  * modelInfo[vnf].modelCustomizationName // to join vnf_to_resource_customizations with
1178                  * vf_resource_customization to confirm a vf_resource_customization.model_customization_uuid record
1179                  * exists. // Once the vnfs model_customization_uuid has been obtained, use it to find all vfModule
1180                  * customizations for that vnf customization in the vnf_res_custom_to_vf_module_custom join table. //
1181                  * For each vf_module_cust_model_customization_uuid value returned, use that UUID to query
1182                  * vf_module_customization table along with modelInfo[vfModule|volumeGroup].modelVersionId to // confirm
1183                  * record matches request data (and to identify the modelCustomizationId associated with the vfModule in
1184                  * the request). This means taking each record found // in vf_module_customization and looking up in
1185                  * vf_module (using vf_module_customization’s FK into vf_module) to find a match on
1186                  * MODEL_INVARIANT_UUID (modelInvariantId) // and MODEL_VERSION (modelVersion).
1187                  */
1188                 VfModuleCustomization vfmc = null;
1189                 VnfResource vnfr;
1190                 VnfResourceCustomization vnfrc;
1191                 VfModule vfModule = null;
1192
1193                 if (modelInfo.getModelCustomizationId() != null) {
1194                     vfmc = catalogDbClient
1195                             .getVfModuleCustomizationByModelCuztomizationUUID(modelInfo.getModelCustomizationId());
1196                 } else {
1197                     vnfr = catalogDbClient.getVnfResourceByModelUUID(relatedInstanceModelVersionId);
1198                     if (vnfr == null) {
1199                         vnfr = catalogDbClient.getFirstVnfResourceByModelInvariantUUIDAndModelVersion(
1200                                 relatedInstanceModelInvariantId, relatedInstanceVersion);
1201                     }
1202                     vnfrc = catalogDbClient.getFirstVnfResourceCustomizationByModelInstanceNameAndVnfResources(
1203                             relatedInstanceModelCustomizationName, vnfr);
1204
1205                     List<VfModuleCustomization> list = vnfrc.getVfModuleCustomizations();
1206
1207                     String vfModuleModelUUID = modelInfo.getModelVersionId();
1208                     for (VfModuleCustomization vf : list) {
1209                         VfModuleCustomization vfmCustom;
1210                         if (vfModuleModelUUID != null) {
1211                             vfmCustom = catalogDbClient
1212                                     .getVfModuleCustomizationByModelCustomizationUUIDAndVfModuleModelUUID(
1213                                             vf.getModelCustomizationUUID(), vfModuleModelUUID);
1214                             if (vfmCustom != null) {
1215                                 vfModule = vfmCustom.getVfModule();
1216                             }
1217                         } else {
1218                             vfmCustom = catalogDbClient
1219                                     .getVfModuleCustomizationByModelCuztomizationUUID(vf.getModelCustomizationUUID());
1220                             if (vfmCustom != null) {
1221                                 vfModule = vfmCustom.getVfModule();
1222                             } else {
1223                                 vfModule = catalogDbClient.getVfModuleByModelInvariantUUIDAndModelVersion(
1224                                         relatedInstanceModelInvariantId, relatedInstanceVersion);
1225                             }
1226                         }
1227
1228                         if (vfModule != null) {
1229                             modelInfo.setModelCustomizationId(vf.getModelCustomizationUUID());
1230                             modelInfo.setModelCustomizationUuid(vf.getModelCustomizationUUID());
1231                             break;
1232                         }
1233                     }
1234                 }
1235
1236                 if (vfmc == null && vfModule == null) {
1237                     throw new ValidationException("vfModuleCustomization");
1238                 } else if (vfModule == null && vfmc != null) {
1239                     vfModule = vfmc.getVfModule(); // can't be null as vfModuleModelUUID is not-null property in
1240                                                    // VfModuleCustomization table
1241                 }
1242
1243                 if (modelInfo.getModelVersionId() == null) {
1244                     modelInfo.setModelVersionId(vfModule.getModelUUID());
1245                 }
1246
1247
1248                 recipe = catalogDbClient.getFirstVnfComponentsRecipeByVfModuleModelUUIDAndVnfComponentTypeAndAction(
1249                         vfModule.getModelUUID(), vnfComponentType, action.toString());
1250                 if (recipe == null) {
1251                     List<VfModule> vfModuleRecords = catalogDbClient
1252                             .getVfModuleByModelInvariantUUIDOrderByModelVersionDesc(vfModule.getModelInvariantUUID());
1253                     if (!vfModuleRecords.isEmpty()) {
1254                         for (VfModule record : vfModuleRecords) {
1255                             recipe = catalogDbClient
1256                                     .getFirstVnfComponentsRecipeByVfModuleModelUUIDAndVnfComponentTypeAndAction(
1257                                             record.getModelUUID(), vnfComponentType, action.toString());
1258                             if (recipe != null) {
1259                                 break;
1260                             }
1261                         }
1262                     }
1263                 }
1264                 if (recipe == null) {
1265                     recipe = catalogDbClient.getFirstVnfComponentsRecipeByVfModuleModelUUIDAndVnfComponentTypeAndAction(
1266                             defaultSource, vnfComponentType, action.toString());
1267                     if (recipe == null) {
1268                         recipe = catalogDbClient.getFirstVnfComponentsRecipeByVnfComponentTypeAndAction(
1269                                 vnfComponentType, action.toString());
1270                     }
1271
1272                     if (recipe == null) {
1273                         return null;
1274                     }
1275                 }
1276             }
1277         } else {
1278
1279             if (modelInfo.getModelType().equals(ModelType.vnf)) {
1280                 recipe = catalogDbClient.getFirstVnfRecipeByNfRoleAndAction(defaultSource, action.toString());
1281                 if (recipe == null) {
1282                     return null;
1283                 }
1284             } else {
1285                 recipe = catalogDbClient.getFirstVnfComponentsRecipeByVfModuleModelUUIDAndVnfComponentTypeAndAction(
1286                         defaultSource, vnfComponentType, action.toString());
1287
1288                 if (recipe == null) {
1289                     return null;
1290                 }
1291             }
1292         }
1293
1294         return new RecipeLookupResult(recipe.getOrchestrationUri(), recipe.getRecipeTimeout());
1295     }
1296 }