Merge of new rebased code
[appc.git] / appc-dispatcher / appc-dispatcher-common / lock-manager-lib / lock-manager-impl / src / test / java / org / openecomp / appc / lockmanager / impl / sql / pessimistic / MySqlLockManagerMock.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.sql.pessimistic;
23
24 import java.sql.Connection;
25 import java.sql.SQLException;
26 import java.util.HashMap;
27 import java.util.Map;
28 import java.util.concurrent.TimeUnit;
29 import java.util.concurrent.locks.Lock;
30 import java.util.concurrent.locks.ReentrantLock;
31
32 import org.openecomp.appc.lockmanager.api.LockRuntimeException;
33 import org.openecomp.appc.lockmanager.impl.sql.Synchronizer;
34 import org.openecomp.appc.lockmanager.impl.sql.SynchronizerReceiver;
35 import org.openecomp.appc.lockmanager.impl.sql.pessimistic.LockRecord;
36 import org.openecomp.appc.lockmanager.impl.sql.pessimistic.MySqlLockManager;
37
38 class MySqlLockManagerMock extends MySqlLockManager implements SynchronizerReceiver {
39
40         private final Map<String, LockRecord> locks = new HashMap<>();
41         private final Lock lock = new ReentrantLock();
42         private boolean useReal;
43         private Synchronizer synchronizer;
44
45         MySqlLockManagerMock(boolean useReal) {
46                 this.useReal = useReal;
47         }
48
49         @Override
50         public void setSynchronizer(Synchronizer synchronizer) {
51                 this.synchronizer = synchronizer;
52         }
53
54         @Override
55         protected Connection openDbConnection() {
56                 if(useReal) {
57                         return super.openDbConnection();
58                 }
59                 return null;
60         }
61
62         @Override
63         protected void closeDbConnection(Connection connection) {
64                 if(useReal) {
65                         super.closeDbConnection(connection);
66                 }
67         }
68
69         @Override
70         protected LockRecord loadLockRecord(Connection connection, String resource) throws SQLException {
71                 LockRecord res;
72                 if(useReal) {
73                         res = super.loadLockRecord(connection, resource);
74                 } else {
75                         res = locks.get(resource);
76                 }
77                 if(synchronizer != null) {
78                         synchronizer.postLoadLockRecord(resource, (res == null) ? null : res.getOwner());
79                 }
80                 return res;
81         }
82
83         @Override
84         protected void addLockRecord(Connection connection, String resource, String owner, long timeout) throws SQLException {
85                 if(synchronizer != null) {
86                         synchronizer.preAddLockRecord(resource, owner);
87                 }
88                 try {
89                         if(useReal) {
90                                 super.addLockRecord(connection, resource, owner, timeout);
91                                 return;
92                         }
93                         LockRecord lockRecord = new LockRecord(resource);
94                         lockRecord.setOwner(owner);
95                         lockRecord.setUpdated(System.currentTimeMillis());
96                         lockRecord.setTimeout(timeout);
97                         locks.put(resource, lockRecord);
98                 } finally {
99                         if(synchronizer != null) {
100                                 synchronizer.postAddLockRecord(resource, owner);
101                         }
102                 }
103         }
104
105         @Override
106         protected void updateLockRecord(Connection connection, String resource, String owner, long timeout) throws SQLException {
107                 if(synchronizer != null) {
108                         synchronizer.preUpdateLockRecord(resource, owner);
109                 }
110                 try {
111                         if(useReal) {
112                                 super.updateLockRecord(connection, resource, owner, timeout);
113                                 return;
114                         }
115                         LockRecord lockRecord = loadLockRecord(connection, resource);
116                         lockRecord.setOwner(owner);
117                         lockRecord.setUpdated(System.currentTimeMillis());
118                         lockRecord.setTimeout(timeout);
119                         locks.put(resource, lockRecord);
120                 } finally {
121                         if(synchronizer != null) {
122                                 synchronizer.postUpdateLockRecord(resource, owner);
123                         }
124                 }
125         }
126
127         @Override
128         protected void enterCriticalSection(Connection connection, String resource) {
129                 if(useReal) {
130                         super.enterCriticalSection(connection, resource);
131                         return;
132                 }
133                 try {
134                         if(!lock.tryLock(criticalSectionWaitTimeoutSecs, TimeUnit.SECONDS)) {
135                 throw new LockRuntimeException("Cannot obtain critical section lock for resource [" + resource + "].");
136                         }
137                 } catch(InterruptedException e) {
138                         throw new LockRuntimeException("Cannot obtain critical section lock.", e);
139                 }
140         }
141
142         @Override
143         protected void leaveCriticalSection(Connection connection, String resource) {
144                 if(useReal) {
145                         super.leaveCriticalSection(connection, resource);
146                         return;
147                 }
148                 lock.unlock();
149         }
150 }