2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.be.dao.cassandra;
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;
34 import javax.annotation.PostConstruct;
36 @Component("artifact-cassandra-dao")
37 public class ArtifactCassandraDao extends CassandraDao {
39 private static Logger logger = Logger.getLogger(ArtifactCassandraDao.class.getName());
40 private ArtifactAccessor artifactAccessor;
44 public ArtifactCassandraDao(CassandraClient cassandraClient) {
45 super(cassandraClient);
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");
60 logger.info("** ArtifactCassandraDao failed");
61 throw new RuntimeException("Artifact keyspace [" + keyspace + "] failed to connect with error : "
62 + result.right().value());
65 logger.info("** Cassandra client isn't connected");
66 logger.info("** ArtifactCassandraDao created, but not connected");
70 public CassandraOperationStatus saveArtifact(DAOArtifactData artifact) {
71 return client.save(artifact, DAOArtifactData.class, manager);
74 public Either<DAOArtifactData, CassandraOperationStatus> getArtifact(String artifactId) {
75 return client.getById(artifactId, DAOArtifactData.class, manager);
78 public CassandraOperationStatus deleteArtifact(String artifactId) {
79 return client.delete(artifactId, DAOArtifactData.class, manager);
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);
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);
118 return Either.left(artifactCount.one().getLong(0));