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 fj.data.Either;
24 import org.openecomp.sdc.be.components.impl.ComponentBusinessLogic;
25 import org.openecomp.sdc.be.components.lifecycle.LifecycleChangeInfoWithAction.LifecycleChanceActionEnum;
26 import org.openecomp.sdc.be.config.ConfigurationManager;
27 import org.openecomp.sdc.be.dao.api.ActionStatus;
28 import org.openecomp.sdc.be.dao.jsongraph.TitanDao;
29 import org.openecomp.sdc.be.datatypes.components.ResourceMetadataDataDefinition;
30 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
31 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
32 import org.openecomp.sdc.be.impl.ComponentsUtils;
33 import org.openecomp.sdc.be.model.Component;
34 import org.openecomp.sdc.be.model.LifeCycleTransitionEnum;
35 import org.openecomp.sdc.be.model.LifecycleStateEnum;
36 import org.openecomp.sdc.be.model.User;
37 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaElementLifecycleOperation;
38 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
39 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
40 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
41 import org.openecomp.sdc.be.user.Role;
42 import org.openecomp.sdc.exception.ResponseFormat;
43 import org.springframework.beans.factory.annotation.Autowired;
45 import java.util.HashMap;
46 import java.util.List;
49 public abstract class LifeCycleTransition {
51 protected ConfigurationManager configurationManager;
53 protected ToscaElementLifecycleOperation lifeCycleOperation;
55 protected TitanDao titanDao;
56 protected ComponentsUtils componentUtils;
58 protected Map<ComponentTypeEnum, List<Role>> authorizedRoles;
59 protected Map<ResourceTypeEnum, List<Role>> resourceAuthorizedRoles;
61 ToscaOperationFacade toscaOperationFacade;
63 protected LifeCycleTransition(ComponentsUtils componentUtils, ToscaElementLifecycleOperation lifecycleOperation2, ToscaOperationFacade toscaOperationFacade, TitanDao titanDao) {
65 this.configurationManager = ConfigurationManager.getConfigurationManager();
66 this.lifeCycleOperation = lifecycleOperation2;
67 this.componentUtils = componentUtils;
68 this.authorizedRoles = new HashMap<>();
69 this.resourceAuthorizedRoles = new HashMap<>();
70 this.toscaOperationFacade = toscaOperationFacade;
71 this.titanDao = titanDao;
74 public abstract LifeCycleTransitionEnum getName();
76 public abstract AuditingActionEnum getAuditingAction();
78 public ConfigurationManager getConfigurationManager() {
79 return configurationManager;
82 public void setConfigurationManager(ConfigurationManager configurationManager) {
83 this.configurationManager = configurationManager;
86 public ToscaElementLifecycleOperation getLifeCycleOperation() {
87 return lifeCycleOperation;
90 public void setLifeCycleOperation(ToscaElementLifecycleOperation lifeCycleOperation) {
91 this.lifeCycleOperation = lifeCycleOperation;
94 public List<Role> getAuthorizedRoles(ComponentTypeEnum componentType) {
95 return authorizedRoles.get(componentType);
98 public void addAuthorizedRoles(ComponentTypeEnum componentType, List<Role> authorizedRoles) {
99 this.authorizedRoles.put(componentType, authorizedRoles);
102 public List<Role> getResourceAuthorizedRoles(ResourceTypeEnum resourceType) {
103 return resourceAuthorizedRoles.get(resourceType);
106 public void addResouceAuthorizedRoles(ResourceTypeEnum resourceType, List<Role> authorizedRoles) {
107 this.resourceAuthorizedRoles.put(resourceType, authorizedRoles);
110 public abstract Either<? extends Component, ResponseFormat> changeState(ComponentTypeEnum componentType, Component component, ComponentBusinessLogic componentBl, User modifier, User owner, boolean needLock, boolean inTransaction);
112 public abstract Either<Boolean, ResponseFormat> validateBeforeTransition(Component component, ComponentTypeEnum componentType, User modifier, User owner, LifecycleStateEnum oldState, LifecycleChangeInfoWithAction lifecycleChangeInfo);
114 public Either<Boolean, ResponseFormat> validateBeforeTransition(Component component, ComponentTypeEnum componentType, User modifier, User owner, LifecycleStateEnum oldState) {
116 return this.validateBeforeTransition(component, componentType, modifier, owner, oldState, null);
125 protected Either<User, ResponseFormat> getComponentOwner(Component component, ComponentTypeEnum componentType) {
127 return getComponentOwner(component, componentType, false);
130 protected Either<User, ResponseFormat> getComponentOwner(Component component, ComponentTypeEnum componentType, boolean inTransaction) {
132 Either<User, StorageOperationStatus> resourceOwnerResult = getLifeCycleOperation().getToscaElementOwner(component.getUniqueId());
133 if (resourceOwnerResult.isRight()) {
134 ResponseFormat responseFormat = componentUtils.getResponseFormatByComponent(componentUtils.convertFromStorageResponse(resourceOwnerResult.right().value()), component, componentType);
135 return Either.right(responseFormat);
137 return Either.left(resourceOwnerResult.left().value());
140 protected Either<Boolean, ResponseFormat> userRoleValidation(User modifier,Component component, ComponentTypeEnum componentType, LifecycleChangeInfoWithAction lifecycleChangeInfo) {
143 //first check the user for the component and then for the resource
144 if (getAuthorizedRoles(componentType).contains(Role.valueOf(modifier.getRole())) || userResourceRoleValidation(component,componentType,modifier)) {
145 return Either.left(true);
147 // this is only used in 2 cases
148 //1. when creating vfc/cp when import vf from csar - when we
149 // create resources from node type, we create need to change the state
151 //2. certification flow upno upgrade migration
152 if (lifecycleChangeInfo != null && lifecycleChangeInfo.getAction() != null && (lifecycleChangeInfo.getAction() == LifecycleChanceActionEnum.CREATE_FROM_CSAR|| lifecycleChangeInfo.getAction() == LifecycleChanceActionEnum.UPGRADE_MIGRATION)) {
153 return Either.left(true);
156 ResponseFormat responseFormat = componentUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION);
157 return Either.right(responseFormat);
160 protected boolean userResourceRoleValidation(Component component, ComponentTypeEnum componentType, User modifier) {
161 if (componentType.equals(ComponentTypeEnum.RESOURCE)){
162 ResourceTypeEnum resourceType = ((ResourceMetadataDataDefinition)component.getComponentMetadataDefinition().getMetadataDataDefinition()).getResourceType();
163 if (getResourceAuthorizedRoles(resourceType)!=null && getResourceAuthorizedRoles(resourceType).contains(Role.valueOf(modifier.getRole()))) {