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