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.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;
33 import javax.annotation.PostConstruct;
34 import java.util.List;
35 import java.util.stream.Collectors;
38 public class CassandraCustomStateRepository implements StateRepository {
40 private static final Logger logger = Logger.getLogger(CassandraCustomStateRepository.class);
41 private final FeatureToggleDao featureToggleDao;
43 public CassandraCustomStateRepository(FeatureToggleDao featureToggleDao) {
44 this.featureToggleDao = featureToggleDao;
53 void removeUnusedItems() {
54 List<FeatureToggleEvent> allEvents = featureToggleDao.getAllFeatures();
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()));
63 eventsToDelete.forEach(e->featureToggleDao.delete(e.getFeatureName()));
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");
72 FeatureState state = null;
73 FeatureToggleEvent event = featureToggleDao.get(feature.name());
76 state = event.getFeatureState();
77 logger.debug("State of feature {} is {}", feature, state.getFeature());
83 public void setFeatureState(FeatureState featureState) {
84 if (featureState == null) {
85 throw new IllegalArgumentException("FeatureState object is null");
87 CassandraOperationStatus status = featureToggleDao.save(new FeatureToggleEvent(featureState));
88 logger.debug("setFeatureState=> FeatureState {} is set with status {}", featureState.getFeature(), status);