Update vulnerable package dependencies
[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 package org.openecomp.sdc.be.components.validation;
21
22 import fj.data.Either;
23 import java.util.Arrays;
24 import java.util.List;
25 import javax.annotation.Resource;
26 import org.apache.commons.lang3.StringUtils;
27 import org.openecomp.sdc.be.components.distribution.engine.IDistributionEngine;
28 import org.openecomp.sdc.be.components.impl.ActivationRequestInformation;
29 import org.openecomp.sdc.be.dao.api.ActionStatus;
30 import org.openecomp.sdc.be.datatypes.enums.EnvironmentStatusEnum;
31 import org.openecomp.sdc.be.externalapi.servlet.representation.ServiceDistributionReqInfo;
32 import org.openecomp.sdc.be.impl.ComponentsUtils;
33 import org.openecomp.sdc.be.model.Component;
34 import org.openecomp.sdc.be.model.LifecycleStateEnum;
35 import org.openecomp.sdc.be.model.Service;
36 import org.openecomp.sdc.be.model.User;
37 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
38 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
39 import org.openecomp.sdc.be.resources.data.OperationalEnvironmentEntry;
40 import org.openecomp.sdc.common.log.wrappers.Logger;
41 import org.openecomp.sdc.exception.ResponseFormat;
42
43 /**
44  * Created by chaya on 10/18/2017.
45  */
46 @org.springframework.stereotype.Component("serviceDistributionValidation")
47 public class ServiceDistributionValidation {
48
49     private static final Logger log = Logger.getLogger(ServiceDistributionValidation.class);
50     @Resource
51     private ComponentsUtils componentsUtils;
52     @Resource
53     private ToscaOperationFacade toscaOperationFacade;
54     @Resource
55     private UserValidations userValidations;
56     @Resource
57     private IDistributionEngine distributionEngine;
58
59     public Either<ActivationRequestInformation, ResponseFormat> validateActivateServiceRequest(String serviceUUID, String opEnvId, User modifier,
60                                                                                                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,
68                 operationalEnvironmentEntry.getTenant());
69             return Either.left(activationRequestInformation);
70         } catch (ValidationException e) {
71             log.error("failed while validating activate service UUID {} request. error {}", serviceUUID, e.getExceptionResponseFormat(), e);
72             return Either.right(e.getExceptionResponseFormat());
73         }
74     }
75
76     public void validateAbstractServiceRequest(String serviceUUID, User modifier) {
77         validateUserExists(modifier.getUserId());
78         Service abstractService = validateServiceExists(serviceUUID);
79         validateDistributionServiceLifeCycleState(abstractService);
80     }
81
82     private Service validateServiceExists(String serviceUUID) {
83         if (StringUtils.isEmpty(serviceUUID.trim())) {
84             ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.BAD_REQUEST_MISSING_RESOURCE);
85             throw new ValidationException(responseFormat);
86         }
87         Either<Component, StorageOperationStatus> latestComponentByUuid = toscaOperationFacade.getLatestServiceByUuid(serviceUUID);
88         if (latestComponentByUuid.isRight()) {
89             log.error("failed retrieving service {}", serviceUUID);
90             ResponseFormat responseFormat = componentsUtils
91                 .getResponseFormat(ActionStatus.API_RESOURCE_NOT_FOUND, ApiResourceEnum.SERVICE_ID.getValue());
92             throw new ValidationException(responseFormat);
93         }
94         return (Service) latestComponentByUuid.left().value();
95     }
96
97     private String validateWorkloadContext(ServiceDistributionReqInfo data) {
98         String workloadContext = data.getWorkloadContext();
99         if (workloadContext == null || workloadContext.isEmpty()) {
100             log.error("workload context does not exist on data to distribute");
101             ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.MISSING_BODY);
102             throw new ValidationException(responseFormat);
103         }
104         return workloadContext;
105     }
106
107     private OperationalEnvironmentEntry validateOperationalEnvExists(String opEnvId) {
108         if (StringUtils.isEmpty(opEnvId.trim())) {
109             ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.BAD_REQUEST_MISSING_RESOURCE);
110             throw new ValidationException(responseFormat);
111         }
112         OperationalEnvironmentEntry operationalEnvironment = distributionEngine.getEnvironmentById(opEnvId);
113         if (operationalEnvironment == null) {
114             return failOnEnvNotExist(opEnvId);
115         }
116         if (!operationalEnvironment.getStatus().equals(EnvironmentStatusEnum.COMPLETED.getName())) {
117             log.error("the operational environment is not ready to receive distributions. environment status: {}",
118                 operationalEnvironment.getStatus());
119             ResponseFormat responseFormat = componentsUtils
120                 .getResponseFormat(ActionStatus.API_RESOURCE_NOT_FOUND, ApiResourceEnum.ENVIRONMENT_ID.getValue());
121             throw new ValidationException(responseFormat);
122         }
123         return operationalEnvironment;
124     }
125
126     private OperationalEnvironmentEntry failOnEnvNotExist(String opEnvId) {
127         return ValidationUtils.throwValidationException(
128             componentsUtils.getResponseFormat(ActionStatus.API_RESOURCE_NOT_FOUND, ApiResourceEnum.ENVIRONMENT_ID.getValue()),
129             "failed to get operational environment {}", opEnvId);
130     }
131
132     private void validateServiceState(Service service, List<LifecycleStateEnum> allowedStates) {
133         LifecycleStateEnum state = service.getLifecycleState();
134         if (!allowedStates.contains(state)) {
135             log.error("service {} life cycle state {} is not valid for distribution", service.getUniqueId(), service.getLifecycleState());
136             ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.INVALID_SERVICE_STATE);
137             throw new ValidationException(responseFormat);
138         }
139     }
140
141     private void validateUserExists(String userId) {
142         userValidations.validateUserExists(userId);
143     }
144
145     private void validateDistributionServiceLifeCycleState(Service serviceToActivate) {
146         validateServiceState(serviceToActivate, Arrays.asList(LifecycleStateEnum.CERTIFIED));
147     }
148 }