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