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