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