Read lock promotion
[music.git] / src / main / java / org / onap / music / main / MusicCore.java
1 /*
2  * ============LICENSE_START==========================================
3  * org.onap.music
4  * ===================================================================
5  *  Copyright (c) 2017 AT&T Intellectual Property
6  * ===================================================================
7  *  Licensed under the Apache License, Version 2.0 (the "License");
8  *  you may not use this file except in compliance with the License.
9  *  You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  *  Unless required by applicable law or agreed to in writing, software
14  *  distributed under the License is distributed on an "AS IS" BASIS,
15  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *  See the License for the specific language governing permissions and
17  *  limitations under the License.
18  *
19  * ============LICENSE_END=============================================
20  * ====================================================================
21  */
22
23 package org.onap.music.main;
24
25 import java.util.List;
26 import java.util.Map;
27
28 import javax.ws.rs.core.MultivaluedMap;
29
30 import org.onap.music.datastore.Condition;
31 import org.onap.music.datastore.PreparedQueryObject;
32 import org.onap.music.datastore.jsonobjects.JsonDelete;
33 import org.onap.music.datastore.jsonobjects.JsonIndex;
34 import org.onap.music.datastore.jsonobjects.JsonInsert;
35 import org.onap.music.datastore.jsonobjects.JsonKeySpace;
36 import org.onap.music.datastore.jsonobjects.JsonSelect;
37 import org.onap.music.datastore.jsonobjects.JsonTable;
38 import org.onap.music.datastore.jsonobjects.JsonUpdate;
39 import org.onap.music.eelf.logging.EELFLoggerDelegate;
40 import org.onap.music.exceptions.MusicLockingException;
41 import org.onap.music.exceptions.MusicQueryException;
42 import org.onap.music.exceptions.MusicServiceException;
43 import org.onap.music.lockingservice.cassandra.CassaLockStore;
44 import org.onap.music.lockingservice.cassandra.LockType;
45 import org.onap.music.lockingservice.cassandra.MusicLockState;
46 import org.onap.music.service.MusicCoreService;
47
48 import com.datastax.driver.core.ResultSet;
49
50 public class MusicCore {
51
52     private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MusicCore.class);
53     private static MusicCoreService musicCore = MusicUtil.getMusicCoreService();
54     private static CassaLockStore mLockHandle;
55     
56     public static CassaLockStore getmLockHandle() {
57         return mLockHandle;
58     }
59
60     public static void setmLockHandle(CassaLockStore mLockHandleIn) {
61         mLockHandle = mLockHandleIn;
62     }
63
64     /**
65      * Acquire lock
66      * 
67      * @param fullyQualifiedKey DO NOT RELY ON THIS KEY WORKING. INCLUDE THE KEY IN THE LOCKID.
68      * @param lockId - the full lock id (key + lockRef)
69      * @return
70      * @throws MusicLockingException
71      * @throws MusicQueryException
72      * @throws MusicServiceException
73      */
74     public static ReturnType acquireLock(String fullyQualifiedKey, String lockId)
75             throws MusicLockingException, MusicQueryException, MusicServiceException {
76         return musicCore.acquireLock(fullyQualifiedKey, lockId);
77     }
78
79     public static ReturnType acquireLockWithLease(String key, String lockId, long leasePeriod)
80             throws MusicLockingException, MusicQueryException, MusicServiceException {
81         return musicCore.acquireLockWithLease(key, lockId, leasePeriod);
82     }
83
84     public static String createLockReference(String fullyQualifiedKey) throws MusicLockingException {
85         return musicCore.createLockReference(fullyQualifiedKey);
86     }
87
88     public static String createLockReference(String fullyQualifiedKey, LockType locktype) throws MusicLockingException {
89         return musicCore.createLockReference(fullyQualifiedKey, locktype);
90     }
91
92     public static String createLockReference(String fullyQualifiedKey, LockType locktype, String owner) throws MusicLockingException {
93         return musicCore.createLockReference(fullyQualifiedKey, locktype, owner);
94     }
95
96     public static ResultType createTable(String keyspace, String table, PreparedQueryObject tableQueryObject,
97             String consistency) throws MusicServiceException {
98         return musicCore.createTable(keyspace, table, tableQueryObject, consistency);
99     }
100
101     public static ResultSet quorumGet(PreparedQueryObject query) {
102         return musicCore.quorumGet(query);
103     }
104
105     /**
106      * Gets the top of queue for fullyQualifiedKey
107      * 
108      * @param fullyQualifiedKey
109      * @return
110      */
111     public static String whoseTurnIsIt(String fullyQualifiedKey) {
112         return musicCore.whoseTurnIsIt(fullyQualifiedKey);
113     }
114
115     /**
116      * Gets the current lockholder(s) for fullyQualifiedKey
117      * 
118      * @param fullyQualifiedKey
119      * @return
120      */
121     public static List<String> getCurrentLockHolders(String fullyQualifiedKey) {
122         return musicCore.getCurrentLockHolders(fullyQualifiedKey);
123     }
124     
125     public static ReturnType promoteLock(String lockIdToPromote) throws MusicLockingException {
126         return musicCore.promoteLock(lockIdToPromote);
127     }
128
129     public static void destroyLockRef(String lockId) throws MusicLockingException {
130         musicCore.destroyLockRef(lockId);
131     }
132
133     public static ReturnType eventualPut(PreparedQueryObject queryObject) {
134         return musicCore.eventualPut(queryObject);
135     }
136
137     public static ReturnType eventualPut_nb(PreparedQueryObject queryObject, String keyspace, String tablename,
138             String primaryKey) {
139         return musicCore.eventualPut_nb(queryObject, keyspace, tablename, primaryKey);
140     }
141
142     public static ReturnType criticalPut(String keyspace, String table, String primaryKeyValue,
143             PreparedQueryObject queryObject, String lockReference, Condition conditionInfo) {
144         return musicCore.criticalPut(keyspace, table, primaryKeyValue, queryObject, lockReference, conditionInfo);
145     }
146
147     public static ResultType nonKeyRelatedPut(PreparedQueryObject queryObject, String consistency)
148             throws MusicServiceException,MusicQueryException {
149         return musicCore.nonKeyRelatedPut(queryObject, consistency);
150     }
151
152     public static ResultSet get(PreparedQueryObject queryObject) throws MusicServiceException {
153         return musicCore.get(queryObject);
154     }
155
156     public static ResultSet criticalGet(String keyspace, String table, String primaryKeyValue,
157             PreparedQueryObject queryObject, String lockReference) throws MusicServiceException {
158         return musicCore.criticalGet(keyspace, table, primaryKeyValue, queryObject, lockReference);
159     }
160
161     public static ReturnType atomicPut(String keyspaceName, String tableName, String primaryKey,
162             PreparedQueryObject queryObject, Condition conditionInfo)
163             throws MusicLockingException, MusicQueryException, MusicServiceException {
164         return musicCore.atomicPut(keyspaceName, tableName, primaryKey, queryObject, conditionInfo);
165     }
166
167     public static ResultSet atomicGet(String keyspaceName, String tableName, String primaryKey,
168             PreparedQueryObject queryObject) throws MusicServiceException, MusicLockingException, MusicQueryException {
169         return musicCore.atomicGet(keyspaceName, tableName, primaryKey, queryObject);
170     }
171
172     public static List<String> getLockQueue(String fullyQualifiedKey)
173             throws MusicServiceException, MusicQueryException, MusicLockingException {
174         return musicCore.getLockQueue(fullyQualifiedKey);
175     }
176
177     public static long getLockQueueSize(String fullyQualifiedKey)
178             throws MusicServiceException, MusicQueryException, MusicLockingException {
179         return musicCore.getLockQueueSize(fullyQualifiedKey);
180     }
181
182     public static void deleteLock(String lockName) throws MusicLockingException {
183         musicCore.deleteLock(lockName);
184     }
185
186     public static ReturnType atomicPutWithDeleteLock(String keyspaceName, String tableName, String primaryKey,
187             PreparedQueryObject queryObject, Condition conditionInfo) throws MusicLockingException {
188         return musicCore.atomicPutWithDeleteLock(keyspaceName, tableName, primaryKey, queryObject, conditionInfo);
189     }
190
191     public static ResultSet atomicGetWithDeleteLock(String keyspaceName, String tableName, String primaryKey,
192             PreparedQueryObject queryObject) throws MusicServiceException, MusicLockingException {
193         return musicCore.atomicGetWithDeleteLock(keyspaceName, tableName, primaryKey, queryObject);
194     }
195
196     public static Map<String, Object> validateLock(String lockName) {
197         return musicCore.validateLock(lockName);
198     }
199
200     public static MusicLockState releaseLock(String lockId, boolean voluntaryRelease) throws MusicLockingException {
201         return musicCore.releaseLock(lockId, voluntaryRelease);
202     }
203     
204     public static List<String> releaseAllLocksForOwner(String ownerId, String keyspace, String table) throws MusicLockingException, MusicServiceException, MusicQueryException {
205         return musicCore.releaseAllLocksForOwner(ownerId, keyspace, table);
206         }
207
208     //Added changes for orm implementation.
209     
210     public static ResultType createKeyspace(JsonKeySpace jsonKeySpaceObject, String consistencyInfo) 
211             throws MusicServiceException, MusicQueryException {
212         return musicCore.createKeyspace(jsonKeySpaceObject,consistencyInfo);
213     }
214     
215     public static ResultType dropKeyspace(JsonKeySpace josnKeyspaceObject, String consistencyInfo)
216             throws MusicServiceException,MusicQueryException {
217         return musicCore.dropKeyspace(josnKeyspaceObject, consistencyInfo);
218     }
219     
220     public static ResultType createTable(JsonTable jsonTableObject,String consistencyInfo) 
221             throws MusicServiceException,MusicQueryException {
222         return musicCore.createTable(jsonTableObject, consistencyInfo);
223     }
224     
225     public static ResultType dropTable(JsonTable jsonTableObject, String consistencyInfo) 
226             throws MusicServiceException, MusicQueryException {
227         return musicCore.dropTable(jsonTableObject, consistencyInfo);
228     }
229     
230     public static ResultType createIndex(JsonIndex jsonIndexObject, String consistencyInfo) 
231             throws MusicServiceException,MusicQueryException {
232         return musicCore.createIndex(jsonIndexObject, consistencyInfo);
233     }
234     
235     public static ResultSet select(JsonSelect jsonSelect, MultivaluedMap<String, String> rowParams) 
236             throws MusicServiceException, MusicQueryException{
237         return musicCore.select(jsonSelect, rowParams);
238     }
239     
240     public static ResultSet selectCritical(JsonInsert jsonInsertObj, MultivaluedMap<String, String> rowParams) 
241             throws MusicLockingException, MusicQueryException, MusicServiceException{
242         return musicCore.selectCritical(jsonInsertObj, rowParams);
243     }
244     
245     
246     public static ReturnType insertIntoTable(JsonInsert jsonInsert) throws MusicLockingException, MusicQueryException, MusicServiceException{
247         return musicCore.insertIntoTable(jsonInsert);
248     }
249     
250     public static ReturnType updateTable(JsonUpdate jsonUpdateObj,MultivaluedMap<String, String> rowParams) 
251             throws MusicLockingException, MusicQueryException, MusicServiceException{
252         return musicCore.updateTable(jsonUpdateObj, rowParams);
253     }
254     
255     public static ReturnType deleteFromTable(JsonDelete jsonDeleteObj,MultivaluedMap<String, String> rowParams) 
256             throws MusicLockingException, MusicQueryException, MusicServiceException{
257         return musicCore.deleteFromTable(jsonDeleteObj,rowParams);
258     }
259
260 }