fe5d79a2663437cdba18f442b4e7f8fc73bdc913
[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.model.operations.utils;
22
23 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
24 import org.openecomp.sdc.be.model.Component;
25 import org.openecomp.sdc.be.model.LifecycleStateEnum;
26 import org.openecomp.sdc.be.model.Resource;
27 import org.openecomp.sdc.be.model.Service;
28 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
29 import org.openecomp.sdc.be.model.operations.api.IResourceOperation;
30 import org.openecomp.sdc.be.model.operations.api.IServiceOperation;
31 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 import fj.data.Either;
36
37 public class ComponentValidationUtils {
38
39         private static Logger log = LoggerFactory.getLogger(ComponentValidationUtils.class.getName());
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() == true)) {
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, ToscaOperationFacade toscaOperationFacade, String userId) {
61                 
62                 Either<Component, StorageOperationStatus> getResourceResult = toscaOperationFacade.getToscaElement(componentId, JsonParseFlagEnum.ParseMetadata);
63
64                 if (getResourceResult.isRight()) {
65                         log.debug("Failed to retrieve component, component id {}", componentId);
66                         return false;
67                 }
68                 Component component = getResourceResult.left().value();
69
70                 return canWorkOnComponent(component, userId);
71         }
72         
73         public static boolean canWorkOnComponent(Component component, String userId) {
74                 return canWorkOnComponent(component.getLifecycleState(), component.getLastUpdaterUserId(), userId);
75         }
76         
77         private static boolean canWorkOnComponent(LifecycleStateEnum lifecycleState, String lastUpdaterUserId, String userId) {
78                 // verify resource is checked-out
79                 if (lifecycleState != LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT) {
80                         log.debug("resource is not checked-out");
81                         return false;
82                 }
83
84                 // verify userId is not null
85                 if (userId == null) {
86                         log.debug("current user userId is null");
87                         return false;
88                 }
89
90                 // verify resource last update user is the current user
91                 if (!userId.equals(lastUpdaterUserId)) {
92                         log.debug("resource last updater userId is not {}", userId);
93                         return false;
94                 }
95                 return true;
96         }
97
98 }