Third part of onap rename
[appc.git] / appc-dispatcher / appc-dispatcher-common / lock-manager-lib / lock-manager-impl / src / main / java / org / onap / appc / lockmanager / impl / inmemory / LockManagerInMemoryImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.lockmanager.impl.inmemory;
26
27 import java.util.Map;
28 import java.util.concurrent.ConcurrentHashMap;
29
30 import org.onap.appc.lockmanager.api.LockException;
31 import org.onap.appc.lockmanager.api.LockManager;
32 import com.att.eelf.configuration.EELFLogger;
33 import com.att.eelf.configuration.EELFManager;
34
35
36 public class LockManagerInMemoryImpl implements LockManager {
37
38     private static LockManagerInMemoryImpl instance = null;
39     private Map<String, LockValue> lockedVNFs;
40
41     private static final EELFLogger debugLogger = EELFManager.getInstance().getDebugLogger();
42
43     private LockManagerInMemoryImpl() {
44         lockedVNFs = new ConcurrentHashMap<>();
45     }
46
47     public static LockManager getLockManager() {
48         if(instance == null) {
49             instance = new LockManagerInMemoryImpl();
50         }
51         return instance;
52     }
53
54     @Override
55     public boolean acquireLock(String resource, String owner) throws LockException {
56         return acquireLock(resource, owner, 0);
57     }
58
59     @Override
60     public boolean acquireLock(String resource, String owner, long timeout) throws LockException {
61         debugLogger.debug("Try to acquire lock on resource " + resource + " with owner " + owner);
62         long now = System.currentTimeMillis();
63         LockValue lockValue = lockedVNFs.get(resource);
64         if (lockValue != null) {
65             if (lockIsMine(lockValue, owner, now) || hasExpired(lockValue, now)) {
66                 setExpirationTime(resource, owner, timeout, now);
67                 debugLogger.debug("Locked successfully resource " + resource + " with owner " + owner + " for " + timeout + " ms");
68                 return hasExpired(lockValue, now);
69             }
70             else {
71                 debugLogger.debug("Owner " + owner + " tried to lock resource " + resource + " but it is already locked by owner " + lockValue.getOwner());
72                 throw new LockException("Owner " + owner + " tried to lock resource " + resource + " but it is already locked by owner " + lockValue.getOwner());
73             }
74         }
75         else {
76             setExpirationTime(resource, owner, timeout, now);
77             debugLogger.debug("Locked successfully resource " + resource + " with owner " + owner + " for " + timeout + " ms");
78             return true;
79         }
80     }
81
82     @Override
83     public void releaseLock(String resource, String owner) throws LockException {
84         debugLogger.debug("Try to release lock on resource " + resource + " with owner " + owner);
85         long now = System.currentTimeMillis();
86         LockValue lockValue = lockedVNFs.get(resource);
87         if (lockValue != null) {
88             if (!hasExpired(lockValue, now)) {
89                 if (isOwner(lockValue, owner)) {
90                     debugLogger.debug("Unlocked successfully resource " + resource + " with owner " + owner);
91                     lockedVNFs.remove(resource);
92                 }
93                 else {
94                     debugLogger.debug("Unlock failed. Tried to release lock on resource " + resource + " from owner " + owner + " but it is held by a different owner");
95                     throw new LockException("Unlock failed. Tried to release lock on resource " + resource + " from owner " + owner + " but it is held by a different owner");
96                 }
97             }
98             else {
99                 lockedVNFs.remove(resource);
100                 debugLogger.debug("Unlock failed. lock on resource " + resource + " has expired");
101                 throw new LockException("Unlock failed. lock on resource " + resource + " has expired");
102             }
103
104         }
105         else {
106             debugLogger.debug("Tried to release lock on resource " + resource + " from owner " + owner + " but there  is not lock on this resource");
107             throw new LockException("Tried to release lock on resource " + resource + " from owner " + owner + " but there  is not lock on this resource");
108         }
109     }
110
111     @Override
112     public boolean isLocked(String resource) {
113         return lockedVNFs.get(resource)!=null?true:false;
114     }
115
116     private boolean lockIsMine(LockValue lockValue, String owner, long now) {
117         return isOwner(lockValue, owner) && !hasExpired(lockValue, now);
118     }
119
120     private boolean isOwner(LockValue lockValue, String owner) {
121         return lockValue.getOwner() != null && lockValue.getOwner().equals(owner);
122     }
123
124     private boolean hasExpired(LockValue lockValue, long now) {
125         return (lockValue.getExpirationTime() != 0 && now > lockValue.getExpirationTime());
126     }
127
128     private void setExpirationTime(String resource, String owner, long timeout, long now) {
129         long expirationTime = timeout == 0 ? 0 : now + timeout;
130         LockValue lockValue = new LockValue(owner, expirationTime);
131         lockedVNFs.put(resource, lockValue);
132     }
133 }