Upgrade to Cassandra 3
[sdc.git] / catalog-dao / src / main / java / org / openecomp / sdc / be / resources / data / togglz / FeatureToggleEvent.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.resources.data.togglz;
22
23 import com.datastax.driver.mapping.annotations.Column;
24 import com.datastax.driver.mapping.annotations.PartitionKey;
25 import com.datastax.driver.mapping.annotations.Table;
26 import com.google.common.base.Joiner;
27 import com.google.common.base.Splitter;
28 import org.openecomp.sdc.be.resources.data.auditing.AuditingTypesConstants;
29 import org.openecomp.sdc.common.log.enums.EcompLoggerErrorCode;
30 import org.openecomp.sdc.common.log.wrappers.Logger;
31 import org.togglz.core.Feature;
32 import org.togglz.core.repository.FeatureState;
33 import com.datastax.driver.mapping.annotations.Transient;
34
35 import java.util.Map;
36
37 @Table(keyspace = AuditingTypesConstants.REPO_KEYSPACE, name = AuditingTypesConstants.FEATURE_TOGGLE_STATE)
38 public class FeatureToggleEvent {
39     private static final Logger logger = Logger.getLogger(FeatureToggleEvent.class);
40
41     @PartitionKey
42     @Column(name = "feature_name")
43     private String featureName;
44
45     @Column(name = "enabled")
46     private String enabled;
47
48     @Column(name = "strategy_id")
49     private String strategyId;
50
51     @Column(name = "parameters")
52     private String parameters;
53
54     public void setFeatureName(String featureName) {
55         this.featureName = featureName;
56     }
57
58     public void setEnabled(String enabled) {
59         this.enabled = enabled;
60     }
61
62     public void setStrategyId(String strategyId) {
63         this.strategyId = strategyId;
64     }
65
66     public void setParameters(String parameters) {
67         this.parameters = parameters;
68     }
69
70     public String getFeatureName() {
71         return featureName;
72     }
73
74     public String getEnabled() {
75         return enabled;
76     }
77
78     public String getStrategyId() {
79         return strategyId;
80     }
81
82     public String getParameters() {
83         return parameters;
84     }
85
86     public FeatureToggleEvent() {}
87
88     public FeatureToggleEvent(FeatureState featureState) {
89         this();
90         this.featureName = featureState.getFeature().name();
91         this.enabled = String.valueOf(featureState.isEnabled());
92         this.strategyId = featureState.getStrategyId();
93         this.parameters = Joiner.on(",").withKeyValueSeparator("=").join(featureState.getParameterMap());
94     }
95
96     @Transient
97     public FeatureState getFeatureState() {
98         Feature feature = ToggleableFeature.getFeatureByName(featureName);
99         if (feature == null) {
100             return null;
101         }
102         FeatureState featureState = new FeatureState(feature, Boolean.valueOf(enabled));
103         featureState.setStrategyId(strategyId);
104
105         setParameters(featureState);
106         return featureState;
107
108     }
109
110     private void setParameters(FeatureState featureState) {
111         try {
112             Map<String, String> paramMap = Splitter.on(",").withKeyValueSeparator("=").split(parameters);
113             paramMap.keySet().forEach(p->featureState.setParameter(p, paramMap.get(p)));
114         }
115         catch(IllegalArgumentException e) {
116             logger.warn(EcompLoggerErrorCode.DATA_ERROR, "FeatureToggle",
117                     "FeatureState Object generating", e.getMessage());
118         }
119     }
120
121
122 }