0efd8977a54bae4dbfe13883b7806911be3cbaf4
[ccsdk/sli/adaptors.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-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.onap.ccsdk.sli.adaptors.lock.comp;
23
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.Collections;
27 import java.util.Date;
28 import java.util.List;
29 import org.onap.ccsdk.sli.adaptors.lock.dao.ResourceLockDao;
30 import org.onap.ccsdk.sli.adaptors.lock.data.ResourceLock;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 public class LockHelperImpl implements LockHelper {
35
36     private static final Logger log = LoggerFactory.getLogger(LockHelperImpl.class);
37
38     private ResourceLockDao resourceLockDao;
39     private int retryCount = 10;
40     private int lockWait = 5; // Seconds
41
42     @Override
43     public void lock(String resourceName, String lockRequester, int lockTimeout /* Seconds */) {
44         lock(resourceName, lockRequester, lockTimeout, lockWait, retryCount);
45     }
46
47     @Override
48     public void lock(String resourceName, String lockRequester, int lockTimeout /* Seconds */, int lockWait /* Seconds */, int retryCount) {
49         lock(Collections.singleton(resourceName), lockRequester, lockTimeout, lockWait, retryCount);
50     }
51
52     @Override
53     public void unlock(String resourceName, boolean force) {
54         unlock(Collections.singleton(resourceName), force);
55     }
56
57     @Override
58     public void lock(Collection<String> resourceNameList, String lockRequester, int lockTimeout /* Seconds */) {
59         lock(resourceNameList, lockRequester, lockTimeout, lockWait, retryCount);
60     }
61
62     @Override
63     public void lock(Collection<String> resourceNameList, String lockRequester, int lockTimeout /* Seconds */, int lockWait /* Seconds */, int retryCount) {
64         for (int i = 0; true; i++) {
65             try {
66                 tryLock(resourceNameList, lockRequester, lockTimeout);
67                 log.info("Resources locked: " + resourceNameList);
68                 return;
69             } catch (ResourceLockedException e) {
70                 if (i >= retryCount) {
71                     throw e;
72                 }
73                 try {
74                     Thread.sleep(lockWait * 1000);
75                 } catch (InterruptedException ex) {
76                     log.error("Interrupted Exception", ex);
77                 }
78             }
79         }
80     }
81
82     @Override
83     public void unlock(Collection<String> lockNames, boolean force) {
84         if (lockNames == null || lockNames.isEmpty()) {
85             return;
86         }
87
88         try {
89             for (String name : lockNames) {
90                 ResourceLock l = resourceLockDao.getByResourceName(name);
91                 if (l != null) {
92                     if (force || l.lockCount == 1) {
93                         resourceLockDao.delete(l.id);
94                     } else {
95                         resourceLockDao.decrementLockCount(l.id);
96                     }
97                 }
98             }
99
100             resourceLockDao.commit();
101
102             log.info("Resources unlocked: " + lockNames);
103         } finally {
104             resourceLockDao.rollback();
105         }
106     }
107
108     public void tryLock(Collection<String> resourceNameList, String lockRequester, int lockTimeout /* Seconds */) {
109         if (resourceNameList == null || resourceNameList.isEmpty()) {
110             return;
111         }
112
113         lockRequester = generateLockRequester(lockRequester, 100);
114
115         // First check if all requested records are available to lock
116
117         Date now = new Date();
118
119         try {
120             List<ResourceLock> dbLockList = new ArrayList<>();
121             List<String> insertLockNameList = new ArrayList<>();
122             for (String name : resourceNameList) {
123                 ResourceLock l = resourceLockDao.getByResourceName(name);
124
125                 boolean canLock = l == null || now.getTime() > l.expirationTime.getTime() ||
126                         lockRequester != null && lockRequester.equals(l.lockHolder) || l.lockCount <= 0;
127                 if (!canLock) {
128                     throw new ResourceLockedException(l.resourceName, l.lockHolder, lockRequester);
129                 }
130
131                 if (l != null) {
132                     dbLockList.add(l);
133                 } else {
134                     insertLockNameList.add(name);
135                 }
136             }
137
138             // Update the lock info in DB
139             for (ResourceLock l : dbLockList) {
140                 resourceLockDao.update(l.id, now, new Date(now.getTime() + lockTimeout * 1000), l.lockCount + 1);
141             }
142
143             // Insert records for those that are not yet there
144             for (String lockName : insertLockNameList) {
145                 ResourceLock l = new ResourceLock();
146                 l.resourceName = lockName;
147                 l.lockHolder = lockRequester;
148                 l.lockTime = now;
149                 l.expirationTime = new Date(now.getTime() + lockTimeout * 1000);
150                 l.lockCount = 1;
151
152                 try {
153                     resourceLockDao.add(l);
154                 } catch (Exception e) {
155                     log.info("Failed to insert lock record: " + lockName);
156                     throw new ResourceLockedException(l.resourceName, "unknown", lockRequester);
157                 }
158             }
159
160             resourceLockDao.commit();
161
162         } finally {
163             resourceLockDao.rollback();
164         }
165     }
166
167     private static String generateLockRequester(String name, int maxLength) {
168         if (name == null) {
169             name = "";
170         }
171         int l1 = name.length();
172         String tname = Thread.currentThread().getName();
173         int l2 = tname.length();
174         if (l1 + l2 + 1 > maxLength) {
175             int maxl1 = maxLength / 2;
176             if (l1 > maxl1) {
177                 name = name.substring(0, maxl1);
178                 l1 = maxl1;
179             }
180             int maxl2 = maxLength - l1 - 1;
181             if (l2 > maxl2) {
182                 tname = tname.substring(0, 6) + "..." + tname.substring(l2 - maxl2 + 9);
183             }
184         }
185         return tname + '-' + name;
186     }
187
188     public void setResourceLockDao(ResourceLockDao resourceLockDao) {
189         this.resourceLockDao = resourceLockDao;
190     }
191
192     public void setRetryCount(int retryCount) {
193         this.retryCount = retryCount;
194     }
195
196     public void setLockWait(int lockWait /* Seconds */) {
197         this.lockWait = lockWait;
198     }
199 }