Added oparent to sdc main
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / impl / ComponentLocker.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 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.impl;
22
23 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
24 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
25 import org.openecomp.sdc.be.model.Component;
26 import org.openecomp.sdc.be.model.operations.StorageException;
27 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
28 import org.openecomp.sdc.be.model.operations.impl.GraphLockOperation;
29 import org.openecomp.sdc.common.log.wrappers.Logger;
30
31
32 @org.springframework.stereotype.Component
33 public class ComponentLocker  {
34
35     private static final Logger log = Logger.getLogger(ComponentLocker.class.getName());
36     private final GraphLockOperation graphLockOperation;
37
38     public ComponentLocker(GraphLockOperation graphLockOperation) {
39         this.graphLockOperation = graphLockOperation;
40     }
41
42     public void lock(String id, ComponentTypeEnum componentType) {
43         lock(id, componentType.getNodeType());
44     }
45
46     public void lock(Component component) {
47         doLockComponent(component.getUniqueId(), component.getComponentType().getNodeType());
48     }
49
50     public void lock(String id, NodeTypeEnum nodeTypeEnum) {
51         doLockComponent(id, nodeTypeEnum);
52     }
53
54     private void doLockComponent(String id, NodeTypeEnum nodeTypeEnum) {
55         log.debug("#doLockComponent - locking component {} of type {}", id, nodeTypeEnum);
56         StorageOperationStatus storageOperationStatus = graphLockOperation.lockComponent(id, nodeTypeEnum);
57         if (storageOperationStatus != StorageOperationStatus.OK) {
58                 log.debug("#doLockComponent - failed to lock component {} with status {}", id, storageOperationStatus);
59             throw new StorageException(storageOperationStatus);
60         }
61     }
62
63     public void unlock(String id, ComponentTypeEnum componentType) {
64         unlock(id, componentType.getNodeType());
65     }
66
67     public void unlock(String id, NodeTypeEnum nodeTypeEnum) {
68         log.debug("#unlock - unlocking component {} of type {}", id, nodeTypeEnum);
69         StorageOperationStatus storageOperationStatus = graphLockOperation.unlockComponent(id, nodeTypeEnum);
70         if (storageOperationStatus != StorageOperationStatus.OK) {
71             log.debug("#unlock - failed to unlock component {} with status {}", id, storageOperationStatus);
72             throw new StorageException(storageOperationStatus, id);
73         }
74     }
75 }