da2f10a75b629ed1c701083456c8a0ca2002f833
[ccsdk/features.git] /
1 /*******************************************************************************
2  * ============LICENSE_START========================================================================
3  * ONAP : ccsdk feature sdnr wt
4  * =================================================================================================
5  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
6  * =================================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software distributed under the License
13  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14  * or implied. See the License for the specific language governing permissions and limitations under
15  * the License.
16  * ============LICENSE_END==========================================================================
17  ******************************************************************************/
18 package org.onap.ccsdk.features.sdnr.wt.devicemanager.config.impl;
19
20 import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.internalTypes.IniConfigurationFile;
21 import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.internalTypes.IniConfigurationFile.ConfigurationException;
22 import org.onap.ccsdk.features.sdnr.wt.devicemanager.config.BaseSubConfig;
23 import org.onap.ccsdk.features.sdnr.wt.devicemanager.config.ISubConfigHandler;
24
25 public class ToggleAlarmConfig extends BaseSubConfig{
26
27     private static final String SECTION_MARKER_TA = "toggleAlarmFilter";
28     private static final String PROPERTY_KEY_ENABLED = "taEnabled";
29     private static final String PROPERTY_KEY_DELAY = "taDelay";
30
31     private static final boolean DEFAULT_VALUE_ENABLED = true;
32     private static final long DEFAULT_VALUE_DELAY = 3000;  //in ms
33     private static ToggleAlarmConfig taConfig;
34     private boolean enabled;
35     private long delay;
36
37     public boolean isEnabled() {
38         return this.enabled;
39     }
40     public long getDelay() {
41         return this.delay;
42     }
43     private ToggleAlarmConfig() {
44         super();
45         this.enabled = DEFAULT_VALUE_ENABLED;
46         this.delay=DEFAULT_VALUE_DELAY;
47     }
48     public ToggleAlarmConfig(IniConfigurationFile config, ISubConfigHandler configHandler) throws ConfigurationException {
49         this(config, configHandler, true);
50     }
51
52     public ToggleAlarmConfig(IniConfigurationFile config, ISubConfigHandler configHandler, boolean save)
53             throws ConfigurationException {
54
55         super(config, configHandler, SECTION_MARKER_TA);
56
57         this.enabled = this.getBoolean(PROPERTY_KEY_ENABLED, DEFAULT_VALUE_ENABLED);
58         this.delay = this.getLong(PROPERTY_KEY_DELAY,DEFAULT_VALUE_DELAY);
59         if (save) {
60             config.setProperty(SECTION_MARKER_TA + "." + PROPERTY_KEY_ENABLED, this.enabled);
61             config.setProperty(SECTION_MARKER_TA + "." + PROPERTY_KEY_DELAY, this.delay);
62
63             this.save();
64         }
65     }
66
67     @Override
68     public int hashCode() {
69         final int prime = 31;
70         int result = 1;
71         result = prime * result + (int) (delay ^ delay >>> 32);
72         result = prime * result + (enabled ? 1231 : 1237);
73         return result;
74     }
75     @Override
76     public boolean equals(Object obj) {
77         if (this == obj) {
78             return true;
79         }
80         if (obj == null) {
81             return false;
82         }
83         if (getClass() != obj.getClass()) {
84             return false;
85         }
86         ToggleAlarmConfig other = (ToggleAlarmConfig) obj;
87         if (delay != other.delay) {
88             return false;
89         }
90         if (enabled != other.enabled) {
91             return false;
92         }
93         return true;
94     }
95
96     @Override
97     public String toString() {
98         return "ToggleAlarmConfig [enabled=" + enabled + ", delay=" + delay + "]";
99     }
100
101     public static ToggleAlarmConfig getDefaultConfiguration() {
102         ToggleAlarmConfig c = new ToggleAlarmConfig();
103         c.enabled = DEFAULT_VALUE_ENABLED;
104         c.delay = DEFAULT_VALUE_DELAY;
105         return c;
106     }
107     public static boolean isInstantiated() {
108         return taConfig != null;
109     }
110
111     public static ToggleAlarmConfig getTa(IniConfigurationFile config, ISubConfigHandler configHandler) {
112         if (taConfig == null) {
113             try {
114                 taConfig = new ToggleAlarmConfig(config, configHandler);
115             } catch (ConfigurationException e) {
116                 taConfig = ToggleAlarmConfig.getDefaultConfiguration();
117             }
118         }
119         return taConfig;
120     }
121
122     public static ToggleAlarmConfig reload() {
123         if (taConfig == null) {
124             return null;
125         }
126         ToggleAlarmConfig tmpConfig;
127         try {
128             tmpConfig = new ToggleAlarmConfig(taConfig.getConfig(), taConfig.getConfigHandler(), false);
129         } catch (ConfigurationException e) {
130             tmpConfig = ToggleAlarmConfig.getDefaultConfiguration();
131         }
132         taConfig = tmpConfig;
133         return taConfig;
134     }
135
136     public static void clear() {
137         taConfig=null;
138     }
139 }