2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.be.dao.cassandra;
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;
32 public abstract class CassandraDao {
34 private static Logger logger = Logger.getLogger(CassandraDao.class.getName());
36 protected Session session;
37 protected MappingManager manager;
40 protected CassandraClient client;
43 * the method checks if the given table is empty under the keyspace the
44 * session was opened to.
47 * the name of the table we want to check
48 * @return returns true if the table was empty
50 protected Either<Boolean, CassandraOperationStatus> isTableEmpty(String tableName) {
52 Statement select = QueryBuilder.select().countAll().from(tableName).limit(10);
54 ResultSet res = session.execute(select);
55 return Either.left((res.one().getLong("count") != 0 ? false : true));
57 } catch (Exception e) {
58 logger.debug("Failed check if table is empty", e);
59 return Either.right(CassandraOperationStatus.GENERAL_ERROR);