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