2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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.lifecycle;
23 import java.util.Arrays;
24 import java.util.HashMap;
25 import java.util.List;
27 import java.util.Optional;
30 import org.openecomp.sdc.be.components.distribution.engine.ServiceDistributionArtifactsBuilder;
31 import org.openecomp.sdc.be.components.impl.ComponentBusinessLogic;
32 import org.openecomp.sdc.be.components.impl.ServiceBusinessLogic;
33 import org.openecomp.sdc.be.config.BeEcompErrorManager;
34 import org.openecomp.sdc.be.dao.api.ActionStatus;
35 import org.openecomp.sdc.be.dao.jsongraph.TitanDao;
36 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
37 import org.openecomp.sdc.be.datatypes.enums.OriginTypeEnum;
38 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
39 import org.openecomp.sdc.be.impl.ComponentsUtils;
40 import org.openecomp.sdc.be.model.ArtifactDefinition;
41 import org.openecomp.sdc.be.model.Component;
42 import org.openecomp.sdc.be.model.ComponentInstance;
43 import org.openecomp.sdc.be.model.LifeCycleTransitionEnum;
44 import org.openecomp.sdc.be.model.LifecycleStateEnum;
45 import org.openecomp.sdc.be.model.Operation;
46 import org.openecomp.sdc.be.model.Resource;
47 import org.openecomp.sdc.be.model.Service;
48 import org.openecomp.sdc.be.model.User;
49 import org.openecomp.sdc.be.model.jsontitan.datamodel.ToscaElement;
50 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaElementLifecycleOperation;
51 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
52 import org.openecomp.sdc.be.model.jsontitan.utils.ModelConverter;
53 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
54 import org.openecomp.sdc.be.model.operations.impl.CapabilityOperation;
55 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
56 import org.openecomp.sdc.be.tosca.ToscaExportHandler;
57 import org.openecomp.sdc.be.user.Role;
58 import org.openecomp.sdc.common.util.ValidationUtils;
59 import org.openecomp.sdc.exception.ResponseFormat;
60 import org.slf4j.Logger;
61 import org.slf4j.LoggerFactory;
63 import fj.data.Either;
65 public class CertificationRequestTransition extends LifeCycleTransition {
67 private static final Logger log = LoggerFactory.getLogger(CertificationRequestTransition.class);
69 private CapabilityOperation capabilityOperation;
70 private ServiceBusinessLogic serviceBusinessLogic;
71 public CertificationRequestTransition(ComponentsUtils componentUtils, ToscaElementLifecycleOperation lifecycleOperation, ServiceDistributionArtifactsBuilder serviceDistributionArtifactsBuilder, ServiceBusinessLogic serviceBusinessLogic,
72 CapabilityOperation capabilityOperation, ToscaExportHandler toscaExportUtils, ToscaOperationFacade toscaOperationFacade, TitanDao titanDao) {
73 super(componentUtils, lifecycleOperation, toscaOperationFacade, titanDao);
76 Role[] resourceServiceCheckoutRoles = { Role.ADMIN, Role.DESIGNER };
77 addAuthorizedRoles(ComponentTypeEnum.RESOURCE, Arrays.asList(resourceServiceCheckoutRoles));
78 addAuthorizedRoles(ComponentTypeEnum.SERVICE, Arrays.asList(resourceServiceCheckoutRoles));
79 // TODO to be later defined for product
80 // addAuthorizedRoles(ComponentTypeEnum.PRODUCT,
81 // Arrays.asList(productCheckoutRoles));
83 //additional authorized roles for resource type
84 Role[] resourceRoles = { Role.TESTER};
85 addResouceAuthorizedRoles(ResourceTypeEnum.VFCMT, Arrays.asList(resourceRoles));
87 this.serviceBusinessLogic = serviceBusinessLogic;
88 this.capabilityOperation = capabilityOperation;
92 public LifeCycleTransitionEnum getName() {
93 return LifeCycleTransitionEnum.CERTIFICATION_REQUEST;
97 public AuditingActionEnum getAuditingAction() {
98 return AuditingActionEnum.CERTIFICATION_REQUEST_RESOURCE;
101 protected Either<Boolean, ResponseFormat> validateAllResourceInstanceCertified(Component component) {
102 Either<Boolean, ResponseFormat> eitherResult = Either.left(true);
104 List<ComponentInstance> resourceInstance = component.getComponentInstances();
105 if (resourceInstance != null) {
106 Optional<ComponentInstance> nonCertifiedRIOptional = resourceInstance.stream().filter(p -> !ValidationUtils.validateCertifiedVersion(p.getComponentVersion())).findAny();
107 // Uncertified Resource Found
108 if (nonCertifiedRIOptional.isPresent()) {
109 ComponentInstance nonCertifiedRI = nonCertifiedRIOptional.get();
110 ResponseFormat resFormat = getRelevantResponseFormatUncertifiedRI(nonCertifiedRI, component.getComponentType());
111 eitherResult = Either.right(resFormat);
117 private ResponseFormat getRelevantResponseFormatUncertifiedRI(ComponentInstance nonCertifiedRI, ComponentTypeEnum componentType) {
119 ResponseFormat responseFormat;
120 Either<Resource, StorageOperationStatus> eitherResource = toscaOperationFacade.getToscaElement(nonCertifiedRI.getComponentUid());
121 if (eitherResource.isRight()) {
123 responseFormat = componentUtils.getResponseFormat(ActionStatus.GENERAL_ERROR);
126 ActionStatus actionStatus;
127 Resource resource = eitherResource.left().value();
128 Either<Resource, StorageOperationStatus> status = toscaOperationFacade.findLastCertifiedToscaElementByUUID(resource);
130 if (ValidationUtils.validateMinorVersion(nonCertifiedRI.getComponentVersion())) {
131 if (status.isRight() || status.left().value() == null) {
132 actionStatus = ActionStatus.VALIDATED_RESOURCE_NOT_FOUND;
134 actionStatus = ActionStatus.FOUND_ALREADY_VALIDATED_RESOURCE;
137 if (status.isRight() || status.left().value() == null)
138 actionStatus = ActionStatus.FOUND_LIST_VALIDATED_RESOURCES;
140 actionStatus = ActionStatus.FOUND_ALREADY_VALIDATED_RESOURCE;
144 String compType = (componentType == ComponentTypeEnum.RESOURCE) ? "VF" : "service";
145 responseFormat = componentUtils.getResponseFormat(actionStatus, compType, resource.getName());
147 return responseFormat;
151 public Either<? extends Component, ResponseFormat> changeState(ComponentTypeEnum componentType, Component component, ComponentBusinessLogic componentBl, User modifier, User owner, boolean shouldLock, boolean inTransaction) {
153 log.debug("start performing certification request for resource {}", component.getUniqueId());
155 ActionStatus actionStatus;
156 ResponseFormat responseFormat;
157 Either<? extends Component, ResponseFormat> result = null;
159 if (component.isTopologyTemplate()) {
161 Either<Boolean, ResponseFormat> statusCert = validateAllResourceInstanceCertified(component);
162 if (statusCert.isRight()) {
163 return Either.right(statusCert.right().value());
166 statusCert = validateConfiguredAtomicReqCapSatisfied(component);
167 if (statusCert.isRight()) {
168 return Either.right(statusCert.right().value());
171 if (componentType == ComponentTypeEnum.SERVICE) {
173 Either<Service, ResponseFormat> generateHeatEnvResult = serviceBusinessLogic.generateHeatEnvArtifacts((Service) component, modifier, shouldLock, inTransaction);
175 if (generateHeatEnvResult.isRight()) {
176 return Either.right(generateHeatEnvResult.right().value());
178 Either<Service, ResponseFormat> generateVfModuleResult = serviceBusinessLogic.generateVfModuleArtifacts(generateHeatEnvResult.left().value(), modifier, shouldLock, inTransaction);
179 if (generateVfModuleResult.isRight()) {
180 return Either.right(generateVfModuleResult.right().value());
182 component = generateVfModuleResult.left().value();
186 Either<Either<ArtifactDefinition, Operation>, ResponseFormat> eitherPopulated = componentBl.populateToscaArtifacts(component, modifier, true, inTransaction, shouldLock);
187 if (eitherPopulated != null && eitherPopulated.isRight()) {
188 return Either.right(eitherPopulated.right().value());
191 Either<ToscaElement, StorageOperationStatus> certificationRequestResult = lifeCycleOperation.requestCertificationToscaElement(component.getUniqueId(), modifier.getUserId(), owner.getUserId());
192 if (certificationRequestResult.isRight()) {
193 log.debug("checkout failed on graph");
194 StorageOperationStatus response = certificationRequestResult.right().value();
195 actionStatus = componentUtils.convertFromStorageResponse(response);
197 if (response.equals(StorageOperationStatus.ENTITY_ALREADY_EXISTS)) {
198 actionStatus = ActionStatus.COMPONENT_VERSION_ALREADY_EXIST;
200 responseFormat = componentUtils.getResponseFormatByComponent(actionStatus, component, componentType);
201 result = Either.right(responseFormat);
204 result = Either.left(ModelConverter.convertFromToscaElement(certificationRequestResult.left().value()));
207 if (result == null || result.isRight()) {
208 BeEcompErrorManager.getInstance().logBeDaoSystemError("Change LifecycleState");
209 if (!inTransaction) {
210 log.debug("operation failed. do rollback");
214 if (!inTransaction) {
215 log.debug("operation success. do commit");
223 private Either<Boolean, ResponseFormat> validateConfiguredAtomicReqCapSatisfied(Component component) {
224 log.debug("Submit for testing validation - Start validating configured req/cap satisfied for inner atomic instances, component id:{}", component.getUniqueId());
225 List<ComponentInstance> componentInstances = component.getComponentInstances();
226 if (componentInstances != null) {
227 // Prepare relationships data structures
228 // Better make it list than set in case we need to count req/cap
229 // occurrences in the future
230 Map<String, List<String>> reqName2Ids = new HashMap<>();
231 Map<String, List<String>> capName2Ids = new HashMap<>();
232 Map<String, Set<String>> requirementsToFulfillBeforeCert = configurationManager.getConfiguration().getRequirementsToFulfillBeforeCert();
233 Map<String, Set<String>> capabilitiesToConsumeBeforeCert = configurationManager.getConfiguration().getCapabilitiesToConsumeBeforeCert();
234 for (ComponentInstance compInst : componentInstances) {
235 String compInstId = compInst.getUniqueId();
236 OriginTypeEnum originType = compInst.getOriginType();
237 if (originType == null) {
238 log.error("Origin type is not set for component instance {} - it shouldn't happen. Skipping this component instance...", compInst.getUniqueId());
241 String compInstType = originType.getValue();
242 // Validating configured requirements fulfilled
243 if (null != requirementsToFulfillBeforeCert) {
244 Set<String> reqToFulfillForType = requirementsToFulfillBeforeCert.get(compInstType);
245 if (reqToFulfillForType != null) {
246 for (String reqNameToFulfill : reqToFulfillForType) {
247 List<String> reqNameList = reqName2Ids.get(reqNameToFulfill);
248 if (reqNameList == null || !reqNameList.contains(compInstId)) {
249 log.debug("Requirement {} wasn't fulfilled for component instance {} of type {}", reqNameToFulfill, compInstId, compInstType);
250 ComponentTypeEnum componentType = component.getComponentType();
251 String compParam = (componentType == ComponentTypeEnum.RESOURCE) ? "VF" : componentType.getValue().toLowerCase();
252 ResponseFormat responseFormat = componentUtils.getResponseFormat(ActionStatus.REQ_CAP_NOT_SATISFIED_BEFORE_CERTIFICATION, component.getName(), compParam, originType.getDisplayValue(), compInst.getName(), "requirement",
253 reqNameToFulfill, "fulfilled");
254 return Either.right(responseFormat);
259 // Validating configured capabilities consumed
260 if (null != capabilitiesToConsumeBeforeCert) {
261 Set<String> capToConsumeForType = capabilitiesToConsumeBeforeCert.get(compInstType);
262 if (capToConsumeForType != null) {
263 for (String capNameToConsume : capToConsumeForType) {
264 List<String> capNameList = capName2Ids.get(capNameToConsume);
265 if (capNameList == null || !capNameList.contains(compInstId)) {
266 log.debug("Capability {} wasn't consumed for component instance {} of type {}", capNameToConsume, compInstId, compInstType);
267 ComponentTypeEnum componentType = component.getComponentType();
268 String compParam = (componentType == ComponentTypeEnum.RESOURCE) ? "VF" : componentType.getValue().toLowerCase();
269 ResponseFormat responseFormat = componentUtils.getResponseFormat(ActionStatus.REQ_CAP_NOT_SATISFIED_BEFORE_CERTIFICATION, component.getName(), compParam, originType.getDisplayValue(), compInst.getName(), "capability",
270 capNameToConsume, "consumed");
271 return Either.right(responseFormat);
278 log.debug("Submit for testing validation - validating configured req/cap satisfied for inner atomic instances finished successfully, component id:{}", component.getUniqueId());
279 return Either.left(true);
283 public Either<Boolean, ResponseFormat> validateBeforeTransition(Component component, ComponentTypeEnum componentType, User modifier, User owner, LifecycleStateEnum oldState, LifecycleChangeInfoWithAction lifecycleChangeInfo) {
284 String componentName = component.getComponentMetadataDefinition().getMetadataDataDefinition().getName();
285 log.debug("validate before certification request. resource name={}, oldState={}, owner userId={}", componentName, oldState, owner.getUserId());
288 Either<Boolean, ResponseFormat> userValidationResponse = userRoleValidation(modifier,component, componentType, lifecycleChangeInfo);
289 if (userValidationResponse.isRight()) {
290 return userValidationResponse;
293 // case of "atomic" checkin and certification request - modifier must be
295 if (oldState.equals(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT) && !modifier.equals(owner) && !modifier.getRole().equals(Role.ADMIN.name())) {
296 ResponseFormat error = componentUtils.getResponseFormat(ActionStatus.COMPONENT_CHECKOUT_BY_ANOTHER_USER, componentName, componentType.name().toLowerCase(), owner.getFirstName(), owner.getLastName(), owner.getUserId());
297 return Either.right(error);
301 if (oldState.equals(LifecycleStateEnum.CERTIFIED)) {
302 ResponseFormat error = componentUtils.getResponseFormat(ActionStatus.COMPONENT_ALREADY_CERTIFIED, componentName, componentType.name().toLowerCase(), owner.getFirstName(), owner.getLastName(), owner.getUserId());
303 return Either.right(error);
305 if (oldState.equals(LifecycleStateEnum.CERTIFICATION_IN_PROGRESS)) {
306 ResponseFormat error = componentUtils.getResponseFormat(ActionStatus.COMPONENT_IN_CERT_IN_PROGRESS_STATE, componentName, componentType.name().toLowerCase(), owner.getFirstName(), owner.getLastName(), owner.getUserId());
307 return Either.right(error);
309 if (oldState.equals(LifecycleStateEnum.READY_FOR_CERTIFICATION)) {
310 ResponseFormat error = componentUtils.getResponseFormat(ActionStatus.COMPONENT_SENT_FOR_CERTIFICATION, componentName, componentType.name().toLowerCase(), owner.getFirstName(), owner.getLastName(), owner.getUserId());
311 return Either.right(error);
314 return Either.left(true);