a0e21be010df80ead7bd646f81beb37f6d5b7dde
[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.common.configuration.subtypes;
19
20 import java.util.ArrayList;
21 import java.util.LinkedHashMap;
22 import java.util.List;
23 import java.util.Map.Entry;
24 import java.util.Optional;
25
26 import org.onap.ccsdk.features.sdnr.wt.common.configuration.exception.ConversionException;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 public class Section {
31
32     private static final Logger LOG = LoggerFactory.getLogger(Section.class);
33     private static final String DELIMITER = "=";
34     private static final String COMMENTCHARS[] = {"#", ";"};
35
36     private final String name;
37     private final List<String> rawLines;
38     private final LinkedHashMap<String, SectionValue> values;
39
40     public Section(String name) {
41         LOG.debug("new section created: '{}'", name);
42         this.name = name;
43         this.rawLines = new ArrayList<>();
44         this.values = new LinkedHashMap<>();
45     }
46
47     public void addLine(String line) {
48         LOG.trace("adding raw line:" + line);
49         this.rawLines.add(line);
50     }
51
52     public String getProperty(String key) {
53         return this.getProperty(key, null);
54     }
55
56     public String getProperty(String key, String defValue) {
57         if (values.containsKey(key)) {
58             return values.get(key).getValue();
59         }
60         return defValue;
61     }
62
63     public String getName() {
64         return name;
65     }
66
67     public void setProperty(String key, String value) {
68         boolean isuncommented = this.isCommentLine(key);
69         if (isuncommented) {
70             key = key.substring(1);
71         }
72         if (this.values.containsKey(key)) {
73                 this.values.get(key).setValue(value).setIsUncommented(isuncommented);
74         } else {
75             this.values.put(key, new SectionValue(value,isuncommented));
76         }
77     }
78
79     public void parseLines() {
80         this.values.clear();
81         List<String> commentsForValue = new ArrayList<>();
82         boolean uncommented = false;
83         for (String line : rawLines) {
84
85             if (this.isCommentLine(line)) {
86                 if (!line.contains(DELIMITER)) {
87                     commentsForValue.add(line);
88                     continue;
89                 } else {
90                     uncommented = true;
91                     line = line.substring(1);
92                 }
93             }
94             if (!line.contains(DELIMITER)) {
95                 continue;
96             }
97             String hlp[] = line.split(DELIMITER);
98             if (hlp.length > 1) {
99                 String key = hlp[0];
100                 String value =
101                         line.length() > (key + DELIMITER).length() ? line.substring((key + DELIMITER).length())
102                                 : "";
103                 if (this.values.containsKey(key)) {
104                     this.values.get(key).setValue(value);
105                 } else {
106                     this.values.put(key, new SectionValue(value, commentsForValue, uncommented));
107                     commentsForValue = new ArrayList<>();
108                 }
109             } else {
110                 LOG.warn("ignoring unknown formatted line:" + line);
111             }
112             uncommented = false;
113         }
114     }
115
116     private boolean isCommentLine(String line) {
117         for (String c : COMMENTCHARS) {
118             if (line.startsWith(c)) {
119                 return true;
120             }
121         }
122         return false;
123     }
124
125     public String[] toLines() {
126         List<String> lines = new ArrayList<>();
127         if (!this.name.isEmpty()) {
128             lines.add("[" + this.name + "]");
129         }
130         for (Entry<String, SectionValue> entry : this.values.entrySet()) {
131                 SectionValue sectionValue = entry.getValue();
132             if (sectionValue.getComments().size() > 0) {
133                 for (String comment : sectionValue.getComments()) {
134                     lines.add(comment);
135                 }
136             }
137             lines.add((sectionValue.isUncommented() ? COMMENTCHARS[0] : "") + entry.getKey() + DELIMITER
138                     + sectionValue.getValue());
139         }
140         String[] alines = new String[lines.size()];
141         return lines.toArray(alines);
142     }
143
144     public String getString(String key, String def) {
145         return this.getProperty(key, def);
146     }
147
148     public boolean getBoolean(String key, boolean def) throws ConversionException {
149         String v = this.getProperty(key);
150         if (v == null || v.isEmpty()) {
151             return def;
152         }
153         if (v.equals("true")) {
154             return true;
155         }
156         if (v.equals("false")) {
157             return false;
158         }
159         throw new ConversionException("invalid value for key " + key);
160     }
161
162     public int getInt(String key, int def) throws ConversionException {
163         String v = this.getProperty(key);
164         if (v == null || v.isEmpty()) {
165             return def;
166         }
167         try {
168             return Integer.parseInt(v);
169         } catch (NumberFormatException e) {
170             throw new ConversionException(e.getMessage());
171         }
172     }
173
174     public Optional<Long> getLong(String key) {
175         String v = this.getProperty(key);
176                 try {
177                         return Optional.of(Long.parseLong(v));
178                 } catch (NumberFormatException e) {
179                 }
180         return Optional.empty();
181     }
182
183     public boolean hasValues() {
184         return this.values.size() > 0;
185     }
186
187     public boolean hasKey(String key) {
188         return this.values.containsKey(key);
189     }
190
191         @Override
192         public String toString() {
193                 return "Section [name=" + name + ", rawLines=" + rawLines + ", values=" + values + "]";
194         }
195
196 }