480430ec0bbb8b91c5e524116f8a4f2a9b481e2f
[ccsdk/apps.git] / sdnr / wireless-transport / code-Carbon-SR1 / apps / devicemanager / impl / src / main / java / org / opendaylight / mwtn / config / impl / HtDevicemanagerConfiguration.java
1 package org.opendaylight.mwtn.config.impl;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.util.ArrayList;
6 import java.util.List;
7
8 import org.opendaylight.mwtn.base.internalTypes.FileWatchdog;
9 import org.opendaylight.mwtn.base.internalTypes.IniConfigurationFile;
10 import org.opendaylight.mwtn.base.internalTypes.IniConfigurationFile.ConfigurationException;
11 import org.slf4j.Logger;
12 import org.slf4j.LoggerFactory;
13
14 public class HtDevicemanagerConfiguration {
15
16         private static final long FILE_POLL_INTERVAL_MS = 1000;
17
18         public interface IConfigChangedListener {
19                 void onConfigChanged();
20         }
21         public static class ConfigFileObserver extends FileWatchdog
22         {
23                 private final List<IConfigChangedListener> mConfigChangedHandlers = new ArrayList<IConfigChangedListener>();
24                 protected ConfigFileObserver(String filename) {
25                         super(filename);
26                         this.setDelay(FILE_POLL_INTERVAL_MS);
27                 }
28
29                 @Override
30                 protected void doOnChange() {
31
32                         boolean succeeded=true;
33                         LOG.debug("property file has changed");
34                         try {
35                                 mConfig.reLoad();
36
37                         } catch (ConfigurationException e) {
38                                 LOG.warn("error reloading config: "+e.getMessage());
39                                 succeeded = false;
40
41                         }
42                         if(!succeeded)
43                                 return;
44                         if(this.mConfigChangedHandlers==null)
45                         {
46                                 LOG.debug("handler list is null");//should never happen
47                                 return;
48                         }
49                         //push event to all listeners
50                         for (IConfigChangedListener listener : this.mConfigChangedHandlers) {
51                                 if (listener != null)
52                                         listener.onConfigChanged();
53                         }
54                 }
55
56                 public void registerConfigChangedListener(IConfigChangedListener l) {
57                         if (!this.mConfigChangedHandlers.contains(l))
58                                 this.mConfigChangedHandlers.add(l);
59                 }
60
61                 public void unregisterConfigChangedListener(IConfigChangedListener l) {
62                         this.mConfigChangedHandlers.remove(l);
63                 }
64
65
66         }
67         public interface ISubConfigHandler {
68                 void save();
69         }
70
71         private static final Logger LOG = LoggerFactory.getLogger(HtDevicemanagerConfiguration.class);
72
73         private static final String CONFIGURATIONFILE = "etc/devicemanager.properties";
74         private static final String CONFIGURATIONTESTFILE = "test.properties"; // for
75         // testing
76
77         private static HtDevicemanagerConfiguration mObj;
78         private static HtDevicemanagerConfiguration mObjTest;
79
80         public static HtDevicemanagerConfiguration getConfiguration() {
81                 if (mObj == null)
82                         mObj = new HtDevicemanagerConfiguration(CONFIGURATIONFILE);
83                 return mObj;
84         }
85         public static HtDevicemanagerConfiguration getTestConfiguration() {
86                 if (mObjTest == null)
87                         mObjTest = new HtDevicemanagerConfiguration(CONFIGURATIONTESTFILE);
88                 return mObjTest;
89         }
90         private static IniConfigurationFile mConfig;
91         private File mFile;
92
93         public IniConfigurationFile getMConfig() {
94                 return mConfig;
95         }
96
97         private final ConfigFileObserver fileObserver;
98         public void registerConfigChangedListener(IConfigChangedListener l) {
99                 this.fileObserver.registerConfigChangedListener(l);
100         }
101
102         public void unregisterConfigChangedListener(IConfigChangedListener l) {
103                 this.fileObserver.unregisterConfigChangedListener(l);
104         }
105
106         private HtDevicemanagerConfiguration(String filename) {
107
108                 try {
109                         this.mFile = new File(filename);
110                         if (!this.mFile.exists())
111                                 this.mFile.createNewFile();
112                         if(mConfig==null)
113                                 mConfig = new IniConfigurationFile(this.mFile);
114                         mConfig.load();
115
116                 } catch (ConfigurationException e) {
117                         LOG.error("error loading config values:" + e.getMessage());
118
119                 } catch (IOException e) {
120                         LOG.error("error loading config file " + filename + ": " + e.getMessage());
121                 }
122
123                 this.fileObserver=new ConfigFileObserver(filename);
124                 this.fileObserver.start();
125         }
126
127         @Override
128         protected void finalize() throws Throwable {
129                 if(this.fileObserver!=null)
130                         this.fileObserver.interrupt();
131                 super.finalize();
132         }
133         public DcaeConfig getDcae() {
134                 return DcaeConfig.getDcae(mConfig, this.subconfigHandler);
135         }
136
137         public AaiConfig getAai() {
138                 return AaiConfig.getAai(mConfig, this.subconfigHandler);
139         }
140
141         public EsConfig getEs() {
142                 return EsConfig.getEs(mConfig, this.subconfigHandler);
143         }
144
145         public PmConfig getPm() {
146                 return PmConfig.getPm(mConfig, this.subconfigHandler);
147         }
148
149         private final ISubConfigHandler subconfigHandler = new ISubConfigHandler() {
150
151                 @Override
152                 public void save() {
153                         mConfig.save();
154                 }
155         };
156
157         public ISubConfigHandler getSubconfigHandler() {
158                 return subconfigHandler;
159         }
160
161
162
163 }