re base code
[sdc.git] / catalog-dao / src / main / java / org / openecomp / sdc / be / dao / cassandra / CassandraDao.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
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  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.be.dao.cassandra;
22
23 import com.datastax.driver.core.ResultSet;
24 import com.datastax.driver.core.Session;
25 import com.datastax.driver.core.Statement;
26 import com.datastax.driver.core.querybuilder.QueryBuilder;
27 import com.datastax.driver.mapping.MappingManager;
28 import fj.data.Either;
29 import org.openecomp.sdc.common.log.wrappers.Logger;
30 import org.springframework.beans.factory.annotation.Autowired;
31
32 public abstract class CassandraDao {
33
34         private static Logger logger = Logger.getLogger(CassandraDao.class.getName());
35
36         protected Session session;
37         protected MappingManager manager;
38
39         @Autowired
40         protected CassandraClient client;
41
42         /**
43          * the method checks if the given table is empty under the keyspace the
44          * session was opened to.
45          * 
46          * @param tableName
47          *            the name of the table we want to check
48          * @return returns true if the table was empty
49          */
50         protected Either<Boolean, CassandraOperationStatus> isTableEmpty(String tableName) {
51
52                 Statement select = QueryBuilder.select().countAll().from(tableName).limit(10);
53                 try {
54                         ResultSet res = session.execute(select);
55                         return Either.left((res.one().getLong("count") != 0 ? false : true));
56
57                 } catch (Exception e) {
58                         logger.debug("Failed check if table is empty", e);
59                         return Either.right(CassandraOperationStatus.GENERAL_ERROR);
60                 }
61         }
62
63 }