Merge "fix connection state machine"
[ccsdk/features.git] / sdnr / wt / common / src / main / java / org / onap / ccsdk / features / sdnr / wt / common / configuration / subtypes / Section.java
index 094da63..b6d277f 100644 (file)
@@ -1,20 +1,24 @@
-/*******************************************************************************
- * ============LICENSE_START========================================================================
- * ONAP : ccsdk feature sdnr wt
- * =================================================================================================
- * 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
+/*
+ * ============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
+ *     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==========================================================================
- ******************************************************************************/
+ * 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.subtypes;
 
 import java.util.ArrayList;
@@ -22,28 +26,59 @@ import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map.Entry;
 import java.util.Optional;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 import org.onap.ccsdk.features.sdnr.wt.common.configuration.exception.ConversionException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-
+/**
+ * 
+ * @author Michael Dürre, Herbert Eiselt
+ *
+ * subset of configuration identified by its name
+ */
 public class Section {
 
+       // constants
     private static final Logger LOG = LoggerFactory.getLogger(Section.class);
     private static final String DELIMITER = "=";
     private static final String COMMENTCHARS[] = {"#", ";"};
-
+    // end of constants
+    
+    // variables
     private final String name;
     private final List<String> rawLines;
     private final LinkedHashMap<String, SectionValue> values;
-
+    // end of variables
+    
+    // constructors
     public Section(String name) {
         LOG.debug("new section created: '{}'", name);
         this.name = name;
         this.rawLines = new ArrayList<>();
         this.values = new LinkedHashMap<>();
     }
+    //end of constructors
+    
+    // getters and setters
+    public String getName() {
+        return name;
+    }
+    // end of getters and setters
 
+    // private methods
+    private boolean isCommentLine(String line) {
+        for (String c : COMMENTCHARS) {
+            if (line.startsWith(c)) {
+                return true;
+            }
+        }
+        return false;
+    }
+    // end of private methods
+    
+    // public methods
     public void addLine(String line) {
         LOG.trace("adding raw line:" + line);
         this.rawLines.add(line);
@@ -53,16 +88,38 @@ public class Section {
         return this.getProperty(key, "");
     }
 
-    public String getProperty(String key, String defValue) {
-        if (values.containsKey(key)) {
-            return values.get(key).getValue();
-        }
-        return defValue;
-    }
-
-    public String getName() {
-        return name;
-    }
+       public String getProperty(final String key, final String defValue) {
+               String value=defValue;
+               LOG.debug("try to get property for {} with def {}",key,defValue);
+               if (values.containsKey(key)) {
+                       value = values.get(key).getValue();
+               }
+               //try to read env var
+               if (value != null && value.contains("${")) {
+                       
+                       LOG.debug("try to find env var(s) for {}",value);
+                       final String regex = "(\\$\\{[A-Z]+\\})";
+                       final Pattern pattern = Pattern.compile(regex);
+                       final Matcher matcher = pattern.matcher(value);
+                       String tmp=new String(value);
+                       while(matcher.find() && matcher.groupCount()>0) {
+                               final String mkey = matcher.group(1);
+                               if(mkey!=null) {
+                                       try {
+                                               LOG.debug("match found for v={} and env key={}",tmp,mkey);
+                                               String env=System.getenv(mkey.substring(2,mkey.length()-1));
+                                               tmp = tmp.replace(mkey, env==null?"":env );     
+                                       } catch (SecurityException e) {
+                                               LOG.warn("unable to read env {}: {}", value, e);
+                                       }
+                               }
+                       }
+                       value=tmp;
+               }
+               return value;
+       }
+
+    
 
     public void setProperty(String key, String value) {
         boolean isuncommented = this.isCommentLine(key);
@@ -113,14 +170,7 @@ public class Section {
         }
     }
 
-    private boolean isCommentLine(String line) {
-        for (String c : COMMENTCHARS) {
-            if (line.startsWith(c)) {
-                return true;
-            }
-        }
-        return false;
-    }
+    
 
     public String[] toLines() {
         List<String> lines = new ArrayList<>();
@@ -192,5 +242,6 @@ public class Section {
     public String toString() {
         return "Section [name=" + name + ", rawLines=" + rawLines + ", values=" + values + "]";
     }
+    // end of public methods
 
 }