summaryrefslogtreecommitdiffstats
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.java35
-rw-r--r--sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/Resources.java25
-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/configuration/subtypes/Section.java26
-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/BaseHTTPResponseFromDevicemanager.java38
-rw-r--r--sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/util/Environment.java46
-rw-r--r--sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/util/FileWatchdog.java129
-rw-r--r--sdnr/wt/common/src/test/java/org/onap/ccsdk/features/sdnr/wt/common/test/TestDbClient.java71
-rw-r--r--sdnr/wt/common/src/test/java/org/onap/ccsdk/features/sdnr/wt/common/test/TestDbRequests.java412
10 files changed, 937 insertions, 512 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
new file mode 100644
index 000000000..873b5c947
--- /dev/null
+++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/HtAssert.java
@@ -0,0 +1,35 @@
+/*******************************************************************************
+ * ============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;
+
+public class HtAssert {
+
+
+ public static class HtNullable<T> {
+ }
+
+ public static void nonnull(Object ... oList) {
+ for (Object o : oList) {
+ if (o == null) {
+ throw new IllegalArgumentException("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 3597f2e57..fe608a503 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
@@ -20,22 +20,15 @@ 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;
@@ -48,7 +41,7 @@ public class Resources {
private static final String RESSOURCEROOT = "src/main/resources";
private static URL getFileURL(Class<?> cls,String resFile) {
- Bundle b = FrameworkUtil.getBundle(cls);
+ Bundle b = FrameworkUtil.getBundle(cls);
URL u = null;
LOG.debug("try to get file {}", resFile);
if (b == null) {
@@ -83,7 +76,7 @@ public class Resources {
private static String readFile(final InputStream s) throws IOException {
// read file
- final String LR = "\n";
+ final String LR = "\n";
BufferedReader in = new BufferedReader(new InputStreamReader(s));
StringBuilder sb = new StringBuilder();
String inputLine;
@@ -97,7 +90,7 @@ public class Resources {
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 -> {
@@ -185,7 +178,7 @@ public class Resources {
// return list;
// }
public static String getFileContent( Class<?> cls, String resFile) {
- LOG.debug("loading file {} from res", resFile);
+ LOG.debug("loading file {} from res", resFile);
URL u = getFileURL(cls,resFile);
String s=null;
if (u == null) {
@@ -199,7 +192,7 @@ public class Resources {
}
return s;
- }
+ }
// public static JSONObject getJSONFile(Class<?> cls,String resFile) {
// LOG.debug("loading json file {} from res", resFile);
// JSONObject o = null;
@@ -269,11 +262,11 @@ public class Resources {
// Collection<URL> urlCollection = new ArrayList<>();
// URL url = getUrlForRessource(cls,folder);
// if(url==null) {
-// return Collections.enumeration(urlCollection);
+// return Collections.enumeration(urlCollection);
// }
// String path = url.getPath();
// File[] files = new File(path).listFiles();
-//
+//
// if (files != null) {
// for (File f : files) {
// try {
@@ -290,7 +283,7 @@ public class Resources {
// }
//
// return Collections.enumeration(urlCollection);
-//
+//
// }
public static URL getUrlForRessource(Class<?> cls,String fileOrDirectory) {
@@ -298,7 +291,7 @@ public class Resources {
ClassLoader loader = cls.getClassLoader();
URL url = loader.getResource(fileOrDirectory);
if(url==null && fileOrDirectory.startsWith("/")) {
- url = loader.getResource(fileOrDirectory.substring(1));
+ url = loader.getResource(fileOrDirectory.substring(1));
}
return url;
}
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
new file mode 100644
index 000000000..32a21a801
--- /dev/null
+++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/ResourcesFromDeviceManager.java
@@ -0,0 +1,340 @@
+/*******************************************************************************
+ * ============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/configuration/subtypes/Section.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/configuration/subtypes/Section.java
index a0e21be01..094da6389 100644
--- a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/configuration/subtypes/Section.java
+++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/configuration/subtypes/Section.java
@@ -50,7 +50,7 @@ public class Section {
}
public String getProperty(String key) {
- return this.getProperty(key, null);
+ return this.getProperty(key, "");
}
public String getProperty(String key, String defValue) {
@@ -61,7 +61,7 @@ public class Section {
}
public String getName() {
- return name;
+ return name;
}
public void setProperty(String key, String value) {
@@ -70,7 +70,7 @@ public class Section {
key = key.substring(1);
}
if (this.values.containsKey(key)) {
- this.values.get(key).setValue(value).setIsUncommented(isuncommented);
+ this.values.get(key).setValue(value).setIsUncommented(isuncommented);
} else {
this.values.put(key, new SectionValue(value,isuncommented));
}
@@ -128,7 +128,7 @@ public class Section {
lines.add("[" + this.name + "]");
}
for (Entry<String, SectionValue> entry : this.values.entrySet()) {
- SectionValue sectionValue = entry.getValue();
+ SectionValue sectionValue = entry.getValue();
if (sectionValue.getComments().size() > 0) {
for (String comment : sectionValue.getComments()) {
lines.add(comment);
@@ -173,11 +173,11 @@ public class Section {
public Optional<Long> getLong(String key) {
String v = this.getProperty(key);
- try {
- return Optional.of(Long.parseLong(v));
- } catch (NumberFormatException e) {
- }
- return Optional.empty();
+ try {
+ return Optional.of(Long.parseLong(v));
+ } catch (NumberFormatException e) {
+ }
+ return Optional.empty();
}
public boolean hasValues() {
@@ -188,9 +188,9 @@ public class Section {
return this.values.containsKey(key);
}
- @Override
- public String toString() {
- return "Section [name=" + name + ", rawLines=" + rawLines + ", values=" + values + "]";
- }
+ @Override
+ public String toString() {
+ return "Section [name=" + name + ", rawLines=" + rawLines + ", values=" + values + "]";
+ }
}
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
new file mode 100644
index 000000000..e330fb42a
--- /dev/null
+++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/http/BaseHTTPClientFromDevicemanager.java
@@ -0,0 +1,327 @@
+/*******************************************************************************
+ * ============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/BaseHTTPResponseFromDevicemanager.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/http/BaseHTTPResponseFromDevicemanager.java
new file mode 100644
index 000000000..0fd0a6459
--- /dev/null
+++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/http/BaseHTTPResponseFromDevicemanager.java
@@ -0,0 +1,38 @@
+/*******************************************************************************
+ * ============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;
+
+public class BaseHTTPResponseFromDevicemanager {
+
+ 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;
+
+ public BaseHTTPResponseFromDevicemanager(int code,String body)
+ {
+ this.code=code;
+ this.body=body;
+ }
+
+ @Override
+ public String toString() {
+ return "BaseHTTPResponse [code=" + code + ", body=" + body + "]";
+ }
+}
diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/util/Environment.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/util/Environment.java
new file mode 100644
index 000000000..1ac55ad15
--- /dev/null
+++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/util/Environment.java
@@ -0,0 +1,46 @@
+/*******************************************************************************
+ * ============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.util;
+
+import java.net.Inet4Address;
+import java.net.UnknownHostException;
+import java.util.Map;
+
+public class Environment {
+
+ public static String getVar(String v)
+ {
+ if(v.equals("$HOSTNAME"))
+ {
+ String s=null;
+ try {
+ s = Inet4Address.getLocalHost().getHostName();
+ } catch (UnknownHostException e) {
+
+ }
+ if(s!=null && s.length()>0)
+ return s;
+ }
+ Map<String, String> env = System.getenv();
+ for (String envName : env.keySet()) {
+ if(envName!=null && envName.equals(v))
+ return env.get(envName);
+ }
+ return null;
+ }
+}
diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/util/FileWatchdog.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/util/FileWatchdog.java
new file mode 100644
index 000000000..55fe83300
--- /dev/null
+++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/util/FileWatchdog.java
@@ -0,0 +1,129 @@
+/*******************************************************************************
+ * ============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==========================================================================
+ ******************************************************************************/
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
+ * agreements. See the NOTICE file distributed with this work for additional information regarding
+ * copyright ownership. The ASF licenses this file to You 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.
+ */
+
+package org.onap.ccsdk.features.sdnr.wt.common.util;
+
+import java.io.File;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Check every now and then that a certain file has not changed. If it has, then call the
+ * {@link #doOnChange} method.
+ *
+ * @author JunHo Yoon
+ * @since 3.1.1
+ */
+public abstract class FileWatchdog extends Thread {
+ private static final Logger LOGGER = LoggerFactory.getLogger(FileWatchdog.class);
+ /**
+ * The default delay between every file modification check, set to 60 seconds.
+ */
+ public static final long DEFAULT_DELAY = 60000;
+ /**
+ * The name of the file to observe for changes.
+ */
+ private final String filename;
+
+ /**
+ * The delay to observe between every check. By default set {@link #DEFAULT_DELAY}.
+ */
+ private long delay = DEFAULT_DELAY;
+
+ private final File file;
+ private long lastModified = 0;
+ private boolean warnedAlready = false;
+
+ protected FileWatchdog(String filename) {
+ this.filename = filename;
+ file = new File(filename);
+ setDaemon(true);
+ checkAndConfigure();
+ }
+
+ /**
+ * Set the delay to observe between each check of the file changes.
+ *
+ * @param delay the frequency of file watch.
+ */
+ public void setDelay(long delay) {
+ this.delay = delay;
+ }
+
+ /**
+ * abstract method to be run when the file is changed.
+ */
+ protected abstract void doOnChange();
+
+ protected void checkAndConfigure() {
+ boolean fileExists;
+ try {
+ fileExists = file.exists();
+ } catch (SecurityException e) {
+ LOGGER.warn("Was not allowed to read check file existence, file:[{}].",filename);
+ this.interrupt(); // there is no point in continuing
+ return;
+ }
+
+ if (fileExists) {
+ long l = file.lastModified(); // this can also throw a
+ if (lastModified == 0) {
+ lastModified = l; // is very unlikely.
+ }
+ if (l > lastModified) { // however, if we reached this point this
+ lastModified = l; // is very unlikely.
+ doOnChange();
+ warnedAlready = false;
+ }
+ } else {
+ if (!warnedAlready) {
+ LOGGER.debug("[{}] does not exist.", filename);
+ warnedAlready = true;
+ }
+ }
+ }
+
+ @Override
+ public void run() {
+ while (!isInterrupted()) {
+ checkAndConfigure();
+ try {
+ Thread.sleep(delay);
+ } catch (InterruptedException e) {
+ LOGGER.debug("Interrupted sleep. {}", e.getMessage());
+ Thread.currentThread().interrupt();
+ }
+ }
+ LOGGER.debug("Stoppen file watchdog for file {}", filename);
+ }
+}
diff --git a/sdnr/wt/common/src/test/java/org/onap/ccsdk/features/sdnr/wt/common/test/TestDbClient.java b/sdnr/wt/common/src/test/java/org/onap/ccsdk/features/sdnr/wt/common/test/TestDbClient.java
deleted file mode 100644
index 0d46b4d20..000000000
--- a/sdnr/wt/common/src/test/java/org/onap/ccsdk/features/sdnr/wt/common/test/TestDbClient.java
+++ /dev/null
@@ -1,71 +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.test;
-
-import static org.junit.Assert.*;
-
-import org.junit.BeforeClass;
-import org.junit.Test;
-import org.onap.ccsdk.features.sdnr.wt.common.database.HtDatabaseClient;
-import org.onap.ccsdk.features.sdnr.wt.common.database.SearchHit;
-import org.onap.ccsdk.features.sdnr.wt.common.database.SearchResult;
-import org.onap.ccsdk.features.sdnr.wt.common.database.config.HostInfo;
-import org.onap.ccsdk.features.sdnr.wt.common.database.queries.QueryBuilders;
-
-public class TestDbClient {
-
- private static HtDatabaseClient dbClient;
- private static HostInfo[] hosts = new HostInfo[] { new HostInfo("localhost", Integer
- .valueOf(System.getProperty("databaseport") != null ? System.getProperty("databaseport") : "49200")) };
-
- @BeforeClass
- public static void init() {
-
- dbClient = new HtDatabaseClient(hosts);
- dbClient.waitForYellowStatus(20000);
-
- }
- @Test
- public void testCRUD() {
- final String IDX = "test23-knmoinsd";
- final String ID = "abcddd";
- final String JSON = "{\"data\":{\"inner\":\"more\"}}";
- final String JSON2 = "{\"data\":{\"inner\":\"more2\"}}";
- //Create
- String esId=dbClient.doWriteRaw(IDX, ID, JSON);
- assertEquals("inserted id is wrong",ID,esId);
- //Read
- SearchResult<SearchHit> result = dbClient.doReadByQueryJsonData(IDX, QueryBuilders.matchQuery("_id", ID));
- assertEquals("amount of results is wrong",1,result.getTotal());
- assertEquals("data not valid", JSON,result.getHits().get(0).getSourceAsString());
- //Update
- esId= dbClient.doUpdateOrCreate(IDX, ID, JSON2);
- assertEquals("update response not successfull",ID,esId);
- //Verify
- result = dbClient.doReadByQueryJsonData( IDX, QueryBuilders.matchQuery("_id", ID));
- assertEquals("amount of results is wrong",1,result.getTotal());
- assertEquals("data not valid", JSON2,result.getHits().get(0).getSourceAsString());
- //Delete
- boolean del=dbClient.doRemove(IDX, ID);
- assertTrue("item not deleted",del);
- //Verify
- result = dbClient.doReadByQueryJsonData(IDX, QueryBuilders.matchQuery("_id", ID));
- assertEquals("amount of results is wrong",0,result.getTotal());
- }
-
-}
diff --git a/sdnr/wt/common/src/test/java/org/onap/ccsdk/features/sdnr/wt/common/test/TestDbRequests.java b/sdnr/wt/common/src/test/java/org/onap/ccsdk/features/sdnr/wt/common/test/TestDbRequests.java
deleted file mode 100644
index 36bbebefd..000000000
--- a/sdnr/wt/common/src/test/java/org/onap/ccsdk/features/sdnr/wt/common/test/TestDbRequests.java
+++ /dev/null
@@ -1,412 +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.test;
-
-import org.junit.AfterClass;
-import org.junit.BeforeClass;
-import org.junit.Test;
-import org.onap.ccsdk.features.sdnr.wt.common.database.HtDatabaseClient;
-import org.onap.ccsdk.features.sdnr.wt.common.database.config.HostInfo;
-import org.onap.ccsdk.features.sdnr.wt.common.database.queries.QueryBuilders;
-import org.onap.ccsdk.features.sdnr.wt.common.database.requests.ClusterHealthRequest;
-import org.onap.ccsdk.features.sdnr.wt.common.database.requests.CreateIndexRequest;
-import org.onap.ccsdk.features.sdnr.wt.common.database.requests.DeleteByQueryRequest;
-import org.onap.ccsdk.features.sdnr.wt.common.database.requests.DeleteIndexRequest;
-import org.onap.ccsdk.features.sdnr.wt.common.database.requests.DeleteRequest;
-import org.onap.ccsdk.features.sdnr.wt.common.database.requests.GetRequest;
-import org.onap.ccsdk.features.sdnr.wt.common.database.requests.IndexRequest;
-import org.onap.ccsdk.features.sdnr.wt.common.database.requests.NodeStatsRequest;
-import org.onap.ccsdk.features.sdnr.wt.common.database.requests.RefreshIndexRequest;
-import org.onap.ccsdk.features.sdnr.wt.common.database.requests.SearchRequest;
-import org.onap.ccsdk.features.sdnr.wt.common.database.requests.UpdateByQueryRequest;
-import org.onap.ccsdk.features.sdnr.wt.common.database.requests.UpdateRequest;
-import org.onap.ccsdk.features.sdnr.wt.common.database.responses.ClusterHealthResponse;
-import org.onap.ccsdk.features.sdnr.wt.common.database.responses.CreateIndexResponse;
-import org.onap.ccsdk.features.sdnr.wt.common.database.responses.DeleteByQueryResponse;
-import org.onap.ccsdk.features.sdnr.wt.common.database.responses.DeleteIndexResponse;
-import org.onap.ccsdk.features.sdnr.wt.common.database.responses.DeleteResponse;
-import org.onap.ccsdk.features.sdnr.wt.common.database.responses.GetResponse;
-import org.onap.ccsdk.features.sdnr.wt.common.database.responses.IndexResponse;
-import org.onap.ccsdk.features.sdnr.wt.common.database.responses.NodeStatsResponse;
-import org.onap.ccsdk.features.sdnr.wt.common.database.responses.SearchResponse;
-import org.onap.ccsdk.features.sdnr.wt.common.database.responses.UpdateByQueryResponse;
-import org.onap.ccsdk.features.sdnr.wt.common.database.responses.UpdateResponse;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
-import java.io.IOException;
-import java.util.Arrays;
-import java.util.List;
-
-import org.json.JSONException;
-import org.json.JSONObject;
-
-public class TestDbRequests {
-
- private static HtDatabaseClient dbClient;
- private static HostInfo[] hosts = new HostInfo[] { new HostInfo("localhost", Integer
- .valueOf(System.getProperty("databaseport") != null ? System.getProperty("databaseport") : "49200")) };
-
- @BeforeClass
- public static void init() {
-
- dbClient = new HtDatabaseClient(hosts);
-
- }
- @AfterClass
- public static void deinit() {
- if(dbClient!=null) {
- dbClient.close();
- }
- }
- @Test
- public void testHealth() {
-
- ClusterHealthResponse response = null;
- ClusterHealthRequest request = new ClusterHealthRequest();
- request.timeout(10);
- try {
- response = dbClient.health(request);
- } catch (UnsupportedOperationException | IOException | JSONException e) {
- fail(e.getMessage());
- }
- assertNotNull("response is null", response);
- assertTrue(response.isStatusMinimal(ClusterHealthResponse.HEALTHSTATUS_YELLOW));
- }
-
- @Test
- public void testCount() {
-
- }
-
- @Test
- public void testCreateAndDeleteIndex() {
- final String IDX = "testcidx1";
- CreateIndexRequest request = new CreateIndexRequest(IDX);
- CreateIndexResponse response = null;
- try {
- response = dbClient.createIndex(request);
- } catch (IOException e) {
- fail(e.getMessage());
- }
- assertNotNull(response);
-
- assertTrue("index not existing", dbClient.isExistsIndex(IDX));
-
- DeleteIndexRequest request2 = new DeleteIndexRequest(IDX);
-
- DeleteIndexResponse response2 = null;
- try {
- response2 = dbClient.deleteIndex(request2);
- } catch (IOException e) {
- fail(e.getMessage());
- }
- assertNotNull(response2);
- assertFalse("index still existing", dbClient.isExistsIndex(IDX));
- this.deleteIndex(IDX);
- }
-
- @Test
- public void testInsertAndDelete() {
- final String IDX = "test23-knmoinsd";
- final String ID = "abcddd";
- final String JSON = "{\"data\":{\"inner\":\"more\"}}";
- this.insert(IDX, ID, JSON);
- // delete data
- DeleteRequest request2 = new DeleteRequest(IDX, IDX, ID);
- DeleteResponse response2 = null;
- try {
- response2 = dbClient.delete(request2);
- } catch (IOException e) {
- fail(e.getMessage());
- }
- assertNotNull(response2);
- assertTrue(response2.isDeleted());
- try {
- dbClient.refreshIndex(new RefreshIndexRequest(IDX));
- } catch (IOException e) {
- fail(e.getMessage());
- }
- // verify data deleted
- GetRequest request4 = new GetRequest(IDX, IDX, ID);
- GetResponse response4 = null;
- try {
- response4 = dbClient.get(request4);
- } catch (IOException e1) {
- fail(e1.getMessage());
- }
- assertNotNull(response4);
- assertFalse("data still existing", response4.isExists());
- this.deleteIndex(IDX);
- }
-
- @Test
- public void testInsertAndDeleteByQuery() {
- final String IDX = "test34-knmoinsd";
- final String ID = "abcdddseae";
- final String JSON = "{\"data\":{\"inner\":\"more\"}}";
- this.insert(IDX, ID, JSON);
-
- // delete data
- DeleteByQueryRequest request2 = new DeleteByQueryRequest(IDX);
- request2.source(QueryBuilders.matchQuery("_id", ID));
- DeleteByQueryResponse response2 = null;
- try {
- response2 = dbClient.deleteByQuery(request2);
- } catch (IOException e) {
- fail(e.getMessage());
- }
- assertNotNull(response2);
- assertTrue(response2.isResponseSucceeded());
- try {
- dbClient.refreshIndex(new RefreshIndexRequest(IDX));
- } catch (IOException e) {
- fail(e.getMessage());
- }
- // verify data deleted
- GetRequest request4 = new GetRequest(IDX, IDX, ID);
- GetResponse response4 = null;
- try {
- response4 = dbClient.get(request4);
- } catch (IOException e1) {
- fail(e1.getMessage());
- }
- assertNotNull(response4);
- assertFalse("data still existing", response4.isExists());
- this.deleteIndex(IDX);
- }
-
- private void insert(String IDX, String ID, String JSON) {
-
- // create data
- IndexRequest request = new IndexRequest(IDX, IDX, ID);
- request.source(JSON);
- IndexResponse response = null;
- try {
- response = dbClient.index(request);
- } catch (IOException e) {
- fail(e.getMessage());
- }
- assertNotNull(response);
- if (ID != null) {
- assertEquals("id not correct", ID, response.getId());
- } else {
- ID = response.getId();
- }
- // do db refresh
- try {
- dbClient.refreshIndex(new RefreshIndexRequest(IDX));
- } catch (IOException e) {
- fail(e.getMessage());
- }
- // verify data exists
- GetRequest request3 = new GetRequest(IDX, IDX, ID);
- GetResponse response3 = null;
- try {
- response3 = dbClient.get(request3);
- } catch (IOException e1) {
- fail(e1.getMessage());
- }
- assertNotNull(response3);
- JSONAssert.assertEquals("could not verify update", JSON, response3.getSourceAsBytesRef(), true);
- }
-
- @Test
- public void testSearch() {
- final String IDX = "test44-moinsd";
- final String ID = "abe";
- final String JSON = "{\"data\":{\"inner\":\"more\"}}";
- final String ID2 = "abe2";
- final String JSON2 = "{\"data\":{\"inner\":\"more2\"}}";
- final String ID3 = "abe3";
- final String JSON3 = "{\"data\":{\"inner\":\"more3\"}}";
- this.insert(IDX, ID, JSON);
- this.insert(IDX, ID2, JSON2);
- this.insert(IDX, ID3, JSON3);
- SearchRequest request = new SearchRequest(IDX, IDX);
- request.setQuery(QueryBuilders.matchAllQuery());
- SearchResponse response = null;
- try {
- response = dbClient.search(request);
- } catch (IOException e) {
- fail(e.getMessage());
- }
- assertNotNull(response);
- assertEquals("not all items found", 3, response.getHits().length);
- assertEquals("incorrect index",IDX,response.getHits()[0].getIndex());
- assertEquals("incorrect type",IDX,response.getHits()[0].getType());
- this.deleteIndex(IDX);
- }
-
- @Test
- public void testUpdate() {
- final String IDX = "test4534-moinsd";
- final String ID = "assbe";
- final String JSON = "{\"data\":{\"inner\":\"more\"}}";
- final String JSON2 = "{\"data\":{\"inner\":\"more2\"},\"data2\":\"value2\",\"data3\":true}";
-
- this.insert(IDX, ID, JSON);
- UpdateRequest request = new UpdateRequest(IDX, IDX, ID);
- UpdateResponse response = null;
- try {
- request.source(new JSONObject(JSON2));
- response = dbClient.update(request);
- } catch (JSONException | IOException e) {
- fail(e.getMessage());
- }
- assertNotNull(response);
- assertTrue(response.succeeded());
- // refresh index
- try {
- dbClient.refreshIndex(new RefreshIndexRequest(IDX));
- } catch (IOException e) {
- fail(e.getMessage());
- }
- // verify update
- GetRequest request3 = new GetRequest(IDX, IDX, ID);
- GetResponse response3 = null;
- try {
- response3 = dbClient.get(request3);
- } catch (IOException e1) {
- fail(e1.getMessage());
- }
- assertNotNull(response3);
- JSONAssert.assertEquals("could not verify update", JSON2, response3.getSourceAsBytesRef(), true);
- this.deleteIndex(IDX);
- }
-
- @Test
- public void testUpdateByQuery() {
- final String IDX = "test224534-moinsd";
- final String ID = "asssabe";
- final String JSON = "{\"data\":{\"inner\":\"more\"}}";
- final String JSON2 = "{\"data\":{\"inner\":\"more2\"},\"data2\":\"value2\",\"data3\":true}";
-
- this.insert(IDX, ID, JSON);
- UpdateByQueryRequest request = new UpdateByQueryRequest(IDX, IDX);
- UpdateByQueryResponse response = null;
- try {
- request.source(ID, new JSONObject(JSON2));
- response = dbClient.update(request);
- } catch (JSONException | IOException e) {
- fail(e.getMessage());
- }
- assertNotNull(response);
- assertTrue(response.isUpdated());
- // refresh index
- try {
- dbClient.refreshIndex(new RefreshIndexRequest(IDX));
- } catch (IOException e) {
- fail(e.getMessage());
- }
- // verify update
- GetRequest request3 = new GetRequest(IDX, IDX, ID);
- GetResponse response3 = null;
- try {
- response3 = dbClient.get(request3);
- } catch (IOException e1) {
- fail(e1.getMessage());
- }
- assertNotNull(response3);
- JSONAssert.assertEquals("could not verify update", JSON2, response3.getSourceAsBytesRef(), true);
- this.deleteIndex(IDX);
- }
-
- @Test
- public void testAggregations() {
- final String IDX = "test3227533677-moisnsd";
- final String JSON = "{ \"node-id\":\"sim1\",\"severity\":\"critical\"}";
- final String JSON2 = "{ \"node-id\":\"sim2\",\"severity\":\"critical\"}";
- final String JSON3 = "{ \"node-id\":\"sim3\",\"severity\":\"minor\"}";
- final String JSON4 = "{ \"node-id\":\"sim4\",\"severity\":\"warning\"}";
- final String JSON5 = "{ \"node-id\":\"sim5\",\"severity\":\"major\"}";
- final String MAPPINGS = "{\""+IDX+"\":{\"properties\":{\"node-id\": {\"type\": \"keyword\"},\"severity\": {\"type\": \"keyword\"}}}}";
- //create index with mapping keyword
- CreateIndexRequest irequest = new CreateIndexRequest(IDX);
- irequest.mappings(new JSONObject(MAPPINGS));
- CreateIndexResponse iresponse = null;
- try {
- iresponse = dbClient.createIndex(irequest);
- } catch (IOException e1) {
- fail("unable to create index: "+e1.getMessage());
- }
- assertNotNull(iresponse);
- assertTrue(iresponse.isAcknowledged());
- // fill index
- this.insert(IDX, null, JSON);
- this.insert(IDX, null, JSON2);
- this.insert(IDX, null, JSON3);
- this.insert(IDX, null, JSON4);
- this.insert(IDX, null, JSON5);
- // refresh index
- try {
- dbClient.refreshIndex(new RefreshIndexRequest(IDX));
- } catch (IOException e) {
- fail(e.getMessage());
- }
-
- SearchRequest request = new SearchRequest(IDX, IDX);
- request.setQuery(QueryBuilders.matchAllQuery().aggregations("severity").size(0));
- SearchResponse response = null;
- try {
- response = dbClient.search(request);
- } catch (IOException e) {
- fail(e.getMessage());
- }
- assertNotNull(response);
- assertTrue(response.hasAggregations());
- assertEquals("aggregation size not correct", 4, response.getAggregations("severity").size());
-
- List<String> items1 = Arrays.asList(response.getAggregations("severity").getKeysAsPagedStringList(2, 0));
- List<String> items2 = Arrays.asList(response.getAggregations("severity").getKeysAsPagedStringList(2, 2));
- assertEquals("pagination does not work", 2,items1.size());
- assertEquals("pagination does not work", 2,items2.size());
- for(String s:items1) {
- assertFalse("pagination overlap is not allowed",items2.contains(s));
- }
- for(String s:items2) {
- assertFalse("pagination overlap is not allowed",items1.contains(s));
- }
-
- this.deleteIndex(IDX);
- }
-
- @Test
- public void testStatistics() {
- NodeStatsResponse stats=null;
- try {
- stats = dbClient.stats(new NodeStatsRequest());
- } catch (IOException e) {
- fail(e.getMessage());
- }
- assertNotNull(stats);
- System.out.println(stats.getNodesInfo());
- System.out.println(stats.getNodeStatistics());
- }
- private void deleteIndex(String idx) {
- try {
- dbClient.deleteIndex( new DeleteIndexRequest(idx));
- } catch (IOException e) {
-
- }
- }
-
-}