Upgrade SDC from Titan to Janus Graph
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / model / operations / utils / ComponentValidationUtils.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.model.operations.utils;
22
23 import fj.data.Either;
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 public class ComponentValidationUtils {
33
34     private static final Logger log = Logger.getLogger(ComponentValidationUtils.class.getName());
35
36     public static boolean canWorkOnResource(Resource resource, String userId) {
37         // verify resource is checked-out
38         if (resource.getLifecycleState() != LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT) {
39             log.debug("resource is not checked-out");
40             return false;
41         }
42         // verify resource is not deleted
43         if ((resource.getIsDeleted() != null) && (resource.getIsDeleted())) {
44             log.debug("resource is marked as delete");
45             return false;
46         }
47         // verify resource last update user is the current user
48         if (!userId.equals(resource.getLastUpdaterUserId())) {
49             log.debug("resource last update is not {}", userId);
50             return false;
51         }
52         return true;
53     }
54
55     public static boolean canWorkOnComponent(String componentId, ToscaOperationFacade toscaOperationFacade, String userId) {
56
57         Either<Component, StorageOperationStatus> getResourceResult = toscaOperationFacade.getToscaElement(componentId, JsonParseFlagEnum.ParseMetadata);
58
59         if (getResourceResult.isRight()) {
60             log.debug("Failed to retrieve component, component id {}", componentId);
61             return false;
62         }
63         Component component = getResourceResult.left().value();
64
65         return canWorkOnComponent(component, userId);
66     }
67
68     public static boolean canWorkOnComponent(Component component, String userId) {
69         return canWorkOnComponent(component.getLifecycleState(), component.getLastUpdaterUserId(), userId);
70     }
71
72     private static boolean canWorkOnComponent(LifecycleStateEnum lifecycleState, String lastUpdaterUserId, String userId) {
73         // verify resource is checked-out
74         if (lifecycleState != LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT) {
75             log.debug("resource is not checked-out");
76             return false;
77         }
78
79         // verify userId is not null
80         if (userId == null) {
81             log.debug("current user userId is null");
82             return false;
83         }
84
85         // verify resource last update user is the current user
86         if (!userId.equals(lastUpdaterUserId)) {
87             log.debug("resource last updater userId is not {}", userId);
88             return false;
89         }
90         return true;
91     }
92
93 }