aboutsummaryrefslogtreecommitdiffstats
path: root/sdnr/wt/common/src
diff options
context:
space:
mode:
Diffstat (limited to 'sdnr/wt/common/src')
-rw-r--r--sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/HtAssert.java2
-rw-r--r--sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/Resources.java253
-rw-r--r--sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/ResourcesFromDeviceManager.java340
-rw-r--r--sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/DatabaseClient.java10
-rw-r--r--sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/ExtRestClient.java128
-rw-r--r--sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/HtDatabaseClient.java22
-rw-r--r--sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/queries/BoolQueryBuilder.java10
-rw-r--r--sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/file/PomFile.java15
-rw-r--r--sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/http/BaseHTTPClientFromDevicemanager.java327
-rw-r--r--sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/http/BaseServlet.java375
-rw-r--r--sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/util/ResourceFileLoader.java (renamed from sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/InvalidProtocolException.java)18
-rw-r--r--sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/util/StackTrace.java (renamed from sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/http/BaseHTTPResponseFromDevicemanager.java)39
-rw-r--r--sdnr/wt/common/src/test/java/org/onap/ccsdk/features/sdnr/wt/common/test/TestConfig.java4
-rw-r--r--sdnr/wt/common/src/test/java/org/onap/ccsdk/features/sdnr/wt/common/test/TestPomfile.java68
14 files changed, 595 insertions, 1016 deletions
diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/HtAssert.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/HtAssert.java
index 873b5c947..133080f28 100644
--- a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/HtAssert.java
+++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/HtAssert.java
@@ -27,7 +27,7 @@ public class HtAssert {
public static void nonnull(Object ... oList) {
for (Object o : oList) {
if (o == null) {
- throw new IllegalArgumentException("Null not allowed here.");
+ throw new NullPointerException("Null not allowed here.");
}
}
};
diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/Resources.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/Resources.java
index fe608a503..3261b832a 100644
--- a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/Resources.java
+++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/Resources.java
@@ -18,17 +18,10 @@
package org.onap.ccsdk.features.sdnr.wt.common;
import java.io.BufferedReader;
-import java.io.File;
-import java.io.FileFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
-import java.net.MalformedURLException;
-import java.net.URISyntaxException;
import java.net.URL;
-import java.util.ArrayList;
-import java.util.Enumeration;
-import java.util.List;
import org.osgi.framework.Bundle;
import org.osgi.framework.FrameworkUtil;
import org.slf4j.Logger;
@@ -38,8 +31,6 @@ public class Resources {
private static final Logger LOG = LoggerFactory.getLogger(Resources.class);
- private static final String RESSOURCEROOT = "src/main/resources";
-
private static URL getFileURL(Class<?> cls,String resFile) {
Bundle b = FrameworkUtil.getBundle(cls);
URL u = null;
@@ -54,22 +45,6 @@ public class Resources {
return u;
}
- private static File getFile(Bundle b,String resFile) {
- File f = null;
- LOG.debug("try to get file {}", resFile);
- if (b == null) {
- LOG.warn("cannot load bundle resources");
- f = new File(RESSOURCEROOT + resFile);
- } else {
- try {
- f = new File(b.getEntry(resFile).toURI());
- } catch (URISyntaxException e) {
- LOG.warn("Con not load file: {}",e.getMessage());
- }
- }
- return f;
- }
-
private static String readFile(final URL u) throws IOException {
return readFile(u.openStream());
}
@@ -88,95 +63,6 @@ public class Resources {
return sb.toString();
}
- private static List<URL> getFileURLs(Bundle b,String folder, final String filter, final boolean recursive)
- throws IOException {
-
- List<URL> list = new ArrayList<>();
- if (b == null) {
- FileFilter ff = pathname -> {
- if (pathname.isFile()) {
- return pathname.getName().contains(filter);
- } else {
- return true;
- }
- };
- File ffolder = getFile(b,folder);
- if (ffolder != null && ffolder.isDirectory()) {
- File[] files = ffolder.listFiles(ff);
- if (files != null && files.length > 0) {
- for (File f : files) {
- if (f.isFile()) {
- list.add(f.toURI().toURL());
- } else if (f.isDirectory() && recursive) {
- getFileURLsRecursive(f, ff, list);
- }
- }
- }
- }
- } else {
- getResourceURLsTreeRecurse(b, filter, b.getEntryPaths(folder), recursive, list);
- }
- return list;
- }
-
- private static void getFileURLsRecursive(File root, FileFilter ff, List<URL> list) throws MalformedURLException {
- if (root != null && root.isDirectory()) {
- File[] files = root.listFiles(ff);
- if (files != null && files.length > 0) {
- for (File f : files) {
- if (f.isFile()) {
- list.add(f.toURI().toURL());
- } else if (f.isDirectory()) {
- getFileURLsRecursive(f, ff, list);
- }
- }
- }
- }
-
- }
-
- private static void getResourceURLsTreeRecurse(Bundle b, String filter, Enumeration<String> resource,
- boolean recursive, List<URL> outp) throws IOException {
- while (resource.hasMoreElements()) {
- String name = resource.nextElement();
- Enumeration<String> list = b.getEntryPaths(name);
- if (list != null) {
- if (recursive) {
- getResourceURLsTreeRecurse(b, filter, list, recursive, outp);
- }
- } else {
- // Read
- if (name.contains(filter)) {
- LOG.debug("add {} to list", name);
- outp.add(b.getEntry(name));
- } else {
- LOG.debug("filtered out {}", name);
- }
- }
- }
- }
-
-// public static List<JSONObject> getJSONFiles(Bundle b,String folder, boolean recursive) {
-// List<JSONObject> list = new ArrayList<>();
-// List<URL> urls;
-// try {
-// urls = getFileURLs(b,folder, ".json", recursive);
-// LOG.debug("found {} files", urls.size());
-// } catch (IOException e1) {
-// urls = new ArrayList<>();
-// LOG.warn("failed to get urls from resfolder {} : {}", folder, e1.getMessage());
-// }
-// for (URL u : urls) {
-// LOG.debug("try to parse " + u.toString());
-// try {
-// JSONObject o = new JSONObject(readFile(u));
-// list.add(o);
-// } catch (JSONException | IOException e) {
-// LOG.warn("problem reading/parsing file {} : {}", u, e.getMessage());
-// }
-// }
-// return list;
-// }
public static String getFileContent( Class<?> cls, String resFile) {
LOG.debug("loading file {} from res", resFile);
URL u = getFileURL(cls,resFile);
@@ -193,98 +79,6 @@ public class Resources {
return s;
}
-// public static JSONObject getJSONFile(Class<?> cls,String resFile) {
-// LOG.debug("loading json file {} from res", resFile);
-// JSONObject o = null;
-// try {
-// // parse to jsonobject
-// o = new JSONObject(getFileContent(cls,resFile));
-// } catch (Exception e) {
-// LOG.warn("problem reading/parsing file: {}", e.getMessage());
-// }
-// return o;
-// }
-
- /**
- * Used for reading plugins from resource files /elasticsearch/plugins/head
- * /etc/elasticsearch-plugins /elasticsearch/plugins
- *
- * @param resFolder resource folder pointing to the related files
- * @param dstFolder destination
- * @param rootDirToRemove part from full path to remove
- * @return true if files could be extracted
- */
-// public static boolean copyFolderInto(Bundle b,Class<?> cls,String resFolder, String dstFolder, String rootDirToRemove) {
-//
-// Enumeration<URL> urls = null;
-// if (b == null) {
-// LOG.info("Running in file text.");
-// urls = getResourceFolderFiles(cls,resFolder);
-// } else {
-// urls = b.findEntries(resFolder, "*", true);
-// }
-//
-// boolean success = true;
-// URL srcUrl;
-// String srcFilename;
-// String dstFilename;
-// while (urls.hasMoreElements()) {
-// srcUrl = urls.nextElement();
-// srcFilename = srcUrl.getFile();
-//
-// if (srcFilename.endsWith("/")) {
-// LOG.debug("Skip directory: {}", srcFilename);
-// continue;
-// }
-//
-// LOG.debug("try to copy res {} to {}", srcFilename, dstFolder);
-// if (rootDirToRemove != null) {
-// srcFilename =
-// srcFilename.substring(srcFilename.indexOf(rootDirToRemove) + rootDirToRemove.length() + 1);
-// LOG.debug("dstfilename trimmed to {}", srcFilename);
-// }
-// dstFilename = dstFolder + "/" + srcFilename;
-// try {
-// if (!extractFileTo(srcUrl, new File(dstFilename))) {
-// success = false;
-// }
-// } catch (Exception e) {
-// LOG.warn("problem copying res {} to {}: {}", srcFilename, dstFilename, e.getMessage());
-// }
-// }
-//
-// return success;
-//
-// }
-
-// private static Enumeration<URL> getResourceFolderFiles(Class<?> cls,String folder) {
-// LOG.debug("Get resource: {}", folder);
-// Collection<URL> urlCollection = new ArrayList<>();
-// URL url = getUrlForRessource(cls,folder);
-// if(url==null) {
-// return Collections.enumeration(urlCollection);
-// }
-// String path = url.getPath();
-// File[] files = new File(path).listFiles();
-//
-// if (files != null) {
-// for (File f : files) {
-// try {
-// if (f.isDirectory()) {
-// urlCollection.addAll(Collections.list(getResourceFolderFiles(cls,folder + "/" + f.getName())));
-// } else {
-// urlCollection.add(f.toURI().toURL());
-// }
-// } catch (MalformedURLException e) {
-// LOG.error("Can not read ressources", e);
-// break;
-// }
-// }
-// }
-//
-// return Collections.enumeration(urlCollection);
-//
-// }
public static URL getUrlForRessource(Class<?> cls,String fileOrDirectory) {
//ClassLoader loader = Thread.currentThread().getContextClassLoader();
@@ -295,51 +89,4 @@ public class Resources {
}
return url;
}
-
-// public static boolean extractFileTo(Class<?> cls,String resFile, File oFile) {
-// if (oFile == null) {
-// return false;
-// }
-// LOG.debug("try to copy {} from res to {}", resFile, oFile.getAbsolutePath());
-// URL u = getFileURL(cls,resFile);
-// if (u == null) {
-// LOG.warn("cannot find resfile: {}", resFile);
-// return false;
-// }
-// return extractFileTo(u, oFile);
-// }
-//
-// public static boolean extractFileTo(URL u, File oFile) {
-//
-// if (oFile.isDirectory()) {
-// oFile.mkdirs();
-// return true;
-// } else {
-// oFile.getParentFile().mkdirs();
-// }
-//
-// if (!oFile.exists()) {
-// try {
-// oFile.createNewFile();
-// } catch (IOException e) {
-// LOG.warn("problem creating file {}: {}", oFile.getAbsoluteFile(), e.getMessage());
-// }
-// }
-// try (InputStream in = u.openStream(); OutputStream outStream = new FileOutputStream(oFile);) {
-//
-// int theInt;
-// while ((theInt = in.read()) >= 0) {
-// outStream.write(theInt);
-// }
-// in.close();
-// outStream.flush();
-// outStream.close();
-// LOG.debug("file written successfully");
-// } catch (IOException e) {
-// LOG.error("problem writing file: {}", e.getMessage());
-// return false;
-// }
-// return true;
-// }
-
}
diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/ResourcesFromDeviceManager.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/ResourcesFromDeviceManager.java
deleted file mode 100644
index 32a21a801..000000000
--- a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/ResourcesFromDeviceManager.java
+++ /dev/null
@@ -1,340 +0,0 @@
-/*******************************************************************************
- * ============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
- *
- * 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.features.sdnr.wt.common;
-
-import java.io.BufferedReader;
-import java.io.File;
-import java.io.FileFilter;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.io.OutputStream;
-import java.net.MalformedURLException;
-import java.net.URISyntaxException;
-import java.net.URL;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.Enumeration;
-import java.util.List;
-
-import org.json.JSONException;
-import org.json.JSONObject;
-import org.osgi.framework.Bundle;
-import org.osgi.framework.FrameworkUtil;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-public class ResourcesFromDeviceManager {
-
- private static final Logger LOG = LoggerFactory.getLogger(ResourcesFromDeviceManager.class);
-
- private static final String RESSOURCEROOT = "src/main/resources";
-
- private static URL getFileURL(String resFile) {
- Bundle b = FrameworkUtil.getBundle(ResourcesFromDeviceManager.class);
- URL u = null;
- LOG.debug("try to get file {}", resFile);
- if (b == null) {
- LOG.info("Load resource as file: {}", resFile);
- u = getUrlForRessource(resFile);
- } else {
- LOG.info("Load resource from bundle: {}", resFile);
- u = b.getEntry(resFile);
- }
- return u;
- }
-
- private static File getFile(String resFile) {
- Bundle b = FrameworkUtil.getBundle(ResourcesFromDeviceManager.class);
- File f = null;
- LOG.debug("try to get file {}", resFile);
- if (b == null) {
- LOG.warn("cannot load bundle resources");
- f = new File(RESSOURCEROOT + resFile);
- } else {
- try {
- f = new File(b.getEntry(resFile).toURI());
- } catch (URISyntaxException e) {
- LOG.warn("Con not load file: {}",e.getMessage());
- }
- }
- return f;
- }
-
- private static String readFile(final URL u) throws IOException {
- return readFile(u.openStream());
- }
-
- private static String readFile(final InputStream s) throws IOException {
- // read file
- BufferedReader in = new BufferedReader(new InputStreamReader(s));
- StringBuilder sb = new StringBuilder();
- String inputLine;
- while ((inputLine = in.readLine()) != null) {
- sb.append(inputLine);
- }
- in.close();
- s.close();
- return sb.toString();
- }
-
- public static List<URL> getFileURLs(String folder, final String filter, final boolean recursive)
- throws IOException {
- Bundle b = FrameworkUtil.getBundle(ResourcesFromDeviceManager.class);
- List<URL> list = new ArrayList<>();
- if (b == null) {
- FileFilter ff = pathname -> {
- if (pathname.isFile()) {
- return pathname.getName().contains(filter);
- } else {
- return true;
- }
- };
- File ffolder = getFile(folder);
- if (ffolder != null && ffolder.isDirectory()) {
- File[] files = ffolder.listFiles(ff);
- if (files != null && files.length > 0) {
- for (File f : files) {
- if (f.isFile()) {
- list.add(f.toURI().toURL());
- } else if (f.isDirectory() && recursive) {
- getFileURLsRecursive(f, ff, list);
- }
- }
- }
- }
- } else {
- getResourceURLsTreeRecurse(b, filter, b.getEntryPaths(folder), recursive, list);
- }
- return list;
- }
-
- private static void getFileURLsRecursive(File root, FileFilter ff, List<URL> list) throws MalformedURLException {
- if (root != null && root.isDirectory()) {
- File[] files = root.listFiles(ff);
- if (files != null && files.length > 0) {
- for (File f : files) {
- if (f.isFile()) {
- list.add(f.toURI().toURL());
- } else if (f.isDirectory()) {
- getFileURLsRecursive(f, ff, list);
- }
- }
- }
- }
-
- }
-
- private static void getResourceURLsTreeRecurse(Bundle b, String filter, Enumeration<String> resource,
- boolean recursive, List<URL> outp) throws IOException {
- while (resource.hasMoreElements()) {
- String name = resource.nextElement();
- Enumeration<String> list = b.getEntryPaths(name);
- if (list != null) {
- if (recursive) {
- getResourceURLsTreeRecurse(b, filter, list, recursive, outp);
- }
- } else {
- // Read
- if (name.contains(filter)) {
- LOG.debug("add {} to list", name);
- outp.add(b.getEntry(name));
- } else {
- LOG.debug("filtered out {}", name);
- }
- }
- }
- }
-
- public static List<JSONObject> getJSONFiles(String folder, boolean recursive) {
- List<JSONObject> list = new ArrayList<>();
- List<URL> urls;
- try {
- urls = getFileURLs(folder, ".json", recursive);
- LOG.debug("found {} files", urls.size());
- } catch (IOException e1) {
- urls = new ArrayList<>();
- LOG.warn("failed to get urls from resfolder {} : {}", folder, e1.getMessage());
- }
- for (URL u : urls) {
- LOG.debug("try to parse " + u.toString());
- try {
- JSONObject o = new JSONObject(readFile(u));
- list.add(o);
- } catch (JSONException | IOException e) {
- LOG.warn("problem reading/parsing file {} : {}", u, e.getMessage());
- }
- }
- return list;
- }
-
- public static JSONObject getJSONFile(String resFile) {
- LOG.debug("loading json file {} from res", resFile);
- URL u = getFileURL(resFile);
- if (u == null) {
- LOG.warn("cannot find resfile: {}", resFile);
- return null;
- }
- JSONObject o = null;
- try {
- // parse to jsonobject
- o = new JSONObject(readFile(u));
- } catch (Exception e) {
- LOG.warn("problem reading/parsing file: {}", e.getMessage());
- }
- return o;
- }
-
- /**
- * Used for reading plugins from resource files /elasticsearch/plugins/head
- * /etc/elasticsearch-plugins /elasticsearch/plugins
- *
- * @param resFolder resource folder pointing to the related files
- * @param dstFolder destination
- * @param rootDirToRemove part from full path to remove
- * @return true if files could be extracted
- */
- public static boolean copyFolderInto(String resFolder, String dstFolder, String rootDirToRemove) {
-
- Enumeration<URL> urls = null;
- Bundle b = FrameworkUtil.getBundle(ResourcesFromDeviceManager.class);
- if (b == null) {
- LOG.info("Running in file text.");
- urls = getResourceFolderFiles(resFolder);
- } else {
- urls = b.findEntries(resFolder, "*", true);
- }
-
- boolean success = true;
- URL srcUrl;
- String srcFilename;
- String dstFilename;
- while (urls.hasMoreElements()) {
- srcUrl = urls.nextElement();
- srcFilename = srcUrl.getFile();
-
- if (srcFilename.endsWith("/")) {
- LOG.debug("Skip directory: {}", srcFilename);
- continue;
- }
-
- LOG.debug("try to copy res {} to {}", srcFilename, dstFolder);
- if (rootDirToRemove != null) {
- srcFilename =
- srcFilename.substring(srcFilename.indexOf(rootDirToRemove) + rootDirToRemove.length() + 1);
- LOG.debug("dstfilename trimmed to {}", srcFilename);
- }
- dstFilename = dstFolder + "/" + srcFilename;
- try {
- if (!extractFileTo(srcUrl, new File(dstFilename))) {
- success = false;
- }
- } catch (Exception e) {
- LOG.warn("problem copying res {} to {}: {}", srcFilename, dstFilename, e.getMessage());
- }
- }
-
- return success;
-
- }
-
- private static Enumeration<URL> getResourceFolderFiles(String folder) {
- LOG.debug("Get resource: {}", folder);
- URL url = getUrlForRessource(folder);
- String path = url.getPath();
- File[] files = new File(path).listFiles();
- Collection<URL> urlCollection = new ArrayList<>();
-
- if (files != null) {
- for (File f : files) {
- try {
- if (f.isDirectory()) {
- urlCollection.addAll(Collections.list(getResourceFolderFiles(folder + "/" + f.getName())));
- } else {
- urlCollection.add(f.toURI().toURL());
- }
- } catch (MalformedURLException e) {
- LOG.error("Can not read ressources", e);
- break;
- }
- }
- }
-
- Enumeration<URL> urls = Collections.enumeration(urlCollection);
- return urls;
- }
-
- private static URL getUrlForRessource(String fileOrDirectory) {
- //ClassLoader loader = Thread.currentThread().getContextClassLoader();
- ClassLoader loader = ResourcesFromDeviceManager.class.getClassLoader();
- URL url = loader.getResource(fileOrDirectory);
- if(url==null && fileOrDirectory.startsWith("/")) {
- url = loader.getResource(fileOrDirectory.substring(1));
- }
- return url;
- }
-
- public static boolean extractFileTo(String resFile, File oFile) {
- if (oFile == null) {
- return false;
- }
- LOG.debug("try to copy {} from res to {}", resFile, oFile.getAbsolutePath());
- URL u = getFileURL(resFile);
- if (u == null) {
- LOG.warn("cannot find resfile: {}", resFile);
- return false;
- }
- return extractFileTo(u, oFile);
- }
-
- public static boolean extractFileTo(URL u, File oFile) {
-
- if (oFile.isDirectory()) {
- oFile.mkdirs();
- return true;
- } else {
- oFile.getParentFile().mkdirs();
- }
-
- if (!oFile.exists()) {
- try {
- oFile.createNewFile();
- } catch (IOException e) {
- LOG.warn("problem creating file {}: {}", oFile.getAbsoluteFile(), e.getMessage());
- }
- }
- try (InputStream in = u.openStream(); OutputStream outStream = new FileOutputStream(oFile);) {
-
- int theInt;
- while ((theInt = in.read()) >= 0) {
- outStream.write(theInt);
- }
- in.close();
- outStream.flush();
- outStream.close();
- LOG.debug("file written successfully");
- } catch (IOException e) {
- LOG.error("problem writing file: {}", e.getMessage());
- return false;
- }
- return true;
- }
-
-}
diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/DatabaseClient.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/DatabaseClient.java
index 78501a8db..7c48bf8a1 100644
--- a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/DatabaseClient.java
+++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/DatabaseClient.java
@@ -80,6 +80,16 @@ public interface DatabaseClient {
public @Nullable String doWriteRaw( String dataTypeName, @Nullable String esId, String json);
/**
+ * Write one object into Database
+ * @param indexName Name of index
+ * @param dataTypeName Name of datatype
+ * @param esId of object to be replaced or null for new entry.
+ * @param json String in JSON format.
+ * @return esId String of the database object or null in case of write problems.
+ */
+ public @Nullable String doWriteRaw(String indexName,String dataTypeName, @Nullable String esId, String json);
+
+ /**
* Remove Object from database
* @param dataTypeName of object
* @param esId of object to be deleted
diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/ExtRestClient.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/ExtRestClient.java
index 0e544acec..50a8244cb 100644
--- a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/ExtRestClient.java
+++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/ExtRestClient.java
@@ -17,11 +17,27 @@
******************************************************************************/
package org.onap.ccsdk.features.sdnr.wt.common.database;
+import java.io.FileInputStream;
import java.io.IOException;
+import java.security.KeyManagementException;
+import java.security.KeyStore;
+import java.security.KeyStoreException;
+import java.security.NoSuchAlgorithmException;
+import java.security.UnrecoverableKeyException;
+
+import javax.net.ssl.SSLContext;
+
import org.apache.http.HttpHost;
+import org.apache.http.auth.AuthScope;
+import org.apache.http.auth.UsernamePasswordCredentials;
+import org.apache.http.client.CredentialsProvider;
+import org.apache.http.impl.client.BasicCredentialsProvider;
+import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
+import org.apache.http.ssl.SSLContexts;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.ResponseException;
import org.elasticsearch.client.RestClient;
+import org.elasticsearch.client.RestClientBuilder.HttpClientConfigCallback;
import org.json.JSONException;
import org.onap.ccsdk.features.sdnr.wt.common.database.config.HostInfo;
import org.onap.ccsdk.features.sdnr.wt.common.database.config.HostInfo.Protocol;
@@ -55,15 +71,92 @@ import org.onap.ccsdk.features.sdnr.wt.common.database.responses.UpdateResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-
public class ExtRestClient {
- private static final Logger LOGGER = LoggerFactory.getLogger(ExtRestClient.class);
-
+ private static final Logger LOG = LoggerFactory.getLogger(ExtRestClient.class);
+
+ private class BasicAuthHttpClientConfigCallback implements HttpClientConfigCallback {
+
+ private final String basicAuthUsername;
+ private final String basicAuthPassword;
+
+ BasicAuthHttpClientConfigCallback(String username, String password) {
+ this.basicAuthUsername = username;
+ this.basicAuthPassword = password;
+ }
+
+ @Override
+ public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
+ if (basicAuthPassword == null || basicAuthUsername == null) {
+ return httpClientBuilder;
+ }
+ final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
+ credentialsProvider.setCredentials(AuthScope.ANY,
+ new UsernamePasswordCredentials(basicAuthUsername, basicAuthPassword));
+
+ return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
+ }
+
+ }
+ private class SSLCercAuthHttpClientConfigCallback implements HttpClientConfigCallback {
+
+ private final String certFilename;
+
+ SSLCercAuthHttpClientConfigCallback(String certfile) {
+ this.certFilename = certfile;
+ }
+
+ @Override
+ public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
+ if (this.certFilename == null) {
+ return httpClientBuilder;
+ }
+
+ char[] keystorePass = "MY PASSWORD".toCharArray();
+
+ FileInputStream fis = null;
+
+ // Loading KEYSTORE in JKS format
+ KeyStore keyStorePci = null;
+ try {
+ keyStorePci = KeyStore.getInstance(KeyStore.getDefaultType());
+ } catch (KeyStoreException e1) {
+ LOG.warn("unable to load keystore: {}",e1);
+ }
+ if (keyStorePci != null) {
+ try {
+ fis = new FileInputStream(this.certFilename);
+ keyStorePci.load(fis, keystorePass);
+ } catch (Exception e) {
+ LOG.error("Error loading keystore: " + this.certFilename);
+ } finally {
+ if (fis != null) {
+ try {
+ fis.close();
+ } catch (IOException e) {
+
+ }
+ }
+ }
+ }
+ SSLContext sslcontext=null;
+ try {
+ sslcontext = SSLContexts.custom().loadKeyMaterial(keyStorePci, keystorePass).build();
+ } catch (KeyManagementException | UnrecoverableKeyException | NoSuchAlgorithmException
+ | KeyStoreException e) {
+ LOG.warn("unable to load sslcontext: {}",e);
+ }
+ return httpClientBuilder.setSSLContext(sslcontext);
+ }
+ }
+
private RestClient client;
- public ExtRestClient(RestClient client) {
- this.client = client;
+ protected ExtRestClient(HostInfo[] hosts) {
+ this(hosts, null, null);
+ }
+ protected ExtRestClient(HostInfo[] hosts,String username,String password) {
+ this.client = RestClient.builder(get(hosts)).setHttpClientConfigCallback(new BasicAuthHttpClientConfigCallback(username, password) ).build();
}
public ClusterHealthResponse health(ClusterHealthRequest request)
@@ -132,7 +225,7 @@ public class ExtRestClient {
try {
return new SearchResponse(this.client.performRequest(request.getInner()));
} catch (ResponseException e) {
- LOGGER.debug("ignoring Exception for request {}: {}",request,e.getMessage());
+ LOG.debug("ignoring Exception for request {}: {}",request,e.getMessage());
return new SearchResponse(e.getResponse());
}
} else {
@@ -148,6 +241,8 @@ public class ExtRestClient {
return new GetResponse(e.getResponse());
}
}
+
+
public UpdateByQueryResponse update(UpdateByQueryRequest request) throws IOException {
return new UpdateByQueryResponse(this.client.performRequest(request.getInner()));
@@ -174,24 +269,21 @@ public class ExtRestClient {
response = this.health(request);
} catch (UnsupportedOperationException | IOException | JSONException e) {
- LOGGER.error(e.getMessage());
+ LOG.error(e.getMessage());
}
if(response!=null) {
status=response.getStatus();
- LOGGER.debug("Elasticsearch service started with status {}", response.getStatus());
+ LOG.debug("Elasticsearch service started with status {}", response.getStatus());
}
else {
- LOGGER.warn("Elasticsearch service not started yet with status {}. current status is {}",status,"none");
+ LOG.warn("Elasticsearch service not started yet with status {}. current status is {}",status,"none");
return false;
}
return response.isStatusMinimal(ClusterHealthResponse.HEALTHSTATUS_YELLOW);
}
- protected ExtRestClient(HostInfo[] hosts) {
- this(RestClient.builder(get(hosts)).build());
- }
private static HttpHost[] get(HostInfo[] hosts) {
HttpHost[] httphosts = new HttpHost[hosts.length];
for(int i=0;i<hosts.length;i++) {
@@ -200,17 +292,15 @@ public class ExtRestClient {
return httphosts;
}
public static ExtRestClient createInstance(HostInfo[] hosts) {
- HttpHost[] httphosts = new HttpHost[hosts.length];
- for(int i=0;i<hosts.length;i++) {
- httphosts[i]=new HttpHost(hosts[i].hostname, hosts[i].port, hosts[i].protocol.toString());
- }
- return new ExtRestClient(RestClient.builder(httphosts).build());
+ return new ExtRestClient(hosts);
}
- public static ExtRestClient createInstance(String hostname, int port, Protocol protocol) throws InvalidProtocolException {
+ public static ExtRestClient createInstance(HostInfo[] hosts,String username,String password) {
+ return new ExtRestClient(hosts,username,password);
+ }
+ public static ExtRestClient createInstance(String hostname, int port, Protocol protocol){
return createInstance(new HostInfo[] {new HostInfo(hostname,port,protocol)});
}
-
}
diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/HtDatabaseClient.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/HtDatabaseClient.java
index c3bd7c5eb..56402bdfc 100644
--- a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/HtDatabaseClient.java
+++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/HtDatabaseClient.java
@@ -55,17 +55,27 @@ import org.slf4j.LoggerFactory;
*/
public class HtDatabaseClient extends ExtRestClient implements DatabaseClient, AutoCloseable {
- private final Logger LOG = LoggerFactory.getLogger(HtDatabaseClient.class);
+ private static final boolean REFRESH_AFTER_REWRITE_DEFAULT = true;
+
+ private final Logger LOG = LoggerFactory.getLogger(HtDatabaseClient.class);
private boolean doRefreshAfterWrite;
public HtDatabaseClient(HostInfo[] hosts) {
- this(hosts,true);
+ this(hosts,REFRESH_AFTER_REWRITE_DEFAULT);
}
public HtDatabaseClient(HostInfo[] hosts, boolean refreshAfterWrite) {
super(hosts);
this.doRefreshAfterWrite = refreshAfterWrite;
}
+ public HtDatabaseClient(HostInfo[] hosts, boolean refreshAfterWrite,String username,String password) {
+ super(hosts,username,password);
+ this.doRefreshAfterWrite = refreshAfterWrite;
+ }
+ public HtDatabaseClient(HostInfo[] hosts,String username,String password) {
+ this(hosts,REFRESH_AFTER_REWRITE_DEFAULT,username,password);
+ }
+
/*----------------------------------
@@ -109,9 +119,13 @@ public class HtDatabaseClient extends ExtRestClient implements DatabaseClient, A
@Override
public @Nullable String doWriteRaw(String dataTypeName, @Nullable String esId, String json) {
-
+ return this.doWriteRaw(dataTypeName, dataTypeName, esId, json);
+ }
+ @Override
+ public @Nullable String doWriteRaw(String indexName,String dataTypeName, @Nullable String esId, String json) {
+
IndexResponse response = null;
- IndexRequest indexRequest = new IndexRequest(dataTypeName,dataTypeName,esId);
+ IndexRequest indexRequest = new IndexRequest(indexName,dataTypeName,esId);
indexRequest.source(json);
try {
response = this.index(indexRequest );
diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/queries/BoolQueryBuilder.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/queries/BoolQueryBuilder.java
index 5edc2613d..84f812f2c 100644
--- a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/queries/BoolQueryBuilder.java
+++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/queries/BoolQueryBuilder.java
@@ -35,25 +35,25 @@ public class BoolQueryBuilder extends QueryBuilder {
return "BoolQueryBuilder [inner=" + inner + "]";
}
- public BoolQueryBuilder must(QueryBuilder matchQuery) {
+ public BoolQueryBuilder must(QueryBuilder query) {
- if(this.inner.has("must") || this.inner.has("match") || this.inner.has("regexp")) {
+ if(this.inner.has("must") || this.inner.has("match") || this.inner.has("regexp") || this.inner.has("range")) {
Object x = this.inner.has("must") ?this.inner.get("must"):this.inner;
if(x instanceof JSONArray) {
- ((JSONArray)x).put(matchQuery.getInner());
+ ((JSONArray)x).put(query.getInner());
}
else {
this.inner = new JSONObject();
this.inner.put("must", new JSONObject());
JSONArray a=new JSONArray();
a.put(x);
- a.put(matchQuery.getInner());
+ a.put(query.getInner());
this.inner.put("must", a);
}
}
else {
- this.inner = matchQuery.getInner();
+ this.inner = query.getInner();
}
this.setQuery("bool", this.inner);
return this;
diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/file/PomFile.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/file/PomFile.java
index d415d601f..6301a917e 100644
--- a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/file/PomFile.java
+++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/file/PomFile.java
@@ -51,13 +51,22 @@ public class PomFile {
if(props!=null) {
Node prop = findChild(key, props);
if(prop!=null) {
- return getTextValue(prop);
+ return getTextValue(prop);
}
}
return null;
}
-
- public static String getTextValue(Node node) {
+ public String getParentVersion() {
+ Node parent = findChild("parent",this.xmlDoc.getDocumentElement());
+ if(parent!=null) {
+ Node version = findChild("version", parent);
+ if(version!=null) {
+ return getTextValue(version);
+ }
+ }
+ return null;
+ }
+ private static String getTextValue(Node node) {
StringBuffer textValue = new StringBuffer();
for (int i = 0,length = node.getChildNodes().getLength(); i < length; i ++) {
Node c = node.getChildNodes().item(i);
diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/http/BaseHTTPClientFromDevicemanager.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/http/BaseHTTPClientFromDevicemanager.java
deleted file mode 100644
index e330fb42a..000000000
--- a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/http/BaseHTTPClientFromDevicemanager.java
+++ /dev/null
@@ -1,327 +0,0 @@
-/*******************************************************************************
- * ============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
- *
- * 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.features.sdnr.wt.common.http;
-
-import java.io.ByteArrayInputStream;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.net.HttpURLConnection;
-import java.net.URL;
-import java.net.URLConnection;
-import java.nio.charset.Charset;
-import java.nio.charset.StandardCharsets;
-import java.nio.file.Files;
-import java.security.KeyFactory;
-import java.security.KeyManagementException;
-import java.security.KeyStore;
-import java.security.KeyStoreException;
-import java.security.NoSuchAlgorithmException;
-import java.security.UnrecoverableKeyException;
-import java.security.cert.Certificate;
-import java.security.cert.CertificateException;
-import java.security.cert.CertificateFactory;
-import java.security.cert.X509Certificate;
-import java.security.interfaces.RSAPrivateKey;
-import java.security.spec.InvalidKeySpecException;
-import java.security.spec.PKCS8EncodedKeySpec;
-import java.util.Base64;
-import java.util.Map;
-
-import javax.annotation.Nonnull;
-import javax.net.ssl.HostnameVerifier;
-import javax.net.ssl.HttpsURLConnection;
-import javax.net.ssl.KeyManager;
-import javax.net.ssl.KeyManagerFactory;
-import javax.net.ssl.SSLContext;
-import javax.net.ssl.TrustManager;
-import javax.xml.bind.DatatypeConverter;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-public class BaseHTTPClientFromDevicemanager {
-
- private static Logger LOG = LoggerFactory.getLogger(BaseHTTPClientFromDevicemanager.class);
- private static final int SSLCERT_NONE = -1;
- private static final int SSLCERT_PCKS = 0;
- private static final int SSLCERT_PEM = 1;
- private static final int BUFSIZE = 1024;
- private static final Charset CHARSET = StandardCharsets.UTF_8;
- private static final String SSLCONTEXT = "TLSv1.2";
- private static final int DEFAULT_HTTP_TIMEOUT_MS = 30000; // in ms
-
- private final boolean trustAll;
- private final String baseUrl;
-
- private int timeout = DEFAULT_HTTP_TIMEOUT_MS;
- private SSLContext sc = null;
-
- public BaseHTTPClientFromDevicemanager(String base) {
- this(base, false);
- }
-
- public BaseHTTPClientFromDevicemanager(String base, boolean trustAllCerts) {
- this(base, trustAllCerts, null, null, SSLCERT_NONE);
- }
-
- public BaseHTTPClientFromDevicemanager(String base, boolean trustAllCerts, String certFilename, String passphrase, int sslCertType) {
- this.baseUrl = base;
- this.trustAll = trustAllCerts;
- try {
- sc = setupSsl(trustAll, certFilename, passphrase, sslCertType);
- } catch (KeyManagementException | NoSuchAlgorithmException | UnrecoverableKeyException | CertificateException
- | KeyStoreException | IOException | InvalidKeySpecException e) {
- LOG.warn("problem ssl setup: " + e.getMessage());
- }
- }
-
- protected @Nonnull BaseHTTPResponseFromDevicemanager sendRequest(String uri, String method, String body, Map<String, String> headers)
- throws IOException {
- return this.sendRequest(uri, method, body != null ? body.getBytes(CHARSET) : null, headers);
- }
-
- protected @Nonnull BaseHTTPResponseFromDevicemanager sendRequest(String uri, String method, byte[] body, Map<String, String> headers)
- throws IOException {
- if (uri == null) {
- uri = "";
- }
- String surl = this.baseUrl;
- if (!surl.endsWith("/") && uri.length() > 0) {
- surl += "/";
- }
- if (uri.startsWith("/")) {
- uri = uri.substring(1);
- }
- surl += uri;
- LOG.debug("try to send request with url=" + this.baseUrl + uri + " as method=" + method);
- LOG.trace("body:" + (body == null ? "null" : new String(body, CHARSET)));
- URL url = new URL(surl);
- URLConnection http = url.openConnection();
- http.setConnectTimeout(this.timeout);
- if (surl.toString().startsWith("https")) {
- if (sc != null) {
- ((HttpsURLConnection) http).setSSLSocketFactory(sc.getSocketFactory());
- if (trustAll) {
- LOG.debug("trusting all certs");
- HostnameVerifier allHostsValid = (hostname, session) -> true;
- ((HttpsURLConnection) http).setHostnameVerifier(allHostsValid);
- }
- } else // Should never happen
- {
- LOG.warn("No SSL context available");
- return new BaseHTTPResponseFromDevicemanager(-1, "");
- }
- }
- ((HttpURLConnection) http).setRequestMethod(method);
- http.setDoOutput(true);
- if (headers != null && headers.size() > 0) {
- for (String key : headers.keySet()) {
- http.setRequestProperty(key, headers.get(key));
- LOG.trace("set http header " + key + ": " + headers.get(key));
- }
- }
- byte[] buffer = new byte[BUFSIZE];
- int len = 0, lensum = 0;
- // send request
- // Send the message to destination
- if (!method.equals("GET") && body != null && body.length > 0) {
- try (OutputStream output = http.getOutputStream()) {
- output.write(body);
- }
- }
- // Receive answer
- int responseCode = ((HttpURLConnection) http).getResponseCode();
- String sresponse = "";
- InputStream response = null;
- try {
- if (responseCode >= 200 && responseCode < 300) {
- response = http.getInputStream();
- } else {
- response = ((HttpURLConnection) http).getErrorStream();
- if (response == null) {
- response = http.getInputStream();
- }
- }
- if (response != null) {
- while (true) {
- len = response.read(buffer, 0, BUFSIZE);
- if (len <= 0) {
- break;
- }
- lensum += len;
- sresponse += new String(buffer, 0, len, CHARSET);
- }
- } else {
- LOG.debug("response is null");
- }
- } catch (Exception e) {
- LOG.debug("No response. ", e);
- } finally {
- if (response != null) {
- response.close();
- }
- }
- LOG.debug("ResponseCode: " + responseCode);
- LOG.trace("Response (len:{}): {}", String.valueOf(lensum), sresponse);
- return new BaseHTTPResponseFromDevicemanager(responseCode, sresponse);
- }
-
- public static SSLContext setupSsl(boolean trustall)
- throws NoSuchAlgorithmException, KeyManagementException, CertificateException, FileNotFoundException,
- IOException, UnrecoverableKeyException, KeyStoreException, InvalidKeySpecException {
-
- return setupSsl(trustall, null, null, SSLCERT_NONE);
- }
-
- /**
- * @param keyFilename filename for key file
- * @param certFilename filename for cert file
- * @throws NoSuchAlgorithmException
- * @throws KeyManagementException
- * @throws IOException
- * @throws FileNotFoundException
- * @throws CertificateException
- * @throws KeyStoreException
- * @throws UnrecoverableKeyException
- * @throws InvalidKeySpecException
- */
- /**
- * Setup of SSLContext
- *
- * @param trustall true to switch of certificate verification
- * @param certFilename filename for certificate file
- * @param passPhrase for certificate
- * @param certType of certificate
- * @return SSL Context according to parameters
- * @throws NoSuchAlgorithmException according name
- * @throws KeyManagementException according name
- * @throws CertificateException according name
- * @throws FileNotFoundException according name
- * @throws IOException according name
- * @throws UnrecoverableKeyException according name
- * @throws KeyStoreException according name
- * @throws InvalidKeySpecException according name
- */
- public static SSLContext setupSsl(boolean trustall, String certFilename, String passPhrase, int certType)
- throws NoSuchAlgorithmException, KeyManagementException, CertificateException, FileNotFoundException,
- IOException, UnrecoverableKeyException, KeyStoreException, InvalidKeySpecException {
-
- SSLContext sc = SSLContext.getInstance(SSLCONTEXT);
- TrustManager[] trustCerts = null;
- if (trustall) {
- trustCerts = new TrustManager[] {new javax.net.ssl.X509TrustManager() {
- @Override
- public java.security.cert.X509Certificate[] getAcceptedIssuers() {
- return null;
- }
-
- @Override
- public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) {}
-
- @Override
- public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) {}
- }};
-
- }
- KeyManager[] kms = null;
- if (certFilename != null && passPhrase != null && !certFilename.isEmpty() && !passPhrase.isEmpty()) {
- if (certType == SSLCERT_PCKS) {
- LOG.debug("try to load pcks file " + certFilename + " with passphrase=" + passPhrase);
- KeyStore keyStore = KeyStore.getInstance("PKCS12");
- FileInputStream fileInputStream = new FileInputStream(certFilename);
- keyStore.load(fileInputStream, passPhrase.toCharArray());
- KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
- kmf.init(keyStore, passPhrase.toCharArray());
- kms = kmf.getKeyManagers();
- fileInputStream.close();
- LOG.debug("successful");
-
- } else if (certType == SSLCERT_PEM) {
- LOG.debug("try to load pem files cert=" + certFilename + " key=" + passPhrase);
- File fCert = new File(certFilename);
- File fKey = new File(passPhrase);
- KeyStore keyStore = KeyStore.getInstance("JKS");
- keyStore.load(null);
- byte[] certBytes = parseDERFromPEM(Files.readAllBytes(fCert.toPath()), "-----BEGIN CERTIFICATE-----",
- "-----END CERTIFICATE-----");
- byte[] keyBytes = parseDERFromPEM(Files.readAllBytes(fKey.toPath()), "-----BEGIN PRIVATE KEY-----",
- "-----END PRIVATE KEY-----");
-
- X509Certificate cert = generateCertificateFromDER(certBytes);
- RSAPrivateKey key = generatePrivateKeyFromDER(keyBytes);
- keyStore.setCertificateEntry("cert-alias", cert);
- keyStore.setKeyEntry("key-alias", key, "changeit".toCharArray(), new Certificate[] {cert});
-
- KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
- kmf.init(keyStore, "changeit".toCharArray());
- kms = kmf.getKeyManagers();
- LOG.debug("successful");
- }
- }
- // Init the SSLContext with a TrustManager[] and SecureRandom()
- sc.init(kms, trustCerts, new java.security.SecureRandom());
- return sc;
- }
-
- protected static byte[] parseDERFromPEM(byte[] pem, String beginDelimiter, String endDelimiter) {
- String data = new String(pem);
- String[] tokens = data.split(beginDelimiter);
- tokens = tokens[1].split(endDelimiter);
- return DatatypeConverter.parseBase64Binary(tokens[0]);
- }
-
- protected static RSAPrivateKey generatePrivateKeyFromDER(byte[] keyBytes)
- throws InvalidKeySpecException, NoSuchAlgorithmException {
- PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes);
-
- KeyFactory factory = KeyFactory.getInstance("RSA");
-
- return (RSAPrivateKey) factory.generatePrivate(spec);
- }
-
- protected static X509Certificate generateCertificateFromDER(byte[] certBytes) throws CertificateException {
- CertificateFactory factory = CertificateFactory.getInstance("X.509");
-
- return (X509Certificate) factory.generateCertificate(new ByteArrayInputStream(certBytes));
- }
-
- public static String getAuthorizationHeaderValue(String username, String password) {
- return "Basic " + new String(Base64.getEncoder().encode((username + ":" + password).getBytes()));
- }
-
- public void setTimeout(int timeout) {
- this.timeout = timeout;
- }
-
- public static int getSslCertPcks() {
- return SSLCERT_PCKS;
- }
-
- public static int getSslCertNone() {
- return SSLCERT_NONE;
- }
-
- public static int getSslCertPEM() {
- return SSLCERT_PEM;
- }
-
-}
diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/http/BaseServlet.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/http/BaseServlet.java
new file mode 100644
index 000000000..8242d35d4
--- /dev/null
+++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/http/BaseServlet.java
@@ -0,0 +1,375 @@
+/*
+ * ============LICENSE_START=======================================================
+ * ONAP : CCSDK.apps.sdnr.wt.apigateway
+ * ================================================================================
+ * 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
+ *
+ * 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.http;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.net.URLConnection;
+import java.nio.charset.StandardCharsets;
+import java.security.KeyManagementException;
+import java.security.NoSuchAlgorithmException;
+import java.util.Enumeration;
+import java.util.List;
+import java.util.Map;
+
+import javax.net.ssl.HostnameVerifier;
+import javax.net.ssl.HttpsURLConnection;
+import javax.net.ssl.SSLContext;
+import javax.net.ssl.TrustManager;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public abstract class BaseServlet extends HttpServlet {
+
+ /**
+ *
+ */
+ private static final long serialVersionUID = 7403047480257892794L;
+ private static Logger LOG = LoggerFactory.getLogger(BaseServlet.class);
+ private static SSLContext sc;
+ private static TrustManager[] trustCerts = null;
+ private static final int BUFSIZE = 2048;
+
+ protected abstract String getOfflineResponse();
+
+ protected abstract boolean isOff();
+
+ protected abstract boolean doTrustAll();
+ protected abstract void trustAll(boolean trust);
+ protected abstract String getRemoteUrl(String uri);
+
+ /**
+ *
+ * @throws NoSuchAlgorithmException
+ * @throws KeyManagementException
+ */
+ private static void setupSslTrustAll(boolean trustall) throws NoSuchAlgorithmException, KeyManagementException {
+
+ sc = SSLContext.getInstance("TLSv1.2");
+ if (trustall) {
+ if (trustCerts == null) {
+ trustCerts = new TrustManager[] { new javax.net.ssl.X509TrustManager() {
+ @Override
+ public java.security.cert.X509Certificate[] getAcceptedIssuers() {
+ return new java.security.cert.X509Certificate[] {};
+ }
+
+ @Override
+ public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) {
+ // do not check anything when trust all
+ }
+
+ @Override
+ public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) {
+ // do not check anything when trust all
+ }
+ } };
+ }
+ } else {
+ if (trustCerts != null) {
+ trustCerts = null;
+ }
+ }
+ // Init the SSLContext with a TrustManager[] and SecureRandom()
+ sc.init(null, trustCerts, new java.security.SecureRandom());
+ }
+
+ private boolean trustInsecure;
+ private boolean isCorsEnabled;
+ public BaseServlet(boolean trustInsecure) {
+ this.trustInsecure = trustInsecure;
+ this.trysslSetup(true);
+ }
+
+ private void trysslSetup() {
+ this.trysslSetup(false);
+ }
+
+ /**
+ * init or deinit ssl insecure mode regarding to property
+ *
+ * @param force init independent from property
+ */
+ private void trysslSetup(boolean force) {
+ // if trustall config has changed
+ if (force || this.doTrustAll() != this.trustInsecure) {
+ this.trustAll(this.trustInsecure);
+ // resetup ssl config
+ try {
+ setupSslTrustAll(this.trustInsecure);
+ } catch (Exception e) {
+ LOG.error("problem setting up SSL: {}", e.getMessage());
+ }
+ }
+ }
+
+ protected void sendOffResponse(HttpServletResponse response) {
+ response.setStatus(200);// HTML/OK
+ response.setHeader("Content-Type", "text/html; charset=utf-8");
+ try {
+ response.getOutputStream().write(this.getOfflineResponse().getBytes(StandardCharsets.UTF_8));
+ } catch (IOException e) {
+ LOG.debug("problem writing offline response");
+ }
+
+ }
+
+ @Override
+ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
+ if (this.isOff()) {
+ this.sendOffResponse(resp);
+ } else {
+ this.trysslSetup();
+ HttpURLConnection http = null;
+ try {
+ http = (HttpURLConnection) this.getConnection(req, "GET");
+ } catch (IOException e) {
+ LOG.warn(e.getMessage());
+ }
+ if (http != null) {
+ try {
+ this.handleRequest(http, req, resp, "GET");
+ } catch (IOException e) {
+ LOG.warn(e.getMessage());
+ }
+ http.disconnect();
+ }
+ else {
+ this.set404Response(resp);
+ }
+ }
+ }
+
+ @Override
+ protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
+ if (this.isOff()) {
+ this.sendOffResponse(resp);
+ } else {
+ this.trysslSetup();
+ HttpURLConnection http = null;
+ try {
+ http = (HttpURLConnection) this.getConnection(req, "PUT");
+ } catch (IOException e) {
+ LOG.warn(e.getMessage());
+ }
+ if (http != null) {
+ try {
+ this.handleRequest(http, req, resp, "PUT");
+ } catch (IOException e) {
+ LOG.warn(e.getMessage());
+ }
+ http.disconnect();
+ }
+ else {
+ this.set404Response(resp);
+ }
+ }
+ }
+
+ @Override
+ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
+ if (this.isOff()) {
+ this.sendOffResponse(resp);
+ } else {
+ this.trysslSetup();
+ HttpURLConnection http = null;
+ try {
+ http = (HttpURLConnection) this.getConnection(req, "POST");
+ } catch (IOException e) {
+ LOG.warn(e.getMessage());
+ }
+ if (http != null) {
+ try {
+ this.handleRequest(http, req, resp, "POST");
+ } catch (IOException e) {
+ LOG.warn(e.getMessage());
+ }
+ http.disconnect();
+ }
+ else {
+ this.set404Response(resp);
+ }
+ }
+ }
+
+ @Override
+ protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
+ if (this.isOff()) {
+ this.sendOffResponse(resp);
+ } else {
+ this.trysslSetup();
+ HttpURLConnection http = null;
+ try {
+ http = (HttpURLConnection) this.getConnection(req, "DELETE");
+ } catch (IOException e) {
+ LOG.warn(e.getMessage());
+ }
+ if (http != null) {
+ try {
+ this.handleRequest(http, req, resp, "DELETE");
+ } catch (IOException e) {
+ LOG.warn(e.getMessage());
+ }
+ http.disconnect();
+ }
+ else {
+ this.set404Response(resp);
+ }
+ }
+ }
+
+ private void set404Response(HttpServletResponse resp) {
+ resp.setStatus(404);
+ }
+
+ private URLConnection getConnection(HttpServletRequest req, final String method) throws IOException {
+
+ LOG.debug("{} Request to {}", method,req.getRequestURL());
+ String surl = this.getRemoteUrl(req.getRequestURI());
+ if("GET".equals(method)) {
+ Enumeration<?> params = req.getParameterNames();
+ if(params!=null) {
+ String param;
+ if(params.hasMoreElements()) {
+ param=(String)params.nextElement();
+ surl+="?"+param+"="+req.getParameter(param);
+ }
+ while(params.hasMoreElements()) {
+ param=(String)params.nextElement();
+ surl+="&"+param+"="+req.getParameter(param);
+ }
+ }
+ }
+ LOG.debug("RemoteURL: {}", surl);
+ if(surl==null) {
+ return null;
+ }
+ URL url = new URL(surl);
+ URLConnection http = url.openConnection();
+ ((HttpURLConnection) http).setRequestMethod(method);
+ if (url.toString().startsWith("https")) {
+ ((HttpsURLConnection) http).setSSLSocketFactory(sc.getSocketFactory());
+ if (this.doTrustAll()) {
+ HostnameVerifier allHostsValid = (hostname, session) -> true;
+ ((HttpsURLConnection) http).setHostnameVerifier(allHostsValid);
+ }
+ }
+ http.setDoOutput(true);
+ // copy request headers
+ String s = "";
+ Enumeration<?> headers = req.getHeaderNames();
+ while (headers.hasMoreElements()) {
+ String h = (String)headers.nextElement();
+ String v = req.getHeader(h);
+ if (h != null && h.equals("Host")) {
+ v = url.getAuthority();
+ }
+ s += String.format("%s:%s;", h, v);
+ http.setRequestProperty(h, v);
+ }
+ LOG.debug("Request Headers: {}", s);
+ return http;
+ }
+
+ private void handleRequest(HttpURLConnection http, HttpServletRequest req, HttpServletResponse resp, String method)
+ throws IOException {
+ byte[] buffer = new byte[BUFSIZE];
+ int len = 0, lensum = 0;
+ // send request
+ // Send the message to destination
+ OutputStream output = null;
+ if (!method.equals("GET")) {
+ try {
+ output = http.getOutputStream();
+ } catch (Exception e) {
+ LOG.debug("problem reading output stream: {}", e.getMessage());
+ }
+ }
+ if (output != null) {
+ while (true) {
+ len = req.getInputStream().read(buffer, 0, BUFSIZE);
+ if (len <= 0) {
+ break;
+ }
+ lensum += len;
+ output.write(buffer, 0, len);
+ }
+ }
+ LOG.debug("written {} data out", lensum);
+ int responseCode = http.getResponseCode();
+ // Receive answer
+ InputStream response;
+ if (responseCode >= 200 && responseCode < 300) {
+ response = http.getInputStream();
+ } else {
+ response = http.getErrorStream();
+ if (response == null) {
+ http.getInputStream();
+ }
+ }
+
+ LOG.debug("ResponseCode: {}", responseCode);
+ resp.setStatus(responseCode);
+ Map<String, List<String>> set = http.getHeaderFields();
+ String s = "";
+ if (set != null) {
+ for (Map.Entry<String, List<String>> entry : set.entrySet()) {
+ if (entry.getKey() == null) {
+ continue;
+ }
+ for (String v : entry.getValue()) {
+ resp.setHeader(entry.getKey(), v);
+ s += String.format("%s:%s;", entry.getKey(), v);
+ }
+ if (this.isCorsEnabled) {
+ resp.setHeader("Access-Control-Allow-Origin", "*");
+ // resp.setHeader("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE");
+ resp.setHeader("Access-Control-Allow-Headers", "Content-Type, Authorization");
+ }
+
+ }
+ }
+ LOG.debug("Received Headers: {}", s);
+ lensum = 0;
+ if (response != null) {
+ while (true) {
+ len = response.read(buffer, 0, BUFSIZE);
+ if (len <= 0) {
+ break;
+ }
+ lensum += len;
+ resp.getOutputStream().write(buffer, 0, len);
+ }
+ } else {
+ LOG.debug("response is null");
+ }
+ LOG.debug("Received {} bytes", lensum);
+ }
+
+}
diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/InvalidProtocolException.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/util/ResourceFileLoader.java
index c7d27204a..134aca4d6 100644
--- a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/InvalidProtocolException.java
+++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/util/ResourceFileLoader.java
@@ -15,18 +15,16 @@
* the License.
* ============LICENSE_END==========================================================================
******************************************************************************/
-package org.onap.ccsdk.features.sdnr.wt.common.database;
-public class InvalidProtocolException extends Exception {
+package org.onap.ccsdk.features.sdnr.wt.common.util;
-
- /**
- *
- */
- private static final long serialVersionUID = 1L;
-
- public InvalidProtocolException(String e) {
- super(e);
+import java.io.File;
+
+public class ResourceFileLoader {
+
+ public static File getFile(Object o, String fileName) {
+ ClassLoader classLoader = o.getClass().getClassLoader();
+ return new File(classLoader.getResource(fileName).getFile());
}
}
diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/http/BaseHTTPResponseFromDevicemanager.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/util/StackTrace.java
index 0fd0a6459..4bde44535 100644
--- a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/http/BaseHTTPResponseFromDevicemanager.java
+++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/util/StackTrace.java
@@ -6,33 +6,36 @@
* =================================================================================================
* 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.features.sdnr.wt.common.http;
+package org.onap.ccsdk.features.sdnr.wt.common.util;
-public class BaseHTTPResponseFromDevicemanager {
+import java.io.PrintWriter;
+import java.io.StringWriter;
- public static final int CODE404 = 404;
- public static final int CODE200 = 200;
- public static final BaseHTTPResponseFromDevicemanager UNKNOWN = new BaseHTTPResponseFromDevicemanager(-1, "");
- public final int code;
- public final String body;
+/**
+ * Stack related utils
+ *
+ */
+public class StackTrace {
- public BaseHTTPResponseFromDevicemanager(int code,String body)
- {
- this.code=code;
- this.body=body;
- }
+ /**
+ * Return stacktrace as String
+ * @param e with stacktrace information to be converte
+ * @return String with call stack
+ */
+ public static String toString(Exception e) {
+ StringWriter sw = new StringWriter();
+ PrintWriter pw = new PrintWriter(sw);
+ e.printStackTrace(pw);
+ return sw.toString(); // stack trace as a string
+ }
- @Override
- public String toString() {
- return "BaseHTTPResponse [code=" + code + ", body=" + body + "]";
- }
}
diff --git a/sdnr/wt/common/src/test/java/org/onap/ccsdk/features/sdnr/wt/common/test/TestConfig.java b/sdnr/wt/common/src/test/java/org/onap/ccsdk/features/sdnr/wt/common/test/TestConfig.java
index f6f741197..591ea49dd 100644
--- a/sdnr/wt/common/src/test/java/org/onap/ccsdk/features/sdnr/wt/common/test/TestConfig.java
+++ b/sdnr/wt/common/src/test/java/org/onap/ccsdk/features/sdnr/wt/common/test/TestConfig.java
@@ -197,7 +197,7 @@ public class TestConfig {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
- Thread.interrupted();
+ Thread.currentThread().interrupt();
}
this.write(TESTFILENAME, TESTCONTENT1);
int i=10;
@@ -205,7 +205,7 @@ public class TestConfig {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
- Thread.interrupted();
+ Thread.currentThread().interrupt();
}
if(changeFlag) {
break;
diff --git a/sdnr/wt/common/src/test/java/org/onap/ccsdk/features/sdnr/wt/common/test/TestPomfile.java b/sdnr/wt/common/src/test/java/org/onap/ccsdk/features/sdnr/wt/common/test/TestPomfile.java
index 49e0445a1..02233aff4 100644
--- a/sdnr/wt/common/src/test/java/org/onap/ccsdk/features/sdnr/wt/common/test/TestPomfile.java
+++ b/sdnr/wt/common/src/test/java/org/onap/ccsdk/features/sdnr/wt/common/test/TestPomfile.java
@@ -33,38 +33,38 @@ import org.xml.sax.SAXException;
public class TestPomfile {
- private static final String TESTPROPERTY_KEY="elasticsearch.version";
- private static final String TESTPROPERTY_VALUE = "6.4.3";
- private static final String POMFILENAME = "pom.xml";
- private static final String POM_PROPERTY = "#Generated by org.apache.felix.bundleplugin\n" +
- "#Tue Nov 19 11:20:33 CET 2019\n" +
- "version=0.7.0-SNAPSHOT\n" +
- "groupId=org.onap.ccsdk.features.sdnr.wt\n" +
- "artifactId=sdnr-wt-data-provider-provider\n";
- //private static final Date DATE_EXPECTED = new Date(119, 10, 19, 11, 20, 33);
-
- @Test
- public void test() {
- PomFile pom=null;
- try {
- pom = new PomFile(new FileInputStream(POMFILENAME));
- } catch (ParserConfigurationException | SAXException | IOException e) {
- fail(e.getMessage());
- }
- assertNotNull(pom);
- assertEquals(TESTPROPERTY_VALUE, pom.getProperty(TESTPROPERTY_KEY));
-
- }
- @Test
- public void testProp() {
- PomPropertiesFile file = null;
- try {
- file = new PomPropertiesFile(new ByteArrayInputStream(POM_PROPERTY.getBytes()));
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- assertNotNull(file);
- assertNotNull(file.getBuildDate());
- }
+ private static final String TESTPROPERTY_KEY="elasticsearch-rest-client.version";
+ private static final String TESTPROPERTY_VALUE = "6.4.3";
+ private static final String POMFILENAME = "pom.xml";
+ private static final String POM_PROPERTY = "#Generated by org.apache.felix.bundleplugin\n" +
+ "#Tue Nov 19 11:20:33 CET 2019\n" +
+ "version=0.7.0-SNAPSHOT\n" +
+ "groupId=org.onap.ccsdk.features.sdnr.wt\n" +
+ "artifactId=sdnr-wt-data-provider-provider\n";
+ //private static final Date DATE_EXPECTED = new Date(119, 10, 19, 11, 20, 33);
+
+ @Test
+ public void test() {
+ PomFile pom=null;
+ try {
+ pom = new PomFile(new FileInputStream(POMFILENAME));
+ } catch (ParserConfigurationException | SAXException | IOException e) {
+ fail(e.getMessage());
+ }
+ assertNotNull(pom);
+ assertEquals(TESTPROPERTY_VALUE, pom.getProperty(TESTPROPERTY_KEY));
+
+ }
+ @Test
+ public void testProp() {
+ PomPropertiesFile file = null;
+ try {
+ file = new PomPropertiesFile(new ByteArrayInputStream(POM_PROPERTY.getBytes()));
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ assertNotNull(file);
+ assertNotNull(file.getBuildDate());
+ }
}