fd6563ac4f75b1940adf283cb8656b0f4e48bddc
[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.model.Component;
24 import org.openecomp.sdc.be.model.LifecycleStateEnum;
25 import org.openecomp.sdc.be.model.Resource;
26 import org.openecomp.sdc.be.model.Service;
27 import org.openecomp.sdc.be.model.operations.api.IComponentOperation;
28 import org.openecomp.sdc.be.model.operations.api.IResourceOperation;
29 import org.openecomp.sdc.be.model.operations.api.IServiceOperation;
30 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 import fj.data.Either;
35
36 public class ComponentValidationUtils {
37
38         private static Logger log = LoggerFactory.getLogger(ComponentValidationUtils.class.getName());
39
40         public static boolean canWorkOnResource(Resource resource, String userId) {
41                 // verify resource is checked-out
42                 if (resource.getLifecycleState() != LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT) {
43                         log.debug("resource is not checked-out");
44                         return false;
45                 }
46                 // verify resource is not deleted
47                 if ((resource.getIsDeleted() != null) && (resource.getIsDeleted() == true)) {
48                         log.debug("resource is marked as delete");
49                         return false;
50                 }
51                 // verify resource last update user is the current user
52                 if (!userId.equals(resource.getLastUpdaterUserId())) {
53                         log.debug("resource last update is not {}", userId);
54                         return false;
55                 }
56                 return true;
57         }
58
59         public static boolean canWorkOnResource(String resourceId, IResourceOperation resourceOperation,
60                         String userId) {
61                 Either<Resource, StorageOperationStatus> getResourceResult = resourceOperation.getLightComponent(resourceId,
62                                 false);
63
64                 if (getResourceResult.isRight()) {
65                         log.debug("Failed to retrive resource, resource id {}", resourceId);
66                         return false;
67                 }
68                 Resource resource = getResourceResult.left().value();
69
70                 return canWorkOnResource(resource, userId);
71
72         }
73
74         public static boolean canWorkOnService(String serviceId, IServiceOperation serviceOperation, String userId) {
75                 Either<Service, StorageOperationStatus> getResourceResult = serviceOperation.getLightComponent(serviceId,
76                                 false);
77
78                 if (getResourceResult.isRight()) {
79                         log.debug("Failed to retrieve service, service id {}", serviceId);
80                         return false;
81                 }
82                 Service service = getResourceResult.left().value();
83
84                 return canWorkOnComponent(service, userId);
85
86         }
87
88         public static boolean canWorkOnComponent(String componentId, IComponentOperation componentOperation,
89                         String userId) {
90                 Either<Component, StorageOperationStatus> getResourceResult = componentOperation.getLightComponent(componentId,
91                                 false);
92
93                 if (getResourceResult.isRight()) {
94                         log.debug("Failed to retrieve component, component id {}", componentId);
95                         return false;
96                 }
97                 Component service = getResourceResult.left().value();
98
99                 return canWorkOnComponent(service, userId);
100         }
101
102         public static boolean canWorkOnComponent(Component component, String userId) {
103                 // verify resource is checked-out
104                 if (component.getLifecycleState() != LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT) {
105                         log.debug("resource is not checked-out");
106                         return false;
107                 }
108
109                 // verify userId is not null
110                 if (userId == null) {
111                         log.debug("current user userId is null");
112                         return false;
113                 }
114
115                 // verify resource last update user is the current user
116                 if (!userId.equals(component.getLastUpdaterUserId())) {
117                         log.debug("resource last updater userId is not {}", userId);
118                         return false;
119                 }
120                 return true;
121         }
122
123 }