695ee583006effd2514d46697a88c95110ccd9ad
[sdc.git] /
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     public void validateAbstractServiceRequest(String serviceUUID, User modifier) {
76         validateUserExists(modifier.getUserId());
77         Service abstractService = validateServiceExists(serviceUUID);
78         validateDistributionServiceLifeCycleState(abstractService);
79     }
80         
81     private Service validateServiceExists(String serviceUUID) {
82         if (StringUtils.isEmpty(serviceUUID.trim())) {
83             ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.BAD_REQUEST_MISSING_RESOURCE);
84             throw new ValidationException(responseFormat);
85         }
86         Either<Component, StorageOperationStatus> latestComponentByUuid = toscaOperationFacade.getLatestServiceByUuid(serviceUUID);
87         if (latestComponentByUuid.isRight()) {
88             log.error("failed retrieving service {}", serviceUUID);
89             ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.API_RESOURCE_NOT_FOUND, ApiResourceEnum.SERVICE_ID.getValue());
90             throw new ValidationException(responseFormat);
91         }
92         return (Service)latestComponentByUuid.left().value();
93     }
94
95     private String validateWorkloadContext(ServiceDistributionReqInfo data) {
96         String workloadContext = data.getWorkloadContext();
97         if (workloadContext == null || workloadContext.isEmpty()) {
98             log.error("workload context does not exist on data to distribute");
99             ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.MISSING_BODY);
100             throw new ValidationException(responseFormat);
101         }
102         return workloadContext;
103     }
104
105     private OperationalEnvironmentEntry validateOperationalEnvExists(String opEnvId) {
106         if (StringUtils.isEmpty(opEnvId.trim())) {
107             ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.BAD_REQUEST_MISSING_RESOURCE);
108             throw new ValidationException(responseFormat);
109         }
110         OperationalEnvironmentEntry operationalEnvironment = distributionEngine.getEnvironmentById(opEnvId);
111         if (operationalEnvironment == null) {
112             return failOnEnvNotExist(opEnvId);
113         }
114         if (!operationalEnvironment.getStatus().equals(EnvironmentStatusEnum.COMPLETED.getName())) {
115             log.error("the operational environment is not ready to receive distributions. environment status: {}", operationalEnvironment.getStatus());
116             ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.API_RESOURCE_NOT_FOUND , ApiResourceEnum.ENVIRONMENT_ID.getValue());
117             throw new ValidationException(responseFormat);
118         }
119         return operationalEnvironment;
120     }
121
122     private OperationalEnvironmentEntry failOnEnvNotExist(String opEnvId) {
123         return ValidationUtils.throwValidationException(componentsUtils.getResponseFormat(ActionStatus.API_RESOURCE_NOT_FOUND, ApiResourceEnum.ENVIRONMENT_ID.getValue()), "failed to get operational environment {}", opEnvId);
124     }
125
126     private void validateServiceState(Service service, List<LifecycleStateEnum> allowedStates) {
127         LifecycleStateEnum state = service.getLifecycleState();
128         if (!allowedStates.contains(state)) {
129             log.error("service {} life cycle state {} is not valid for distribution", service.getUniqueId(), service.getLifecycleState());
130             ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.INVALID_SERVICE_STATE);
131             throw new ValidationException(responseFormat);
132         }
133     }
134     private void validateUserExists(String userId) {
135         userValidations.validateUserExists(userId);
136     }
137
138     private void validateDistributionServiceLifeCycleState(Service serviceToActivate) {
139         validateServiceState(serviceToActivate,
140                 Arrays.asList(LifecycleStateEnum.CERTIFIED));
141     }
142 }