Initial OpenECOMP APPC commit
[appc.git] / app-c / appc / appc-dispatcher / appc-dispatcher-common / lock-manager-lib / lock-manager-impl / src / test / java / org / openecomp / appc / lockmanager / impl / sql / optimistic / 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.optimistic;
23
24 import java.sql.Connection;
25 import java.sql.SQLException;
26 import java.util.concurrent.ConcurrentHashMap;
27 import java.util.concurrent.ConcurrentMap;
28
29 import org.openecomp.appc.lockmanager.impl.sql.Synchronizer;
30 import org.openecomp.appc.lockmanager.impl.sql.SynchronizerReceiver;
31 import org.openecomp.appc.lockmanager.impl.sql.optimistic.LockRecord;
32 import org.openecomp.appc.lockmanager.impl.sql.optimistic.MySqlLockManager;
33
34 class MySqlLockManagerMock extends MySqlLockManager implements SynchronizerReceiver {
35
36         private final ConcurrentMap<String, LockRecord> locks = new ConcurrentHashMap<>();
37         private boolean useReal;
38         private Synchronizer synchronizer;
39
40         MySqlLockManagerMock(boolean useReal) {
41                 this.useReal = useReal;
42         }
43
44         @Override
45         public void setSynchronizer(Synchronizer synchronizer) {
46                 this.synchronizer = synchronizer;
47         }
48
49         @Override
50         protected Connection openDbConnection() {
51                 if(useReal) {
52                         return super.openDbConnection();
53                 }
54                 return null;
55         }
56
57         @Override
58         protected void closeDbConnection(Connection connection) {
59                 if(useReal) {
60                         super.closeDbConnection(connection);
61                 }
62         }
63
64         @Override
65         protected LockRecord loadLockRecord(Connection connection, String resource) throws SQLException {
66                 LockRecord res;
67                 if(useReal) {
68                         res = super.loadLockRecord(connection, resource);
69                 } else {
70                         res = locks.get(resource);
71                 }
72                 if(synchronizer != null) {
73                         synchronizer.postLoadLockRecord(resource, (res == null) ? null : res.getOwner());
74                 }
75                 return res;
76         }
77
78         @Override
79         protected void addLockRecord(Connection connection, String resource, String owner, long timeout) throws SQLException {
80                 if(synchronizer != null) {
81                         synchronizer.preAddLockRecord(resource, owner);
82                 }
83                 try {
84                         if(useReal) {
85                                 super.addLockRecord(connection, resource, owner, timeout);
86                                 return;
87                         }
88                         LockRecord lockRecord = new LockRecord(resource);
89                         lockRecord.setOwner(owner);
90                         lockRecord.setUpdated(System.currentTimeMillis());
91                         lockRecord.setTimeout(timeout);
92                         lockRecord.setVer(1);
93                         LockRecord prevLockRecord = locks.putIfAbsent(resource, lockRecord);
94                         if(prevLockRecord != null) {
95                                 // simulate unique constraint violation
96                                 throw new SQLException("Duplicate PK exception", "23000", 1062);
97                         }
98                 } finally {
99                         if(synchronizer != null) {
100                                 synchronizer.postAddLockRecord(resource, owner);
101                         }
102                 }
103         }
104
105         @Override
106         protected boolean updateLockRecord(Connection connection, String resource, String owner, long timeout, long ver) throws SQLException {
107                 if(synchronizer != null) {
108                         synchronizer.preUpdateLockRecord(resource, owner);
109                 }
110                 try {
111                         if(useReal) {
112                                 return super.updateLockRecord(connection, resource, owner, timeout, ver);
113                         }
114                         LockRecord lockRecord = loadLockRecord(connection, resource);
115                         synchronized(lockRecord) {
116                                 // should be atomic operation
117                                 if(ver != lockRecord.getVer()) {
118                                         return false;
119                                 }
120                                 lockRecord.setOwner(owner);
121                                 lockRecord.setUpdated(System.currentTimeMillis());
122                                 lockRecord.setTimeout(timeout);
123                                 lockRecord.setVer(ver + 1);
124                         }
125                         return true;
126                 } finally {
127                         if(synchronizer != null) {
128                                 synchronizer.postUpdateLockRecord(resource, owner);
129                         }
130                 }
131         }
132 }