1bb0f058989f71d2776561abff511902030b9a43
[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.LinkedList;
4 import java.util.List;
5
6 import javax.annotation.PostConstruct;
7
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;
15
16 import com.datastax.driver.core.Session;
17 import com.datastax.driver.mapping.MappingManager;
18 import com.datastax.driver.mapping.Result;
19
20 import fj.data.Either;
21
22 @Component("sdc-schema-files-cassandra-dao")
23 public class SdcSchemaFilesCassandraDao extends CassandraDao {
24         
25         private static Logger logger = LoggerFactory.getLogger(SdcSchemaFilesCassandraDao.class.getName());
26         private SdcSchemaFilesAccessor sdcSchemaFilesAccessor;
27         
28         public SdcSchemaFilesCassandraDao() {
29                 super();
30         }
31         
32         @PostConstruct
33         public void init() {
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");
42                         } else {
43                                 logger.info("** SdcSchemaFilesCassandraDao failed");
44                                 throw new RuntimeException("Artifact keyspace [" + keyspace + "] failed to connect with error : "
45                                                 + result.right().value());
46                         }
47                 } else {
48                         logger.info("** Cassandra client isn't connected");
49                         logger.info("** SdcSchemaFilesCassandraDao created, but not connected");
50                 }
51         }
52         
53         public CassandraOperationStatus saveArtifact(ESSdcSchemaFilesData artifact) {
54                 return client.save(artifact, ESSdcSchemaFilesData.class, manager);
55         }
56
57         public Either<ESSdcSchemaFilesData, CassandraOperationStatus> getArtifact(String artifactId) {
58                 return client.getById(artifactId, ESSdcSchemaFilesData.class, manager);
59         }
60
61         public CassandraOperationStatus deleteArtifact(String artifactId) {
62                 return client.delete(artifactId, ESSdcSchemaFilesData.class, manager);
63         }
64         
65         public Either<List<ESSdcSchemaFilesData>, ActionStatus> getSpecificSchemaFiles(String sdcreleasenum, String conformancelevel) {         
66                 Result<ESSdcSchemaFilesData> specificSdcSchemaFiles = sdcSchemaFilesAccessor.getSpecificSdcSchemaFiles(sdcreleasenum, conformancelevel);
67                 
68                 if(specificSdcSchemaFiles == null) {
69                         logger.debug("not found specific SdcSchemaFiles for sdcreleasenum {}, conformancelevel {}", sdcreleasenum, conformancelevel);
70                         return Either.left(new LinkedList<ESSdcSchemaFilesData>());
71                 }
72                 
73                 if(logger.isDebugEnabled()){
74                         for (ESSdcSchemaFilesData esSdcSchemaFilesData : specificSdcSchemaFiles) {
75                                 logger.debug(esSdcSchemaFilesData.toString());
76                         }                       
77                 }
78                 
79                 return Either.left(specificSdcSchemaFiles.all());
80         }
81         
82         /**
83          * ---------for use in JUnit only--------------- the method deletes all the
84          * tables in the audit keyspace
85          * 
86          * @return the status of the last failed operation or ok if all the deletes
87          *         were successful
88          */
89         public CassandraOperationStatus deleteAllArtifacts() {
90                 logger.info("cleaning all artifacts.");
91                 String query = "truncate sdcartifact.resources;";
92                 try {
93                         session.execute(query);
94                 } catch (Exception e) {
95                         logger.debug("Failed to clean artifacts", e);
96                         return CassandraOperationStatus.GENERAL_ERROR;
97                 }
98                 logger.info("cleaning all artifacts finished succsesfully.");
99                 return CassandraOperationStatus.OK;
100         }
101
102         /**
103          * the method checks if the given table is empty in the artifact keyspace
104          * 
105          * @param tableName
106          *            the name of the table we want to check
107          * @return true if the table is empty
108          */
109         public Either<Boolean, CassandraOperationStatus> isTableEmpty(String tableName) {
110                 return super.isTableEmpty(tableName);
111         }
112 }