re base code
[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.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;
32
33 import javax.annotation.PostConstruct;
34
35 @Component("artifact-cassandra-dao")
36 public class ArtifactCassandraDao extends CassandraDao {
37
38         private static Logger logger = Logger.getLogger(ArtifactCassandraDao.class.getName());
39         private ArtifactAccessor artifactAccessor;
40
41         public ArtifactCassandraDao() {
42                 super();
43
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                                 artifactAccessor = manager.createAccessor(ArtifactAccessor.class);
55                                 logger.info("** ArtifactCassandraDao created");
56                         } else {
57                                 logger.info("** ArtifactCassandraDao 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("** ArtifactCassandraDao created, but not connected");
64                 }
65         }
66
67         public CassandraOperationStatus saveArtifact(ESArtifactData artifact) {
68                 return client.save(artifact, ESArtifactData.class, manager);
69         }
70
71         public Either<ESArtifactData, CassandraOperationStatus> getArtifact(String artifactId) {
72                 return client.getById(artifactId, ESArtifactData.class, manager);
73         }
74
75         public CassandraOperationStatus deleteArtifact(String artifactId) {
76                 return client.delete(artifactId, ESArtifactData.class, manager);
77         }
78
79         /**
80          * ---------for use in JUnit only--------------- the method deletes all the
81          * tables in the audit keyspace
82          * 
83          * @return the status of the last failed operation or ok if all the deletes
84          *         were successful
85          */
86         public CassandraOperationStatus deleteAllArtifacts() {
87                 logger.info("cleaning all artifacts.");
88                 String query = "truncate sdcartifact.resources;";
89                 try {
90                         session.execute(query);
91                 } catch (Exception e) {
92                         logger.debug("Failed to clean artifacts", e);
93                         return CassandraOperationStatus.GENERAL_ERROR;
94                 }
95                 logger.info("cleaning all artifacts finished succsesfully.");
96                 return CassandraOperationStatus.OK;
97         }
98
99         /**
100          * the method checks if the given table is empty in the artifact keyspace
101          * 
102          * @param tableName
103          *            the name of the table we want to check
104          * @return true if the table is empty
105          */
106         public Either<Boolean, CassandraOperationStatus> isTableEmpty(String tableName) {
107                 return super.isTableEmpty(tableName);
108         }
109
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);
114                 }
115                 return Either.left(artifactCount.one().getLong(0));
116         }
117
118 }