fa12ede9e5bc3165be0c637898fb9e1d517e8563
[ccsdk/apps.git] / sdnr / wireless-transport / code-Carbon-SR1 / apps / devicemanager / impl / src / main / java / org / opendaylight / mwtn / config / impl / DcaeConfig.java
1 package org.opendaylight.mwtn.config.impl;
2
3 import org.opendaylight.mwtn.base.internalTypes.IniConfigurationFile;
4 import org.opendaylight.mwtn.base.internalTypes.IniConfigurationFile.ConfigurationException;
5 import org.opendaylight.mwtn.config.impl.HtDevicemanagerConfiguration.ISubConfigHandler;
6
7 public class DcaeConfig extends BaseSubConfig {
8         private static final String SECTION_MARKER_DCAE = "dcae";
9
10         private static final String PROPERTY_KEY_EVENTRECEIVERURL = "dcaeUrl";
11         //private static final String PROPERTY_KEY_TESTCOLLECTOR = "dcaeTestCollector";
12         private static final String PROPERTY_KEY_USERCREDENTIALS = "dcaeUserCredentials";
13         private static final String PROPERTY_KEY_TIMERPERIOD = "dcaeHeartbeatPeriodSeconds";
14
15         private static final String DEFAULT_VALUE_EVENTRECEIVERURL = "off";
16         private static final String DEFAULT_VALUE_TESTCOLLECTOR = "no";
17         private static final String DEFAULT_VALUE_USERCREDENTIALS = "admin:admin";
18         private static final int DEFAULT_VALUE_TIMERPERIOD = 120;
19
20         private static DcaeConfig dcaeConfig = null; // Singleton of configuration data
21
22         private String eventReceiverUrl;
23         private String userCredentials;
24         private Integer timerPeriodSeconds;
25
26         private DcaeConfig() {
27                 super();
28                 this.eventReceiverUrl = DEFAULT_VALUE_EVENTRECEIVERURL;
29                 this.userCredentials = DEFAULT_VALUE_USERCREDENTIALS;
30                 this.timerPeriodSeconds = DEFAULT_VALUE_TIMERPERIOD;
31         }
32
33         private DcaeConfig(IniConfigurationFile config, ISubConfigHandler configHandler) throws ConfigurationException {
34                 this(config, configHandler, true);
35         }
36
37         private DcaeConfig(IniConfigurationFile config, ISubConfigHandler configHandler, boolean save)
38                         throws ConfigurationException {
39
40                 super(config, configHandler, SECTION_MARKER_DCAE);
41
42                 this.eventReceiverUrl = this.getString(PROPERTY_KEY_EVENTRECEIVERURL, DEFAULT_VALUE_EVENTRECEIVERURL);
43                 this.userCredentials = this.getString(PROPERTY_KEY_USERCREDENTIALS, DEFAULT_VALUE_USERCREDENTIALS);
44                 this.timerPeriodSeconds = this.getInt(PROPERTY_KEY_TIMERPERIOD, DEFAULT_VALUE_TIMERPERIOD);
45                 if (save) {
46                         config.setProperty(SECTION_MARKER_DCAE + "." + PROPERTY_KEY_EVENTRECEIVERURL, this.eventReceiverUrl);
47                         config.setProperty(SECTION_MARKER_DCAE + "." + PROPERTY_KEY_USERCREDENTIALS, this.userCredentials);
48                         config.setProperty(SECTION_MARKER_DCAE + "." + PROPERTY_KEY_TIMERPERIOD, this.timerPeriodSeconds);
49
50                         this.save();
51                 }
52         }
53
54         /*
55          * Setter
56          */
57
58         public void setEventReceiverUrl(String eventReveicerUrl) {
59                 this.eventReceiverUrl = eventReveicerUrl;
60         }
61
62         public void setUserCredentials(String userCredentials) {
63                 this.userCredentials = userCredentials;
64         }
65
66
67
68         public void setTimerPeriodSeconds(Integer timerPeriodSeconds) {
69                 this.timerPeriodSeconds = timerPeriodSeconds;
70         }
71
72         /*
73          * Getter
74          */
75
76         public String getEventReveicerUrl() {
77                 return eventReceiverUrl;
78         }
79
80         public String getUserCredentials() {
81                 return userCredentials;
82         }
83
84
85         public Integer getTimerPeriodSeconds() {
86                 return timerPeriodSeconds;
87         }
88
89         @Override
90         public String toString() {
91                 return "DcaeConfig [eventReceiverUrl=" + eventReceiverUrl + ", userCredentials=" + userCredentials
92                                 + ", timerPeriodSeconds=" + timerPeriodSeconds + "]";
93         }
94
95         @Override
96         public boolean equals(Object obj) {
97                 if (obj instanceof DcaeConfig) {
98                         DcaeConfig cobj = (DcaeConfig) obj;
99                         if (!((cobj.eventReceiverUrl == null && this.eventReceiverUrl == null)
100                                         || cobj.eventReceiverUrl.equals(this.eventReceiverUrl)))
101                                 return false;
102                         if (!((cobj.userCredentials == null && this.userCredentials == null)
103                                         || cobj.userCredentials.equals(this.userCredentials)))
104                                 return false;
105                         if (cobj.timerPeriodSeconds != this.timerPeriodSeconds)
106                                 return false;
107                         return true;
108                 }
109                 return super.equals(obj);
110         }
111
112         /*-------------------------------------
113          * static Functions
114          */
115
116         public static DcaeConfig getDefaultConfiguration() {
117                 return (new DcaeConfig());
118         }
119
120         public static DcaeConfig getDcae(IniConfigurationFile config, ISubConfigHandler configHandler) {
121                 if (dcaeConfig == null)
122                         try {
123                                 dcaeConfig = new DcaeConfig(config, configHandler);
124                         } catch (ConfigurationException e) {
125                                 dcaeConfig = DcaeConfig.getDefaultConfiguration();
126                         }
127                 return dcaeConfig;
128         }
129
130         public static boolean isInstantiated() {
131                 return dcaeConfig != null;
132         }
133
134         public static DcaeConfig reload() {
135                 if (dcaeConfig == null)
136                         return null;
137                 DcaeConfig tmpConfig;
138                 try {
139                         tmpConfig = new DcaeConfig(dcaeConfig.getConfig(), dcaeConfig.getConfigHandler(), false);
140                 } catch (ConfigurationException e) {
141                         tmpConfig = DcaeConfig.getDefaultConfiguration();
142                 }
143                 dcaeConfig = tmpConfig;
144                 return dcaeConfig;
145         }
146 }