From b942c2091cacfe09e503e445c7a15b77080ab357 Mon Sep 17 00:00:00 2001 From: Ganesh Chandrasekaran Date: Fri, 15 Jun 2018 10:17:06 +0900 Subject: CCSDK PropNode can read JSON and put to ctx Issue-ID: CCSDK-304 Change-Id: I510fe73c3eedc49071e386689090104a761a03a6 CCSDK PropertyNode can now read a JSON file and put to ctx memory Change-Id: I440ae043c020a08cd869df587916799dd7e3aeea CCSDK PropertyNode can now read a JSON file and put to ctx memory Issue-ID: CCSDK-304 Change-Id: Ic06a89c2327d07b31a1f45b9cc57783d8faf370a Signed-off-by: Ganesh Chandrasekaran --- .../onap/ccsdk/sli/plugins/prop/JsonParser.java | 94 ++++++++++++++++++++++ .../onap/ccsdk/sli/plugins/prop/Parameters.java | 27 +++++++ .../ccsdk/sli/plugins/prop/PropertiesNode.java | 64 +++++++++++---- 3 files changed, 171 insertions(+), 14 deletions(-) create mode 100644 properties-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/prop/JsonParser.java create mode 100644 properties-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/prop/Parameters.java (limited to 'properties-node/provider/src/main') diff --git a/properties-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/prop/JsonParser.java b/properties-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/prop/JsonParser.java new file mode 100644 index 00000000..89243a05 --- /dev/null +++ b/properties-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/prop/JsonParser.java @@ -0,0 +1,94 @@ +/*- + * ============LICENSE_START======================================================= + * openECOMP : SDN-C + * ================================================================================ + * Copyright (C) 2017 AT&T 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.sli.plugins.prop; + +import org.codehaus.jettison.json.JSONArray; +import org.codehaus.jettison.json.JSONException; +import org.codehaus.jettison.json.JSONObject; +import org.onap.ccsdk.sli.core.sli.SvcLogicException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; + +import static com.google.common.base.Preconditions.checkNotNull; + +public final class JsonParser { + + private static final Logger log = LoggerFactory.getLogger(JsonParser.class); + + private JsonParser() { + // Preventing instantiation of the same. + } + + @SuppressWarnings("unchecked") + public static Map convertToProperties(String s) + throws SvcLogicException { + + checkNotNull(s, "Input should not be null."); + + try { + JSONObject json = new JSONObject(s); + Map wm = new HashMap<>(); + Iterator ii = json.keys(); + while (ii.hasNext()) { + String key1 = ii.next(); + wm.put(key1, json.get(key1)); + } + + Map mm = new HashMap<>(); + + while (!wm.isEmpty()) + for (String key : new ArrayList<>(wm.keySet())) { + Object o = wm.get(key); + wm.remove(key); + + if (o instanceof Boolean || o instanceof Number || o instanceof String) { + mm.put(key, o.toString()); + + log.info("Added property: {} : {}", key, o.toString()); + } else if (o instanceof JSONObject) { + JSONObject jo = (JSONObject) o; + Iterator i = jo.keys(); + while (i.hasNext()) { + String key1 = i.next(); + wm.put(key + "." + key1, jo.get(key1)); + } + } else if (o instanceof JSONArray) { + JSONArray ja = (JSONArray) o; + mm.put(key + "_length", String.valueOf(ja.length())); + + log.info("Added property: {}_length: {}", key, String.valueOf(ja.length())); + + for (int i = 0; i < ja.length(); i++) + wm.put(key + '[' + i + ']', ja.get(i)); + } + } + return mm; + } catch (JSONException e) { + throw new SvcLogicException("Unable to convert JSON to properties " + e.getLocalizedMessage(), e); + } + } +} diff --git a/properties-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/prop/Parameters.java b/properties-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/prop/Parameters.java new file mode 100644 index 00000000..e15f0822 --- /dev/null +++ b/properties-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/prop/Parameters.java @@ -0,0 +1,27 @@ +/*- + * ============LICENSE_START======================================================= + * openECOMP : SDN-C + * ================================================================================ + * Copyright (C) 2017 AT&T 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.sli.plugins.prop; + +public class Parameters { + public String fileName; + public String contextPrefix; +} diff --git a/properties-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/prop/PropertiesNode.java b/properties-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/prop/PropertiesNode.java index b4886d55..63fdeded 100644 --- a/properties-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/prop/PropertiesNode.java +++ b/properties-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/prop/PropertiesNode.java @@ -21,6 +21,7 @@ package org.onap.ccsdk.sli.plugins.prop; +import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; @@ -38,27 +39,62 @@ public class PropertiesNode implements SvcLogicJavaPlugin { private static final Logger log = LoggerFactory.getLogger(PropertiesNode.class); public void readProperties(Map paramMap, SvcLogicContext ctx) throws SvcLogicException { - String fileName = parseParam(paramMap, "fileName", true, null); - String contextPrefix = parseParam(paramMap, "contextPrefix", false, null); - + Parameters param = getParameters(paramMap); + Properties prop = new Properties(); try { - Properties pp = new Properties(); - InputStream in = new FileInputStream(fileName); - pp.load(in); - for (Object key : pp.keySet()) { - String pfx = contextPrefix != null ? contextPrefix + '.' : ""; - String name = (String) key; - String value = pp.getProperty(name); - if (value != null && value.trim().length() > 0) { - ctx.setAttribute(pfx + name, value.trim()); - log.info("+++ " + pfx + name + ": [" + value + "]"); + File file = new File(param.fileName); + InputStream in = new FileInputStream(file); + Map mm = null; + String pfx = param.contextPrefix != null ? param.contextPrefix + '.' : ""; + if ("json".equalsIgnoreCase(getFileExtension(param.fileName))){ + byte[] data = new byte[(int) file.length()]; + in.read(data); + in.close(); + String str = new String(data, "UTF-8"); + mm = JsonParser.convertToProperties(str); + } else { + prop.load(in); + for (Object key : prop.keySet()) { + String name = (String) key; + String value = prop.getProperty(name); + if (value != null && value.trim().length() > 0) { + ctx.setAttribute(pfx + name, value.trim()); + log.info("+++ " + pfx + name + ": [" + value + "]"); + } + } + } + if (mm != null){ + for (Map.Entry entry : mm.entrySet()){ + ctx.setAttribute(pfx + entry.getKey(), entry.getValue()); + log.info("+++ " + pfx + entry.getKey() + ": [" + entry.getValue() + "]"); } } + } catch (IOException e) { - throw new SvcLogicException("Cannot read property file: " + fileName + ": " + e.getMessage(), e); + throw new SvcLogicException("Cannot read property file: " + param.fileName + ": " + e.getMessage(), e); } } + /* Getting extension has to do the following + * "" --> "" + * "name" --> "" + * "name.txt" --> "txt" + * ".htpasswd" --> "" + * "name.with.many.dots.myext" --> "myext" + */ + private static String getFileExtension(String fileName) { + if(fileName.lastIndexOf(".") != -1 && fileName.lastIndexOf(".") != 0) + return fileName.substring(fileName.lastIndexOf(".")+1); + else return ""; + } + + protected Parameters getParameters(Map paramMap) throws SvcLogicException { + Parameters p = new Parameters(); + p.fileName = parseParam(paramMap, "fileName", true, null); + p.contextPrefix = parseParam(paramMap, "contextPrefix", false, null); + return p; + } + private String parseParam(Map paramMap, String name, boolean required, String def) throws SvcLogicException { String s = paramMap.get(name); -- cgit 1.2.3-korg