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