Assign image keyname and pubkey at vnf level
[ccsdk/apps.git] / sdnr / wireless-transport / code-Carbon-SR1 / apps / devicemanager / impl / src / main / java / org / opendaylight / mwtn / base / internalTypes / IniConfigurationFile.java
1 package org.opendaylight.mwtn.base.internalTypes;
2
3 import java.io.BufferedReader;
4 import java.io.BufferedWriter;
5 import java.io.File;
6 import java.io.FileReader;
7 import java.io.FileWriter;
8 import java.io.IOException;
9 import java.util.ArrayList;
10 import java.util.LinkedHashMap;
11 import java.util.List;
12 import java.util.Map.Entry;
13
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
16
17 public class IniConfigurationFile {
18
19         private static final Logger LOG = LoggerFactory.getLogger(IniConfigurationFile.class);
20
21         public static class ConfigurationException extends Exception {
22                 /**
23                  *
24                  */
25                 private static final long serialVersionUID = 733061908616404383L;
26
27                 public ConfigurationException(String m) {
28                         super(m);
29                 }
30         }
31
32         public static class ConversionException extends Exception {
33                 /**
34                  *
35                  */
36                 private static final long serialVersionUID = 5179891576029923079L;
37
38                 public ConversionException(String m) {
39                         super(m);
40                 }
41         }
42
43         public static final String SECTIONNAME_ROOT = "";
44         public static final String DELIMITER = "=";
45
46         private static class SectionValue {
47                 public String Value;
48                 public final List<String> Comments;
49                 public boolean IsUncommented;
50
51                 public SectionValue(String value) {
52                         this(value, new ArrayList<String>(), false);
53                 }
54
55                 public SectionValue(String value, List<String> commentsForValue, boolean isuncommented) {
56                         this.Comments = commentsForValue;
57                         this.Value = value;
58                         this.IsUncommented = isuncommented;
59                 }
60         }
61
62         public static class Section {
63                 public final String Name;
64                 public final List<String> rawLines;
65                 public final LinkedHashMap<String, SectionValue> values;
66
67                 public Section(String name) {
68                         LOG.debug("new section created:" + name);
69                         this.Name = name;
70                         this.rawLines = new ArrayList<String>();
71                         this.values = new LinkedHashMap<String, SectionValue>();
72                 }
73
74                 public void addLine(String line) {
75                         LOG.trace("adding raw line:" + line);
76                         this.rawLines.add(line);
77                 }
78
79                 public String getProperty(String key) {
80                         return this.getProperty(key, null);
81                 }
82
83                 public String getProperty(String key, String defValue) {
84                         if (values.containsKey(key))
85                                 return values.get(key).Value;
86                         return defValue;
87                 }
88
89                 public void setProperty(String key, String value) {
90                         boolean isuncommented = this.isCommentLine(key);
91                         if(isuncommented)
92                                 key=key.substring(1);
93                         if (this.values.containsKey(key)) {
94                                 this.values.get(key).Value = value;
95                                 this.values.get(key).IsUncommented = isuncommented;
96                         } else {
97                                 SectionValue sv = new SectionValue(value);
98                                 sv.IsUncommented = isuncommented;
99                                 this.values.put(key, sv);
100                         }
101                 }
102
103                 public void parseLines() {
104                         this.values.clear();
105                         List<String> commentsForValue = new ArrayList<String>();
106                         boolean uncommented = false;
107                         for (String line : rawLines) {
108
109                                 if (this.isCommentLine(line)) {
110                                         if (!line.contains(DELIMITER)) {
111                                                 commentsForValue.add(line);
112                                                 continue;
113                                         } else {
114                                                 uncommented = true;
115                                                 line = line.substring(1);
116                                         }
117                                 }
118                                 if (!line.contains(DELIMITER))
119                                         continue;
120                                 String hlp[] = line.split(DELIMITER);
121                                 if(hlp.length>1)
122                                 {
123                                         String key=hlp[0];
124                                         String value=line.length()>(key+DELIMITER).length()?line.substring((key+DELIMITER).length()):"";
125                                         if (this.values.containsKey(key))
126                                                 this.values.get(key).Value = value;
127                                         else {
128                                                 this.values.put(key, new SectionValue(value, commentsForValue, uncommented));
129                                                 commentsForValue = new ArrayList<String>();
130                                         }
131                                 }
132                                 else
133                                 {
134                                         LOG.warn("ignoring unknown formatted line:"+line);
135                                 }
136                                 uncommented = false;
137                         }
138                 }
139
140                 private boolean isCommentLine(String line) {
141                         for (String c : commentChars) {
142                                 if (line.startsWith(c))
143                                         return true;
144                         }
145                         return false;
146                 }
147
148                 public String[] toLines() {
149                         List<String> lines = new ArrayList<String>();
150                         if (!this.Name.isEmpty())
151                                 lines.add("[" + this.Name + "]");
152                         for (Entry<String, SectionValue> entry : this.values.entrySet()) {
153                                 if (entry.getValue().Comments.size() > 0) {
154                                         for (String comment : entry.getValue().Comments)
155                                                 lines.add(comment);
156                                 }
157                                 lines.add((entry.getValue().IsUncommented ? commentChars[0] : "") + entry.getKey() + DELIMITER
158                                                 + entry.getValue().Value);
159                         }
160                         String[] alines = new String[lines.size()];
161                         return lines.toArray(alines);
162                 }
163
164                 public String getString(String key, String def) {
165                         return this.getProperty(key, def);
166                 }
167
168                 public boolean getBoolean(String key, boolean def) throws ConversionException {
169                         String v = this.getProperty(key);
170                         if (v == null || v.isEmpty())
171                                 return def;
172                         if (v.equals("true"))
173                                 return true;
174                         if (v.equals("false"))
175                                 return false;
176                         throw new ConversionException("invalid value for key " + key);
177                 }
178
179                 public int getInt(String key, int def) throws ConversionException {
180                         String v = this.getProperty(key);
181                         if (v == null || v.isEmpty())
182                                 return def;
183                         try {
184                                 return Integer.parseInt(v);
185                         } catch (NumberFormatException e) {
186                                 throw new ConversionException(e.getMessage());
187                         }
188                 }
189
190                 public boolean hasValues() {
191                         return this.values.size()>0;
192                 }
193
194                 public boolean hasKey(String key) {
195                         return this.values.containsKey(key);
196                 }
197
198         }
199
200         private final File mFile;
201         private final List<Section> sections;
202         private static final String commentChars[] = { "#", ";" };
203
204         public IniConfigurationFile(File f) {
205                 this.mFile = f;
206                 this.sections = new ArrayList<Section>();
207                 this.sections.add(new Section(SECTIONNAME_ROOT));
208
209         }
210
211         public void load() throws ConfigurationException {
212                 String curSectionName = SECTIONNAME_ROOT;
213                 LOG.debug("loading file");
214                 BufferedReader br = null;
215                 try {
216                         br = new BufferedReader(new FileReader(this.mFile));
217                         for (String line; (line = br.readLine()) != null;) {
218                                 line = line.trim();
219                                 if (line.isEmpty())
220                                         continue;
221                                 if (line.startsWith("[") && line.endsWith("]")) {
222                                         curSectionName = line.substring(1, line.length() - 1);
223                                         this.addSection(curSectionName);
224                                 } else
225                                         this.getSection(curSectionName).addLine(line);
226                         }
227
228                 } catch (Exception e) {
229                         throw new ConfigurationException(e.getMessage());
230                 } finally {
231                         try {
232                                 if (br != null)
233                                         br.close();
234                         } catch (IOException e) {
235                         }
236                 }
237                 LOG.debug("finished loading file");
238                 LOG.debug("start parsing sections");
239                 for (Section section : this.sections)
240                         section.parseLines();
241                 LOG.debug("finished parsing " + this.sections.size() + " sections");
242         }
243
244         private Section getSection(String name) {
245                 for (Section s : this.sections) {
246                         if (s.Name.equals(name))
247                                 return s;
248                 }
249                 return this.addSection(name);
250
251         }
252
253         private Section addSection(String name) {
254
255                 Section s = new Section(name);
256                 this.sections.add(s);
257                 return s;
258         }
259
260         public void reLoad() throws ConfigurationException {
261                 this.sections.clear();
262                 this.sections.add(new Section(SECTIONNAME_ROOT));
263                 this.load();
264         }
265
266         public String getProperty(String key, String defValue) {
267                 Section s;
268                 if (key.contains(".")) {
269                         s = this.getSection(key.substring(0, key.indexOf(".")));
270                         key = key.substring(key.indexOf(".") + 1);
271                 } else
272                         s = this.getSection(SECTIONNAME_ROOT);
273
274                 String v = s.getProperty(key);
275                 if (v == null || v.isEmpty())
276                         return defValue;
277                 return v;
278         }
279
280         public void setProperty(String key, String value) {
281                 Section s;
282                 if (key.contains(".")) {
283                         s = this.getSection(key.substring(0, key.indexOf(".")));
284                         key = key.substring(key.indexOf(".") + 1);
285                 } else
286                         s = this.getSection(SECTIONNAME_ROOT);
287                 s.setProperty(key, value);
288         }
289
290         public int getProperty(String key, int defValue) throws ConversionException {
291                 Section s;
292                 if (key.contains(".")) {
293                         s = this.getSection(key.substring(0, key.indexOf(".")));
294                         key = key.substring(key.indexOf(".") + 1);
295                 } else
296                         s = this.getSection(SECTIONNAME_ROOT);
297
298                 return s.getInt(key, defValue);
299         }
300
301         public void setProperty(String key, int value) {
302                 Section s;
303                 if (key.contains(".")) {
304                         s = this.getSection(key.substring(0, key.indexOf(".")));
305                         key = key.substring(key.indexOf(".") + 1);
306                 } else
307                         s = this.getSection(SECTIONNAME_ROOT);
308                 s.setProperty(key, String.format("%d", value));
309         }
310
311         public boolean getProperty(String key, boolean defValue) throws ConversionException {
312                 Section s;
313                 if (key.contains(".")) {
314                         s = this.getSection(key.substring(0, key.indexOf(".")));
315                         key = key.substring(key.indexOf(".") + 1);
316                 } else
317                         s = this.getSection(SECTIONNAME_ROOT);
318
319                 return s.getBoolean(key, defValue);
320         }
321
322         public void setProperty(String key, boolean value) {
323                 Section s;
324                 if (key.contains(".")) {
325                         s = this.getSection(key.substring(0, key.indexOf(".")));
326                         key = key.substring(key.indexOf(".") + 1);
327                 } else
328                         s = this.getSection(SECTIONNAME_ROOT);
329                 s.setProperty(key, value ? "true" : "false");
330         }
331
332         public void setProperty(String key, Object value) {
333                 this.setProperty(key, value==null?"null":value.toString());
334         }
335
336         public void save() {
337                 final String LR = "\n";
338                 BufferedWriter bw;
339                 try {
340                         bw = new BufferedWriter(new FileWriter(this.mFile, false));
341                         for (Section section : this.sections) {
342                                 if(section.hasValues())
343                                         bw.write(String.join(LR, section.toLines()) + LR + LR);
344                         }
345                         bw.close();
346                 } catch (Exception e) {
347
348                 }
349
350         }
351
352         public Section subset(String section) {
353                 return this.getSection(section);
354         }
355
356 }