Sync Integ to Master
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / lifecycle / LifeCycleTransition.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
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.lifecycle;
22
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;
44
45 import java.util.HashMap;
46 import java.util.List;
47 import java.util.Map;
48
49 public abstract class LifeCycleTransition {
50
51     protected ConfigurationManager configurationManager;
52     @Autowired
53     protected ToscaElementLifecycleOperation lifeCycleOperation;
54     @Autowired
55     protected TitanDao titanDao;
56     protected ComponentsUtils componentUtils;
57
58     protected Map<ComponentTypeEnum, List<Role>> authorizedRoles;
59     protected Map<ResourceTypeEnum, List<Role>> resourceAuthorizedRoles;
60
61     ToscaOperationFacade toscaOperationFacade;
62
63     protected LifeCycleTransition(ComponentsUtils componentUtils, ToscaElementLifecycleOperation lifecycleOperation2, ToscaOperationFacade toscaOperationFacade, TitanDao titanDao) {
64
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;
72     }
73
74     public abstract LifeCycleTransitionEnum getName();
75
76     public abstract AuditingActionEnum getAuditingAction();
77
78     public ConfigurationManager getConfigurationManager() {
79         return configurationManager;
80     }
81
82     public void setConfigurationManager(ConfigurationManager configurationManager) {
83         this.configurationManager = configurationManager;
84     }
85
86     public ToscaElementLifecycleOperation getLifeCycleOperation() {
87         return lifeCycleOperation;
88     }
89
90     public void setLifeCycleOperation(ToscaElementLifecycleOperation lifeCycleOperation) {
91         this.lifeCycleOperation = lifeCycleOperation;
92     }
93
94     public List<Role> getAuthorizedRoles(ComponentTypeEnum componentType) {
95         return authorizedRoles.get(componentType);
96     }
97
98     public void addAuthorizedRoles(ComponentTypeEnum componentType, List<Role> authorizedRoles) {
99         this.authorizedRoles.put(componentType, authorizedRoles);
100     }
101
102     public List<Role> getResourceAuthorizedRoles(ResourceTypeEnum resourceType) {
103         return resourceAuthorizedRoles.get(resourceType);
104     }
105
106     public void addResouceAuthorizedRoles(ResourceTypeEnum resourceType, List<Role> authorizedRoles) {
107         this.resourceAuthorizedRoles.put(resourceType, authorizedRoles);
108     }
109
110     public abstract Either<? extends Component, ResponseFormat> changeState(ComponentTypeEnum componentType, Component component, ComponentBusinessLogic componentBl, User modifier, User owner, boolean needLock, boolean inTransaction);
111
112     public abstract Either<Boolean, ResponseFormat> validateBeforeTransition(Component component, ComponentTypeEnum componentType, User modifier, User owner, LifecycleStateEnum oldState, LifecycleChangeInfoWithAction lifecycleChangeInfo);
113
114     public Either<Boolean, ResponseFormat> validateBeforeTransition(Component component, ComponentTypeEnum componentType, User modifier, User owner, LifecycleStateEnum oldState) {
115
116         return this.validateBeforeTransition(component, componentType, modifier, owner, oldState, null);
117     }
118
119     /**
120      * getComponentOwner
121      *
122      * @param resource
123      * @return
124      */
125     protected Either<User, ResponseFormat> getComponentOwner(Component component, ComponentTypeEnum componentType) {
126
127         return getComponentOwner(component, componentType, false);
128     }
129
130     protected Either<User, ResponseFormat> getComponentOwner(Component component, ComponentTypeEnum componentType, boolean inTransaction) {
131
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);
136         }
137         return Either.left(resourceOwnerResult.left().value());
138     }
139
140     protected Either<Boolean, ResponseFormat> userRoleValidation(User modifier,Component component, ComponentTypeEnum componentType, LifecycleChangeInfoWithAction lifecycleChangeInfo) {
141
142         // validate user
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);
146         }
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
150         // to certified
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);
154         }
155
156         ResponseFormat responseFormat = componentUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION);
157         return Either.right(responseFormat);
158     }
159
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()))) {
164                 return true;
165             }
166         }
167         return false;
168     }
169
170 }