Implement 'Update Service by importing Tosca Model'-story
[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 package org.openecomp.sdc.be.components.lifecycle;
21
22 import fj.data.Either;
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Map;
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;
46
47 public abstract class LifeCycleTransition {
48
49     protected ConfigurationManager configurationManager;
50     @Autowired
51     protected ToscaElementLifecycleOperation lifeCycleOperation;
52     @Autowired
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;
58
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;
68     }
69
70     public abstract LifeCycleTransitionEnum getName();
71
72     public abstract AuditingActionEnum getAuditingAction();
73
74     public ConfigurationManager getConfigurationManager() {
75         return configurationManager;
76     }
77
78     public void setConfigurationManager(ConfigurationManager configurationManager) {
79         this.configurationManager = configurationManager;
80     }
81
82     public ToscaElementLifecycleOperation getLifeCycleOperation() {
83         return lifeCycleOperation;
84     }
85
86     public void setLifeCycleOperation(ToscaElementLifecycleOperation lifeCycleOperation) {
87         this.lifeCycleOperation = lifeCycleOperation;
88     }
89
90     public List<Role> getAuthorizedRoles(ComponentTypeEnum componentType) {
91         return authorizedRoles.get(componentType);
92     }
93
94     public void addAuthorizedRoles(ComponentTypeEnum componentType, List<Role> authorizedRoles) {
95         this.authorizedRoles.put(componentType, authorizedRoles);
96     }
97
98     public List<Role> getResourceAuthorizedRoles(ResourceTypeEnum resourceType) {
99         return resourceAuthorizedRoles.get(resourceType);
100     }
101
102     public void addResouceAuthorizedRoles(ResourceTypeEnum resourceType, List<Role> authorizedRoles) {
103         this.resourceAuthorizedRoles.put(resourceType, authorizedRoles);
104     }
105
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);
109
110     public abstract Either<Boolean, ResponseFormat> validateBeforeTransition(Component component, ComponentTypeEnum componentType, User modifier,
111                                                                              User owner, LifecycleStateEnum oldState,
112                                                                              LifecycleChangeInfoWithAction lifecycleChangeInfo);
113
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);
117     }
118
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,
124                     componentType);
125             return Either.right(responseFormat);
126         }
127         return Either.left(resourceOwnerResult.left().value());
128     }
129
130     protected Either<Boolean, ResponseFormat> userRoleValidation(User modifier, Component component, ComponentTypeEnum componentType,
131                                                                  LifecycleChangeInfoWithAction lifecycleChangeInfo) {
132         // validate user
133
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,
136             modifier)) {
137             return Either.left(true);
138         }
139         // this is only used in 2 cases
140
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
143
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);
149         }
150         return Either.right(componentUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION));
151     }
152
153     protected boolean userResourceRoleValidation(Component component, ComponentTypeEnum componentType, User modifier) {
154         if (componentType.equals(ComponentTypeEnum.RESOURCE)) {
155             ResourceTypeEnum resourceType = ((ResourceMetadataDataDefinition) component.getComponentMetadataDefinition().getMetadataDataDefinition())
156                 .getResourceType();
157             if (getResourceAuthorizedRoles(resourceType) != null && getResourceAuthorizedRoles(resourceType)
158                 .contains(Role.valueOf(modifier.getRole()))) {
159                 return true;
160             }
161         }
162         return false;
163     }
164 }