aboutsummaryrefslogtreecommitdiffstats
path: root/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/configuration/ConfigurationFileRepresentation.java
blob: 96bfa070ba325603ac25978f7cf4ffe30b925da7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
 * ============LICENSE_START=======================================================
 * ONAP : ccsdk features
 * ================================================================================
 * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property.
 * All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 *
 */
package org.onap.ccsdk.features.sdnr.wt.common.configuration;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Optional;
import org.onap.ccsdk.features.sdnr.wt.common.configuration.filechange.ConfigFileObserver;
import org.onap.ccsdk.features.sdnr.wt.common.configuration.filechange.IConfigChangedListener;
import org.onap.ccsdk.features.sdnr.wt.common.configuration.subtypes.Section;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Representation of configuration file with section.<br>
 * A root section is used for parameters, not assigned to a specific section.<br>
 * The definitions of the configuration are attributes of a java class<br>
 */
public class ConfigurationFileRepresentation implements IConfigChangedListener {

    // constants
    private static final Logger LOG = LoggerFactory.getLogger(ConfigurationFileRepresentation.class);

    private static final long FILE_POLL_INTERVAL_MS = 1000;
    private static final String SECTIONNAME_ROOT = "";
    private static final String LR = "\n";
    private static final String EMPTY = "";
    // end of constants

    // variables
    /** Related configuration file **/
    private final File mFile;
    /** Monitor changes of file **/
    private final ConfigFileObserver fileObserver;
    /** List of sections **/
    private final HashMap<String, Section> sections;
    // end of variables

    // constructors
    public ConfigurationFileRepresentation(File f) {

        this.mFile = f;
        this.sections = new HashMap<String, Section>();
        try {
            if (!this.mFile.exists()) {
                if (!this.mFile.createNewFile()) {
                    LOG.error("Can not create file {}", f.getAbsolutePath());
                }
                this.mFile.setReadable(true, false);
                this.mFile.setWritable(true, false);
            }
            reLoad();

        } catch (IOException e) {
            LOG.error("Problem loading config file {} : {}", f.getAbsolutePath(), e.getMessage());
        }
        this.fileObserver = new ConfigFileObserver(f.getAbsolutePath(), FILE_POLL_INTERVAL_MS);
        this.fileObserver.start();
        this.fileObserver.registerConfigChangedListener(this);
    }

    public ConfigurationFileRepresentation(String configurationfile) {
        this(new File(configurationfile));
    }
    // end of constructors

    // getters and setters
    public synchronized Optional<Section> getSection(String name) {
        return Optional.ofNullable(sections.get(name));
    }
    // end of getters and setters

    // private methods
    private synchronized void reLoad() {
        sections.clear();
        addSection(SECTIONNAME_ROOT);
        load();
    }

    private synchronized void load() {
        LOG.debug("loading file {}", getMFileName());
        String curSectionName = SECTIONNAME_ROOT;
        Optional<Section> sectionOptional = this.getSection(curSectionName);
        Section curSection = sectionOptional.isPresent() ? sectionOptional.get() : this.addSection(curSectionName);
        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader(this.mFile));
            for (String line; (line = br.readLine()) != null;) {
                line = line.trim();
                if (line.isEmpty()) {
                    continue;
                }
                if (line.startsWith("[") && line.endsWith("]")) {
                    curSectionName = line.substring(1, line.length() - 1);
                    curSection = this.addSection(curSectionName);
                } else {
                    curSection.addLine(line);
                }
            }

        } catch (Exception e) {
            LOG.info("Problem loading configuration file. {} {}", getMFileName(), e);
        } finally {
            try {
                if (br != null) {
                    br.close();
                }
            } catch (IOException e) {
            }
        }
        LOG.debug("finished loading file");
        LOG.debug("start parsing sections");
        for (Section section : this.sections.values()) {
            section.parseLines();
        }
        LOG.debug("finished parsing " + this.sections.size() + " sections");
    }

    private String getMFileName() {
        return mFile.getAbsolutePath();
    }

    // end of private methods

    // public methods
    public synchronized Section addSection(String name) {
        if (this.sections.containsKey(name)) {
            return this.sections.get(name);
        }
        Section s = new Section(name);
        this.sections.put(name, s);
        return s;
    }

    public synchronized void save() {
        LOG.debug("Write configuration to {}", getMFileName());
        try (BufferedWriter bw = new BufferedWriter(new FileWriter(this.mFile, false))) {
            for (Section section : this.sections.values()) {
                if (section.hasValues()) {
                    bw.write(String.join(LR, section.toLines()) + LR + LR);
                }
            }
            bw.close();
        } catch (Exception e) {
            LOG.warn("problem saving value: " + e.getMessage());
        }
    }

    public void registerConfigChangedListener(IConfigChangedListener l) {
        this.fileObserver.registerConfigChangedListener(l);
    }

    public void unregisterConfigChangedListener(IConfigChangedListener l) {
        this.fileObserver.unregisterConfigChangedListener(l);
    }

    @Override
    public void onConfigChanged() {
        LOG.debug("Reload on change {}", getMFileName());
        reLoad();
    }

    @Override
    public String toString() {
        return "ConfigurationFileRepresentation [mFile=" + mFile + ", sections=" + sections + "]";
    }

    @Override
    protected void finalize() throws Throwable {
        if (this.fileObserver != null) {
            this.fileObserver.interrupt();
        }
        super.finalize();
    }

    /*
     * Property access set/get
     */
    public synchronized void setProperty(String section, String key, Object value) {
        Optional<Section> os = this.getSection(section);
        if (os.isPresent()) {
            os.get().setProperty(key, value == null ? "null" : value.toString());
            save();
        } else {
            LOG.info("Unknown configuration section {}", section);
        }
    }

    public synchronized String getProperty(String section, String propertyKey) {
        Optional<Section> os = this.getSection(section);
        if (os.isPresent()) {
            return os.get().getProperty(propertyKey);
        } else {
            LOG.debug("Unknown configuration section {}", section);
            return EMPTY;
        }
    }

    public synchronized Optional<Long> getPropertyLong(String section, String propertyKey) {
        Optional<Section> os = this.getSection(section);
        if (os.isPresent()) {
            return os.get().getLong(propertyKey);
        } else {
            LOG.debug("Unknown configuration section {}", section);
            return Optional.empty();
        }
    }

    public synchronized boolean isPropertyAvailable(String section, String propertyKey) {
        Optional<Section> s = this.getSection(section);
        return s.isPresent() && s.get().hasKey(propertyKey);
    }

    public synchronized void setPropertyIfNotAvailable(String section, String propertyKey, Object propertyValue) {
        if (!isPropertyAvailable(section, propertyKey)) {
            setProperty(section, propertyKey, propertyValue.toString());
        }
    }

    public synchronized boolean getPropertyBoolean(String section, String propertyKey) {
        return getProperty(section, propertyKey).equalsIgnoreCase("true");
    }
    // end of public methods

}