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