Toggle
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / togglz / CassandraCustomStateRepository.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.togglz;
22
23 import com.google.common.annotations.VisibleForTesting;
24 import org.openecomp.sdc.be.dao.cassandra.CassandraOperationStatus;
25 import org.openecomp.sdc.be.dao.cassandra.FeatureToggleDao;
26 import org.openecomp.sdc.be.resources.data.togglz.FeatureToggleEvent;
27 import org.openecomp.sdc.common.log.wrappers.Logger;
28 import org.springframework.stereotype.Component;
29 import org.togglz.core.Feature;
30 import org.togglz.core.repository.FeatureState;
31 import org.togglz.core.repository.StateRepository;
32
33 import javax.annotation.PostConstruct;
34 import java.util.List;
35 import java.util.stream.Collectors;
36
37 @Component
38 public class CassandraCustomStateRepository implements StateRepository {
39
40     private static final Logger logger = Logger.getLogger(CassandraCustomStateRepository.class);
41     private final FeatureToggleDao featureToggleDao;
42
43     public CassandraCustomStateRepository(FeatureToggleDao featureToggleDao) {
44         this.featureToggleDao = featureToggleDao;
45     }
46
47     @PostConstruct
48     private void init() {
49         removeUnusedItems();
50     }
51
52     @VisibleForTesting
53     void removeUnusedItems() {
54         List<FeatureToggleEvent> allEvents = featureToggleDao.getAllFeatures();
55
56         List<FeatureToggleEvent> eventsToDelete = allEvents.stream()
57                 .filter(e-> ToggleableFeature.getFeatureByName(e.getFeatureName()) == null)
58                 .collect(Collectors.toList());
59         if (!eventsToDelete.isEmpty()) {
60             logger.debug("Found Feature toggles not in use [{}], they will be deleted",
61                     eventsToDelete.stream().map(FeatureToggleEvent::getFeatureName).collect(Collectors.toList()));
62         }
63         eventsToDelete.forEach(e->featureToggleDao.delete(e.getFeatureName()));
64     }
65
66     @Override
67     public FeatureState getFeatureState(Feature feature) {
68         logger.debug("getFeatureState=> Request is received for a Feature {}", feature);
69         if (feature == null) {
70             throw new IllegalArgumentException("Feature object is null");
71         }
72         FeatureState state = null;
73         FeatureToggleEvent event = featureToggleDao.get(feature.name());
74
75         if (event != null) {
76             state = event.getFeatureState();
77             logger.debug("State of feature {} is {}", feature, state.getFeature());
78         }
79         return state;
80     }
81
82     @Override
83     public void setFeatureState(FeatureState featureState) {
84         if (featureState == null) {
85             throw new IllegalArgumentException("FeatureState object is null");
86         }
87         CassandraOperationStatus status = featureToggleDao.save(new FeatureToggleEvent(featureState));
88         logger.debug("setFeatureState=> FeatureState {} is set with status {}", featureState.getFeature(), status);
89     }
90
91 }