2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.be.togglz;
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;
34 import javax.annotation.PostConstruct;
35 import java.util.List;
36 import java.util.stream.Collectors;
39 public class CassandraCustomStateRepository implements StateRepository {
41 private final static Logger logger = Logger.getLogger(CassandraCustomStateRepository.class);
42 private final FeatureToggleDao featureToggleDao;
44 public CassandraCustomStateRepository(FeatureToggleDao featureToggleDao) {
45 this.featureToggleDao = featureToggleDao;
54 void removeUnusedItems() {
55 List<FeatureToggleEvent> allEvents = featureToggleDao.getAllFeatures();
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()));
64 eventsToDelete.forEach(e->featureToggleDao.delete(e.getFeatureName()));
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");
73 FeatureState state = null;
74 FeatureToggleEvent event = featureToggleDao.get(feature.name());
77 state = event.getFeatureState();
78 logger.debug("State of feature {} is {}", feature, state.getFeature());
84 public void setFeatureState(FeatureState featureState) {
85 if (featureState == null) {
86 throw new IllegalArgumentException("FeatureState object is null");
88 CassandraOperationStatus status = featureToggleDao.save(new FeatureToggleEvent(featureState));
89 logger.debug("setFeatureState=> FeatureState {} is set with status {}", featureState.getFeature(), status);