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