9621f9e401b82d51057234fed8c9b174cfa5aa09
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (c) 2019 Samsung
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.openecomp.sdc.be.model.operations.utils;
23
24 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
25 import org.openecomp.sdc.be.model.Component;
26 import org.openecomp.sdc.be.model.LifecycleStateEnum;
27 import org.openecomp.sdc.be.model.Resource;
28 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
29 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
30 import org.openecomp.sdc.common.log.wrappers.Logger;
31
32 import fj.data.Either;
33
34 public class ComponentValidationUtils {
35
36     private static final Logger log = Logger.getLogger(ComponentValidationUtils.class.getName());
37
38     private ComponentValidationUtils() {
39     }
40
41     public static boolean canWorkOnResource(Resource resource, String userId) {
42         // verify resource is checked-out
43         if (resource.getLifecycleState() != LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT) {
44             log.debug("resource is not checked-out");
45             return false;
46         }
47         // verify resource is not deleted
48         if ((resource.getIsDeleted() != null) && (resource.getIsDeleted())) {
49             log.debug("resource is marked as delete");
50             return false;
51         }
52         // verify resource last update user is the current user
53         if (!userId.equals(resource.getLastUpdaterUserId())) {
54             log.debug("resource last update is not {}", userId);
55             return false;
56         }
57         return true;
58     }
59
60     public static boolean canWorkOnComponent(String componentId,
61         ToscaOperationFacade toscaOperationFacade, String userId) {
62
63         Either<Component, StorageOperationStatus> getResourceResult =
64             toscaOperationFacade.getToscaElement(componentId, JsonParseFlagEnum.ParseMetadata);
65
66         if (getResourceResult.isRight()) {
67             log.debug("Failed to retrieve component, component id {}", componentId);
68             return false;
69         }
70         Component component = getResourceResult.left().value();
71
72         return canWorkOnComponent(component, userId);
73     }
74
75     public static boolean canWorkOnComponent(Component component, String userId) {
76         return canWorkOnComponent(component.getLifecycleState(), component.getLastUpdaterUserId(), userId);
77     }
78
79     private static boolean canWorkOnComponent(LifecycleStateEnum lifecycleState,
80         String lastUpdaterUserId, String userId) {
81         // verify resource is checked-out
82         if (lifecycleState != LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT) {
83             log.debug("resource is not checked-out");
84             return false;
85         }
86
87         // verify userId is not null
88         if (userId == null) {
89             log.debug("current user userId is null");
90             return false;
91         }
92
93         // verify resource last update user is the current user
94         if (!userId.equals(lastUpdaterUserId)) {
95             log.debug("resource last updater userId is not {}", userId);
96             return false;
97         }
98         return true;
99     }
100
101 }