re base code
[sdc.git] / catalog-dao / src / main / java / org / openecomp / sdc / be / dao / cassandra / SdcSchemaFilesCassandraDao.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.be.dao.cassandra;
22
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;
32
33 import javax.annotation.PostConstruct;
34 import java.util.List;
35
36 @Component("sdc-schema-files-cassandra-dao")
37 public class SdcSchemaFilesCassandraDao extends CassandraDao {
38         
39         private static Logger logger = Logger.getLogger(SdcSchemaFilesCassandraDao.class.getName());
40         private SdcSchemaFilesAccessor sdcSchemaFilesAccessor;
41         
42         public SdcSchemaFilesCassandraDao() {
43                 super();
44         }
45         
46         @PostConstruct
47         public void init() {
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");
56                         } else {
57                                 logger.info("** SdcSchemaFilesCassandraDao failed");
58                                 throw new RuntimeException("Artifact keyspace [" + keyspace + "] failed to connect with error : "
59                                                 + result.right().value());
60                         }
61                 } else {
62                         logger.info("** Cassandra client isn't connected");
63                         logger.info("** SdcSchemaFilesCassandraDao created, but not connected");
64                 }
65         }
66         
67         public CassandraOperationStatus saveSchemaFile(SdcSchemaFilesData schemaFileData) {
68                 return client.save(schemaFileData, SdcSchemaFilesData.class, manager);
69         }
70
71         public Either<SdcSchemaFilesData, CassandraOperationStatus> getSchemaFile(String schemaFileId) {
72                 return client.getById(schemaFileId, SdcSchemaFilesData.class, manager);
73         }
74
75         public CassandraOperationStatus deleteSchemaFile(String schemaFileId) {
76                 return client.delete(schemaFileId, SdcSchemaFilesData.class, manager);
77         }
78         
79         public Either<List<SdcSchemaFilesData>, CassandraOperationStatus> getSpecificSchemaFiles(String sdcreleasenum, String conformancelevel) {               
80                 
81                 Result<SdcSchemaFilesData> specificSdcSchemaFiles = null;
82                 try {
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);
87                 }
88                 
89                 if(specificSdcSchemaFiles == null) {
90                         logger.debug("not found specific SdcSchemaFiles for sdcreleasenum {}, conformancelevel {}", sdcreleasenum, conformancelevel);
91                         return Either.right(CassandraOperationStatus.NOT_FOUND);
92                 }
93                 
94                 List<SdcSchemaFilesData> list = specificSdcSchemaFiles.all();
95                 if(logger.isDebugEnabled()){
96                         for (SdcSchemaFilesData esSdcSchemaFilesData : list) {
97                                 logger.trace(esSdcSchemaFilesData.toString());
98                         }                       
99                 }
100                 
101                 return Either.left(list);
102         }
103         
104         /**
105          * ---------for use in JUnit only--------------- the method deletes all the
106          * tables in the audit keyspace
107          * 
108          * @return the status of the last failed operation or ok if all the deletes
109          *         were successful
110          */
111         public CassandraOperationStatus deleteAllArtifacts() {
112                 logger.info("cleaning all artifacts.");
113                 String query = "truncate sdcartifact.resources;";
114                 try {
115                         session.execute(query);
116                 } catch (Exception e) {
117                         logger.debug("Failed to clean artifacts", e);
118                         return CassandraOperationStatus.GENERAL_ERROR;
119                 }
120                 logger.info("cleaning all artifacts finished succsesfully.");
121                 return CassandraOperationStatus.OK;
122         }
123
124         /**
125          * the method checks if the given table is empty in the artifact keyspace
126          * 
127          * @param tableName
128          *            the name of the table we want to check
129          * @return true if the table is empty
130          */
131         public Either<Boolean, CassandraOperationStatus> isTableEmpty(String tableName) {
132                 return super.isTableEmpty(tableName);
133         }
134 }