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=========================================================
20 package org.openecomp.sdc.be.components.lifecycle;
22 import fj.data.Either;
23 import java.util.HashMap;
24 import java.util.List;
26 import org.openecomp.sdc.be.components.impl.ComponentBusinessLogic;
27 import org.openecomp.sdc.be.components.lifecycle.LifecycleChangeInfoWithAction.LifecycleChanceActionEnum;
28 import org.openecomp.sdc.be.config.ConfigurationManager;
29 import org.openecomp.sdc.be.dao.api.ActionStatus;
30 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphDao;
31 import org.openecomp.sdc.be.datatypes.components.ResourceMetadataDataDefinition;
32 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
33 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
34 import org.openecomp.sdc.be.impl.ComponentsUtils;
35 import org.openecomp.sdc.be.model.Component;
36 import org.openecomp.sdc.be.model.LifeCycleTransitionEnum;
37 import org.openecomp.sdc.be.model.LifecycleStateEnum;
38 import org.openecomp.sdc.be.model.User;
39 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaElementLifecycleOperation;
40 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
41 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
42 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
43 import org.openecomp.sdc.be.user.Role;
44 import org.openecomp.sdc.exception.ResponseFormat;
45 import org.springframework.beans.factory.annotation.Autowired;
47 public abstract class LifeCycleTransition {
49 protected ConfigurationManager configurationManager;
51 protected ToscaElementLifecycleOperation lifeCycleOperation;
53 protected JanusGraphDao janusGraphDao;
54 protected ComponentsUtils componentUtils;
55 protected Map<ComponentTypeEnum, List<Role>> authorizedRoles;
56 protected Map<ResourceTypeEnum, List<Role>> resourceAuthorizedRoles;
57 ToscaOperationFacade toscaOperationFacade;
59 protected LifeCycleTransition(ComponentsUtils componentUtils, ToscaElementLifecycleOperation lifecycleOperation2,
60 ToscaOperationFacade toscaOperationFacade, JanusGraphDao janusGraphDao) {
61 this.configurationManager = ConfigurationManager.getConfigurationManager();
62 this.lifeCycleOperation = lifecycleOperation2;
63 this.componentUtils = componentUtils;
64 this.authorizedRoles = new HashMap<>();
65 this.resourceAuthorizedRoles = new HashMap<>();
66 this.toscaOperationFacade = toscaOperationFacade;
67 this.janusGraphDao = janusGraphDao;
70 public abstract LifeCycleTransitionEnum getName();
72 public abstract AuditingActionEnum getAuditingAction();
74 public ConfigurationManager getConfigurationManager() {
75 return configurationManager;
78 public void setConfigurationManager(ConfigurationManager configurationManager) {
79 this.configurationManager = configurationManager;
82 public ToscaElementLifecycleOperation getLifeCycleOperation() {
83 return lifeCycleOperation;
86 public void setLifeCycleOperation(ToscaElementLifecycleOperation lifeCycleOperation) {
87 this.lifeCycleOperation = lifeCycleOperation;
90 public List<Role> getAuthorizedRoles(ComponentTypeEnum componentType) {
91 return authorizedRoles.get(componentType);
94 public void addAuthorizedRoles(ComponentTypeEnum componentType, List<Role> authorizedRoles) {
95 this.authorizedRoles.put(componentType, authorizedRoles);
98 public List<Role> getResourceAuthorizedRoles(ResourceTypeEnum resourceType) {
99 return resourceAuthorizedRoles.get(resourceType);
102 public void addResouceAuthorizedRoles(ResourceTypeEnum resourceType, List<Role> authorizedRoles) {
103 this.resourceAuthorizedRoles.put(resourceType, authorizedRoles);
106 public abstract <T extends Component> Either<T, ResponseFormat> changeState(ComponentTypeEnum componentType, Component component,
107 ComponentBusinessLogic componentBl, User modifier, User owner,
108 boolean needLock, boolean inTransaction);
110 public abstract Either<Boolean, ResponseFormat> validateBeforeTransition(Component component, ComponentTypeEnum componentType, User modifier,
111 User owner, LifecycleStateEnum oldState,
112 LifecycleChangeInfoWithAction lifecycleChangeInfo);
114 public Either<Boolean, ResponseFormat> validateBeforeTransition(Component component, ComponentTypeEnum componentType, User modifier, User owner,
115 LifecycleStateEnum oldState) {
116 return this.validateBeforeTransition(component, componentType, modifier, owner, oldState, null);
119 protected Either<User, ResponseFormat> getComponentOwner(Component component, ComponentTypeEnum componentType) {
120 Either<User, StorageOperationStatus> resourceOwnerResult = getLifeCycleOperation().getToscaElementOwner(component.getUniqueId());
121 if (resourceOwnerResult.isRight()) {
122 ResponseFormat responseFormat = componentUtils
123 .getResponseFormatByComponent(componentUtils.convertFromStorageResponse(resourceOwnerResult.right().value()), component,
125 return Either.right(responseFormat);
127 return Either.left(resourceOwnerResult.left().value());
130 protected Either<Boolean, ResponseFormat> userRoleValidation(User modifier, Component component, ComponentTypeEnum componentType,
131 LifecycleChangeInfoWithAction lifecycleChangeInfo) {
134 //first check the user for the component and then for the resource
135 if (getAuthorizedRoles(componentType).contains(Role.valueOf(modifier.getRole())) || userResourceRoleValidation(component, componentType,
137 return Either.left(true);
139 // this is only used in 2 cases
141 //1. when creating vfc/cp when import vf from CSAR - when we
142 // create resources from node type, we create need to change the state to certified
144 //2. certification flow upno upgrade migration
145 if (lifecycleChangeInfo != null && lifecycleChangeInfo.getAction() != null && (
146 lifecycleChangeInfo.getAction() == LifecycleChanceActionEnum.CREATE_FROM_CSAR
147 || lifecycleChangeInfo.getAction() == LifecycleChanceActionEnum.UPGRADE_MIGRATION)) {
148 return Either.left(true);
150 return Either.right(componentUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION));
153 protected boolean userResourceRoleValidation(Component component, ComponentTypeEnum componentType, User modifier) {
154 if (componentType.equals(ComponentTypeEnum.RESOURCE)) {
155 ResourceTypeEnum resourceType = ((ResourceMetadataDataDefinition) component.getComponentMetadataDefinition().getMetadataDataDefinition())
157 if (getResourceAuthorizedRoles(resourceType) != null && getResourceAuthorizedRoles(resourceType)
158 .contains(Role.valueOf(modifier.getRole()))) {