Catalog alignment
[sdc.git] / catalog-dao / src / main / java / org / openecomp / sdc / be / dao / cassandra / ArtifactCassandraDao.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.ResultSet;
24 import com.datastax.driver.core.Session;
25 import com.datastax.driver.mapping.MappingManager;
26 import fj.data.Either;
27 import org.apache.commons.lang3.tuple.ImmutablePair;
28 import org.openecomp.sdc.be.resources.data.DAOArtifactData;
29 import org.openecomp.sdc.be.resources.data.auditing.AuditingTypesConstants;
30 import org.openecomp.sdc.common.log.wrappers.Logger;
31 import org.springframework.beans.factory.annotation.Autowired;
32 import org.springframework.stereotype.Component;
33
34 import javax.annotation.PostConstruct;
35
36 @Component("artifact-cassandra-dao")
37 public class ArtifactCassandraDao extends CassandraDao {
38
39         private static Logger logger = Logger.getLogger(ArtifactCassandraDao.class.getName());
40         private ArtifactAccessor artifactAccessor;
41
42
43         @Autowired
44         public ArtifactCassandraDao(CassandraClient cassandraClient) {
45                 super(cassandraClient);
46
47         }
48
49         @PostConstruct
50         public void init() {
51                 String keyspace = AuditingTypesConstants.ARTIFACT_KEYSPACE;
52                 if (client.isConnected()) {
53                         Either<ImmutablePair<Session, MappingManager>, CassandraOperationStatus> result = client.connect(keyspace);
54                         if (result.isLeft()) {
55                                 session = result.left().value().left;
56                                 manager = result.left().value().right;
57                                 artifactAccessor = manager.createAccessor(ArtifactAccessor.class);
58                                 logger.info("** ArtifactCassandraDao created");
59                         } else {
60                                 logger.info("** ArtifactCassandraDao failed");
61                                 throw new RuntimeException("Artifact keyspace [" + keyspace + "] failed to connect with error : "
62                                                 + result.right().value());
63                         }
64                 } else {
65                         logger.info("** Cassandra client isn't connected");
66                         logger.info("** ArtifactCassandraDao created, but not connected");
67                 }
68         }
69
70         public CassandraOperationStatus saveArtifact(DAOArtifactData artifact) {
71                 return client.save(artifact, DAOArtifactData.class, manager);
72         }
73
74         public Either<DAOArtifactData, CassandraOperationStatus> getArtifact(String artifactId) {
75                 return client.getById(artifactId, DAOArtifactData.class, manager);
76         }
77
78         public CassandraOperationStatus deleteArtifact(String artifactId) {
79                 return client.delete(artifactId, DAOArtifactData.class, manager);
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
113         public Either<Long, CassandraOperationStatus> getCountOfArtifactById(String uniqeId) {
114                 ResultSet artifactCount = artifactAccessor.getNumOfArtifactsById(uniqeId);
115                 if (artifactCount == null) {
116                         return Either.right(CassandraOperationStatus.NOT_FOUND);
117                 }
118                 return Either.left(artifactCount.one().getLong(0));
119         }
120
121 }