1f2dde235601da23ee04ea0fe2633964d64fea53
[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;
19
20 import java.io.File;
21 import java.io.IOException;
22 import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.internalTypes.IniConfigurationFile;
23 import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.internalTypes.IniConfigurationFile.ConfigurationException;
24 import org.onap.ccsdk.features.sdnr.wt.devicemanager.config.impl.AaiConfig;
25 import org.onap.ccsdk.features.sdnr.wt.devicemanager.config.impl.DcaeConfig;
26 import org.onap.ccsdk.features.sdnr.wt.devicemanager.config.impl.DmConfig;
27 import org.onap.ccsdk.features.sdnr.wt.devicemanager.config.impl.EsConfig;
28 import org.onap.ccsdk.features.sdnr.wt.devicemanager.config.impl.PmConfig;
29 import org.onap.ccsdk.features.sdnr.wt.devicemanager.config.impl.ToggleAlarmConfig;
30 import org.onap.ccsdk.features.sdnr.wt.devicemanager.config.util.ConfigFileObserver;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 public class HtDevicemanagerConfiguration {
35
36     private static final long FILE_POLL_INTERVAL_MS = 1000;
37
38     private static final Logger LOG = LoggerFactory.getLogger(HtDevicemanagerConfiguration.class);
39
40     private static final String CONFIGURATIONFILE = "etc/devicemanager.properties";
41     private static final String CONFIGURATIONTESTFILE = "test.properties"; // for
42     // testing
43
44     private static HtDevicemanagerConfiguration mObj;
45     private static HtDevicemanagerConfiguration mObjTest;
46     private static IniConfigurationFile mConfig;
47     private final ISubConfigHandler subconfigHandler = () -> mConfig.save();
48
49     private final ConfigFileObserver fileObserver;
50     private File mFile;
51
52     private HtDevicemanagerConfiguration(String filename) {
53
54         try {
55             this.mFile = new File(filename);
56             if (!this.mFile.exists()) {
57                 if (!this.mFile.createNewFile()) {
58                     LOG.error("Can not create file {}", filename);
59                 }
60             }
61             if (mConfig == null) {
62                 mConfig = new IniConfigurationFile(this.mFile);
63             }
64             mConfig.load();
65
66         } catch (ConfigurationException e) {
67             LOG.error("Problem loading config values: {}", e.getMessage());
68         } catch (IOException e) {
69             LOG.error("Problem loading config file {} : {}", filename, e.getMessage());
70         }
71
72         this.fileObserver = new ConfigFileObserver(filename, FILE_POLL_INTERVAL_MS, mConfig);
73         this.fileObserver.start();
74     }
75
76
77     public static HtDevicemanagerConfiguration getConfiguration() {
78         if (mObj == null) {
79             mObj = new HtDevicemanagerConfiguration(CONFIGURATIONFILE);
80         }
81         return mObj;
82     }
83     public static HtDevicemanagerConfiguration getTestConfiguration() {
84         return getTestConfiguration(CONFIGURATIONTESTFILE,false);
85     }
86
87     public static HtDevicemanagerConfiguration getTestConfiguration(boolean newInstance) {
88         return getTestConfiguration(CONFIGURATIONTESTFILE,newInstance);
89     }
90     public static HtDevicemanagerConfiguration getTestConfiguration(String filename) {
91         return getTestConfiguration(filename,false);
92     }
93     public static HtDevicemanagerConfiguration getTestConfiguration(final String filename,boolean newInstance) {
94         if (mObjTest == null || newInstance) {
95             mObjTest = new HtDevicemanagerConfiguration(filename);
96         }
97         return mObjTest;
98     }
99
100     public IniConfigurationFile getMConfig() {
101         return mConfig;
102     }
103
104     public void registerConfigChangedListener(IConfigChangedListener l) {
105         this.fileObserver.registerConfigChangedListener(l);
106     }
107
108     public void unregisterConfigChangedListener(IConfigChangedListener l) {
109         this.fileObserver.unregisterConfigChangedListener(l);
110     }
111
112     @Override
113     protected void finalize() throws Throwable {
114         if (this.fileObserver != null) {
115             this.fileObserver.interrupt();
116         }
117         super.finalize();
118     }
119
120     public DcaeConfig getDcae() {
121         return DcaeConfig.getDcae(mConfig, this.subconfigHandler);
122     }
123
124     public AaiConfig getAai() {
125         return AaiConfig.getAai(mConfig, this.subconfigHandler);
126     }
127
128     public EsConfig getEs() {
129         return EsConfig.getEs(mConfig, this.subconfigHandler);
130     }
131
132     public PmConfig getPm() {
133         return PmConfig.getPm(mConfig, this.subconfigHandler);
134     }
135
136     public ToggleAlarmConfig getToggleAlarm() {
137         return ToggleAlarmConfig.getTa(mConfig, this.subconfigHandler);
138     }
139
140     public DmConfig getDmConfig() {
141         return DmConfig.getDmConfig(mConfig, this.subconfigHandler);
142     }
143
144     public ISubConfigHandler getSubconfigHandler() {
145         return subconfigHandler;
146     }
147
148     public static void clear() {
149         mObj = null;
150         mObjTest = null;
151         DcaeConfig.clear();
152         AaiConfig.clear();
153         EsConfig.clear();
154         PmConfig.clear();
155         ToggleAlarmConfig.clear();
156         DmConfig.clear();
157     }
158 }