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
10 * http://www.apache.org/licenses/LICENSE-2.0
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
16 * ============LICENSE_END==========================================================================
17 ******************************************************************************/
18 package org.onap.ccsdk.features.sdnr.wt.common.configuration.subtypes;
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 import java.util.regex.Matcher;
26 import java.util.regex.Pattern;
28 import org.onap.ccsdk.features.sdnr.wt.common.configuration.exception.ConversionException;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 public class Section {
34 private static final Logger LOG = LoggerFactory.getLogger(Section.class);
35 private static final String DELIMITER = "=";
36 private static final String COMMENTCHARS[] = {"#", ";"};
38 private final String name;
39 private final List<String> rawLines;
40 private final LinkedHashMap<String, SectionValue> values;
42 public Section(String name) {
43 LOG.debug("new section created: '{}'", name);
45 this.rawLines = new ArrayList<>();
46 this.values = new LinkedHashMap<>();
49 public void addLine(String line) {
50 LOG.trace("adding raw line:" + line);
51 this.rawLines.add(line);
54 public String getProperty(String key) {
55 return this.getProperty(key, "");
58 public String getProperty(final String key, final String defValue) {
59 String value=defValue;
60 LOG.debug("try to get property for {} with def {}",key,defValue);
61 if (values.containsKey(key)) {
62 value = values.get(key).getValue();
65 if (value != null && value.contains("${")) {
67 LOG.debug("try to find env var(s) for {}",value);
68 final String regex = "(\\$\\{[A-Z]+\\})";
69 final Pattern pattern = Pattern.compile(regex);
70 final Matcher matcher = pattern.matcher(value);
71 String tmp=new String(value);
72 while(matcher.find() && matcher.groupCount()>0) {
73 final String mkey = matcher.group(1);
76 LOG.debug("match found for v={} and env key={}",tmp,mkey);
77 String env=System.getenv(mkey.substring(2,mkey.length()-1));
78 tmp = tmp.replace(mkey, env==null?"":env );
79 } catch (SecurityException e) {
80 LOG.warn("unable to read env {}: {}", value, e);
89 public String getName() {
93 public void setProperty(String key, String value) {
94 boolean isuncommented = this.isCommentLine(key);
96 key = key.substring(1);
98 if (this.values.containsKey(key)) {
99 this.values.get(key).setValue(value).setIsUncommented(isuncommented);
101 this.values.put(key, new SectionValue(value,isuncommented));
105 public void parseLines() {
107 List<String> commentsForValue = new ArrayList<>();
108 boolean uncommented = false;
109 for (String line : rawLines) {
111 if (this.isCommentLine(line)) {
112 if (!line.contains(DELIMITER)) {
113 commentsForValue.add(line);
117 line = line.substring(1);
120 if (!line.contains(DELIMITER)) {
123 String hlp[] = line.split(DELIMITER);
124 if (hlp.length > 1) {
127 line.length() > (key + DELIMITER).length() ? line.substring((key + DELIMITER).length())
129 if (this.values.containsKey(key)) {
130 this.values.get(key).setValue(value);
132 this.values.put(key, new SectionValue(value, commentsForValue, uncommented));
133 commentsForValue = new ArrayList<>();
136 LOG.warn("ignoring unknown formatted line:" + line);
142 private boolean isCommentLine(String line) {
143 for (String c : COMMENTCHARS) {
144 if (line.startsWith(c)) {
151 public String[] toLines() {
152 List<String> lines = new ArrayList<>();
153 if (!this.name.isEmpty()) {
154 lines.add("[" + this.name + "]");
156 for (Entry<String, SectionValue> entry : this.values.entrySet()) {
157 SectionValue sectionValue = entry.getValue();
158 if (sectionValue.getComments().size() > 0) {
159 for (String comment : sectionValue.getComments()) {
163 lines.add((sectionValue.isUncommented() ? COMMENTCHARS[0] : "") + entry.getKey() + DELIMITER
164 + sectionValue.getValue());
166 String[] alines = new String[lines.size()];
167 return lines.toArray(alines);
170 public String getString(String key, String def) {
171 return this.getProperty(key, def);
174 public boolean getBoolean(String key, boolean def) throws ConversionException {
175 String v = this.getProperty(key);
176 if (v == null || v.isEmpty()) {
179 if (v.equals("true")) {
182 if (v.equals("false")) {
185 throw new ConversionException("invalid value for key " + key);
188 public int getInt(String key, int def) throws ConversionException {
189 String v = this.getProperty(key);
190 if (v == null || v.isEmpty()) {
194 return Integer.parseInt(v);
195 } catch (NumberFormatException e) {
196 throw new ConversionException(e.getMessage());
200 public Optional<Long> getLong(String key) {
201 String v = this.getProperty(key);
203 return Optional.of(Long.parseLong(v));
204 } catch (NumberFormatException e) {
206 return Optional.empty();
209 public boolean hasValues() {
210 return this.values.size() > 0;
213 public boolean hasKey(String key) {
214 return this.values.containsKey(key);
218 public String toString() {
219 return "Section [name=" + name + ", rawLines=" + rawLines + ", values=" + values + "]";