Catalog alignment
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / validation / ServiceDistributionValidation.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.be.components.validation;
22
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;
40
41 import javax.annotation.Resource;
42 import java.util.Arrays;
43 import java.util.List;
44
45 /**
46  * Created by chaya on 10/18/2017.
47  */
48 @org.springframework.stereotype.Component("serviceDistributionValidation")
49 public class ServiceDistributionValidation {
50     private static final Logger log = Logger.getLogger(ServiceDistributionValidation.class);
51     @Resource
52     private ComponentsUtils componentsUtils;
53     @Resource
54     private ToscaOperationFacade toscaOperationFacade;
55     @Resource
56     private UserValidations userValidations;
57     @Resource
58     private IDistributionEngine distributionEngine;
59
60     public Either<ActivationRequestInformation, ResponseFormat> validateActivateServiceRequest(String serviceUUID, String opEnvId, User modifier, ServiceDistributionReqInfo data) {
61         try {
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());
72         }
73     }
74
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);
79         }
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);
85         }
86         return (Service)latestComponentByUuid.left().value();
87     }
88
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);
95         }
96         return workloadContext;
97     }
98
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);
103         }
104         OperationalEnvironmentEntry operationalEnvironment = distributionEngine.getEnvironmentById(opEnvId);
105         if (operationalEnvironment == null) {
106             return failOnEnvNotExist(opEnvId);
107         }
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);
112         }
113         return operationalEnvironment;
114     }
115
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);
118     }
119
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);
126         }
127     }
128     private void validateUserExists(String userId) {
129         userValidations.validateUserExists(userId);
130     }
131
132     private void validateDistributionServiceLifeCycleState(Service serviceToActivate) {
133         validateServiceState(serviceToActivate,
134                 Arrays.asList(LifecycleStateEnum.CERTIFIED));
135     }
136 }