[SDC-29] rebase continue work to align source
[sdc.git] / catalog-dao / src / main / java / org / openecomp / sdc / be / dao / cassandra / SdcSchemaFilesCassandraDao.java
1 package org.openecomp.sdc.be.dao.cassandra;
2
3 import java.util.List;
4
5 import javax.annotation.PostConstruct;
6
7 import org.apache.commons.lang3.tuple.ImmutablePair;
8 import org.openecomp.sdc.be.resources.data.SdcSchemaFilesData;
9 import org.openecomp.sdc.be.resources.data.auditing.AuditingTypesConstants;
10 import org.slf4j.Logger;
11 import org.slf4j.LoggerFactory;
12 import org.springframework.stereotype.Component;
13
14 import com.datastax.driver.core.Session;
15 import com.datastax.driver.mapping.MappingManager;
16 import com.datastax.driver.mapping.Result;
17
18 import fj.data.Either;
19
20 @Component("sdc-schema-files-cassandra-dao")
21 public class SdcSchemaFilesCassandraDao extends CassandraDao {
22         
23         private static Logger logger = LoggerFactory.getLogger(SdcSchemaFilesCassandraDao.class.getName());
24         private SdcSchemaFilesAccessor sdcSchemaFilesAccessor;
25         
26         public SdcSchemaFilesCassandraDao() {
27                 super();
28         }
29         
30         @PostConstruct
31         public void init() {
32                 String keyspace = AuditingTypesConstants.ARTIFACT_KEYSPACE;
33                 if (client.isConnected()) {
34                         Either<ImmutablePair<Session, MappingManager>, CassandraOperationStatus> result = client.connect(keyspace);
35                         if (result.isLeft()) {
36                                 session = result.left().value().left;
37                                 manager = result.left().value().right;
38                                 sdcSchemaFilesAccessor = manager.createAccessor(SdcSchemaFilesAccessor.class);
39                                 logger.info("** SdcSchemaFilesCassandraDao created");
40                         } else {
41                                 logger.info("** SdcSchemaFilesCassandraDao failed");
42                                 throw new RuntimeException("Artifact keyspace [" + keyspace + "] failed to connect with error : "
43                                                 + result.right().value());
44                         }
45                 } else {
46                         logger.info("** Cassandra client isn't connected");
47                         logger.info("** SdcSchemaFilesCassandraDao created, but not connected");
48                 }
49         }
50         
51         public CassandraOperationStatus saveSchemaFile(SdcSchemaFilesData schemaFileData) {
52                 return client.save(schemaFileData, SdcSchemaFilesData.class, manager);
53         }
54
55         public Either<SdcSchemaFilesData, CassandraOperationStatus> getSchemaFile(String schemaFileId) {
56                 return client.getById(schemaFileId, SdcSchemaFilesData.class, manager);
57         }
58
59         public CassandraOperationStatus deleteSchemaFile(String schemaFileId) {
60                 return client.delete(schemaFileId, SdcSchemaFilesData.class, manager);
61         }
62         
63         public Either<List<SdcSchemaFilesData>, CassandraOperationStatus> getSpecificSchemaFiles(String sdcreleasenum, String conformancelevel) {               
64                 
65                 Result<SdcSchemaFilesData> specificSdcSchemaFiles = null;
66                 try {
67                         specificSdcSchemaFiles = sdcSchemaFilesAccessor.getSpecificSdcSchemaFiles(sdcreleasenum, conformancelevel);
68                 } catch (Exception e) {
69                         logger.debug("getSpecificSchemaFiles failed with exception {}", e);
70                         return Either.right(CassandraOperationStatus.GENERAL_ERROR);
71                 }
72                 
73                 if(specificSdcSchemaFiles == null) {
74                         logger.debug("not found specific SdcSchemaFiles for sdcreleasenum {}, conformancelevel {}", sdcreleasenum, conformancelevel);
75                         return Either.right(CassandraOperationStatus.NOT_FOUND);
76                 }
77                 
78                 List<SdcSchemaFilesData> list = specificSdcSchemaFiles.all();
79                 if(logger.isDebugEnabled()){
80                         for (SdcSchemaFilesData esSdcSchemaFilesData : list) {
81                                 logger.trace(esSdcSchemaFilesData.toString());
82                         }                       
83                 }
84                 
85                 return Either.left(list);
86         }
87         
88         /**
89          * ---------for use in JUnit only--------------- the method deletes all the
90          * tables in the audit keyspace
91          * 
92          * @return the status of the last failed operation or ok if all the deletes
93          *         were successful
94          */
95         public CassandraOperationStatus deleteAllArtifacts() {
96                 logger.info("cleaning all artifacts.");
97                 String query = "truncate sdcartifact.resources;";
98                 try {
99                         session.execute(query);
100                 } catch (Exception e) {
101                         logger.debug("Failed to clean artifacts", e);
102                         return CassandraOperationStatus.GENERAL_ERROR;
103                 }
104                 logger.info("cleaning all artifacts finished succsesfully.");
105                 return CassandraOperationStatus.OK;
106         }
107
108         /**
109          * the method checks if the given table is empty in the artifact keyspace
110          * 
111          * @param tableName
112          *            the name of the table we want to check
113          * @return true if the table is empty
114          */
115         public Either<Boolean, CassandraOperationStatus> isTableEmpty(String tableName) {
116                 return super.isTableEmpty(tableName);
117         }
118 }