Moving all files to root directory
[appc.git] / appc-dispatcher / appc-dispatcher-common / lock-manager-lib / lock-manager-impl / src / main / java / org / openecomp / appc / lockmanager / impl / inmemory / LockManagerInMemoryImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : APP-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
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.appc.lockmanager.impl.inmemory;
23
24 import java.util.Map;
25 import java.util.concurrent.ConcurrentHashMap;
26
27 import org.openecomp.appc.lockmanager.api.LockException;
28 import org.openecomp.appc.lockmanager.api.LockManager;
29 import com.att.eelf.configuration.EELFLogger;
30 import com.att.eelf.configuration.EELFManager;
31
32
33 public class LockManagerInMemoryImpl implements LockManager {
34
35     private static LockManagerInMemoryImpl instance = null;
36     private Map<String, LockValue> lockedVNFs;
37
38     private static final EELFLogger debugLogger = EELFManager.getInstance().getDebugLogger();
39
40     private LockManagerInMemoryImpl() {
41         lockedVNFs = new ConcurrentHashMap<>();
42     }
43
44     public static LockManager getLockManager() {
45         if(instance == null) {
46             instance = new LockManagerInMemoryImpl();
47         }
48         return instance;
49     }
50
51     @Override
52     public boolean acquireLock(String resource, String owner) throws LockException {
53         return acquireLock(resource, owner, 0);
54     }
55
56     @Override
57     public boolean acquireLock(String resource, String owner, long timeout) throws LockException {
58         debugLogger.debug("Try to acquire lock on resource " + resource + " with owner " + owner);
59         long now = System.currentTimeMillis();
60         LockValue lockValue = lockedVNFs.get(resource);
61         if (lockValue != null) {
62             if (lockIsMine(lockValue, owner, now) || hasExpired(lockValue, now)) {
63                 setExpirationTime(resource, owner, timeout, now);
64                 debugLogger.debug("Locked successfully resource " + resource + " with owner " + owner + " for " + timeout + " ms");
65                 return hasExpired(lockValue, now);
66             }
67             else {
68                 debugLogger.debug("Owner " + owner + " tried to lock resource " + resource + " but it is already locked by owner " + lockValue.getOwner());
69                 throw new LockException("Owner " + owner + " tried to lock resource " + resource + " but it is already locked by owner " + lockValue.getOwner());
70             }
71         }
72         else {
73             setExpirationTime(resource, owner, timeout, now);
74             debugLogger.debug("Locked successfully resource " + resource + " with owner " + owner + " for " + timeout + " ms");
75             return true;
76         }
77     }
78
79     @Override
80     public void releaseLock(String resource, String owner) throws LockException {
81         debugLogger.debug("Try to release lock on resource " + resource + " with owner " + owner);
82         long now = System.currentTimeMillis();
83         LockValue lockValue = lockedVNFs.get(resource);
84         if (lockValue != null) {
85             if (!hasExpired(lockValue, now)) {
86                 if (isOwner(lockValue, owner)) {
87                     debugLogger.debug("Unlocked successfully resource " + resource + " with owner " + owner);
88                     lockedVNFs.remove(resource);
89                 }
90                 else {
91                     debugLogger.debug("Unlock failed. Tried to release lock on resource " + resource + " from owner " + owner + " but it is held by a different owner");
92                     throw new LockException("Unlock failed. Tried to release lock on resource " + resource + " from owner " + owner + " but it is held by a different owner");
93                 }
94             }
95             else {
96                 lockedVNFs.remove(resource);
97                 debugLogger.debug("Unlock failed. lock on resource " + resource + " has expired");
98                 throw new LockException("Unlock failed. lock on resource " + resource + " has expired");
99             }
100
101         }
102         else {
103             debugLogger.debug("Tried to release lock on resource " + resource + " from owner " + owner + " but there  is not lock on this resource");
104             throw new LockException("Tried to release lock on resource " + resource + " from owner " + owner + " but there  is not lock on this resource");
105         }
106     }
107
108     @Override
109     public boolean isLocked(String resource) {
110         return lockedVNFs.get(resource)!=null?true:false;
111     }
112
113     private boolean lockIsMine(LockValue lockValue, String owner, long now) {
114         return isOwner(lockValue, owner) && !hasExpired(lockValue, now);
115     }
116
117     private boolean isOwner(LockValue lockValue, String owner) {
118         return lockValue.getOwner() != null && lockValue.getOwner().equals(owner);
119     }
120
121     private boolean hasExpired(LockValue lockValue, long now) {
122         return (lockValue.getExpirationTime() != 0 && now > lockValue.getExpirationTime());
123     }
124
125     private void setExpirationTime(String resource, String owner, long timeout, long now) {
126         long expirationTime = timeout == 0 ? 0 : now + timeout;
127         LockValue lockValue = new LockValue(owner, expirationTime);
128         lockedVNFs.put(resource, lockValue);
129     }
130 }