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.ESArtifactData;
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;
33 import javax.annotation.PostConstruct;
35 @Component("artifact-cassandra-dao")
36 public class ArtifactCassandraDao extends CassandraDao {
38 private static Logger logger = Logger.getLogger(ArtifactCassandraDao.class.getName());
39 private ArtifactAccessor artifactAccessor;
41 public ArtifactCassandraDao() {
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 artifactAccessor = manager.createAccessor(ArtifactAccessor.class);
55 logger.info("** ArtifactCassandraDao created");
57 logger.info("** ArtifactCassandraDao failed");
58 throw new RuntimeException("Artifact keyspace [" + keyspace + "] failed to connect with error : "
59 + result.right().value());
62 logger.info("** Cassandra client isn't connected");
63 logger.info("** ArtifactCassandraDao created, but not connected");
67 public CassandraOperationStatus saveArtifact(ESArtifactData artifact) {
68 return client.save(artifact, ESArtifactData.class, manager);
71 public Either<ESArtifactData, CassandraOperationStatus> getArtifact(String artifactId) {
72 return client.getById(artifactId, ESArtifactData.class, manager);
75 public CassandraOperationStatus deleteArtifact(String artifactId) {
76 return client.delete(artifactId, ESArtifactData.class, manager);
80 * ---------for use in JUnit only--------------- the method deletes all the
81 * tables in the audit keyspace
83 * @return the status of the last failed operation or ok if all the deletes
86 public CassandraOperationStatus deleteAllArtifacts() {
87 logger.info("cleaning all artifacts.");
88 String query = "truncate sdcartifact.resources;";
90 session.execute(query);
91 } catch (Exception e) {
92 logger.debug("Failed to clean artifacts", e);
93 return CassandraOperationStatus.GENERAL_ERROR;
95 logger.info("cleaning all artifacts finished succsesfully.");
96 return CassandraOperationStatus.OK;
100 * the method checks if the given table is empty in the artifact keyspace
103 * the name of the table we want to check
104 * @return true if the table is empty
106 public Either<Boolean, CassandraOperationStatus> isTableEmpty(String tableName) {
107 return super.isTableEmpty(tableName);
110 public Either<Long, CassandraOperationStatus> getCountOfArtifactById(String uniqeId) {
111 ResultSet artifactCount = artifactAccessor.getNumOfArtifactsById(uniqeId);
112 if (artifactCount == null) {
113 return Either.right(CassandraOperationStatus.NOT_FOUND);
115 return Either.left(artifactCount.one().getLong(0));