2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.be.components.validation;
23 import fj.data.Either;
24 import org.apache.commons.lang.StringUtils;
25 import org.openecomp.sdc.be.components.distribution.engine.IDistributionEngine;
26 import org.openecomp.sdc.be.components.impl.ActivationRequestInformation;
27 import org.openecomp.sdc.be.dao.api.ActionStatus;
28 import org.openecomp.sdc.be.datatypes.enums.EnvironmentStatusEnum;
29 import org.openecomp.sdc.be.externalapi.servlet.representation.ServiceDistributionReqInfo;
30 import org.openecomp.sdc.be.impl.ComponentsUtils;
31 import org.openecomp.sdc.be.model.Component;
32 import org.openecomp.sdc.be.model.LifecycleStateEnum;
33 import org.openecomp.sdc.be.model.Service;
34 import org.openecomp.sdc.be.model.User;
35 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
36 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
37 import org.openecomp.sdc.be.resources.data.OperationalEnvironmentEntry;
38 import org.openecomp.sdc.common.log.wrappers.Logger;
39 import org.openecomp.sdc.exception.ResponseFormat;
41 import javax.annotation.Resource;
42 import java.util.Arrays;
43 import java.util.List;
46 * Created by chaya on 10/18/2017.
48 @org.springframework.stereotype.Component("serviceDistributionValidation")
49 public class ServiceDistributionValidation {
50 private static final Logger log = Logger.getLogger(ServiceDistributionValidation.class);
52 private ComponentsUtils componentsUtils;
54 private ToscaOperationFacade toscaOperationFacade;
56 private UserValidations userValidations;
58 private IDistributionEngine distributionEngine;
60 public Either<ActivationRequestInformation, ResponseFormat> validateActivateServiceRequest(String serviceUUID, String opEnvId, User modifier, ServiceDistributionReqInfo data) {
62 validateUserExists(modifier.getUserId());
63 Service serviceToActivate = validateServiceExists(serviceUUID);
64 validateDistributionServiceLifeCycleState(serviceToActivate);
65 OperationalEnvironmentEntry operationalEnvironmentEntry = validateOperationalEnvExists(opEnvId);
66 String workloadContext = validateWorkloadContext(data);
67 ActivationRequestInformation activationRequestInformation = new ActivationRequestInformation(serviceToActivate, workloadContext, operationalEnvironmentEntry.getTenant());
68 return Either.left(activationRequestInformation);
69 } catch (ValidationException e) {
70 log.error("failed while validating activate service UUID {} request. error {}", serviceUUID, e.getExceptionResponseFormat(), e);
71 return Either.right(e.getExceptionResponseFormat());
75 private Service validateServiceExists(String serviceUUID) {
76 if (StringUtils.isEmpty(serviceUUID.trim())) {
77 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.BAD_REQUEST_MISSING_RESOURCE);
78 throw new ValidationException(responseFormat);
80 Either<Component, StorageOperationStatus> latestComponentByUuid = toscaOperationFacade.getLatestServiceByUuid(serviceUUID);
81 if (latestComponentByUuid.isRight()) {
82 log.error("failed retrieving service {}", serviceUUID);
83 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.API_RESOURCE_NOT_FOUND, ApiResourceEnum.SERVICE_ID.getValue());
84 throw new ValidationException(responseFormat);
86 return (Service)latestComponentByUuid.left().value();
89 private String validateWorkloadContext(ServiceDistributionReqInfo data) {
90 String workloadContext = data.getWorkloadContext();
91 if (workloadContext == null || workloadContext.isEmpty()) {
92 log.error("workload context does not exist on data to distribute");
93 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.MISSING_BODY);
94 throw new ValidationException(responseFormat);
96 return workloadContext;
99 private OperationalEnvironmentEntry validateOperationalEnvExists(String opEnvId) {
100 if (StringUtils.isEmpty(opEnvId.trim())) {
101 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.BAD_REQUEST_MISSING_RESOURCE);
102 throw new ValidationException(responseFormat);
104 OperationalEnvironmentEntry operationalEnvironment = distributionEngine.getEnvironmentById(opEnvId);
105 if (operationalEnvironment == null) {
106 return failOnEnvNotExist(opEnvId);
108 if (!operationalEnvironment.getStatus().equals(EnvironmentStatusEnum.COMPLETED.getName())) {
109 log.error("the operational environment is not ready to receive distributions. environment status: {}", operationalEnvironment.getStatus());
110 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.API_RESOURCE_NOT_FOUND , ApiResourceEnum.ENVIRONMENT_ID.getValue());
111 throw new ValidationException(responseFormat);
113 return operationalEnvironment;
116 private OperationalEnvironmentEntry failOnEnvNotExist(String opEnvId) {
117 return ValidationUtils.throwValidationException(componentsUtils.getResponseFormat(ActionStatus.API_RESOURCE_NOT_FOUND, ApiResourceEnum.ENVIRONMENT_ID.getValue()), "failed to get operational environment {}", opEnvId);
120 private void validateServiceState(Service service, List<LifecycleStateEnum> allowedStates) {
121 LifecycleStateEnum state = service.getLifecycleState();
122 if (!allowedStates.contains(state)) {
123 log.error("service {} life cycle state {} is not valid for distribution", service.getUniqueId(), service.getLifecycleState());
124 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.INVALID_SERVICE_STATE);
125 throw new ValidationException(responseFormat);
128 private void validateUserExists(String userId) {
129 userValidations.validateUserExists(userId);
132 private void validateDistributionServiceLifeCycleState(Service serviceToActivate) {
133 validateServiceState(serviceToActivate,
134 Arrays.asList(LifecycleStateEnum.CERTIFIED));