1 package org.openecomp.sdc.be.dao.cassandra;
3 import java.util.LinkedList;
6 import javax.annotation.PostConstruct;
8 import org.apache.commons.lang3.tuple.ImmutablePair;
9 import org.openecomp.sdc.be.dao.api.ActionStatus;
10 import org.openecomp.sdc.be.resources.data.ESSdcSchemaFilesData;
11 import org.openecomp.sdc.be.resources.data.auditing.AuditingTypesConstants;
12 import org.slf4j.Logger;
13 import org.slf4j.LoggerFactory;
14 import org.springframework.stereotype.Component;
16 import com.datastax.driver.core.Session;
17 import com.datastax.driver.mapping.MappingManager;
18 import com.datastax.driver.mapping.Result;
20 import fj.data.Either;
22 @Component("sdc-schema-files-cassandra-dao")
23 public class SdcSchemaFilesCassandraDao extends CassandraDao {
25 private static Logger logger = LoggerFactory.getLogger(SdcSchemaFilesCassandraDao.class.getName());
26 private SdcSchemaFilesAccessor sdcSchemaFilesAccessor;
28 public SdcSchemaFilesCassandraDao() {
34 String keyspace = AuditingTypesConstants.ARTIFACT_KEYSPACE;
35 if (client.isConnected()) {
36 Either<ImmutablePair<Session, MappingManager>, CassandraOperationStatus> result = client.connect(keyspace);
37 if (result.isLeft()) {
38 session = result.left().value().left;
39 manager = result.left().value().right;
40 sdcSchemaFilesAccessor = manager.createAccessor(SdcSchemaFilesAccessor.class);
41 logger.info("** SdcSchemaFilesCassandraDao created");
43 logger.info("** SdcSchemaFilesCassandraDao failed");
44 throw new RuntimeException("Artifact keyspace [" + keyspace + "] failed to connect with error : "
45 + result.right().value());
48 logger.info("** Cassandra client isn't connected");
49 logger.info("** SdcSchemaFilesCassandraDao created, but not connected");
53 public CassandraOperationStatus saveArtifact(ESSdcSchemaFilesData artifact) {
54 return client.save(artifact, ESSdcSchemaFilesData.class, manager);
57 public Either<ESSdcSchemaFilesData, CassandraOperationStatus> getArtifact(String artifactId) {
58 return client.getById(artifactId, ESSdcSchemaFilesData.class, manager);
61 public CassandraOperationStatus deleteArtifact(String artifactId) {
62 return client.delete(artifactId, ESSdcSchemaFilesData.class, manager);
65 public Either<List<ESSdcSchemaFilesData>, ActionStatus> getSpecificSchemaFiles(String sdcreleasenum, String conformancelevel) {
66 Result<ESSdcSchemaFilesData> specificSdcSchemaFiles = sdcSchemaFilesAccessor.getSpecificSdcSchemaFiles(sdcreleasenum, conformancelevel);
68 if(specificSdcSchemaFiles == null) {
69 logger.debug("not found specific SdcSchemaFiles for sdcreleasenum {}, conformancelevel {}", sdcreleasenum, conformancelevel);
70 return Either.left(new LinkedList<ESSdcSchemaFilesData>());
73 if(logger.isDebugEnabled()){
74 for (ESSdcSchemaFilesData esSdcSchemaFilesData : specificSdcSchemaFiles) {
75 logger.debug(esSdcSchemaFilesData.toString());
79 return Either.left(specificSdcSchemaFiles.all());
83 * ---------for use in JUnit only--------------- the method deletes all the
84 * tables in the audit keyspace
86 * @return the status of the last failed operation or ok if all the deletes
89 public CassandraOperationStatus deleteAllArtifacts() {
90 logger.info("cleaning all artifacts.");
91 String query = "truncate sdcartifact.resources;";
93 session.execute(query);
94 } catch (Exception e) {
95 logger.debug("Failed to clean artifacts", e);
96 return CassandraOperationStatus.GENERAL_ERROR;
98 logger.info("cleaning all artifacts finished succsesfully.");
99 return CassandraOperationStatus.OK;
103 * the method checks if the given table is empty in the artifact keyspace
106 * the name of the table we want to check
107 * @return true if the table is empty
109 public Either<Boolean, CassandraOperationStatus> isTableEmpty(String tableName) {
110 return super.isTableEmpty(tableName);