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