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.Session;
24 import com.datastax.driver.mapping.MappingManager;
25 import com.datastax.driver.mapping.Result;
26 import fj.data.Either;
27 import org.apache.commons.lang3.tuple.ImmutablePair;
28 import org.openecomp.sdc.be.resources.data.SdcSchemaFilesData;
29 import org.openecomp.sdc.be.resources.data.auditing.AuditingTypesConstants;
30 import org.openecomp.sdc.common.log.wrappers.Logger;
31 import org.springframework.stereotype.Component;
33 import javax.annotation.PostConstruct;
34 import java.util.List;
36 @Component("sdc-schema-files-cassandra-dao")
37 public class SdcSchemaFilesCassandraDao extends CassandraDao {
39 private static Logger logger = Logger.getLogger(SdcSchemaFilesCassandraDao.class.getName());
40 private SdcSchemaFilesAccessor sdcSchemaFilesAccessor;
42 public SdcSchemaFilesCassandraDao() {
48 String keyspace = AuditingTypesConstants.ARTIFACT_KEYSPACE;
49 if (client.isConnected()) {
50 Either<ImmutablePair<Session, MappingManager>, CassandraOperationStatus> result = client.connect(keyspace);
51 if (result.isLeft()) {
52 session = result.left().value().left;
53 manager = result.left().value().right;
54 sdcSchemaFilesAccessor = manager.createAccessor(SdcSchemaFilesAccessor.class);
55 logger.info("** SdcSchemaFilesCassandraDao created");
57 logger.info("** SdcSchemaFilesCassandraDao failed");
58 throw new RuntimeException("Artifact keyspace [" + keyspace + "] failed to connect with error : "
59 + result.right().value());
62 logger.info("** Cassandra client isn't connected");
63 logger.info("** SdcSchemaFilesCassandraDao created, but not connected");
67 public CassandraOperationStatus saveSchemaFile(SdcSchemaFilesData schemaFileData) {
68 return client.save(schemaFileData, SdcSchemaFilesData.class, manager);
71 public Either<SdcSchemaFilesData, CassandraOperationStatus> getSchemaFile(String schemaFileId) {
72 return client.getById(schemaFileId, SdcSchemaFilesData.class, manager);
75 public CassandraOperationStatus deleteSchemaFile(String schemaFileId) {
76 return client.delete(schemaFileId, SdcSchemaFilesData.class, manager);
79 public Either<List<SdcSchemaFilesData>, CassandraOperationStatus> getSpecificSchemaFiles(String sdcreleasenum, String conformancelevel) {
81 Result<SdcSchemaFilesData> specificSdcSchemaFiles = null;
83 specificSdcSchemaFiles = sdcSchemaFilesAccessor.getSpecificSdcSchemaFiles(sdcreleasenum, conformancelevel);
84 } catch (Exception e) {
85 logger.debug("getSpecificSchemaFiles failed with exception {}", e);
86 return Either.right(CassandraOperationStatus.GENERAL_ERROR);
89 if(specificSdcSchemaFiles == null) {
90 logger.debug("not found specific SdcSchemaFiles for sdcreleasenum {}, conformancelevel {}", sdcreleasenum, conformancelevel);
91 return Either.right(CassandraOperationStatus.NOT_FOUND);
94 List<SdcSchemaFilesData> list = specificSdcSchemaFiles.all();
95 if(logger.isDebugEnabled()){
96 for (SdcSchemaFilesData esSdcSchemaFilesData : list) {
97 logger.trace(esSdcSchemaFilesData.toString());
101 return Either.left(list);
105 * ---------for use in JUnit only--------------- the method deletes all the
106 * tables in the audit keyspace
108 * @return the status of the last failed operation or ok if all the deletes
111 public CassandraOperationStatus deleteAllArtifacts() {
112 logger.info("cleaning all artifacts.");
113 String query = "truncate sdcartifact.resources;";
115 session.execute(query);
116 } catch (Exception e) {
117 logger.debug("Failed to clean artifacts", e);
118 return CassandraOperationStatus.GENERAL_ERROR;
120 logger.info("cleaning all artifacts finished succsesfully.");
121 return CassandraOperationStatus.OK;
125 * the method checks if the given table is empty in the artifact keyspace
128 * the name of the table we want to check
129 * @return true if the table is empty
131 public Either<Boolean, CassandraOperationStatus> isTableEmpty(String tableName) {
132 return super.isTableEmpty(tableName);