2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright (C) 2017 Huawei Technologies Co., Ltd. All rights reserved.
7 * Modifications Copyright (C) 2019 IBM.
8 * ================================================================================
9 * Modifications Copyright (c) 2019 Samsung
10 * ================================================================================
11 * Licensed under the Apache License, Version 2.0 (the "License");
12 * you may not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
15 * http://www.apache.org/licenses/LICENSE-2.0
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
22 * ============LICENSE_END=========================================================
25 package org.onap.so.apihandlerinfra;
28 import static org.onap.so.logger.HttpHeadersConstants.REQUESTOR_ID;
29 import java.io.IOException;
31 import java.sql.Timestamp;
32 import java.util.ArrayList;
33 import java.util.HashMap;
34 import java.util.List;
36 import java.util.Optional;
37 import javax.ws.rs.container.ContainerRequestContext;
38 import javax.ws.rs.core.MultivaluedMap;
39 import javax.ws.rs.core.Response;
40 import org.apache.commons.lang.StringUtils;
41 import org.apache.http.HttpResponse;
42 import org.apache.http.HttpStatus;
43 import org.camunda.bpm.engine.history.HistoricProcessInstance;
44 import org.camunda.bpm.engine.impl.persistence.entity.HistoricProcessInstanceEntity;
45 import org.onap.logging.ref.slf4j.ONAPLogConstants;
46 import org.onap.so.apihandler.camundabeans.CamundaResponse;
47 import org.onap.so.apihandler.common.CommonConstants;
48 import org.onap.so.apihandler.common.ErrorNumbers;
49 import org.onap.so.apihandler.common.RequestClient;
50 import org.onap.so.apihandler.common.RequestClientFactory;
51 import org.onap.so.apihandler.common.RequestClientParameter;
52 import org.onap.so.apihandler.common.ResponseBuilder;
53 import org.onap.so.apihandler.common.ResponseHandler;
54 import org.onap.so.apihandlerinfra.exceptions.ApiException;
55 import org.onap.so.apihandlerinfra.exceptions.BPMNFailureException;
56 import org.onap.so.apihandlerinfra.exceptions.ClientConnectionException;
57 import org.onap.so.apihandlerinfra.exceptions.ContactCamundaException;
58 import org.onap.so.apihandlerinfra.exceptions.DuplicateRequestException;
59 import org.onap.so.apihandlerinfra.exceptions.RecipeNotFoundException;
60 import org.onap.so.apihandlerinfra.exceptions.RequestDbFailureException;
61 import org.onap.so.apihandlerinfra.exceptions.ValidateException;
62 import org.onap.so.apihandlerinfra.exceptions.VfModuleNotFoundException;
63 import org.onap.so.apihandlerinfra.infra.rest.handler.AbstractRestHandler;
64 import org.onap.so.apihandlerinfra.logging.ErrorLoggerInfo;
65 import org.onap.so.constants.Status;
66 import org.onap.so.db.catalog.beans.NetworkResource;
67 import org.onap.so.db.catalog.beans.NetworkResourceCustomization;
68 import org.onap.so.db.catalog.beans.Recipe;
69 import org.onap.so.db.catalog.beans.ServiceRecipe;
70 import org.onap.so.db.catalog.beans.VfModule;
71 import org.onap.so.db.catalog.beans.VfModuleCustomization;
72 import org.onap.so.db.catalog.beans.VnfRecipe;
73 import org.onap.so.db.catalog.beans.VnfResource;
74 import org.onap.so.db.catalog.beans.VnfResourceCustomization;
75 import org.onap.so.db.catalog.client.CatalogDbClient;
76 import org.onap.so.db.request.beans.InfraActiveRequests;
77 import org.onap.so.db.request.client.RequestsDbClient;
78 import org.onap.so.exceptions.ValidationException;
79 import org.onap.so.logger.ErrorCode;
80 import org.onap.so.logger.LogConstants;
81 import org.onap.so.logger.MessageEnum;
82 import org.onap.so.serviceinstancebeans.CloudConfiguration;
83 import org.onap.so.serviceinstancebeans.ModelInfo;
84 import org.onap.so.serviceinstancebeans.ModelType;
85 import org.onap.so.serviceinstancebeans.Networks;
86 import org.onap.so.serviceinstancebeans.RelatedInstance;
87 import org.onap.so.serviceinstancebeans.RelatedInstanceList;
88 import org.onap.so.serviceinstancebeans.RequestDetails;
89 import org.onap.so.serviceinstancebeans.RequestParameters;
90 import org.onap.so.serviceinstancebeans.Service;
91 import org.onap.so.serviceinstancebeans.ServiceInstancesRequest;
92 import org.onap.so.serviceinstancebeans.ServiceInstancesResponse;
93 import org.onap.so.serviceinstancebeans.VfModules;
94 import org.onap.so.serviceinstancebeans.Vnfs;
95 import org.onap.so.utils.UUIDChecker;
96 import org.slf4j.Logger;
97 import org.slf4j.LoggerFactory;
99 import org.springframework.beans.factory.annotation.Autowired;
100 import org.springframework.core.env.Environment;
101 import org.springframework.http.ResponseEntity;
102 import org.springframework.stereotype.Component;
103 import org.springframework.web.client.RestClientException;
104 import com.fasterxml.jackson.annotation.JsonInclude.Include;
105 import com.fasterxml.jackson.databind.ObjectMapper;
108 public class RequestHandlerUtils extends AbstractRestHandler {
110 private static Logger logger = LoggerFactory.getLogger(RequestHandlerUtils.class);
112 protected static final String SAVE_TO_DB = "save instance to db";
113 private static final String NAME = "name";
114 private static final String VALUE = "value";
117 private Environment env;
120 private RequestClientFactory reqClientFactory;
123 private ResponseBuilder builder;
126 private MsoRequest msoRequest;
129 private CamundaRequestHandler camundaRequestHandler;
132 private CatalogDbClient catalogDbClient;
134 public Response postBPELRequest(InfraActiveRequests currentActiveReq, RequestClientParameter requestClientParameter,
135 String orchestrationUri, String requestScope) throws ApiException {
136 HttpResponse response = null;
137 RequestClient requestClient = null;
140 requestClient = reqClientFactory.getRequestClient(orchestrationUri);
141 response = requestClient.post(requestClientParameter);
142 } catch (Exception e) {
143 logger.error("Error posting request to BPMN", e);
144 ErrorLoggerInfo errorLoggerInfo =
145 new ErrorLoggerInfo.Builder(MessageEnum.APIH_BPEL_COMMUNICATE_ERROR, ErrorCode.AvailabilityError)
146 .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
147 String url = requestClient != null ? requestClient.getUrl() : "";
148 ClientConnectionException clientException =
149 new ClientConnectionException.Builder(url, HttpStatus.SC_BAD_GATEWAY,
150 ErrorNumbers.SVC_NO_SERVER_RESOURCES).cause(e).errorInfo(errorLoggerInfo).build();
151 updateStatus(currentActiveReq, Status.FAILED, clientException.getMessage());
152 throw clientException;
155 if (response == null) {
157 ErrorLoggerInfo errorLoggerInfo =
158 new ErrorLoggerInfo.Builder(MessageEnum.APIH_BPEL_COMMUNICATE_ERROR, ErrorCode.BusinessProcessError)
159 .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
160 ClientConnectionException clientException = new ClientConnectionException.Builder(requestClient.getUrl(),
161 HttpStatus.SC_BAD_GATEWAY, ErrorNumbers.SVC_NO_SERVER_RESOURCES).errorInfo(errorLoggerInfo).build();
162 updateStatus(currentActiveReq, Status.FAILED, clientException.getMessage());
163 throw clientException;
166 ResponseHandler respHandler = null;
167 int bpelStatus = 500;
169 respHandler = new ResponseHandler(response, requestClient.getType());
170 bpelStatus = respHandler.getStatus();
171 } catch (ApiException e) {
172 logger.error("Exception occurred", e);
173 ErrorLoggerInfo errorLoggerInfo =
174 new ErrorLoggerInfo.Builder(MessageEnum.APIH_BPEL_RESPONSE_ERROR, ErrorCode.SchemaError)
175 .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
176 ValidateException validateException =
177 new ValidateException.Builder("Exception caught mapping Camunda JSON response to object",
178 HttpStatus.SC_INTERNAL_SERVER_ERROR, ErrorNumbers.SVC_BAD_PARAMETER).cause(e)
179 .errorInfo(errorLoggerInfo).build();
180 updateStatus(currentActiveReq, Status.FAILED, validateException.getMessage());
181 throw validateException;
184 // BPEL accepted the request, the request is in progress
185 if (bpelStatus == HttpStatus.SC_ACCEPTED) {
186 ServiceInstancesResponse jsonResponse;
187 CamundaResponse camundaResp = respHandler.getResponse();
189 if ("Success".equalsIgnoreCase(camundaResp.getMessage())) {
191 ObjectMapper mapper = new ObjectMapper();
192 jsonResponse = mapper.readValue(camundaResp.getResponse(), ServiceInstancesResponse.class);
193 jsonResponse.getRequestReferences().setRequestId(requestClientParameter.getRequestId());
194 Optional<URL> selfLinkUrl =
195 buildSelfLinkUrl(currentActiveReq.getRequestUrl(), requestClientParameter.getRequestId());
196 if (selfLinkUrl.isPresent()) {
197 jsonResponse.getRequestReferences().setRequestSelfLink(selfLinkUrl.get());
199 jsonResponse.getRequestReferences().setRequestSelfLink(null);
201 } catch (IOException e) {
202 logger.error("Exception occurred", e);
203 ErrorLoggerInfo errorLoggerInfo =
204 new ErrorLoggerInfo.Builder(MessageEnum.APIH_BPEL_RESPONSE_ERROR, ErrorCode.SchemaError)
205 .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
206 ValidateException validateException =
207 new ValidateException.Builder("Exception caught mapping Camunda JSON response to object",
208 HttpStatus.SC_NOT_ACCEPTABLE, ErrorNumbers.SVC_BAD_PARAMETER).cause(e)
209 .errorInfo(errorLoggerInfo).build();
210 updateStatus(currentActiveReq, Status.FAILED, validateException.getMessage());
211 throw validateException;
213 return builder.buildResponse(HttpStatus.SC_ACCEPTED, requestClientParameter.getRequestId(),
214 jsonResponse, requestClientParameter.getApiVersion());
218 List<String> variables = new ArrayList<>();
219 variables.add(bpelStatus + "");
220 String camundaJSONResponseBody = respHandler.getResponseBody();
221 if (camundaJSONResponseBody != null && !camundaJSONResponseBody.isEmpty()) {
223 ErrorLoggerInfo errorLoggerInfo =
224 new ErrorLoggerInfo.Builder(MessageEnum.APIH_BPEL_RESPONSE_ERROR, ErrorCode.BusinessProcessError)
225 .errorSource(requestClient.getUrl()).build();
226 BPMNFailureException bpmnException =
227 new BPMNFailureException.Builder(String.valueOf(bpelStatus) + camundaJSONResponseBody, bpelStatus,
228 ErrorNumbers.SVC_DETAILED_SERVICE_ERROR).errorInfo(errorLoggerInfo).build();
230 updateStatus(currentActiveReq, Status.FAILED, bpmnException.getMessage());
235 ErrorLoggerInfo errorLoggerInfo =
236 new ErrorLoggerInfo.Builder(MessageEnum.APIH_BPEL_RESPONSE_ERROR, ErrorCode.BusinessProcessError)
237 .errorSource(requestClient.getUrl()).build();
240 BPMNFailureException servException = new BPMNFailureException.Builder(String.valueOf(bpelStatus),
241 bpelStatus, ErrorNumbers.SVC_DETAILED_SERVICE_ERROR).errorInfo(errorLoggerInfo).build();
242 updateStatus(currentActiveReq, Status.FAILED, servException.getMessage());
251 public void updateStatus(InfraActiveRequests aq, Status status, String errorMessage)
252 throws RequestDbFailureException {
253 if ((status == Status.FAILED) || (status == Status.COMPLETE)) {
254 aq.setStatusMessage(errorMessage);
255 aq.setProgress(new Long(100));
256 aq.setRequestStatus(status.toString());
257 Timestamp endTimeStamp = new Timestamp(System.currentTimeMillis());
258 aq.setEndTime(endTimeStamp);
260 infraActiveRequestsClient.save(aq);
261 } catch (Exception e) {
262 ErrorLoggerInfo errorLoggerInfo =
263 new ErrorLoggerInfo.Builder(MessageEnum.APIH_DB_ACCESS_EXC, ErrorCode.DataError)
264 .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
265 throw new RequestDbFailureException.Builder(SAVE_TO_DB, e.toString(),
266 HttpStatus.SC_INTERNAL_SERVER_ERROR, ErrorNumbers.SVC_DETAILED_SERVICE_ERROR).cause(e)
267 .errorInfo(errorLoggerInfo).build();
272 public String deriveRequestScope(Actions action, ServiceInstancesRequest sir, String requestUri) {
273 if (action == Action.inPlaceSoftwareUpdate || action == Action.applyUpdatedConfig) {
274 return (ModelType.vnf.name());
275 } else if (action == Action.addMembers || action == Action.removeMembers) {
276 return (ModelType.instanceGroup.toString());
278 String requestScope = requestScopeFromUri(requestUri);;
280 if (sir.getRequestDetails() == null) {
283 if (sir.getRequestDetails().getModelInfo() == null) {
286 if (sir.getRequestDetails().getModelInfo().getModelType() == null) {
289 requestScope = sir.getRequestDetails().getModelInfo().getModelType().name();
295 public void validateHeaders(ContainerRequestContext context) throws ValidationException {
296 MultivaluedMap<String, String> headers = context.getHeaders();
297 if (!headers.containsKey(ONAPLogConstants.Headers.REQUEST_ID)) {
298 throw new ValidationException(ONAPLogConstants.Headers.REQUEST_ID + " header", true);
300 if (!headers.containsKey(ONAPLogConstants.Headers.PARTNER_NAME)) {
301 throw new ValidationException(ONAPLogConstants.Headers.PARTNER_NAME + " header", true);
303 if (!headers.containsKey(REQUESTOR_ID)) {
304 throw new ValidationException(REQUESTOR_ID + " header", true);
308 public String getRequestUri(ContainerRequestContext context, String uriPrefix) {
309 String requestUri = context.getUriInfo().getPath();
310 String httpUrl = MDC.get(LogConstants.URI_BASE).concat(requestUri);
311 MDC.put(LogConstants.HTTP_URL, httpUrl);
312 requestUri = requestUri.substring(requestUri.indexOf(uriPrefix) + uriPrefix.length());
316 public void checkForDuplicateRequests(Actions action, HashMap<String, String> instanceIdMap, String requestScope,
317 InfraActiveRequests currentActiveReq, String instanceName) throws ApiException {
318 InfraActiveRequests dup = null;
319 boolean inProgress = false;
321 dup = duplicateCheck(action, instanceIdMap, instanceName, requestScope, currentActiveReq);
324 inProgress = camundaHistoryCheck(dup, currentActiveReq);
327 if (dup != null && inProgress) {
328 buildErrorOnDuplicateRecord(currentActiveReq, action, instanceIdMap, instanceName, requestScope, dup);
332 public InfraActiveRequests duplicateCheck(Actions action, HashMap<String, String> instanceIdMap,
333 String instanceName, String requestScope, InfraActiveRequests currentActiveReq) throws ApiException {
334 InfraActiveRequests dup = null;
336 if (!(instanceName == null && "service".equals(requestScope) && (action == Action.createInstance
337 || action == Action.activateInstance || action == Action.assignInstance))) {
338 dup = infraActiveRequestsClient.checkInstanceNameDuplicate(instanceIdMap, instanceName, requestScope);
340 } catch (Exception e) {
341 ErrorLoggerInfo errorLoggerInfo =
342 new ErrorLoggerInfo.Builder(MessageEnum.APIH_DUPLICATE_CHECK_EXC, ErrorCode.DataError)
343 .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
344 RequestDbFailureException requestDbFailureException =
345 new RequestDbFailureException.Builder("check for duplicate instance", e.toString(),
346 HttpStatus.SC_INTERNAL_SERVER_ERROR, ErrorNumbers.SVC_DETAILED_SERVICE_ERROR).cause(e)
347 .errorInfo(errorLoggerInfo).build();
348 updateStatus(currentActiveReq, Status.FAILED, requestDbFailureException.getMessage());
349 throw requestDbFailureException;
354 public boolean camundaHistoryCheck(InfraActiveRequests duplicateRecord, InfraActiveRequests currentActiveReq)
355 throws RequestDbFailureException, ContactCamundaException {
356 String requestId = duplicateRecord.getRequestId();
357 ResponseEntity<List<HistoricProcessInstanceEntity>> response = null;
359 response = camundaRequestHandler.getCamundaProcessInstanceHistory(requestId, true);
360 } catch (RestClientException e) {
361 logger.error("Error querying Camunda for process-instance history for requestId: {}, exception: {}",
362 requestId, e.getMessage());
363 ContactCamundaException contactCamundaException =
364 new ContactCamundaException.Builder("process-instance", requestId, e.toString(),
365 HttpStatus.SC_INTERNAL_SERVER_ERROR, ErrorNumbers.SVC_DETAILED_SERVICE_ERROR).cause(e)
367 updateStatus(currentActiveReq, Status.FAILED, contactCamundaException.getMessage());
368 throw contactCamundaException;
371 if (response.getBody().isEmpty()) {
372 updateStatus(duplicateRecord, Status.COMPLETE, "Request Completed");
374 for (HistoricProcessInstance instance : response.getBody()) {
375 if (("ACTIVE").equals(instance.getState())) {
378 updateStatus(duplicateRecord, Status.COMPLETE, "Request Completed");
384 public ServiceInstancesRequest convertJsonToServiceInstanceRequest(String requestJSON, Actions action,
385 String requestId, String requestUri) throws ApiException {
387 ObjectMapper mapper = new ObjectMapper();
388 return mapper.readValue(requestJSON, ServiceInstancesRequest.class);
390 } catch (IOException e) {
392 ErrorLoggerInfo errorLoggerInfo =
393 new ErrorLoggerInfo.Builder(MessageEnum.APIH_REQUEST_VALIDATION_ERROR, ErrorCode.SchemaError)
394 .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
396 ValidateException validateException =
397 new ValidateException.Builder("Error mapping request: " + e.getMessage(), HttpStatus.SC_BAD_REQUEST,
398 ErrorNumbers.SVC_BAD_PARAMETER).cause(e).errorInfo(errorLoggerInfo).build();
399 String requestScope = requestScopeFromUri(requestUri);
401 msoRequest.createErrorRequestRecord(Status.FAILED, requestId, validateException.getMessage(), action,
402 requestScope, requestJSON);
404 throw validateException;
408 public void parseRequest(ServiceInstancesRequest sir, HashMap<String, String> instanceIdMap, Actions action,
409 String version, String requestJSON, Boolean aLaCarte, String requestId,
410 InfraActiveRequests currentActiveReq) throws ValidateException, RequestDbFailureException {
411 int reqVersion = Integer.parseInt(version.substring(1));
413 msoRequest.parse(sir, instanceIdMap, action, version, requestJSON, reqVersion, aLaCarte);
414 } catch (Exception e) {
415 logger.error("failed to parse request", e);
416 ErrorLoggerInfo errorLoggerInfo =
417 new ErrorLoggerInfo.Builder(MessageEnum.APIH_REQUEST_VALIDATION_ERROR, ErrorCode.SchemaError)
418 .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
419 ValidateException validateException =
420 new ValidateException.Builder("Error parsing request: " + e.getMessage(), HttpStatus.SC_BAD_REQUEST,
421 ErrorNumbers.SVC_BAD_PARAMETER).cause(e).errorInfo(errorLoggerInfo).build();
423 updateStatus(currentActiveReq, Status.FAILED, validateException.getMessage());
425 throw validateException;
429 public void buildErrorOnDuplicateRecord(InfraActiveRequests currentActiveReq, Actions action,
430 HashMap<String, String> instanceIdMap, String instanceName, String requestScope, InfraActiveRequests dup)
431 throws ApiException {
433 String instance = null;
434 if (instanceName != null) {
435 instance = instanceName;
437 instance = instanceIdMap.get(requestScope + "InstanceId");
439 ErrorLoggerInfo errorLoggerInfo =
440 new ErrorLoggerInfo.Builder(MessageEnum.APIH_DUPLICATE_FOUND, ErrorCode.SchemaError)
441 .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
443 DuplicateRequestException dupException =
444 new DuplicateRequestException.Builder(requestScope, instance, dup.getRequestStatus(),
445 dup.getRequestId(), HttpStatus.SC_CONFLICT, ErrorNumbers.SVC_DETAILED_SERVICE_ERROR)
446 .errorInfo(errorLoggerInfo).build();
448 updateStatus(currentActiveReq, Status.FAILED, dupException.getMessage());
454 public String getRequestId(ContainerRequestContext requestContext) throws ValidateException {
455 String requestId = null;
456 if (requestContext.getProperty("requestId") != null) {
457 requestId = requestContext.getProperty("requestId").toString();
459 if (UUIDChecker.isValidUUID(requestId)) {
462 ErrorLoggerInfo errorLoggerInfo =
463 new ErrorLoggerInfo.Builder(MessageEnum.APIH_BPEL_RESPONSE_ERROR, ErrorCode.SchemaError)
464 .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
465 ValidateException validateException =
466 new ValidateException.Builder("Request Id " + requestId + " is not a valid UUID",
467 HttpStatus.SC_INTERNAL_SERVER_ERROR, ErrorNumbers.SVC_BAD_PARAMETER)
468 .errorInfo(errorLoggerInfo).build();
470 throw validateException;
474 public void setInstanceId(InfraActiveRequests currentActiveReq, String requestScope, String instanceId,
475 Map<String, String> instanceIdMap) {
476 if (StringUtils.isNotBlank(instanceId)) {
477 if (ModelType.service.name().equalsIgnoreCase(requestScope)) {
478 currentActiveReq.setServiceInstanceId(instanceId);
479 } else if (ModelType.vnf.name().equalsIgnoreCase(requestScope)) {
480 currentActiveReq.setVnfId(instanceId);
481 } else if (ModelType.vfModule.name().equalsIgnoreCase(requestScope)) {
482 currentActiveReq.setVfModuleId(instanceId);
483 } else if (ModelType.volumeGroup.name().equalsIgnoreCase(requestScope)) {
484 currentActiveReq.setVolumeGroupId(instanceId);
485 } else if (ModelType.network.name().equalsIgnoreCase(requestScope)) {
486 currentActiveReq.setNetworkId(instanceId);
487 } else if (ModelType.configuration.name().equalsIgnoreCase(requestScope)) {
488 currentActiveReq.setConfigurationId(instanceId);
489 } else if (ModelType.instanceGroup.toString().equalsIgnoreCase(requestScope)) {
490 currentActiveReq.setInstanceGroupId(instanceId);
492 } else if (instanceIdMap != null && !instanceIdMap.isEmpty()) {
493 if (instanceIdMap.get("serviceInstanceId") != null) {
494 currentActiveReq.setServiceInstanceId(instanceIdMap.get("serviceInstanceId"));
496 if (instanceIdMap.get("vnfInstanceId") != null) {
497 currentActiveReq.setVnfId(instanceIdMap.get("vnfInstanceId"));
499 if (instanceIdMap.get("vfModuleInstanceId") != null) {
500 currentActiveReq.setVfModuleId(instanceIdMap.get("vfModuleInstanceId"));
502 if (instanceIdMap.get("volumeGroupInstanceId") != null) {
503 currentActiveReq.setVolumeGroupId(instanceIdMap.get("volumeGroupInstanceId"));
505 if (instanceIdMap.get("networkInstanceId") != null) {
506 currentActiveReq.setNetworkId(instanceIdMap.get("networkInstanceId"));
508 if (instanceIdMap.get("configurationInstanceId") != null) {
509 currentActiveReq.setConfigurationId(instanceIdMap.get("configurationInstanceId"));
511 if (instanceIdMap.get(CommonConstants.INSTANCE_GROUP_INSTANCE_ID) != null) {
512 currentActiveReq.setInstanceGroupId(instanceIdMap.get(CommonConstants.INSTANCE_GROUP_INSTANCE_ID));
514 if (instanceIdMap.get("PnfId") != null) {
515 currentActiveReq.setPnfId(instanceIdMap.get("PnfId"));
520 public String mapJSONtoMSOStyle(String msoRawRequest, ServiceInstancesRequest serviceInstRequest,
521 boolean isAlaCarte, Actions action) throws IOException {
522 ObjectMapper mapper = new ObjectMapper();
523 mapper.setSerializationInclusion(Include.NON_NULL);
524 if (serviceInstRequest != null) {
525 return mapper.writeValueAsString(serviceInstRequest);
527 return msoRawRequest;
531 public Optional<String> retrieveModelName(RequestParameters requestParams) {
532 String requestTestApi = null;
533 TestApi testApi = null;
535 if (requestParams != null) {
536 requestTestApi = requestParams.getTestApi();
539 if (requestTestApi == null) {
540 if (requestParams != null && requestParams.getALaCarte() != null && !requestParams.getALaCarte()) {
541 requestTestApi = env.getProperty(CommonConstants.MACRO_TEST_API);
543 requestTestApi = env.getProperty(CommonConstants.ALACARTE_TEST_API);
548 testApi = TestApi.valueOf(requestTestApi);
549 return Optional.of(testApi.getModelName());
550 } catch (Exception e) {
551 logger.warn("Catching the exception on the valueOf enum call and continuing", e);
552 throw new IllegalArgumentException("Invalid TestApi is provided", e);
556 public String getDefaultModel(ServiceInstancesRequest sir) {
557 String defaultModel = sir.getRequestDetails().getRequestInfo().getSource() + "_DEFAULT";
558 Optional<String> oModelName = retrieveModelName(sir.getRequestDetails().getRequestParameters());
559 if (oModelName.isPresent()) {
560 defaultModel = oModelName.get();
565 public String getServiceType(String requestScope, ServiceInstancesRequest sir, Boolean aLaCarteFlag) {
566 String serviceType = null;
567 if (requestScope.equalsIgnoreCase(ModelType.service.toString())) {
568 String defaultServiceModelName = getDefaultModel(sir);
569 org.onap.so.db.catalog.beans.Service serviceRecord;
571 serviceRecord = catalogDbClient.getFirstByModelNameOrderByModelVersionDesc(defaultServiceModelName);
572 if (serviceRecord != null) {
573 serviceType = serviceRecord.getServiceType();
577 catalogDbClient.getServiceByID(sir.getRequestDetails().getModelInfo().getModelVersionId());
578 if (serviceRecord != null) {
579 serviceType = serviceRecord.getServiceType();
581 serviceRecord = catalogDbClient.getFirstByModelNameOrderByModelVersionDesc(defaultServiceModelName);
582 if (serviceRecord != null) {
583 serviceType = serviceRecord.getServiceType();
588 serviceType = msoRequest.getServiceInstanceType(sir, requestScope);
593 protected String setServiceInstanceId(String requestScope, ServiceInstancesRequest sir) {
594 if (sir.getServiceInstanceId() != null) {
595 return sir.getServiceInstanceId();
596 } else if (requestScope.equalsIgnoreCase(ModelType.instanceGroup.toString())) {
597 RelatedInstanceList[] relatedInstances = sir.getRequestDetails().getRelatedInstanceList();
598 if (relatedInstances != null) {
599 for (RelatedInstanceList relatedInstanceList : relatedInstances) {
600 RelatedInstance relatedInstance = relatedInstanceList.getRelatedInstance();
601 if (relatedInstance.getModelInfo().getModelType() == ModelType.service) {
602 return relatedInstance.getInstanceId();
610 private String requestScopeFromUri(String requestUri) {
612 if (requestUri.contains(ModelType.network.name())) {
613 requestScope = ModelType.network.name();
614 } else if (requestUri.contains(ModelType.vfModule.name())) {
615 requestScope = ModelType.vfModule.name();
616 } else if (requestUri.contains(ModelType.volumeGroup.name())) {
617 requestScope = ModelType.volumeGroup.name();
618 } else if (requestUri.contains(ModelType.configuration.name())) {
619 requestScope = ModelType.configuration.name();
620 } else if (requestUri.contains(ModelType.vnf.name())) {
621 requestScope = ModelType.vnf.name();
622 } else if (requestUri.contains(ModelType.pnf.name())) {
623 requestScope = ModelType.pnf.name();
625 requestScope = ModelType.service.name();
630 protected InfraActiveRequests createNewRecordCopyFromInfraActiveRequest(InfraActiveRequests infraActiveRequest,
631 String requestId, Timestamp startTimeStamp, String source, String requestUri, String requestorId,
632 String originalRequestId) throws ApiException {
633 InfraActiveRequests request = new InfraActiveRequests();
634 request.setRequestId(requestId);
635 request.setStartTime(startTimeStamp);
636 request.setSource(source);
637 request.setRequestUrl(requestUri);
638 request.setProgress(new Long(5));
639 request.setRequestorId(requestorId);
640 request.setRequestStatus(Status.IN_PROGRESS.toString());
641 request.setOriginalRequestId(originalRequestId);
642 request.setLastModifiedBy(Constants.MODIFIED_BY_APIHANDLER);
643 if (infraActiveRequest != null) {
644 request.setTenantId(infraActiveRequest.getTenantId());
645 request.setRequestBody(updateRequestorIdInRequestBody(infraActiveRequest, requestorId));
646 request.setCloudRegion(infraActiveRequest.getCloudRegion());
647 request.setRequestScope(infraActiveRequest.getRequestScope());
648 request.setRequestAction(infraActiveRequest.getRequestAction());
649 setInstanceIdAndName(infraActiveRequest, request);
654 protected void setInstanceIdAndName(InfraActiveRequests infraActiveRequest,
655 InfraActiveRequests currentActiveRequest) throws ApiException {
656 String requestScope = infraActiveRequest.getRequestScope();
658 ModelType type = ModelType.valueOf(requestScope);
659 String instanceName = type.getName(infraActiveRequest);
660 if (instanceName == null && type.equals(ModelType.vfModule)) {
661 logger.error("vfModule for requestId: {} being resumed does not have an instanceName.",
662 infraActiveRequest.getRequestId());
663 ValidateException validateException = new ValidateException.Builder(
664 "vfModule for requestId: " + infraActiveRequest.getRequestId()
665 + " being resumed does not have an instanceName.",
666 HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_BAD_PARAMETER).build();
667 updateStatus(currentActiveRequest, Status.FAILED, validateException.getMessage());
668 throw validateException;
670 if (instanceName != null) {
671 type.setName(currentActiveRequest, instanceName);
673 type.setId(currentActiveRequest, type.getId(infraActiveRequest));
674 } catch (IllegalArgumentException e) {
676 "requestScope \"{}\" does not match a ModelType enum. Unable to set instanceId and instanceName from the original request.",
681 protected Boolean getIsBaseVfModule(ModelInfo modelInfo, Actions action, String vnfType,
682 String sdcServiceModelVersion, InfraActiveRequests currentActiveReq) throws ApiException {
683 // Get VF Module-specific base module indicator
685 String modelVersionId = modelInfo.getModelVersionId();
686 Boolean isBaseVfModule = false;
688 if (modelVersionId != null) {
689 vfm = catalogDbClient.getVfModuleByModelUUID(modelVersionId);
690 } else if (modelInfo.getModelInvariantId() != null && modelInfo.getModelVersion() != null) {
691 vfm = catalogDbClient.getVfModuleByModelInvariantUUIDAndModelVersion(modelInfo.getModelInvariantId(),
692 modelInfo.getModelVersion());
696 if (vfm.getIsBase()) {
697 isBaseVfModule = true;
699 } else if (action == Action.createInstance || action == Action.updateInstance) {
700 String serviceVersionText = "";
701 if (sdcServiceModelVersion != null && !sdcServiceModelVersion.isEmpty()) {
702 serviceVersionText = " with version " + sdcServiceModelVersion;
704 String errorMessage = "VnfType " + vnfType + " and VF Module Model Name " + modelInfo.getModelName()
705 + serviceVersionText + " not found in MSO Catalog DB";
706 ErrorLoggerInfo errorLoggerInfo =
707 new ErrorLoggerInfo.Builder(MessageEnum.APIH_DB_ATTRIBUTE_NOT_FOUND, ErrorCode.DataError)
708 .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
709 VfModuleNotFoundException vfModuleException = new VfModuleNotFoundException.Builder(errorMessage,
710 HttpStatus.SC_NOT_FOUND, ErrorNumbers.SVC_BAD_PARAMETER).errorInfo(errorLoggerInfo).build();
711 updateStatus(currentActiveReq, Status.FAILED, vfModuleException.getMessage());
712 throw vfModuleException;
714 return isBaseVfModule;
717 protected ModelType getModelType(Actions action, ModelInfo modelInfo) {
718 if (action == Action.applyUpdatedConfig || action == Action.inPlaceSoftwareUpdate) {
719 return ModelType.vnf;
720 } else if (action == Action.addMembers || action == Action.removeMembers) {
721 return ModelType.instanceGroup;
723 return modelInfo.getModelType();
727 protected String updateRequestorIdInRequestBody(InfraActiveRequests infraActiveRequest, String newRequestorId) {
728 String requestBody = infraActiveRequest.getRequestBody();
729 return requestBody.replaceAll(
730 "(?s)(\"requestInfo\"\\s*?:\\s*?\\{.*?\"requestorId\"\\s*?:\\s*?\")(.*?)(\"[ ]*(?:,|\\R|\\}))",
731 "$1" + newRequestorId + "$3");
734 public RecipeLookupResult getServiceInstanceOrchestrationURI(ServiceInstancesRequest sir, Actions action,
735 boolean alaCarteFlag, InfraActiveRequests currentActiveReq) throws ApiException {
736 RecipeLookupResult recipeLookupResult = null;
737 // if the aLaCarte flag is set to TRUE, the API-H should choose the VID_DEFAULT recipe for the requested action
738 ModelInfo modelInfo = sir.getRequestDetails().getModelInfo();
739 // Query MSO Catalog DB
741 if (action == Action.applyUpdatedConfig || action == Action.inPlaceSoftwareUpdate) {
742 recipeLookupResult = getDefaultVnfUri(sir, action);
743 } else if (action == Action.addMembers || action == Action.removeMembers) {
744 recipeLookupResult = new RecipeLookupResult("/mso/async/services/WorkflowActionBB", 180);
745 } else if (modelInfo.getModelType().equals(ModelType.service)) {
747 recipeLookupResult = getServiceURI(sir, action, alaCarteFlag);
748 } catch (IOException e) {
749 ErrorLoggerInfo errorLoggerInfo =
750 new ErrorLoggerInfo.Builder(MessageEnum.APIH_REQUEST_VALIDATION_ERROR, ErrorCode.SchemaError)
751 .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
754 ValidateException validateException =
755 new ValidateException.Builder(e.getMessage(), HttpStatus.SC_BAD_REQUEST,
756 ErrorNumbers.SVC_BAD_PARAMETER).cause(e).errorInfo(errorLoggerInfo).build();
758 updateStatus(currentActiveReq, Status.FAILED, validateException.getMessage());
760 throw validateException;
762 } else if (modelInfo.getModelType().equals(ModelType.vfModule)
763 || modelInfo.getModelType().equals(ModelType.volumeGroup)
764 || modelInfo.getModelType().equals(ModelType.vnf)) {
766 recipeLookupResult = getVnfOrVfModuleUri(sir, action);
767 } catch (ValidationException e) {
768 ErrorLoggerInfo errorLoggerInfo =
769 new ErrorLoggerInfo.Builder(MessageEnum.APIH_REQUEST_VALIDATION_ERROR, ErrorCode.SchemaError)
770 .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
773 ValidateException validateException =
774 new ValidateException.Builder(e.getMessage(), HttpStatus.SC_BAD_REQUEST,
775 ErrorNumbers.SVC_BAD_PARAMETER).cause(e).errorInfo(errorLoggerInfo).build();
777 updateStatus(currentActiveReq, Status.FAILED, validateException.getMessage());
779 throw validateException;
781 } else if (modelInfo.getModelType().equals(ModelType.network)) {
783 recipeLookupResult = getNetworkUri(sir, action);
784 } catch (ValidationException e) {
786 ErrorLoggerInfo errorLoggerInfo =
787 new ErrorLoggerInfo.Builder(MessageEnum.APIH_REQUEST_VALIDATION_ERROR, ErrorCode.SchemaError)
788 .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
791 ValidateException validateException =
792 new ValidateException.Builder(e.getMessage(), HttpStatus.SC_BAD_REQUEST,
793 ErrorNumbers.SVC_BAD_PARAMETER).cause(e).errorInfo(errorLoggerInfo).build();
794 updateStatus(currentActiveReq, Status.FAILED, validateException.getMessage());
796 throw validateException;
798 } else if (modelInfo.getModelType().equals(ModelType.instanceGroup)) {
799 recipeLookupResult = new RecipeLookupResult("/mso/async/services/WorkflowActionBB", 180);
802 if (recipeLookupResult == null) {
803 ErrorLoggerInfo errorLoggerInfo =
804 new ErrorLoggerInfo.Builder(MessageEnum.APIH_DB_ACCESS_EXC, ErrorCode.DataError)
805 .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
808 RecipeNotFoundException recipeNotFoundExceptionException =
809 new RecipeNotFoundException.Builder("Recipe could not be retrieved from catalog DB.",
810 HttpStatus.SC_NOT_FOUND, ErrorNumbers.SVC_GENERAL_SERVICE_ERROR).errorInfo(errorLoggerInfo)
813 updateStatus(currentActiveReq, Status.FAILED, recipeNotFoundExceptionException.getMessage());
814 throw recipeNotFoundExceptionException;
816 return recipeLookupResult;
819 protected RecipeLookupResult getServiceURI(ServiceInstancesRequest servInstReq, Actions action,
820 boolean alaCarteFlag) throws IOException {
822 // Construct the default service name
823 // TODO need to make this a configurable property
824 String defaultServiceModelName = getDefaultModel(servInstReq);
825 RequestDetails requestDetails = servInstReq.getRequestDetails();
826 ModelInfo modelInfo = requestDetails.getModelInfo();
827 org.onap.so.db.catalog.beans.Service serviceRecord;
828 List<org.onap.so.db.catalog.beans.Service> serviceRecordList;
829 ServiceRecipe recipe = null;
832 serviceRecord = catalogDbClient.getFirstByModelNameOrderByModelVersionDesc(defaultServiceModelName);
833 if (serviceRecord != null) {
834 recipe = catalogDbClient.getFirstByServiceModelUUIDAndAction(serviceRecord.getModelUUID(),
838 serviceRecord = catalogDbClient.getServiceByID(modelInfo.getModelVersionId());
839 recipe = catalogDbClient.getFirstByServiceModelUUIDAndAction(modelInfo.getModelVersionId(),
841 if (recipe == null) {
842 serviceRecordList = catalogDbClient
843 .getServiceByModelInvariantUUIDOrderByModelVersionDesc(modelInfo.getModelInvariantId());
844 if (!serviceRecordList.isEmpty()) {
845 for (org.onap.so.db.catalog.beans.Service record : serviceRecordList) {
846 recipe = catalogDbClient.getFirstByServiceModelUUIDAndAction(record.getModelUUID(),
848 if (recipe != null) {
856 // if an aLaCarte flag was sent in the request, throw an error if the recipe was not found
857 RequestParameters reqParam = requestDetails.getRequestParameters();
858 if (reqParam != null && alaCarteFlag && recipe == null) {
860 } else if (!alaCarteFlag && recipe != null && Action.createInstance.equals(action)) {
861 mapToLegacyRequest(requestDetails);
862 } else if (recipe == null) { // aLaCarte wasn't sent, so we'll try the default
863 serviceRecord = catalogDbClient.getFirstByModelNameOrderByModelVersionDesc(defaultServiceModelName);
864 recipe = catalogDbClient.getFirstByServiceModelUUIDAndAction(serviceRecord.getModelUUID(),
867 if (modelInfo.getModelVersionId() == null) {
868 modelInfo.setModelVersionId(serviceRecord.getModelUUID());
870 if (recipe == null) {
873 return new RecipeLookupResult(recipe.getOrchestrationUri(), recipe.getRecipeTimeout());
876 protected void mapToLegacyRequest(RequestDetails requestDetails) throws IOException {
877 RequestParameters reqParam;
878 if (requestDetails.getRequestParameters() == null) {
879 reqParam = new RequestParameters();
881 reqParam = requestDetails.getRequestParameters();
883 if (requestDetails.getCloudConfiguration() == null) {
884 CloudConfiguration cloudConfig = configureCloudConfig(reqParam);
885 if (cloudConfig != null) {
886 requestDetails.setCloudConfiguration(cloudConfig);
890 List<Map<String, Object>> userParams = configureUserParams(reqParam);
891 if (!userParams.isEmpty()) {
892 if (reqParam == null) {
893 requestDetails.setRequestParameters(new RequestParameters());
895 requestDetails.getRequestParameters().setUserParams(userParams);
899 private Service serviceMapper(Map<String, Object> params) throws IOException {
900 ObjectMapper obj = new ObjectMapper();
901 String input = obj.writeValueAsString(params.get("service"));
902 return obj.readValue(input, Service.class);
905 private void addUserParams(Map<String, Object> targetUserParams, List<Map<String, String>> sourceUserParams) {
906 for (Map<String, String> map : sourceUserParams) {
907 for (Map.Entry<String, String> entry : map.entrySet()) {
908 targetUserParams.put(entry.getKey(), entry.getValue());
913 protected List<Map<String, Object>> configureUserParams(RequestParameters reqParams) throws IOException {
914 logger.debug("Configuring UserParams for Macro Request");
915 Map<String, Object> userParams = new HashMap<>();
917 for (Map<String, Object> params : reqParams.getUserParams()) {
918 if (params.containsKey("service")) {
919 Service service = serviceMapper(params);
921 addUserParams(userParams, service.getInstanceParams());
923 for (Networks network : service.getResources().getNetworks()) {
924 addUserParams(userParams, network.getInstanceParams());
927 for (Vnfs vnf : service.getResources().getVnfs()) {
928 addUserParams(userParams, vnf.getInstanceParams());
930 for (VfModules vfModule : vnf.getVfModules()) {
931 addUserParams(userParams, vfModule.getInstanceParams());
937 return mapFlatMapToNameValue(userParams);
940 protected List<Map<String, Object>> mapFlatMapToNameValue(Map<String, Object> flatMap) {
941 List<Map<String, Object>> targetUserParams = new ArrayList<>();
943 for (Map.Entry<String, Object> map : flatMap.entrySet()) {
944 Map<String, Object> targetMap = new HashMap<>();
945 targetMap.put(NAME, map.getKey());
946 targetMap.put(VALUE, map.getValue());
947 targetUserParams.add(targetMap);
949 return targetUserParams;
952 protected CloudConfiguration configureCloudConfig(RequestParameters reqParams) throws IOException {
954 for (Map<String, Object> params : reqParams.getUserParams()) {
955 if (params.containsKey("service")) {
956 Service service = serviceMapper(params);
958 Optional<CloudConfiguration> targetConfiguration = addCloudConfig(service.getCloudConfiguration());
960 if (targetConfiguration.isPresent()) {
961 return targetConfiguration.get();
963 for (Networks network : service.getResources().getNetworks()) {
964 targetConfiguration = addCloudConfig(network.getCloudConfiguration());
965 if (targetConfiguration.isPresent()) {
966 return targetConfiguration.get();
970 for (Vnfs vnf : service.getResources().getVnfs()) {
971 targetConfiguration = addCloudConfig(vnf.getCloudConfiguration());
973 if (targetConfiguration.isPresent()) {
974 return targetConfiguration.get();
977 for (VfModules vfModule : vnf.getVfModules()) {
978 targetConfiguration = addCloudConfig(vfModule.getCloudConfiguration());
980 if (targetConfiguration.isPresent()) {
981 return targetConfiguration.get();
992 private RecipeLookupResult getDefaultVnfUri(ServiceInstancesRequest sir, Actions action) {
993 String defaultSource = getDefaultModel(sir);
994 VnfRecipe vnfRecipe = catalogDbClient.getFirstVnfRecipeByNfRoleAndAction(defaultSource, action.toString());
995 if (vnfRecipe == null) {
998 return new RecipeLookupResult(vnfRecipe.getOrchestrationUri(), vnfRecipe.getRecipeTimeout());
1002 private RecipeLookupResult getNetworkUri(ServiceInstancesRequest sir, Actions action) throws ValidationException {
1003 String defaultNetworkType = getDefaultModel(sir);
1004 ModelInfo modelInfo = sir.getRequestDetails().getModelInfo();
1005 String modelName = modelInfo.getModelName();
1006 Recipe recipe = null;
1008 if (modelInfo.getModelCustomizationId() != null) {
1009 NetworkResourceCustomization networkResourceCustomization = catalogDbClient
1010 .getNetworkResourceCustomizationByModelCustomizationUUID(modelInfo.getModelCustomizationId());
1011 if (networkResourceCustomization != null) {
1012 NetworkResource networkResource = networkResourceCustomization.getNetworkResource();
1013 if (networkResource != null) {
1014 if (modelInfo.getModelVersionId() == null) {
1015 modelInfo.setModelVersionId(networkResource.getModelUUID());
1017 recipe = catalogDbClient.getFirstNetworkRecipeByModelNameAndAction(networkResource.getModelName(),
1020 throw new ValidationException("no catalog entry found");
1022 } else if (action != Action.deleteInstance) {
1023 throw new ValidationException("modelCustomizationId for networkResourceCustomization lookup", true);
1026 // ok for version < 3 and action delete
1027 if (modelName != null) {
1028 recipe = catalogDbClient.getFirstNetworkRecipeByModelNameAndAction(modelName, action.toString());
1032 if (recipe == null) {
1033 recipe = catalogDbClient.getFirstNetworkRecipeByModelNameAndAction(defaultNetworkType, action.toString());
1036 return recipe != null ? new RecipeLookupResult(recipe.getOrchestrationUri(), recipe.getRecipeTimeout()) : null;
1040 private Optional<CloudConfiguration> addCloudConfig(CloudConfiguration sourceCloudConfiguration) {
1041 CloudConfiguration targetConfiguration = new CloudConfiguration();
1042 if (sourceCloudConfiguration != null) {
1043 targetConfiguration.setAicNodeClli(sourceCloudConfiguration.getAicNodeClli());
1044 targetConfiguration.setTenantId(sourceCloudConfiguration.getTenantId());
1045 targetConfiguration.setLcpCloudRegionId(sourceCloudConfiguration.getLcpCloudRegionId());
1046 targetConfiguration.setCloudOwner(sourceCloudConfiguration.getCloudOwner());
1047 return Optional.of(targetConfiguration);
1049 return Optional.empty();
1052 private RecipeLookupResult getVnfOrVfModuleUri(ServiceInstancesRequest servInstReq, Actions action)
1053 throws ValidationException {
1055 ModelInfo modelInfo = servInstReq.getRequestDetails().getModelInfo();
1056 String vnfComponentType = modelInfo.getModelType().name();
1058 RelatedInstanceList[] instanceList = null;
1059 if (servInstReq.getRequestDetails() != null) {
1060 instanceList = servInstReq.getRequestDetails().getRelatedInstanceList();
1064 String defaultSource = getDefaultModel(servInstReq);
1065 String modelCustomizationId = modelInfo.getModelCustomizationId();
1066 String modelCustomizationName = modelInfo.getModelCustomizationName();
1067 String relatedInstanceModelVersionId = null;
1068 String relatedInstanceModelInvariantId = null;
1069 String relatedInstanceVersion = null;
1070 String relatedInstanceModelCustomizationName = null;
1072 if (instanceList != null) {
1074 for (RelatedInstanceList relatedInstanceList : instanceList) {
1076 RelatedInstance relatedInstance = relatedInstanceList.getRelatedInstance();
1077 ModelInfo relatedInstanceModelInfo = relatedInstance.getModelInfo();
1078 if (relatedInstanceModelInfo.getModelType().equals(ModelType.service)) {
1079 relatedInstanceModelVersionId = relatedInstanceModelInfo.getModelVersionId();
1080 relatedInstanceVersion = relatedInstanceModelInfo.getModelVersion();
1083 if (relatedInstanceModelInfo.getModelType().equals(ModelType.vnf)) {
1084 relatedInstanceModelVersionId = relatedInstanceModelInfo.getModelVersionId();
1085 relatedInstanceModelInvariantId = relatedInstanceModelInfo.getModelInvariantId();
1086 relatedInstanceVersion = relatedInstanceModelInfo.getModelVersion();
1087 relatedInstanceModelCustomizationName = relatedInstanceModelInfo.getModelCustomizationName();
1091 if (modelInfo.getModelType().equals(ModelType.vnf)) {
1092 // a. For a vnf request (only create, no update currently):
1093 // i. (v3-v4) If modelInfo.modelCustomizationId is provided, use it to validate catalog DB has record in
1094 // vnf_resource_customization.model_customization_uuid.
1095 // ii. (v2-v4) If modelInfo.modelCustomizationId is NOT provided (because it is a pre-1702 ASDC model or
1096 // pre-v3), then modelInfo.modelCustomizationName must have
1097 // been provided (else create request should be rejected). APIH should use the
1098 // relatedInstance.modelInfo[service].modelVersionId** + modelInfo[vnf].modelCustomizationName
1099 // to “join�? service_to_resource_customizations with vnf_resource_customization to confirm a
1100 // vnf_resource_customization.model_customization_uuid record exists.
1101 // **If relatedInstance.modelInfo[service].modelVersionId was not provided, use
1102 // relatedInstance.modelInfo[service].modelInvariantId + modelVersion instead to lookup modelVersionId
1103 // (MODEL_UUID) in SERVICE table.
1104 // iii. Regardless of how the value was provided/obtained above, APIH must always populate
1105 // vnfModelCustomizationId in bpmnRequest. It would be assumed it was MSO generated
1106 // during 1707 data migration if VID did not provide it originally on request.
1107 // iv. Note: continue to construct the “vnf-type�? value and pass to BPMN (must still be populated
1109 // 1. If modelCustomizationName is NOT provided on a vnf/vfModule request, use modelCustomizationId to
1110 // look it up in our catalog to construct vnf-type value to pass to BPMN.
1112 VnfResource vnfResource = null;
1113 VnfResourceCustomization vrc = null;
1114 // Validation for vnfResource
1116 if (modelCustomizationId != null) {
1117 vrc = catalogDbClient.getVnfResourceCustomizationByModelCustomizationUUID(modelCustomizationId);
1119 vnfResource = vrc.getVnfResources();
1122 org.onap.so.db.catalog.beans.Service service =
1123 catalogDbClient.getServiceByID(relatedInstanceModelVersionId);
1124 if (service == null) {
1125 service = catalogDbClient.getServiceByModelVersionAndModelInvariantUUID(relatedInstanceVersion,
1126 relatedInstanceModelInvariantId);
1129 if (service == null) {
1130 throw new ValidationException("service in relatedInstance");
1132 for (VnfResourceCustomization vnfResourceCustom : service.getVnfCustomizations()) {
1133 if (vnfResourceCustom.getModelInstanceName().equals(modelCustomizationName)) {
1134 vrc = vnfResourceCustom;
1139 vnfResource = vrc.getVnfResources();
1140 modelInfo.setModelCustomizationId(vrc.getModelCustomizationUUID());
1141 modelInfo.setModelCustomizationUuid(vrc.getModelCustomizationUUID());
1145 if (vnfResource == null) {
1146 throw new ValidationException("vnfResource");
1148 if (modelInfo.getModelVersionId() == null) {
1149 modelInfo.setModelVersionId(vnfResource.getModelUUID());
1153 VnfRecipe vnfRecipe = null;
1156 String nfRole = vrc.getNfRole();
1157 if (nfRole != null) {
1159 catalogDbClient.getFirstVnfRecipeByNfRoleAndAction(vrc.getNfRole(), action.toString());
1163 if (vnfRecipe == null) {
1164 vnfRecipe = catalogDbClient.getFirstVnfRecipeByNfRoleAndAction(defaultSource, action.toString());
1167 if (vnfRecipe == null) {
1171 return new RecipeLookupResult(vnfRecipe.getOrchestrationUri(), vnfRecipe.getRecipeTimeout());
1174 * (v5-v7) If modelInfo.modelCustomizationId is NOT provided (because it is a pre-1702 ASDC model or
1175 * pre-v3), then modelInfo.modelCustomizationName must have // been provided (else create request should
1176 * be rejected). APIH should use the relatedInstance.modelInfo[vnf].modelVersionId +
1177 * modelInfo[vnf].modelCustomizationName // to join vnf_to_resource_customizations with
1178 * vf_resource_customization to confirm a vf_resource_customization.model_customization_uuid record
1179 * exists. // Once the vnfs model_customization_uuid has been obtained, use it to find all vfModule
1180 * customizations for that vnf customization in the vnf_res_custom_to_vf_module_custom join table. //
1181 * For each vf_module_cust_model_customization_uuid value returned, use that UUID to query
1182 * vf_module_customization table along with modelInfo[vfModule|volumeGroup].modelVersionId to // confirm
1183 * record matches request data (and to identify the modelCustomizationId associated with the vfModule in
1184 * the request). This means taking each record found // in vf_module_customization and looking up in
1185 * vf_module (using vf_module_customization’s FK into vf_module) to find a match on
1186 * MODEL_INVARIANT_UUID (modelInvariantId) // and MODEL_VERSION (modelVersion).
1188 VfModuleCustomization vfmc = null;
1190 VnfResourceCustomization vnfrc;
1191 VfModule vfModule = null;
1193 if (modelInfo.getModelCustomizationId() != null) {
1194 vfmc = catalogDbClient
1195 .getVfModuleCustomizationByModelCuztomizationUUID(modelInfo.getModelCustomizationId());
1197 vnfr = catalogDbClient.getVnfResourceByModelUUID(relatedInstanceModelVersionId);
1199 vnfr = catalogDbClient.getFirstVnfResourceByModelInvariantUUIDAndModelVersion(
1200 relatedInstanceModelInvariantId, relatedInstanceVersion);
1202 vnfrc = catalogDbClient.getFirstVnfResourceCustomizationByModelInstanceNameAndVnfResources(
1203 relatedInstanceModelCustomizationName, vnfr);
1205 List<VfModuleCustomization> list = vnfrc.getVfModuleCustomizations();
1207 String vfModuleModelUUID = modelInfo.getModelVersionId();
1208 for (VfModuleCustomization vf : list) {
1209 VfModuleCustomization vfmCustom;
1210 if (vfModuleModelUUID != null) {
1211 vfmCustom = catalogDbClient
1212 .getVfModuleCustomizationByModelCustomizationUUIDAndVfModuleModelUUID(
1213 vf.getModelCustomizationUUID(), vfModuleModelUUID);
1214 if (vfmCustom != null) {
1215 vfModule = vfmCustom.getVfModule();
1218 vfmCustom = catalogDbClient
1219 .getVfModuleCustomizationByModelCuztomizationUUID(vf.getModelCustomizationUUID());
1220 if (vfmCustom != null) {
1221 vfModule = vfmCustom.getVfModule();
1223 vfModule = catalogDbClient.getVfModuleByModelInvariantUUIDAndModelVersion(
1224 relatedInstanceModelInvariantId, relatedInstanceVersion);
1228 if (vfModule != null) {
1229 modelInfo.setModelCustomizationId(vf.getModelCustomizationUUID());
1230 modelInfo.setModelCustomizationUuid(vf.getModelCustomizationUUID());
1236 if (vfmc == null && vfModule == null) {
1237 throw new ValidationException("vfModuleCustomization");
1238 } else if (vfModule == null && vfmc != null) {
1239 vfModule = vfmc.getVfModule(); // can't be null as vfModuleModelUUID is not-null property in
1240 // VfModuleCustomization table
1243 if (modelInfo.getModelVersionId() == null) {
1244 modelInfo.setModelVersionId(vfModule.getModelUUID());
1248 recipe = catalogDbClient.getFirstVnfComponentsRecipeByVfModuleModelUUIDAndVnfComponentTypeAndAction(
1249 vfModule.getModelUUID(), vnfComponentType, action.toString());
1250 if (recipe == null) {
1251 List<VfModule> vfModuleRecords = catalogDbClient
1252 .getVfModuleByModelInvariantUUIDOrderByModelVersionDesc(vfModule.getModelInvariantUUID());
1253 if (!vfModuleRecords.isEmpty()) {
1254 for (VfModule record : vfModuleRecords) {
1255 recipe = catalogDbClient
1256 .getFirstVnfComponentsRecipeByVfModuleModelUUIDAndVnfComponentTypeAndAction(
1257 record.getModelUUID(), vnfComponentType, action.toString());
1258 if (recipe != null) {
1264 if (recipe == null) {
1265 recipe = catalogDbClient.getFirstVnfComponentsRecipeByVfModuleModelUUIDAndVnfComponentTypeAndAction(
1266 defaultSource, vnfComponentType, action.toString());
1267 if (recipe == null) {
1268 recipe = catalogDbClient.getFirstVnfComponentsRecipeByVnfComponentTypeAndAction(
1269 vnfComponentType, action.toString());
1272 if (recipe == null) {
1279 if (modelInfo.getModelType().equals(ModelType.vnf)) {
1280 recipe = catalogDbClient.getFirstVnfRecipeByNfRoleAndAction(defaultSource, action.toString());
1281 if (recipe == null) {
1285 recipe = catalogDbClient.getFirstVnfComponentsRecipeByVfModuleModelUUIDAndVnfComponentTypeAndAction(
1286 defaultSource, vnfComponentType, action.toString());
1288 if (recipe == null) {
1294 return new RecipeLookupResult(recipe.getOrchestrationUri(), recipe.getRecipeTimeout());