669b211e1c2d7496b20ae96dea938c490f294803
[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 DcaeConfig extends BaseSubConfig {
26     private static final String SECTION_MARKER_DCAE = "dcae";
27
28     private static final String PROPERTY_KEY_EVENTRECEIVERURL = "dcaeUrl";
29     //private static final String PROPERTY_KEY_TESTCOLLECTOR = "dcaeTestCollector";
30     private static final String PROPERTY_KEY_USERCREDENTIALS = "dcaeUserCredentials";
31     private static final String PROPERTY_KEY_TIMERPERIOD = "dcaeHeartbeatPeriodSeconds";
32
33     private static final String DEFAULT_VALUE_EVENTRECEIVERURL = "off";
34     @SuppressWarnings("unused")
35     private static final String DEFAULT_VALUE_TESTCOLLECTOR = "no";
36     private static final String DEFAULT_VALUE_USERCREDENTIALS = "admin:admin";
37     private static final int DEFAULT_VALUE_TIMERPERIOD = 120;
38
39     private static DcaeConfig dcaeConfig = null; // Singleton of configuration data
40
41     private String eventReceiverUrl;
42     private String userCredentials;
43     private Integer timerPeriodSeconds;
44
45     private DcaeConfig() {
46         super();
47         this.eventReceiverUrl = DEFAULT_VALUE_EVENTRECEIVERURL;
48         this.userCredentials = DEFAULT_VALUE_USERCREDENTIALS;
49         this.timerPeriodSeconds = DEFAULT_VALUE_TIMERPERIOD;
50     }
51
52     private DcaeConfig(IniConfigurationFile config, ISubConfigHandler configHandler) throws ConfigurationException {
53         this(config, configHandler, true);
54     }
55
56     private DcaeConfig(IniConfigurationFile config, ISubConfigHandler configHandler, boolean save)
57             throws ConfigurationException {
58
59         super(config, configHandler, SECTION_MARKER_DCAE);
60
61         this.eventReceiverUrl = this.getString(PROPERTY_KEY_EVENTRECEIVERURL, DEFAULT_VALUE_EVENTRECEIVERURL);
62         this.userCredentials = this.getString(PROPERTY_KEY_USERCREDENTIALS, DEFAULT_VALUE_USERCREDENTIALS);
63         this.timerPeriodSeconds = this.getInt(PROPERTY_KEY_TIMERPERIOD, DEFAULT_VALUE_TIMERPERIOD);
64         if (save) {
65             config.setProperty(SECTION_MARKER_DCAE + "." + PROPERTY_KEY_EVENTRECEIVERURL, this.eventReceiverUrl);
66             config.setProperty(SECTION_MARKER_DCAE + "." + PROPERTY_KEY_USERCREDENTIALS, this.userCredentials);
67             config.setProperty(SECTION_MARKER_DCAE + "." + PROPERTY_KEY_TIMERPERIOD, this.timerPeriodSeconds);
68
69             this.save();
70         }
71     }
72
73     /*
74      * Setter
75      */
76
77     public void setEventReceiverUrl(String eventReveicerUrl) {
78         this.eventReceiverUrl = eventReveicerUrl;
79     }
80
81     public void setUserCredentials(String userCredentials) {
82         this.userCredentials = userCredentials;
83     }
84
85
86
87     public void setTimerPeriodSeconds(Integer timerPeriodSeconds) {
88         this.timerPeriodSeconds = timerPeriodSeconds;
89     }
90
91     /*
92      * Getter
93      */
94
95     public String getEventReveicerUrl() {
96         return eventReceiverUrl;
97     }
98
99     public String getUserCredentials() {
100         return userCredentials;
101     }
102
103
104     public Integer getTimerPeriodSeconds() {
105         return timerPeriodSeconds;
106     }
107
108     @Override
109     public String toString() {
110         return "DcaeConfig [eventReceiverUrl=" + eventReceiverUrl + ", userCredentials=" + userCredentials
111                 + ", timerPeriodSeconds=" + timerPeriodSeconds + "]";
112     }
113
114     @Override
115     public int hashCode() {
116         final int prime = 31;
117         int result = 1;
118         result = prime * result + (eventReceiverUrl == null ? 0 : eventReceiverUrl.hashCode());
119         result = prime * result + (timerPeriodSeconds == null ? 0 : timerPeriodSeconds.hashCode());
120         result = prime * result + (userCredentials == null ? 0 : userCredentials.hashCode());
121         return result;
122     }
123
124     @Override
125     public boolean equals(Object obj) {
126         if (this == obj) {
127             return true;
128         }
129         if (obj == null) {
130             return false;
131         }
132         if (getClass() != obj.getClass()) {
133             return false;
134         }
135         DcaeConfig other = (DcaeConfig) obj;
136         if (eventReceiverUrl == null) {
137             if (other.eventReceiverUrl != null) {
138                 return false;
139             }
140         } else if (!eventReceiverUrl.equals(other.eventReceiverUrl)) {
141             return false;
142         }
143         if (timerPeriodSeconds == null) {
144             if (other.timerPeriodSeconds != null) {
145                 return false;
146             }
147         } else if (!timerPeriodSeconds.equals(other.timerPeriodSeconds)) {
148             return false;
149         }
150         if (userCredentials == null) {
151             if (other.userCredentials != null) {
152                 return false;
153             }
154         } else if (!userCredentials.equals(other.userCredentials)) {
155             return false;
156         }
157         return true;
158     }
159
160      /*-------------------------------------
161      * static Functions
162      */
163
164     public static DcaeConfig getDefaultConfiguration() {
165         return new DcaeConfig();
166     }
167
168     public static DcaeConfig getDcae(IniConfigurationFile config, ISubConfigHandler configHandler) {
169         if (dcaeConfig == null) {
170             try {
171                 dcaeConfig = new DcaeConfig(config, configHandler);
172             } catch (ConfigurationException e) {
173                 dcaeConfig = DcaeConfig.getDefaultConfiguration();
174             }
175         }
176         return dcaeConfig;
177     }
178
179     public static boolean isInstantiated() {
180         return dcaeConfig != null;
181     }
182
183     public static DcaeConfig reload() {
184         if (dcaeConfig == null) {
185             return null;
186         }
187         DcaeConfig tmpConfig;
188         try {
189             tmpConfig = new DcaeConfig(dcaeConfig.getConfig(), dcaeConfig.getConfigHandler(), false);
190         } catch (ConfigurationException e) {
191             tmpConfig = DcaeConfig.getDefaultConfiguration();
192         }
193         dcaeConfig = tmpConfig;
194         return dcaeConfig;
195     }
196
197     public static void clear() {
198         dcaeConfig=null;
199     }
200 }