918140b0af5315e43587208b055b1223c9ee8ece
[sdc.git] /
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 java.util.Arrays;
24
25 import org.openecomp.sdc.be.components.impl.ComponentBusinessLogic;
26 import org.openecomp.sdc.be.dao.api.ActionStatus;
27 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
28 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
29 import org.openecomp.sdc.be.impl.ComponentsUtils;
30 import org.openecomp.sdc.be.model.Component;
31 import org.openecomp.sdc.be.model.LifeCycleTransitionEnum;
32 import org.openecomp.sdc.be.model.LifecycleStateEnum;
33 import org.openecomp.sdc.be.model.User;
34 import org.openecomp.sdc.be.model.operations.api.ILifecycleOperation;
35 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
36 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
37 import org.openecomp.sdc.be.user.Role;
38 import org.openecomp.sdc.exception.ResponseFormat;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 import fj.data.Either;
43
44 public class StartCertificationTransition extends LifeCycleTransition {
45
46         private static Logger log = LoggerFactory.getLogger(StartCertificationTransition.class.getName());
47
48         public StartCertificationTransition(ComponentsUtils componentUtils, ILifecycleOperation lifecycleOperation) {
49                 super(componentUtils, lifecycleOperation);
50
51                 // authorized roles
52                 Role[] rsrcServiceStartCertificationRoles = { Role.ADMIN, Role.TESTER };
53                 addAuthorizedRoles(ComponentTypeEnum.RESOURCE, Arrays.asList(rsrcServiceStartCertificationRoles));
54                 addAuthorizedRoles(ComponentTypeEnum.SERVICE, Arrays.asList(rsrcServiceStartCertificationRoles));
55                 // TODO to be later defined for product
56         }
57
58         @Override
59         public LifeCycleTransitionEnum getName() {
60                 return LifeCycleTransitionEnum.START_CERTIFICATION;
61         }
62
63         @Override
64         public AuditingActionEnum getAuditingAction() {
65                 return AuditingActionEnum.START_CERTIFICATION_RESOURCE;
66         }
67
68         @Override
69         public Either<? extends Component, ResponseFormat> changeState(ComponentTypeEnum componentType, Component component, ComponentBusinessLogic componentBl, User modifier, User owner, boolean shouldLock, boolean inTransaction) {
70
71                 log.debug("start performing certification test for resource {}", component.getUniqueId());
72
73                 NodeTypeEnum nodeType = (componentType.equals(ComponentTypeEnum.SERVICE)) ? NodeTypeEnum.Service : NodeTypeEnum.Resource;
74                 Either<? extends Component, StorageOperationStatus> stateChangeResult = lifeCycleOperation.startComponentCertification(nodeType, component, modifier, owner, inTransaction);
75                 if (stateChangeResult.isRight()) {
76                         log.debug("start certification failed on graph");
77                         StorageOperationStatus response = stateChangeResult.right().value();
78                         ActionStatus actionStatus = componentUtils.convertFromStorageResponse(response);
79
80                         if (response.equals(StorageOperationStatus.ENTITY_ALREADY_EXISTS)) {
81                                 actionStatus = ActionStatus.COMPONENT_VERSION_ALREADY_EXIST;
82                         }
83                         ResponseFormat responseFormat = componentUtils.getResponseFormatByComponent(actionStatus, component, componentType);
84                         return Either.right(responseFormat);
85                 }
86
87                 return Either.left(stateChangeResult.left().value());
88         }
89
90         @Override
91         public Either<Boolean, ResponseFormat> validateBeforeTransition(Component component, ComponentTypeEnum componentType, User modifier, User owner, LifecycleStateEnum oldState, LifecycleChangeInfoWithAction lifecycleChangeInfo) {
92                 String componentName = component.getComponentMetadataDefinition().getMetadataDataDefinition().getName();
93                 log.debug("validate before start certification test. resource name={}, oldState={}, owner userId={}", componentName, oldState, owner.getUserId());
94
95                 // validate user
96                 Either<Boolean, ResponseFormat> userValidationResponse = userRoleValidation(modifier, componentType, lifecycleChangeInfo);
97                 if (userValidationResponse.isRight()) {
98                         return userValidationResponse;
99                 }
100
101                 if (oldState.equals(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN) || oldState.equals(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT)) {
102                         ResponseFormat error = componentUtils.getResponseFormat(ActionStatus.COMPONENT_NOT_READY_FOR_CERTIFICATION, componentName, componentType.name().toLowerCase());
103                         return Either.right(error);
104                 }
105
106                 if (oldState.equals(LifecycleStateEnum.CERTIFIED)) {
107                         ResponseFormat error = componentUtils.getResponseFormat(ActionStatus.COMPONENT_ALREADY_CERTIFIED, componentName, componentType.name().toLowerCase(), owner.getFirstName(), owner.getLastName(), owner.getUserId());
108                         return Either.right(error);
109                 }
110
111                 if (oldState.equals(LifecycleStateEnum.CERTIFICATION_IN_PROGRESS)) {
112                         ResponseFormat error = componentUtils.getResponseFormat(ActionStatus.COMPONENT_IN_CERT_IN_PROGRESS_STATE, componentName, componentType.name().toLowerCase(), owner.getFirstName(), owner.getLastName(), owner.getUserId());
113                         return Either.right(error);
114                 }
115
116                 return Either.left(true);
117         }
118
119 }