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