Catalog alignment
[sdc.git] / catalog-dao / src / main / java / org / openecomp / sdc / be / dao / cassandra / FeatureToggleDao.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2020 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 fj.data.Either;
26 import org.apache.commons.lang3.tuple.ImmutablePair;
27 import org.openecomp.sdc.be.resources.data.auditing.AuditingTypesConstants;
28 import org.openecomp.sdc.be.resources.data.togglz.FeatureToggleEvent;
29 import org.openecomp.sdc.common.log.wrappers.Logger;
30 import org.springframework.beans.factory.annotation.Autowired;
31 import org.springframework.stereotype.Component;
32
33 import javax.annotation.PostConstruct;
34 import java.util.List;
35
36 @Component("feature_toggle_dao")
37 public class FeatureToggleDao extends CassandraDao {
38
39     private FeatureToggleAccessor featureToggleAccessor;
40     private static Logger logger = Logger.getLogger(FeatureToggleDao.class.getName());
41
42     public FeatureToggleDao(CassandraClient cassandraClient) {
43         super(cassandraClient);
44     }
45
46
47     @PostConstruct
48     public void init() {
49         String keyspace = AuditingTypesConstants.REPO_KEYSPACE;
50         if (client.isConnected()) {
51             Either<ImmutablePair<Session, MappingManager>, CassandraOperationStatus> result = client.connect(keyspace);
52             if (result.isLeft()) {
53                 session = result.left().value().left;
54                 manager = result.left().value().right;
55                 featureToggleAccessor = manager.createAccessor(FeatureToggleAccessor.class);
56                 logger.info("** FeatureToggleDao created");
57             } else {
58                 logger.info("** FeatureToggleDao failed");
59                 throw new RuntimeException(
60                         "Repo keyspace [" + keyspace + "] failed to connect with error : " + result.right().value());
61             }
62         } else {
63             logger.info("** Cassandra client isn't connected");
64             logger.info("** FeatureToggleDao created, but not connected");
65         }
66     }
67
68     public CassandraOperationStatus save(FeatureToggleEvent featureToggleEvent) {
69        return client.save(featureToggleEvent, FeatureToggleEvent.class, manager);
70     }
71
72     public FeatureToggleEvent get(String feature_name) {
73         return client.getById(feature_name, FeatureToggleEvent.class, manager)
74             .left()
75             .on(r -> {
76                 logger.debug("Failed to retrieve state of feature [{}] due to error {}", feature_name, r.toString());
77                 return null;
78             });
79     }
80
81     public CassandraOperationStatus delete(String feature_name) {
82         return client.delete(feature_name, FeatureToggleEvent.class, manager);
83     }
84
85     public List<FeatureToggleEvent> getAllFeatures() {
86         return featureToggleAccessor.getAllFeatures().all();
87     }
88
89 }