diff options
Diffstat (limited to 'sdnr/wt/common/src/main')
66 files changed, 4962 insertions, 0 deletions
diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/PropertyService.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/PropertyService.java new file mode 100644 index 000000000..3fa9b6982 --- /dev/null +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/PropertyService.java @@ -0,0 +1,23 @@ +/******************************************************************************* + * ============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 interface PropertyService { + + public String getProperty(String property); +} 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 new file mode 100644 index 000000000..3597f2e57 --- /dev/null +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/Resources.java @@ -0,0 +1,352 @@ +/******************************************************************************* + * ============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 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; + LOG.debug("try to get file {}", resFile); + if (b == null) { + LOG.info("Load resource as file: {}", resFile); + u = getUrlForRessource(cls,resFile); + } else { + LOG.info("Load resource from bundle: {}", resFile); + u = b.getEntry(resFile); + } + 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()); + } + + private static String readFile(final InputStream s) throws IOException { + // read file + final String LR = "\n"; + BufferedReader in = new BufferedReader(new InputStreamReader(s)); + StringBuilder sb = new StringBuilder(); + String inputLine; + while ((inputLine = in.readLine()) != null) { + sb.append(inputLine+LR); + } + in.close(); + s.close(); + 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); + String s=null; + if (u == null) { + LOG.warn("cannot find resfile: {}", resFile); + return null; + } + try { + s=readFile(u); + } catch (Exception e) { + LOG.warn("problem reading file: {}", e.getMessage()); + } + 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(); + ClassLoader loader = cls.getClassLoader(); + URL url = loader.getResource(fileOrDirectory); + if(url==null && fileOrDirectory.startsWith("/")) { + url = loader.getResource(fileOrDirectory.substring(1)); + } + 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/configuration/Configuration.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/configuration/Configuration.java new file mode 100644 index 000000000..619191766 --- /dev/null +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/configuration/Configuration.java @@ -0,0 +1,28 @@ +/******************************************************************************* + * ============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.configuration; + +/** + * Marker interface for configurations + */ +public interface Configuration { + + String getSectionName(); + + void defaults(); +} diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/configuration/ConfigurationFileRepresentation.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/configuration/ConfigurationFileRepresentation.java new file mode 100644 index 000000000..36673d318 --- /dev/null +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/configuration/ConfigurationFileRepresentation.java @@ -0,0 +1,235 @@ +/******************************************************************************* + * ============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.configuration; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.util.HashMap; +import java.util.Optional; + +import org.onap.ccsdk.features.sdnr.wt.common.configuration.filechange.ConfigFileObserver; +import org.onap.ccsdk.features.sdnr.wt.common.configuration.filechange.IConfigChangedListener; +import org.onap.ccsdk.features.sdnr.wt.common.configuration.subtypes.Section; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +/** + * Representation of configuration file with section.<br> + * A root section is used for parameters, not assigned to a specific section.<br> + * The definitions of the configuration are attributes of a java class<br> + */ +public class ConfigurationFileRepresentation implements IConfigChangedListener { + + private static final Logger LOG = LoggerFactory.getLogger(ConfigurationFileRepresentation.class); + + private static final long FILE_POLL_INTERVAL_MS = 1000; + private static final String SECTIONNAME_ROOT = ""; + private static final String LR = "\n"; + private static final String EMPTY = ""; + + /** Related configuration file **/ + private final File mFile; + /** Monitor changes of file **/ + private final ConfigFileObserver fileObserver; + /** List of sections **/ + private final HashMap<String, Section> sections; + + public ConfigurationFileRepresentation(File f) { + + this.mFile = f; + this.sections = new HashMap<String, Section>(); + try { + if (!this.mFile.exists()) { + if (!this.mFile.createNewFile()) { + LOG.error("Can not create file {}", f.getAbsolutePath()); + } + } + reLoad(); + + } catch (IOException e) { + LOG.error("Problem loading config file {} : {}", f.getAbsolutePath(), e.getMessage()); + } + this.fileObserver = new ConfigFileObserver(f.getAbsolutePath(), FILE_POLL_INTERVAL_MS); + this.fileObserver.start(); + this.fileObserver.registerConfigChangedListener(this); + } + + public ConfigurationFileRepresentation(String configurationfile) { + this(new File(configurationfile)); + } + + public Optional<Section> getSection(String name) { + return Optional.ofNullable(sections.get(name)); + } + + public Section addSection(String name) { + if (this.sections.containsKey(name)) { + return this.sections.get(name); + } + Section s = new Section(name); + this.sections.put(name, s); + return s; + } + + public void reLoad() { + sections.clear(); + addSection(SECTIONNAME_ROOT); + load(); + } + + public void save() { + LOG.debug("Write configuration to {}", getMFileName()); + try (BufferedWriter bw = new BufferedWriter(new FileWriter(this.mFile, false))) { + for (Section section : this.sections.values()) { + if (section.hasValues()) { + bw.write(String.join(LR, section.toLines()) + LR + LR); + } + } + bw.close(); + } catch (Exception e) { + LOG.warn("problem saving value: " + e.getMessage()); + } + } + + public void registerConfigChangedListener(IConfigChangedListener l) { + this.fileObserver.registerConfigChangedListener(l); + } + + public void unregisterConfigChangedListener(IConfigChangedListener l) { + this.fileObserver.unregisterConfigChangedListener(l); + } + + @Override + public void onConfigChanged() { + LOG.debug("Reload on change {}", getMFileName()); + reLoad(); + } + + @Override + public String toString() { + return "ConfigurationFileRepresentation [mFile=" + mFile + ", sections=" + sections + "]"; + } + + @Override + protected void finalize() throws Throwable { + if (this.fileObserver != null) { + this.fileObserver.interrupt(); + } + super.finalize(); + } + + /* + * Property access set/get + */ + public void setProperty(String section, String key, Object value) { + Optional<Section> os = this.getSection(section); + if (os.isPresent()) { + os.get().setProperty(key, value == null ? "null" : value.toString()); + save(); + } else { + LOG.info("Unknown configuration section {}", section); + } + } + + public String getProperty(String section, String propertyKey) { + Optional<Section> os = this.getSection(section); + if (os.isPresent()) { + return os.get().getProperty(propertyKey); + } else { + LOG.debug("Unknown configuration section {}", section); + return EMPTY; + } + } + + public Optional<Long> getPropertyLong(String section, String propertyKey) { + Optional<Section> os = this.getSection(section); + if (os.isPresent()) { + return os.get().getLong(propertyKey); + } else { + LOG.debug("Unknown configuration section {}", section); + return Optional.empty(); + } + } + + public boolean isPropertyAvailable(String section, String propertyKey) { + Optional<Section> s = this.getSection(section); + return s.isPresent() && s.get().hasKey(propertyKey); + } + + public void setPropertyIfNotAvailable(String section, String propertyKey, + Object propertyValue) { + if (! isPropertyAvailable(section, propertyKey)) { + setProperty(section, propertyKey, propertyValue.toString()); + } + } + + public boolean getPropertyBoolean(String section, String propertyKey) { + return getProperty(section, propertyKey).equalsIgnoreCase("true"); + } + + /* + * Private + */ + private void load() { + LOG.debug("loading file {}", getMFileName()); + String curSectionName = SECTIONNAME_ROOT; + Optional<Section> sectionOptional = this.getSection(curSectionName); + Section curSection=sectionOptional.isPresent()?sectionOptional.get():this.addSection(curSectionName); + BufferedReader br = null; + try { + br = new BufferedReader(new FileReader(this.mFile)); + for (String line; (line = br.readLine()) != null;) { + line = line.trim(); + if (line.isEmpty()) { + continue; + } + if (line.startsWith("[") && line.endsWith("]")) { + curSectionName = line.substring(1, line.length() - 1); + curSection = this.addSection(curSectionName); + } else { + curSection.addLine(line); + } + } + + } catch (Exception e) { + LOG.info("Problem loading configuration file. {} {}", getMFileName(), e); + } finally { + try { + if (br != null) { + br.close(); + } + } catch (IOException e) { + } + } + LOG.debug("finished loading file"); + LOG.debug("start parsing sections"); + for (Section section : this.sections.values()) { + section.parseLines(); + } + LOG.debug("finished parsing " + this.sections.size() + " sections"); + } + + private String getMFileName() { + return mFile.getAbsolutePath(); + } +} diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/configuration/ISubConfigHandler.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/configuration/ISubConfigHandler.java new file mode 100644 index 000000000..f6d3601e9 --- /dev/null +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/configuration/ISubConfigHandler.java @@ -0,0 +1,22 @@ +/******************************************************************************* + * ============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.configuration; + +public interface ISubConfigHandler { + void save(); +} diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/configuration/exception/ConfigurationException.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/configuration/exception/ConfigurationException.java new file mode 100644 index 000000000..18ed409a7 --- /dev/null +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/configuration/exception/ConfigurationException.java @@ -0,0 +1,27 @@ +/******************************************************************************* + * ============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.configuration.exception; + +public class ConfigurationException extends Exception { + + private static final long serialVersionUID = 733061908616404383L; + + public ConfigurationException(String m) { + super(m); + } +}
\ No newline at end of file diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/configuration/exception/ConversionException.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/configuration/exception/ConversionException.java new file mode 100644 index 000000000..d261c73f0 --- /dev/null +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/configuration/exception/ConversionException.java @@ -0,0 +1,27 @@ +/******************************************************************************* + * ============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.configuration.exception; + +public class ConversionException extends Exception { + private static final long serialVersionUID = 5179891576029923079L; + + public ConversionException(String m) { + super(m); + } +} + diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/configuration/filechange/ConfigFileObserver.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/configuration/filechange/ConfigFileObserver.java new file mode 100644 index 000000000..78f116641 --- /dev/null +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/configuration/filechange/ConfigFileObserver.java @@ -0,0 +1,57 @@ +/******************************************************************************* + * ============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.configuration.filechange; + +import java.util.ArrayList; +import java.util.List; + +import javax.annotation.Nonnull; + +import org.onap.ccsdk.features.sdnr.wt.common.file.FileWatchdog; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ConfigFileObserver extends FileWatchdog { + + private static final Logger LOG = LoggerFactory.getLogger(ConfigFileObserver.class); + + private final List<IConfigChangedListener> mConfigChangedHandlers = new ArrayList<>(); + + public ConfigFileObserver(String filename, long pollIntervallMs) { + super(filename); + this.setDelay(pollIntervallMs); + } + + @Override + protected void doOnChange() { + LOG.debug("property file has changed"); + for (IConfigChangedListener listener : this.mConfigChangedHandlers) { + listener.onConfigChanged(); + } + } + + public void registerConfigChangedListener(@Nonnull IConfigChangedListener l) { + if (!this.mConfigChangedHandlers.contains(l)) { + this.mConfigChangedHandlers.add(l); + } + } + + public void unregisterConfigChangedListener(IConfigChangedListener l) { + this.mConfigChangedHandlers.remove(l); + } +} diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/configuration/filechange/IConfigChangedListener.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/configuration/filechange/IConfigChangedListener.java new file mode 100644 index 000000000..32f11cd57 --- /dev/null +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/configuration/filechange/IConfigChangedListener.java @@ -0,0 +1,22 @@ +/******************************************************************************* + * ============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.configuration.filechange; + +public interface IConfigChangedListener { + void onConfigChanged(); +} 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 new file mode 100644 index 000000000..a0e21be01 --- /dev/null +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/configuration/subtypes/Section.java @@ -0,0 +1,196 @@ +/******************************************************************************* + * ============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.configuration.subtypes; + +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map.Entry; +import java.util.Optional; + +import org.onap.ccsdk.features.sdnr.wt.common.configuration.exception.ConversionException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class Section { + + private static final Logger LOG = LoggerFactory.getLogger(Section.class); + private static final String DELIMITER = "="; + private static final String COMMENTCHARS[] = {"#", ";"}; + + private final String name; + private final List<String> rawLines; + private final LinkedHashMap<String, SectionValue> values; + + public Section(String name) { + LOG.debug("new section created: '{}'", name); + this.name = name; + this.rawLines = new ArrayList<>(); + this.values = new LinkedHashMap<>(); + } + + public void addLine(String line) { + LOG.trace("adding raw line:" + line); + this.rawLines.add(line); + } + + public String getProperty(String key) { + return this.getProperty(key, null); + } + + public String getProperty(String key, String defValue) { + if (values.containsKey(key)) { + return values.get(key).getValue(); + } + return defValue; + } + + public String getName() { + return name; + } + + public void setProperty(String key, String value) { + boolean isuncommented = this.isCommentLine(key); + if (isuncommented) { + key = key.substring(1); + } + if (this.values.containsKey(key)) { + this.values.get(key).setValue(value).setIsUncommented(isuncommented); + } else { + this.values.put(key, new SectionValue(value,isuncommented)); + } + } + + public void parseLines() { + this.values.clear(); + List<String> commentsForValue = new ArrayList<>(); + boolean uncommented = false; + for (String line : rawLines) { + + if (this.isCommentLine(line)) { + if (!line.contains(DELIMITER)) { + commentsForValue.add(line); + continue; + } else { + uncommented = true; + line = line.substring(1); + } + } + if (!line.contains(DELIMITER)) { + continue; + } + String hlp[] = line.split(DELIMITER); + if (hlp.length > 1) { + String key = hlp[0]; + String value = + line.length() > (key + DELIMITER).length() ? line.substring((key + DELIMITER).length()) + : ""; + if (this.values.containsKey(key)) { + this.values.get(key).setValue(value); + } else { + this.values.put(key, new SectionValue(value, commentsForValue, uncommented)); + commentsForValue = new ArrayList<>(); + } + } else { + LOG.warn("ignoring unknown formatted line:" + line); + } + uncommented = false; + } + } + + private boolean isCommentLine(String line) { + for (String c : COMMENTCHARS) { + if (line.startsWith(c)) { + return true; + } + } + return false; + } + + public String[] toLines() { + List<String> lines = new ArrayList<>(); + if (!this.name.isEmpty()) { + lines.add("[" + this.name + "]"); + } + for (Entry<String, SectionValue> entry : this.values.entrySet()) { + SectionValue sectionValue = entry.getValue(); + if (sectionValue.getComments().size() > 0) { + for (String comment : sectionValue.getComments()) { + lines.add(comment); + } + } + lines.add((sectionValue.isUncommented() ? COMMENTCHARS[0] : "") + entry.getKey() + DELIMITER + + sectionValue.getValue()); + } + String[] alines = new String[lines.size()]; + return lines.toArray(alines); + } + + public String getString(String key, String def) { + return this.getProperty(key, def); + } + + public boolean getBoolean(String key, boolean def) throws ConversionException { + String v = this.getProperty(key); + if (v == null || v.isEmpty()) { + return def; + } + if (v.equals("true")) { + return true; + } + if (v.equals("false")) { + return false; + } + throw new ConversionException("invalid value for key " + key); + } + + public int getInt(String key, int def) throws ConversionException { + String v = this.getProperty(key); + if (v == null || v.isEmpty()) { + return def; + } + try { + return Integer.parseInt(v); + } catch (NumberFormatException e) { + throw new ConversionException(e.getMessage()); + } + } + + public Optional<Long> getLong(String key) { + String v = this.getProperty(key); + try { + return Optional.of(Long.parseLong(v)); + } catch (NumberFormatException e) { + } + return Optional.empty(); + } + + public boolean hasValues() { + return this.values.size() > 0; + } + + public boolean hasKey(String key) { + return this.values.containsKey(key); + } + + @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/configuration/subtypes/SectionValue.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/configuration/subtypes/SectionValue.java new file mode 100644 index 000000000..8af072493 --- /dev/null +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/configuration/subtypes/SectionValue.java @@ -0,0 +1,73 @@ +/******************************************************************************* + * ============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.configuration.subtypes; + +import java.util.ArrayList; +import java.util.List; + +class SectionValue { + + private String value; + private final List<String> comments; + private boolean isUncommented; + + public SectionValue(String value, List<String> commentsForValue, boolean isuncommented) { + this.comments = commentsForValue; + this.value = value; + this.isUncommented = isuncommented; + } + + public SectionValue(String value) { + this(value, new ArrayList<String>(), false); + } + + public SectionValue(String value, boolean isUncommented) { + this(value, new ArrayList<String>(), isUncommented); + } + + /* Getter / Setter */ + + public String getValue() { + return value; + } + + public SectionValue setValue(String value) { + this.value = value; + return this; + } + + public boolean isUncommented() { + return isUncommented; + } + + public SectionValue setIsUncommented(boolean isUncommented) { + this.isUncommented = isUncommented; + return this; + } + + public List<String> getComments() { + return comments; + } + + @Override + public String toString() { + return "SectionValue [value=" + value + ", comments=" + comments + ", isUncommented=" + isUncommented + "]"; + } + + +} 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 new file mode 100644 index 000000000..78501a8db --- /dev/null +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/DatabaseClient.java @@ -0,0 +1,162 @@ +/******************************************************************************* + * ============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.database; + +import java.util.List; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + +import org.onap.ccsdk.features.sdnr.wt.common.database.queries.QueryBuilder; + +/** + * Access elasticsearch database + */ + +public interface DatabaseClient { + + /** + * Read JSON Object from database + * @param dataTypeName to read + * @param esId to provide id to read + * @return String with json structure + */ + public @Nullable String doReadJsonData(String dataTypeName, @Nonnull IsEsObject esId ); + + /** + * Read JSON Object from database + * @param dataTypeName to read + * @param esId of object to read + * @return String with json structure + */ + public @Nullable String doReadJsonData(String dataTypeName, @Nonnull String esId); + + /** + * Provide all Objects of the specified dataTypeName. + * @param dataTypeName to be used + * @return SearchResult with list of elements + */ + public @Nonnull SearchResult<SearchHit> doReadAllJsonData(String dataTypeName); + + /** + * Provide all Objects that are covered by query. + * @param dataTypeName to be used + * @param queryBuilder with the query to be used. + * @return SearchResult with list of elements + */ + public @Nonnull SearchResult<SearchHit> doReadByQueryJsonData(String dataTypeName, QueryBuilder queryBuilder); + /** + * Write one object into Database + * + * @param esId Database index + * @param dataTypeName Name of datatype + * @param json String in JSON format. + * @return esId String of the database object or null in case of write problems. + */ + public @Nullable String doWriteJsonString( String dataTypeName, @Nonnull IsEsObject esId, String json); + + /** + * Write one object into Database + * @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 dataTypeName, @Nullable String esId, String json); + + /** + * Remove Object from database + * @param dataTypeName of object + * @param esId of object to be deleted + * @return success + */ + public boolean doRemove( String dataTypeName, IsEsObject esId ); + + /** + * Remove Object from database + * @param dataTypeName of object + * @param esId as String of object to be deleted + * @return success + */ + boolean doRemove(String dataTypeName, String esId); + + /** + * Verify if index already created + * @param dataTypeName to be verified. + * @return boolean accordingly + */ + public boolean isExistsIndex(String dataTypeName); + + /** + * Update one object in Database with id=esId or create if not exists. + * @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 String doUpdateOrCreate(String dataTypeName, String esId, String json); + + /** + * Update one object in Database with id=esId or create if not exists. + * @param dataTypeName Name of datatype + * @param esId to use for DB object + * @param json object to write + * @param doNotUpdateField Fields that are not updated, but inserted if DB Object not existing in DB + * @return esId as String or null of not successfully + */ + public String doUpdateOrCreate(String dataTypeName, String esId, String json, List<String> doNotUpdateField); + + /** + * remove items from database by query + * @param dataTypeName Name of datatype + * @param query query to select items to remove + * @return count of removed items + */ + public int doRemove(String dataTypeName, QueryBuilder query); + + /** + * update object in database + * @param dataTypeName Name of datatype + * @param json dataobject + * @param query query to select item to update + * @return esId which was updated or null if failed + */ + public String doUpdate(String dataTypeName, String json, QueryBuilder query); + + /** + * + * @param dataTypeName Name of datatype + * @param queryBuilder query to select items to read + * @param ignoreException flag if serverside exception will be thrown if query is not valid (needed for user entered filters) + * @return results + */ + SearchResult<SearchHit> doReadByQueryJsonData(String dataTypeName, QueryBuilder queryBuilder, + boolean ignoreException); + + + /** + * read all data + * @param dataTypeName Name of datatype + * @param ignoreException flag if serverside exception will be thrown if query is not valid (needed for user entered filters) + * @return results + */ + SearchResult<SearchHit> doReadAllJsonData(String dataTypeName, boolean ignoreException); + + + +} diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/DatabaseConfig.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/DatabaseConfig.java new file mode 100644 index 000000000..828ae8bb0 --- /dev/null +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/DatabaseConfig.java @@ -0,0 +1,26 @@ +/******************************************************************************* + * ============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.database; + +import org.onap.ccsdk.features.sdnr.wt.common.database.config.HostInfo; + +public interface DatabaseConfig { + + HostInfo[] getHosts(); + +} 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 new file mode 100644 index 000000000..0e544acec --- /dev/null +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/ExtRestClient.java @@ -0,0 +1,216 @@ +/******************************************************************************* + * ============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.database; + +import java.io.IOException; +import org.apache.http.HttpHost; +import org.elasticsearch.client.Response; +import org.elasticsearch.client.ResponseException; +import org.elasticsearch.client.RestClient; +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; +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.GetIndexRequest; +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.IndicesAliasesRequest; +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.AcknowledgedResponse; +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.RefreshIndexResponse; +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 org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +public class ExtRestClient { + + private static final Logger LOGGER = LoggerFactory.getLogger(ExtRestClient.class); + + private RestClient client; + + public ExtRestClient(RestClient client) { + this.client = client; + } + + public ClusterHealthResponse health(ClusterHealthRequest request) + throws UnsupportedOperationException, IOException, JSONException { + return new ClusterHealthResponse(this.client.performRequest(request.getInner())); + } + + public void close() throws IOException { + this.client.close(); + + } + // + public boolean indicesExists(GetIndexRequest request) throws IOException { + Response response = this.client.performRequest(request.getInner()); + return response.getStatusLine().getStatusCode()==200; + } + + public AcknowledgedResponse updateAliases(IndicesAliasesRequest request) throws IOException{ + return new AcknowledgedResponse(this.client.performRequest(request.getInner())); + } + + public CreateIndexResponse createIndex(CreateIndexRequest request) throws IOException { + CreateIndexResponse response = new CreateIndexResponse(this.client.performRequest(request.getInner())); + return response; + } + public DeleteIndexResponse deleteIndex(DeleteIndexRequest request) throws IOException { + return new DeleteIndexResponse(this.client.performRequest(request.getInner())); + } + public IndexResponse index(IndexRequest request) throws IOException{ + return new IndexResponse(this.client.performRequest(request.getInner())); + } + + public DeleteResponse delete(DeleteRequest request) throws IOException{ + Response response=null; + try { + response = this.client.performRequest(request.getInner()); + } + catch(ResponseException e) { + new DeleteResponse(e.getResponse()); + } + return new DeleteResponse(response); + } + public DeleteByQueryResponse deleteByQuery(DeleteByQueryRequest request) throws IOException { + Response response=null; + try { + response = this.client.performRequest(request.getInner()); + } + catch(ResponseException e) { + new DeleteResponse(e.getResponse()); + } + return new DeleteByQueryResponse(response); + } + public SearchResponse search(SearchRequest request) throws IOException{ + return this.search(request,false); + } + + /** + * + * @param request + * @param ignoreParseException especially for usercreated filters which may cause ES server response exceptions + * @return + * @throws IOException + */ + public SearchResponse search(SearchRequest request, boolean ignoreParseException) throws IOException { + if (ignoreParseException) { + try { + return new SearchResponse(this.client.performRequest(request.getInner())); + } catch (ResponseException e) { + LOGGER.debug("ignoring Exception for request {}: {}",request,e.getMessage()); + return new SearchResponse(e.getResponse()); + } + } else { + return new SearchResponse(this.client.performRequest(request.getInner())); + } + } + + public GetResponse get(GetRequest request) throws IOException{ + try { + return new GetResponse(this.client.performRequest(request.getInner())); + } + catch (ResponseException e) { + return new GetResponse(e.getResponse()); + } + } + public UpdateByQueryResponse update(UpdateByQueryRequest request) throws IOException { + return new UpdateByQueryResponse(this.client.performRequest(request.getInner())); + + } + public UpdateResponse update(UpdateRequest request) throws IOException { + return new UpdateResponse(this.client.performRequest(request.getInner())); + + } + public RefreshIndexResponse refreshIndex(RefreshIndexRequest request) throws IOException{ + return new RefreshIndexResponse(this.client.performRequest(request.getInner())); + } + + public NodeStatsResponse stats(NodeStatsRequest request) throws IOException{ + return new NodeStatsResponse(this.client.performRequest(request.getInner())); + } + + public boolean waitForYellowStatus(long timeoutms) { + + ClusterHealthRequest request = new ClusterHealthRequest(); + request.timeout(timeoutms/1000); + ClusterHealthResponse response = null; + String status=""; + try { + response = this.health(request); + + } catch (UnsupportedOperationException | IOException | JSONException e) { + LOGGER.error(e.getMessage()); + } + if(response!=null) { + status=response.getStatus(); + LOGGER.debug("Elasticsearch service started with status {}", response.getStatus()); + + } + else { + LOGGER.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++) { + httphosts[i]=new HttpHost(hosts[i].hostname, hosts[i].port, hosts[i].protocol.toString()); + } + 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()); + } + public static ExtRestClient createInstance(String hostname, int port, Protocol protocol) throws InvalidProtocolException { + 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 new file mode 100644 index 000000000..c3bd7c5eb --- /dev/null +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/HtDatabaseClient.java @@ -0,0 +1,286 @@ +/******************************************************************************* + * ============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.database; + +import java.io.IOException; +import java.util.List; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + +import org.json.JSONObject; +import org.onap.ccsdk.features.sdnr.wt.common.database.config.HostInfo; +import org.onap.ccsdk.features.sdnr.wt.common.database.queries.QueryBuilder; +import org.onap.ccsdk.features.sdnr.wt.common.database.queries.QueryBuilders; +import org.onap.ccsdk.features.sdnr.wt.common.database.requests.DeleteByQueryRequest; +import org.onap.ccsdk.features.sdnr.wt.common.database.requests.DeleteRequest; +import org.onap.ccsdk.features.sdnr.wt.common.database.requests.GetIndexRequest; +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.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.DeleteByQueryResponse; +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.RefreshIndexResponse; +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 org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * @author Herbert, Micha + * + * Hint: Elasticsearch index/alias and doctype will be the same value + * server side restricted since ES 5.0 + * + */ +public class HtDatabaseClient extends ExtRestClient implements DatabaseClient, AutoCloseable { + + private final Logger LOG = LoggerFactory.getLogger(HtDatabaseClient.class); + + private boolean doRefreshAfterWrite; + public HtDatabaseClient(HostInfo[] hosts) { + this(hosts,true); + } + public HtDatabaseClient(HostInfo[] hosts, boolean refreshAfterWrite) { + super(hosts); + this.doRefreshAfterWrite = refreshAfterWrite; + } + + + + /*---------------------------------- + * Functions + */ + + /** + * Close function + */ + @Override + public void close() { + try { + super.close(); + } catch (IOException e) { + LOG.warn("Problem closing db client: {}",e); + } + } + + @Override + public boolean isExistsIndex(String esIndexAlias) { + + LOG.debug("Check status of ES index: {}", esIndexAlias); + + GetIndexRequest request = new GetIndexRequest(esIndexAlias); + + boolean indexStatus = false; + try { + indexStatus = this.indicesExists(request); + } catch (IOException e) { + LOG.warn("Problem checking index for {}: {}",esIndexAlias,e); + } + + return indexStatus; + + } + + @Override + public @Nullable String doWriteJsonString(String dataTypeName, @Nonnull IsEsObject esId, String json) { + return doWriteRaw(dataTypeName, esId.getEsId(), json); + } + + @Override + public @Nullable String doWriteRaw(String dataTypeName, @Nullable String esId, String json) { + + IndexResponse response = null; + IndexRequest indexRequest = new IndexRequest(dataTypeName,dataTypeName,esId); + indexRequest.source(json); + try { + response = this.index(indexRequest ); + } catch (IOException e) { + LOG.warn("ES Exception {} Json: {}", e.getMessage(), json); + } + + if (response == null) { + LOG.warn("Response null during write: {} {}", esId, json); + return null; + } + if(this.doRefreshAfterWrite) { + this.doRefresh(dataTypeName); + } + return response.getId(); + } + + private void doRefresh(String dataTypeName) { + try { + RefreshIndexResponse response = this.refreshIndex(new RefreshIndexRequest(dataTypeName)); + if(!response.succeeded()) { + LOG.warn("seems that index {} was not refreshed",dataTypeName); + } + } catch (IOException e) { + LOG.warn("problem with refreshing index: {}",e); + } + + } + @Override + public boolean doRemove(String dataTypeName, IsEsObject esId) { + return doRemove(dataTypeName, esId.getEsId()); + } + + + @Override + public boolean doRemove(String dataTypeName, String esId) { + DeleteRequest deleteRequest = new DeleteRequest(dataTypeName,dataTypeName,esId); + DeleteResponse response = null; + try { + response = this.delete(deleteRequest); + } catch (IOException e) { + LOG.warn("Problem deleting from db: {}",e.getMessage()); + } + if(this.doRefreshAfterWrite) { + this.doRefresh(dataTypeName); + } + return response!=null?response.isDeleted():false; + } + + @Override + public @Nullable String doReadJsonData(String dataTypeName, @Nonnull IsEsObject esId) { + + if (esId.getEsId() == null) { + throw new IllegalArgumentException("Read access to object without database Id"); + } + + return doReadJsonData(dataTypeName, esId.getEsId()); + } + + @Override + public @Nullable String doReadJsonData(String dataTypeName, @Nonnull String esId) { + + LOG.debug("NetworkIndex read: {}", dataTypeName); + GetRequest getRequest = new GetRequest(dataTypeName,dataTypeName,esId); + GetResponse response = null; + try { + response = this.get(getRequest); + } catch (IOException e) { + LOG.warn("problem reading data {} with id {}: {}",dataTypeName,esId,e); + } + return response!=null && response.isExists() ? response.getSourceAsBytesRef() : null; + } + + @Override + public @Nonnull SearchResult<SearchHit> doReadByQueryJsonData(String dataTypeName, QueryBuilder queryBuilder) { + + return this.doReadByQueryJsonData(dataTypeName, queryBuilder, false); + } + + @Override + public @Nonnull SearchResult<SearchHit> doReadByQueryJsonData(String dataTypeName,QueryBuilder queryBuilder, boolean ignoreException) { + + long total = 0; + LOG.debug("NetworkIndex query and read: {}", dataTypeName); + + SearchRequest searchRequest = new SearchRequest(dataTypeName, dataTypeName); + searchRequest.setQuery(queryBuilder); + SearchResponse response = null; + try { + response = this.search(searchRequest,ignoreException); + total = response.getTotal(); + + } catch (IOException e) { + LOG.warn("error do search {}: {}", queryBuilder, e); + } + return new SearchResult<SearchHit>(response != null ? response.getHits() : new SearchHit[] {}, total); + } + @Override + public @Nonnull SearchResult<SearchHit> doReadAllJsonData(String dataTypeName) { + return this.doReadAllJsonData( dataTypeName,false); + } + @Override + public @Nonnull SearchResult<SearchHit> doReadAllJsonData( String dataTypeName, boolean ignoreException) { + // Use query + return doReadByQueryJsonData( dataTypeName, QueryBuilders.matchAllQuery(),ignoreException); + } + + + @Override + public String doUpdateOrCreate(String dataTypeName, String esId, String json) { + return this.doUpdateOrCreate(dataTypeName, esId, json,null); + } + + + + @Override + public String doUpdateOrCreate(String dataTypeName, String esId, String json, List<String> onlyForInsert) { + if(esId==null) { + LOG.warn("try to update or insert {} with id null is not allowed.",dataTypeName); + return null; + } + boolean success = false; + UpdateRequest request = new UpdateRequest(dataTypeName, dataTypeName, esId); + request.source(new JSONObject(json),onlyForInsert); + try { + UpdateResponse response = this.update(request); + success = response.succeeded(); + } catch (IOException e) { + LOG.warn("Problem updating {} with id {} and data {}: {}", dataTypeName, esId, json, e); + } + if(this.doRefreshAfterWrite) { + this.doRefresh(dataTypeName); + } + return success ? esId : null; + } + @Override + public String doUpdate(String dataTypeName, String json, QueryBuilder query) { + boolean success = false; + UpdateByQueryRequest request = new UpdateByQueryRequest(dataTypeName, dataTypeName ); + request.source(new JSONObject(json),query); + try { + UpdateByQueryResponse response = this.update(request); + success = !response.hasFailures(); + } catch (IOException e) { + LOG.warn("Problem updating items in {} with query {} and data {}: {}", dataTypeName, query, json, e); + } + if(this.doRefreshAfterWrite) { + this.doRefresh(dataTypeName); + } + return success?"":null; + } + + + + @Override + public int doRemove(String dataTypeName, QueryBuilder query) { + int del=0; + DeleteByQueryRequest request = new DeleteByQueryRequest(dataTypeName); + request.source(query); + try { + DeleteByQueryResponse response = this.deleteByQuery(request); + del = response.getDeleted(); + } catch (IOException e) { + LOG.warn("Problem delete in {} with query {}:{} ", dataTypeName, query.toJSON(), e); + } + if(this.doRefreshAfterWrite) { + this.doRefresh(dataTypeName); + } + return del; + } + +} 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/database/InvalidProtocolException.java new file mode 100644 index 000000000..c7d27204a --- /dev/null +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/InvalidProtocolException.java @@ -0,0 +1,32 @@ +/******************************************************************************* + * ============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.database; + +public class InvalidProtocolException extends Exception { + + + /** + * + */ + private static final long serialVersionUID = 1L; + + public InvalidProtocolException(String e) { + super(e); + } + +} diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/IsEsObject.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/IsEsObject.java new file mode 100644 index 000000000..a5c7df6bb --- /dev/null +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/IsEsObject.java @@ -0,0 +1,37 @@ +/******************************************************************************* + * ============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.database; + +/** + * Element is a document in the ES database. + */ +public interface IsEsObject { + + /** + * Set the ES Id + * @param id Set the ID, created by ES for this Object + */ + void setEsId( String id ); + + /** + * Get Id content as string that is used in ES + * @return Related ID, that was specified by set command. + */ + String getEsId(); + +} diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/SearchHit.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/SearchHit.java new file mode 100644 index 000000000..e86c5c36b --- /dev/null +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/SearchHit.java @@ -0,0 +1,50 @@ +/******************************************************************************* + * ============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.database; + +import org.json.JSONObject; + +public class SearchHit { + + private String index; + private String type; + private String id; + private JSONObject source; + + public SearchHit(JSONObject o) { + this.index=o.getString("_index"); + this.type = o.getString("_type"); + this.id = o.getString("_id"); + this.source = o.getJSONObject("_source"); + } + + public String getIndex() { + return this.index; + } + public String getType() { + return this.type; + } + public String getId() { + return this.id; + } + + public String getSourceAsString() { + return this.source.toString(); + } + +} diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/SearchResult.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/SearchResult.java new file mode 100644 index 000000000..eedc45c77 --- /dev/null +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/SearchResult.java @@ -0,0 +1,63 @@ +/******************************************************************************* + * ============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.database; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class SearchResult<T> { + + /** + * objects in results + */ + private final List<T> hits; + /** + * size of all potential hits + * not necessarily the number of hits + */ + private long total; + + public SearchResult(T[] hits) { + this(hits,hits==null?0:hits.length); + } + public SearchResult(T[] hits,long total) { + this.hits = Arrays.asList(hits); + this.total = total; + } +// public SearchResult(List<T> hits,long total) { +// this.hits = hits; +// this.total = total; +// } + public SearchResult() { + this.hits=new ArrayList<>(); + this.total = 0; + } + public List<T> getHits() { + return this.hits; + } + public long getTotal() { + return this.total; + } + public void setTotal(long total) { + this.total = total; + } + public void add(T object) { + this.hits.add(object); + } +} diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/config/HostInfo.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/config/HostInfo.java new file mode 100644 index 000000000..11554b5b6 --- /dev/null +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/config/HostInfo.java @@ -0,0 +1,78 @@ +/******************************************************************************* + * ============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.database.config; + +public class HostInfo { + + public enum Protocol{ + HTTP("http"), + HTTPS("https");//, +// FILETRANSFERPROTOCOL("ftp"); + + private final String value; + + private Protocol(String s) { + this.value = s; + } + @Override + public String toString() { + return this.value; + } + public String getValue() { + return value; + } + public static Protocol getValueOf(String s) { + s = s.toLowerCase(); + for(Protocol p:Protocol.values()) { + if(p.value.equals(s)) { + return p; + } + } + return HTTP; + } + } + private static final Protocol DEFAULT_PROTOCOL = Protocol.HTTP; + public final String hostname; + public final int port; + public final Protocol protocol; + + public HostInfo(String hostname,int port, Protocol protocol) { + this.hostname = hostname; + this.port = port; + this.protocol=protocol; + + } + + public HostInfo(String hostname, int port) { + this(hostname,port,DEFAULT_PROTOCOL); + + } + + @Override + public String toString() { + return "HostInfo [hostname=" + hostname + ", port=" + port + ", protocol=" + protocol + "]"; + } + + public String toUrl() { + return String.format("%s://%s:%d",this.protocol,this.hostname,this.port); + } + + public static HostInfo getDefault() { + return new HostInfo("localhost",9200,Protocol.HTTP); + } +} diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/data/DbFilter.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/data/DbFilter.java new file mode 100644 index 000000000..19cb82669 --- /dev/null +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/data/DbFilter.java @@ -0,0 +1,62 @@ +/******************************************************************************* + * ============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.database.data; + +import org.onap.ccsdk.features.sdnr.wt.common.database.queries.RangeQueryBuilder; + +public class DbFilter { + + public static String createDatabaseRegex(String restFilterValue) { + return restFilterValue == null ? null : restFilterValue.replace("?", ".{1,1}").replace("*", ".*"); + } + + public static boolean hasSearchParams(String restFilterValue) { + return restFilterValue == null ? false : restFilterValue.contains("*") || restFilterValue.contains("?"); + } + + public static boolean isComparisonValid(String restFilterValue) { + return restFilterValue == null ? false : restFilterValue.contains(">") || restFilterValue.contains("<"); + } + + public static RangeQueryBuilder getRangeQuery(String key, String restFilterValue) { + RangeQueryBuilder query = new RangeQueryBuilder(key); + restFilterValue = restFilterValue.trim(); + if (restFilterValue.startsWith(">=")) { + query.gte(getObjectFromString(restFilterValue.substring(2).trim())); + } else if (restFilterValue.startsWith(">")) { + query.gt(getObjectFromString(restFilterValue.substring(1).trim())); + } else if (restFilterValue.startsWith("<=")) { + query.lte(getObjectFromString(restFilterValue.substring(2).trim())); + } else if (restFilterValue.startsWith("<")) { + query.lt(getObjectFromString(restFilterValue.substring(1).trim())); + } else { + return null; + } + + return query; + } + + private static Object getObjectFromString(String str) { + try { + return Double.parseDouble(str); + } catch (NumberFormatException | NullPointerException nfe) { + return str; + } + + } +} diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/data/EsObject.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/data/EsObject.java new file mode 100644 index 000000000..a753122ac --- /dev/null +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/data/EsObject.java @@ -0,0 +1,43 @@ +/******************************************************************************* + * ============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.database.data; + +import org.onap.ccsdk.features.sdnr.wt.common.database.IsEsObject; + +import com.fasterxml.jackson.annotation.JsonIgnore; + +/** + * @author Herbert + * + */ +public class EsObject implements IsEsObject { + + @JsonIgnore + private String esId; + + @Override + public String getEsId() { + return esId; + } + + @Override + public void setEsId(String esId) { + this.esId = esId; + } + +} 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 new file mode 100644 index 000000000..5edc2613d --- /dev/null +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/queries/BoolQueryBuilder.java @@ -0,0 +1,62 @@ +/******************************************************************************* + * ============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.database.queries; + +import org.json.JSONArray; +import org.json.JSONObject; + +public class BoolQueryBuilder extends QueryBuilder { + + private JSONObject inner; + + public BoolQueryBuilder() { + super(); + this.inner = new JSONObject(); + this.setQuery("bool", this.inner); + } + + @Override + public String toString() { + return "BoolQueryBuilder [inner=" + inner + "]"; + } + + public BoolQueryBuilder must(QueryBuilder matchQuery) { + + if(this.inner.has("must") || this.inner.has("match") || this.inner.has("regexp")) { + Object x = this.inner.has("must") ?this.inner.get("must"):this.inner; + if(x instanceof JSONArray) { + ((JSONArray)x).put(matchQuery.getInner()); + } + else { + this.inner = new JSONObject(); + this.inner.put("must", new JSONObject()); + JSONArray a=new JSONArray(); + a.put(x); + a.put(matchQuery.getInner()); + this.inner.put("must", a); + } + } + + else { + this.inner = matchQuery.getInner(); + } + this.setQuery("bool", this.inner); + return this; + } + +} diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/queries/QueryBuilder.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/queries/QueryBuilder.java new file mode 100644 index 000000000..c1f372cc9 --- /dev/null +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/queries/QueryBuilder.java @@ -0,0 +1,97 @@ +/******************************************************************************* + * ============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.database.queries; + +import org.json.JSONArray; +import org.json.JSONObject; + +public class QueryBuilder { + + private JSONObject innerQuery; + private final JSONObject outerQuery; + private final JSONObject queryObj; + + public QueryBuilder() { + this.outerQuery = new JSONObject(); + this.queryObj = new JSONObject(); + this.outerQuery.put("query", this.queryObj); + + } + + public QueryBuilder from(long from) { + this.outerQuery.put("from", from); + return this; + } + + public QueryBuilder size(long size) { + this.outerQuery.put("size", size); + return this; + } + + public QueryBuilder sort(String prop, SortOrder order) { + JSONArray a; + if (this.outerQuery.has("sort")) { + a = this.outerQuery.getJSONArray("sort"); + } else { + a = new JSONArray(); + } + JSONObject sortObj = new JSONObject(); + JSONObject orderObj = new JSONObject(); + orderObj.put("order", order.getValue()); + sortObj.put(prop, orderObj); + a.put(sortObj); + this.outerQuery.put("sort", a); + return this; + } + + public QueryBuilder aggregations(String key, SortOrder sortOrder) { + JSONObject keyquery = new JSONObject(); + JSONObject terms = new JSONObject(); + JSONObject field = new JSONObject(); + field.put("field", key); + terms.put("terms", field); + if(sortOrder!=null) { + JSONObject so = new JSONObject(); + so.put("_key",sortOrder.getValue()); + terms.put("order", so); + } + keyquery.put(key, terms); + this.outerQuery.put("aggs", keyquery); + return this; + } + + protected QueryBuilder setQuery(String key, JSONObject query) { + this.innerQuery = query; + this.queryObj.put(key, this.innerQuery); + return this; + } + + public JSONObject getInner() { + return this.queryObj; + } + public boolean contains(String match) { + return this.toJSON().contains(match); + } + public String toJSON() { + return this.outerQuery.toString(); + } + + public QueryBuilder aggregations(String key) { + return this.aggregations(key, null); + } +} diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/queries/QueryBuilders.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/queries/QueryBuilders.java new file mode 100644 index 000000000..b9271c22b --- /dev/null +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/queries/QueryBuilders.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.database.queries; + +import org.json.JSONObject; + +public class QueryBuilders { + + public static QueryBuilder matchAllQuery() { + return new QueryBuilder().setQuery("match_all",new JSONObject()); + } + public static QueryBuilder termQuery(String key, String value) { + JSONObject o=new JSONObject(); + o.put(key, value); + return new QueryBuilder().setQuery("term", o); + } + public static QueryBuilder matchQuery(String key, Object value) { + JSONObject o=new JSONObject(); + o.put(key, value); + return new QueryBuilder().setQuery("match", o); + } + public static BoolQueryBuilder boolQuery() { + return new BoolQueryBuilder(); + } + public static RangeQueryBuilder rangeQuery(String key) { + return new RangeQueryBuilder(key); + } + public static RegexQueryBuilder regex(String propertyName, String re) { + return new RegexQueryBuilder().add(propertyName, re); + } +} diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/queries/RangeQueryBuilder.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/queries/RangeQueryBuilder.java new file mode 100644 index 000000000..e9df263ea --- /dev/null +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/queries/RangeQueryBuilder.java @@ -0,0 +1,85 @@ +/******************************************************************************* + * ============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.database.queries; + +import org.json.JSONObject; + +public class RangeQueryBuilder extends QueryBuilder{ + + private Object gtValue=null; + private Object gteValue=null; + private Float boost=2.0f; + private Object ltValue=null; + private Object lteValue=null; + private String key; + + public RangeQueryBuilder(String key) { + super(); + this.key=key; + + } + + private void setQuery() { + JSONObject r=new JSONObject(); + JSONObject k=new JSONObject(); + if(this.gteValue!=null) { + k.put("gte", this.gteValue); + }else if(this.gtValue!=null) { + k.put("gt", this.gtValue); + } + if(this.lteValue!=null) { + k.put("lte", this.lteValue); + } else if(this.ltValue!=null) { + k.put("lt", this.ltValue); + } + if(this.boost!=null) { + k.put("boost", this.boost); + } + r.put(this.key,k); + + this.setQuery("range",r); + } + + + public RangeQueryBuilder lte(Object compare) { + this.lteValue=compare; + this.ltValue=null; + this.setQuery(); + return this; + } + public RangeQueryBuilder lt(Object compare) { + this.lteValue=null; + this.ltValue=compare; + this.setQuery(); + return this; + } + + public RangeQueryBuilder gte(Object compare) { + this.gteValue=compare; + this.gtValue=null; + this.setQuery(); + return this; + } + public RangeQueryBuilder gt(Object compare) { + this.gteValue=null; + this.gtValue=compare; + this.setQuery(); + return this; + } + +} diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/queries/RegexQueryBuilder.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/queries/RegexQueryBuilder.java new file mode 100644 index 000000000..d9f9c1069 --- /dev/null +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/queries/RegexQueryBuilder.java @@ -0,0 +1,43 @@ +/******************************************************************************* + * ============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.database.queries; + +import org.json.JSONObject; + +public class RegexQueryBuilder extends QueryBuilder { + + private JSONObject inner; + + public RegexQueryBuilder() { + super(); + this.inner = new JSONObject(); + this.setQuery("regexp", this.inner); + } + public RegexQueryBuilder add(String propertyName, String filter) { + JSONObject regexFilter = new JSONObject(); + regexFilter.put("value", filter); + regexFilter.put("flags", "ALL"); + regexFilter.put("max_determinized_states", 10000); + this.inner.put(propertyName, regexFilter); + return this; + } + @Override + public String toString() { + return "RegexQueryBuilder [inner=" + inner + "]"; + } +} diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/queries/SortOrder.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/queries/SortOrder.java new file mode 100644 index 000000000..aae7f6433 --- /dev/null +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/queries/SortOrder.java @@ -0,0 +1,33 @@ +/******************************************************************************* + * ============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.database.queries; + +public enum SortOrder { + + ASCENDING("asc"), + DESCENDING("desc"); + + private final String value; + + SortOrder(String so){ + this.value=so; + } + public String getValue() { + return this.value; + } +} diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/requests/BaseRequest.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/requests/BaseRequest.java new file mode 100644 index 000000000..e00cd1a03 --- /dev/null +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/requests/BaseRequest.java @@ -0,0 +1,75 @@ +/******************************************************************************* + * ============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.database.requests; + +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; + +import org.elasticsearch.client.Request; +import org.json.JSONObject; +import org.onap.ccsdk.features.sdnr.wt.common.database.queries.QueryBuilder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public abstract class BaseRequest { + + private static final Logger LOG = LoggerFactory.getLogger(BaseRequest.class); + + protected final Request request; + private String query; + public BaseRequest(String method, String endpoint) { + LOG.debug("create request {} {}", method, endpoint); + this.request = new Request(method, endpoint); + query=null; + } + + public Request getInner() { + + return this.request; + } + + public static String urlEncodeValue(String value) { + if (value == null) + return null; + try { + return URLEncoder.encode(value, StandardCharsets.UTF_8.toString()).replace("+", "%20"); + } catch (UnsupportedEncodingException ex) { + LOG.warn("encoding problem: {}", ex.getMessage()); + } + return value; + } + @Override + public String toString() { + return this.request.getMethod() + " "+this.request.getEndpoint()+ " : "+(this.query!=null?this.query:"no query"); + } + + protected void setQuery(QueryBuilder query) { + this.setQuery(query.toJSON()); + } + + public void setQuery(JSONObject o) { + this.setQuery(o.toString()); + } + + public void setQuery(String content) { + this.query=content; + LOG.trace("query={}",content); + this.request.setJsonEntity(this.query); + } +} diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/requests/ClusterHealthRequest.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/requests/ClusterHealthRequest.java new file mode 100644 index 000000000..4c1d85870 --- /dev/null +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/requests/ClusterHealthRequest.java @@ -0,0 +1,31 @@ +/******************************************************************************* + * ============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.database.requests; + +public class ClusterHealthRequest extends BaseRequest{ + + + public ClusterHealthRequest() { + super("GET","/_cluster/health"); + } + + public void timeout(long seconds) { + this.request.addParameter("timeout", String.valueOf(seconds)+"s"); + + } +} diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/requests/CountRequest.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/requests/CountRequest.java new file mode 100644 index 000000000..0e00d5d49 --- /dev/null +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/requests/CountRequest.java @@ -0,0 +1,28 @@ +/******************************************************************************* + * ============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.database.requests; + +public class CountRequest extends BaseRequest{ + + + public CountRequest(String alias,String dataType) { + super("GET",String.format("/%s/%s/_count",alias,dataType)); + } + + +} diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/requests/CreateIndexRequest.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/requests/CreateIndexRequest.java new file mode 100644 index 000000000..c7e27ee4a --- /dev/null +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/requests/CreateIndexRequest.java @@ -0,0 +1,63 @@ +/******************************************************************************* + * ============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.database.requests; + +import org.json.JSONObject; + +//https://github.com/elastic/elasticsearch/blob/6.4/rest-api-spec/src/main/resources/rest-api-spec/api/indices.create.json +//https://github.com/elastic/elasticsearch/blob/6.4/rest-api-spec/src/main/resources/rest-api-spec/api/indices.put_mapping.json +public class CreateIndexRequest extends BaseRequest{ + + private JSONObject settings; + private JSONObject mappings; + + public CreateIndexRequest(String index) { + super("PUT","/"+index); + this.mappings=new JSONObject(); + } + + private void setRequest() { + + JSONObject o=new JSONObject(); + if(this.mappings!=null) { + o.put("mappings", this.mappings); + } + if(this.settings!=null) { + o.put("settings", this.settings); + } + super.setQuery(o); + } + public void mappings(JSONObject mappings) { + this.mappings=mappings; + this.setRequest(); + } + + public void settings(JSONObject settings) { + this.settings = settings; + this.setRequest(); + } + + public boolean hasMappings() { + return this.mappings!=null; + } + + public boolean hasSettings() { + return this.settings!=null; + } + +} diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/requests/DeleteByQueryRequest.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/requests/DeleteByQueryRequest.java new file mode 100644 index 000000000..b02f027b2 --- /dev/null +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/requests/DeleteByQueryRequest.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.database.requests; + +import org.onap.ccsdk.features.sdnr.wt.common.database.queries.QueryBuilder; + +public class DeleteByQueryRequest extends BaseRequest { + + public DeleteByQueryRequest(String alias) { + super("POST",String.format("/%s/_delete_by_query",alias)); + } + + public void source(QueryBuilder query) { + this.setQuery(query); + } + + + + +}
\ No newline at end of file diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/requests/DeleteIndexRequest.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/requests/DeleteIndexRequest.java new file mode 100644 index 000000000..f673c53a4 --- /dev/null +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/requests/DeleteIndexRequest.java @@ -0,0 +1,27 @@ +/******************************************************************************* + * ============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.database.requests; + +//https://github.com/elastic/elasticsearch/blob/6.4/rest-api-spec/src/main/resources/rest-api-spec/api/indices.delete.json +public class DeleteIndexRequest extends BaseRequest{ + + public DeleteIndexRequest(String index) { + super("DELETE","/"+index); + } + +} diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/requests/DeleteRequest.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/requests/DeleteRequest.java new file mode 100644 index 000000000..fb84ce4a9 --- /dev/null +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/requests/DeleteRequest.java @@ -0,0 +1,26 @@ +/******************************************************************************* + * ============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.database.requests; + +public class DeleteRequest extends BaseRequest { + + public DeleteRequest(String alias,String dataType,String esId) { + super("DELETE",String.format("/%s/%s/%s",alias,dataType,BaseRequest.urlEncodeValue(esId))); + } + +} diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/requests/GetIndexRequest.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/requests/GetIndexRequest.java new file mode 100644 index 000000000..2a56b2160 --- /dev/null +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/requests/GetIndexRequest.java @@ -0,0 +1,29 @@ +/******************************************************************************* + * ============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.database.requests; + +//https://github.com/elastic/elasticsearch/blob/6.4/rest-api-spec/src/main/resources/rest-api-spec/api/indices.exists.json +public class GetIndexRequest extends BaseRequest { + + public GetIndexRequest(String index) { + super("HEAD","/"+index); + } + + + +} diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/requests/GetRequest.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/requests/GetRequest.java new file mode 100644 index 000000000..855a7c4a6 --- /dev/null +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/requests/GetRequest.java @@ -0,0 +1,30 @@ +/******************************************************************************* + * ============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.database.requests; + +import javax.annotation.Nonnull; + +public class GetRequest extends BaseRequest{ + + public GetRequest(String alias,String dataType,@Nonnull String esId) { + super("GET",String.format("/%s/%s/%s",alias,dataType,BaseRequest.urlEncodeValue(esId))); + } + + + +} diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/requests/IndexRequest.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/requests/IndexRequest.java new file mode 100644 index 000000000..521760c21 --- /dev/null +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/requests/IndexRequest.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.database.requests; + +import javax.annotation.Nullable; + +public class IndexRequest extends BaseRequest{ + + public IndexRequest(String alias, String dataType) { + this(alias,dataType,null); + } + + public IndexRequest(String alias,String dataType, @Nullable String esId) { + super("POST",esId!=null?String.format("/%s/%s/%s",alias,dataType,BaseRequest.urlEncodeValue(esId)):String.format("/%s/%s",alias,dataType)); + } + + public void source(String content) { + super.setQuery(content); + } + + + +} diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/requests/IndicesAliasesRequest.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/requests/IndicesAliasesRequest.java new file mode 100644 index 000000000..e9436e946 --- /dev/null +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/requests/IndicesAliasesRequest.java @@ -0,0 +1,26 @@ +/******************************************************************************* + * ============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.database.requests; + +//https://github.com/elastic/elasticsearch/blob/6.4/rest-api-spec/src/main/resources/rest-api-spec/api/indices.put_alias.json +public class IndicesAliasesRequest extends BaseRequest{ + + public IndicesAliasesRequest(String index, String alias) { + super("PUT", String.format("/%s/_alias/%s",index,alias)); + } +} diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/requests/NodeStatsRequest.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/requests/NodeStatsRequest.java new file mode 100644 index 000000000..e77a39a0a --- /dev/null +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/requests/NodeStatsRequest.java @@ -0,0 +1,26 @@ +/******************************************************************************* + * ============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.database.requests; + +public class NodeStatsRequest extends BaseRequest{ + + + public NodeStatsRequest() { + super("GET","/_nodes/stats"); + } +} diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/requests/RefreshIndexRequest.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/requests/RefreshIndexRequest.java new file mode 100644 index 000000000..e7cdd0c1a --- /dev/null +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/requests/RefreshIndexRequest.java @@ -0,0 +1,28 @@ +/******************************************************************************* + * ============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.database.requests; + +public class RefreshIndexRequest extends BaseRequest{ + + public RefreshIndexRequest(String alias) { + super("GET",String.format("/%s/_refresh",alias)); + } + + + +} diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/requests/SearchRequest.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/requests/SearchRequest.java new file mode 100644 index 000000000..86fa8a91f --- /dev/null +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/requests/SearchRequest.java @@ -0,0 +1,33 @@ +/******************************************************************************* + * ============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.database.requests; + +import org.onap.ccsdk.features.sdnr.wt.common.database.queries.QueryBuilder; + +public class SearchRequest extends BaseRequest{ + + public SearchRequest(String alias,String dataType) { + super("POST",String.format("/%s/%s/_search", alias,dataType)); + } + + @Override + public void setQuery(QueryBuilder query){ + super.setQuery(query); + } + +} diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/requests/UpdateByQueryRequest.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/requests/UpdateByQueryRequest.java new file mode 100644 index 000000000..b1469a57e --- /dev/null +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/requests/UpdateByQueryRequest.java @@ -0,0 +1,102 @@ +/******************************************************************************* + * ============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.database.requests; + +import org.json.JSONArray; +import org.json.JSONObject; +import org.onap.ccsdk.features.sdnr.wt.common.database.queries.QueryBuilder; +import org.onap.ccsdk.features.sdnr.wt.common.database.queries.QueryBuilders; + +public class UpdateByQueryRequest extends BaseRequest { + + private JSONObject params; + + public UpdateByQueryRequest(String alias, String dataType) { + super("POST", String.format("/%s/%s/_update_by_query", alias, dataType)); + this.params = null; + } + + public void source(String esId, JSONObject map) { + this.source(map, QueryBuilders.matchQuery("_id", esId)); + } + + public void source(JSONObject map, QueryBuilder query) { + JSONObject outer = new JSONObject(); + outer.put("query", query.getInner()); + JSONObject script = new JSONObject(); + script.put("lang", "painless"); + script.put("inline", this.createInline(map)); + if(this.params!=null) { + script.put("params",this.params); + } + outer.put("script", script); + super.setQuery(outer.toString()); + } + + private String createInline(JSONObject map) { + String s = "", k, pkey; + int i = 1; + Object value; + for (Object key : map.keySet()) { + k = String.valueOf(key); + value = map.get(k); + if (value instanceof JSONObject || value instanceof JSONArray) { + pkey = String.format("p%d", i++); + if (value instanceof JSONObject) { + this.withParam(pkey, (JSONObject) value); + } else { + this.withParam(pkey, (JSONArray) value); + } + + s += String.format("ctx._source['%s']=%s;", key, "params." + pkey); + } else { + s += String.format("ctx._source['%s']=%s;", key, escpaped(value)); + } + } + return s; + } + + private UpdateByQueryRequest withParam(String key, JSONArray p) { + if (this.params == null) { + this.params = new JSONObject(); + } + this.params.put(key, p); + return this; + } + + private UpdateByQueryRequest withParam(String key, JSONObject p) { + if (this.params == null) { + this.params = new JSONObject(); + } + this.params.put(key, p); + return this; + } + + private String escpaped(Object value) { + String s = ""; + if (value instanceof Boolean || value instanceof Integer || value instanceof Long || value instanceof Float + || value instanceof Double) { + s = String.valueOf(value); + } else { + s = "\"" + String.valueOf(value) + "\""; + } + return s; + + } + +} diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/requests/UpdateRequest.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/requests/UpdateRequest.java new file mode 100644 index 000000000..adab44df9 --- /dev/null +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/requests/UpdateRequest.java @@ -0,0 +1,112 @@ +/******************************************************************************* + * ============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.database.requests; + +import java.util.ArrayList; +import java.util.List; + +import org.json.JSONArray; +import org.json.JSONObject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class UpdateRequest extends BaseRequest { + + private static final Logger LOG = LoggerFactory.getLogger(UpdateRequest.class); + private JSONObject params; + + public UpdateRequest(String alias, String dataType, String esId) { + super("POST", String.format("/%s/%s/%s/_update", alias, dataType, BaseRequest.urlEncodeValue(esId))); + this.params = null; + } + + private UpdateRequest withParam(String key, JSONObject p) { + if (this.params == null) { + this.params = new JSONObject(); + } + this.params.put(key, p); + return this; + } + + private UpdateRequest withParam(String key, JSONArray p) { + if (this.params == null) { + this.params = new JSONObject(); + } + this.params.put(key, p); + return this; + } + public void source(JSONObject map) { + this.source(map,null); + } + public void source(JSONObject map, List<String> onlyForInsert) { + JSONObject outer = new JSONObject(); + JSONObject script = new JSONObject(); + script.put("lang", "painless"); + script.put("source", this.createInline(map,onlyForInsert)); + if(this.params!=null) { + script.put("params",this.params); + } + outer.put("script", script); + outer.put("upsert", map); + LOG.debug("update payload: " + outer.toString()); + super.setQuery(outer.toString()); + } + + private String createInline(JSONObject map, List<String> onlyForInsert) { + if(onlyForInsert==null) { + onlyForInsert = new ArrayList<String>(); + } + String s = "",k=""; + Object value; + String pkey; + int i = 0; + for (Object key : map.keySet()) { + k=String.valueOf(key); + if(onlyForInsert.contains(k)) { + continue; + } + value = map.get(k); + if (value instanceof JSONObject || value instanceof JSONArray) { + pkey = String.format("p%d", i++); + if (value instanceof JSONObject) { + this.withParam(pkey, (JSONObject) value); + } else { + this.withParam(pkey, (JSONArray) value); + } + + s += String.format("ctx._source['%s']=%s;", key, "params."+pkey); + } else { + s += String.format("ctx._source['%s']=%s;", key, escpaped(value)); + } + } + return s; + } + + private String escpaped(Object value) { + String s = ""; + if (value instanceof Boolean || value instanceof Integer || value instanceof Long || value instanceof Float + || value instanceof Double) { + s = String.valueOf(value); + } else { + s = "\"" + String.valueOf(value) + "\""; + } + return s; + + } + +} diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/responses/AcknowledgedResponse.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/responses/AcknowledgedResponse.java new file mode 100644 index 000000000..291ed5190 --- /dev/null +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/responses/AcknowledgedResponse.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.database.responses; + +import org.elasticsearch.client.Response; +import org.json.JSONObject; + +public class AcknowledgedResponse extends BaseResponse{ + + private boolean isAcknowledged; + + public AcknowledgedResponse(Response response) { + super(response); + JSONObject o = this.getJson(response); + if(o!=null) { + this.isAcknowledged=o.getBoolean("acknowledged"); + } + } + + public boolean isAcknowledged() { + return this.isAcknowledged; + } +} diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/responses/AggregationEntries.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/responses/AggregationEntries.java new file mode 100644 index 000000000..debd5e4ef --- /dev/null +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/responses/AggregationEntries.java @@ -0,0 +1,44 @@ +/******************************************************************************* + * ============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.database.responses; + +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; + +public class AggregationEntries extends LinkedHashMap<String,Long>{ + + private static final long serialVersionUID = 1L; + + /** + * Return page with keys + * @param size elements + * @param offset start position in list. + * @return List<String> with selected values + */ + public String[] getKeysAsPagedStringList(long size, long offset) { + List<String> ltps = new ArrayList<String>(); + String[] keys = keySet().toArray(new String[0]); + for (long i = offset; i < keys.length && i < offset + size; i++) { + ltps.add(keys[(int) i]); + } + return ltps.toArray(new String[0]); + } + + +} diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/responses/BaseResponse.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/responses/BaseResponse.java new file mode 100644 index 000000000..49447f660 --- /dev/null +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/responses/BaseResponse.java @@ -0,0 +1,75 @@ +/******************************************************************************* + * ============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.database.responses; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Scanner; + +import org.elasticsearch.client.Response; +import org.json.JSONObject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class BaseResponse { + private static final Logger LOG = LoggerFactory.getLogger(BaseResponse.class); + private int responseCode; + + public int getResponseCode() { + return this.responseCode; + } + public boolean isResponseSucceeded() { + return this.responseCode<300; + } + public BaseResponse() { + } + public BaseResponse(Response response) { + this.responseCode = response!=null?response.getStatusLine().getStatusCode():0; + } + protected JSONObject getJson(Response response) { + Scanner s; + JSONObject o=null; + try { + //input stream used because Scanner ignored whitespaces + InputStream is = response.getEntity().getContent(); + long len; + int BUFSIZE=1024; + byte[] buffer = new byte[BUFSIZE]; + StringBuffer sresponse = new StringBuffer(); + while (true) { + len = is.read(buffer, 0, BUFSIZE); + if (len <= 0) { + break; + } + + sresponse.append(new String(buffer)); + } + + LOG.debug("parsing response={}",sresponse); + o = new JSONObject(sresponse.toString()); + } catch (UnsupportedOperationException | IOException e) { + LOG.warn("error parsing es response: {}",e.getMessage()); + } + + return o; + } + protected JSONObject getJson(String json) { + return new JSONObject(json); + } + +} diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/responses/ClusterHealthResponse.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/responses/ClusterHealthResponse.java new file mode 100644 index 000000000..3f47ac286 --- /dev/null +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/responses/ClusterHealthResponse.java @@ -0,0 +1,76 @@ +/******************************************************************************* + * ============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.database.responses; + +import java.io.IOException; +import org.elasticsearch.client.Response; +import org.json.JSONException; +import org.json.JSONObject; + +public class ClusterHealthResponse extends BaseResponse { + + public static final String HEALTHSTATUS_GREEN = "green"; + public static final String HEALTHSTATUS_YELLOW = "yellow"; + public static final String HEALTSTATUS_RED = "red"; + + private String status; + private boolean timedOut; + + /* + * "cluster_name": "docker-cluster", "status": "yellow", "timed_out": false, + * "number_of_nodes": 1, "number_of_data_nodes": 1, "active_primary_shards": 5, + * "active_shards": 5, "relocating_shards": 0, "initializing_shards": 0, + * "unassigned_shards": 5, "delayed_unassigned_shards": 0, + * "number_of_pending_tasks": 0, "number_of_in_flight_fetch": 0, + * "task_max_waiting_in_queue_millis": 0, "active_shards_percent_as_number": 50 + */ + public ClusterHealthResponse(Response response) throws UnsupportedOperationException, IOException, JSONException { + super(response); + + JSONObject o = this.getJson(response); + if (o != null) { + this.status = o.getString("status"); + this.timedOut = o.getBoolean("timed_out"); + } + } + + public boolean isTimedOut() { + return this.timedOut; + } + public boolean isStatusMinimal(String status) { + if (status == null) { + return true; + } + if (this.status.equals(HEALTHSTATUS_GREEN)) { + return true; + } + if (this.status.equals(HEALTHSTATUS_YELLOW) && !status.equals(HEALTHSTATUS_GREEN)) { + return true; + } + if (this.status.equals(status)) { + return true; + } + return false; + + } + + public String getStatus() { + return this.status; + } + +} diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/responses/CountResponse.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/responses/CountResponse.java new file mode 100644 index 000000000..429c3394d --- /dev/null +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/responses/CountResponse.java @@ -0,0 +1,30 @@ +/******************************************************************************* + * ============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.database.responses; + +import org.elasticsearch.client.Response; + +public class CountResponse extends BaseResponse { + + private long count; + + public CountResponse(Response response) { + super(response); + + } +} diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/responses/CreateIndexResponse.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/responses/CreateIndexResponse.java new file mode 100644 index 000000000..29f7e886e --- /dev/null +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/responses/CreateIndexResponse.java @@ -0,0 +1,28 @@ +/******************************************************************************* + * ============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.database.responses; + +import org.elasticsearch.client.Response; + +public class CreateIndexResponse extends AcknowledgedResponse { + + public CreateIndexResponse(Response response) { + super(response); + } + +} diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/responses/DeleteByQueryResponse.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/responses/DeleteByQueryResponse.java new file mode 100644 index 000000000..467f7e95a --- /dev/null +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/responses/DeleteByQueryResponse.java @@ -0,0 +1,50 @@ +/******************************************************************************* + * ============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.database.responses; + +import org.elasticsearch.client.Response; +import org.json.JSONObject; + +/** + * {"took":1,"batches":0,"retries":{"search":0,"bulk":0},"throttled_millis":0,"total":0,"deleted":0,"noops":0,"requests_per_second":-1,"failures":[],"version_conflicts":0,"throttled_until_millis":0,"timed_out":false} + * @author jack + * + */ +public class DeleteByQueryResponse extends BaseResponse { + + private int deleted; + + public DeleteByQueryResponse(Response response) { + super(response); + try { + JSONObject o = this.getJson(response); + if (o != null) { + this.deleted = o.getInt("deleted"); + } + } + catch(Exception e) { + this.deleted=0; + } + + } + + public int getDeleted() { + return this.deleted; + } + +} diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/responses/DeleteIndexResponse.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/responses/DeleteIndexResponse.java new file mode 100644 index 000000000..7eb0a97ba --- /dev/null +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/responses/DeleteIndexResponse.java @@ -0,0 +1,28 @@ +/******************************************************************************* + * ============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.database.responses; + +import org.elasticsearch.client.Response; + +public class DeleteIndexResponse extends AcknowledgedResponse { + + public DeleteIndexResponse(Response response) { + super(response); + } + +}
\ No newline at end of file diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/responses/DeleteResponse.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/responses/DeleteResponse.java new file mode 100644 index 000000000..db9124df7 --- /dev/null +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/responses/DeleteResponse.java @@ -0,0 +1,47 @@ +/******************************************************************************* + * ============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.database.responses; + +import org.elasticsearch.client.Response; +import org.json.JSONObject; + +public class DeleteResponse extends BaseResponse { + + private boolean isDeleted; + + public DeleteResponse(Response response) { + super(response); + int code = response.getStatusLine().getStatusCode(); + if (code < 210) { + + JSONObject o = this.getJson(response); + if (o != null) { + this.isDeleted = "deleted".equals(o.getString("result")); + } + } + else { + this.isDeleted=false; + } + + } + + public boolean isDeleted() { + return this.isDeleted; + } + +} diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/responses/GetResponse.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/responses/GetResponse.java new file mode 100644 index 000000000..49e3ad9a7 --- /dev/null +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/responses/GetResponse.java @@ -0,0 +1,50 @@ +/******************************************************************************* + * ============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.database.responses; + +import org.elasticsearch.client.Response; +import org.json.JSONObject; +import org.onap.ccsdk.features.sdnr.wt.common.database.SearchHit; + +public class GetResponse extends BaseResponse { + + private boolean found; + private SearchHit result; + + public GetResponse(Response response) { + super(response); + if (this.isResponseSucceeded()) { + JSONObject o = this.getJson(response); + this.found = o.getBoolean("found"); + this.result = new SearchHit(o); + } else { + this.found = false; + this.result = null; + } + + } + + public boolean isExists() { + return this.found; + } + + public String getSourceAsBytesRef() { + return this.result.getSourceAsString(); + } + +} diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/responses/IndexResponse.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/responses/IndexResponse.java new file mode 100644 index 000000000..499cd3719 --- /dev/null +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/responses/IndexResponse.java @@ -0,0 +1,49 @@ +/******************************************************************************* + * ============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.database.responses; + +import org.elasticsearch.client.Response; +import org.json.JSONObject; + +public class IndexResponse extends BaseResponse{ + + private boolean isCreated; + private String id; + private boolean isUpdated; + + public IndexResponse(Response response) { + super(response); + JSONObject o = this.getJson(response); + this.id=o.getString("_id"); + this.isCreated="created".equals(o.getString("result")); + this.isUpdated="updated".equals(o.getString("result")); + //{"_index":"historicalperformance24h","_type":"historicalperformance24h","_id":"CbZxvWwB4xjGPydc9ida","_version":1,"result":"created","_shards":{"total":4,"successful":1,"failed":0},"_seq_no":1,"_primary_term":1} + } + + public String getId() { + return this.id; + } + + public boolean isCreated() { + return this.isCreated; + } + public boolean isUpdated() { + return this.isUpdated; + } + +} diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/responses/NodeStatsResponse.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/responses/NodeStatsResponse.java new file mode 100644 index 000000000..3718fd342 --- /dev/null +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/responses/NodeStatsResponse.java @@ -0,0 +1,109 @@ +/******************************************************************************* + * ============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.database.responses; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +import org.elasticsearch.client.Response; +import org.json.JSONException; +import org.json.JSONObject; + +public class NodeStatsResponse extends BaseResponse { + + private NodesInfo nodesInfo; + private Map<String,NodeStats> nodeStats; + + public NodesInfo getNodesInfo() { + return this.nodesInfo; + } + public Map<String,NodeStats> getNodeStatistics(){ + return this.nodeStats; + } + public NodeStatsResponse(Response response) throws UnsupportedOperationException, IOException, JSONException { + super(response); + + JSONObject o = this.getJson(response); + String k; + if (o != null) { + this.nodesInfo = new NodesInfo(o.getJSONObject("_nodes")); + this.nodeStats = new HashMap<>(); + if(this.nodesInfo.successful>0) { + JSONObject stats = o.getJSONObject("nodes"); + for (Object key : stats.keySet()) { + k=String.valueOf(key); + this.nodeStats.put(k, new NodeStats(k,stats.getJSONObject(k))); + } + } + } + } + + + + public static class NodesInfo{ + @Override + public String toString() { + return "NodesInfo [total=" + total + ", successful=" + successful + ", failed=" + failed + "]"; + } + + public final int total; + public final int successful; + public final int failed; + + public NodesInfo(JSONObject o) { + this.total =o.getInt("total"); + this.successful = o.getInt("successful"); + this.failed = o.getInt("failed"); + } + } + public static class NodeStats{ + public final String name; + public final NodeTotalDiskStats total; + + @Override + public String toString() { + return "NodeStats [name=" + name + ", total=" + total + "]"; + } + + public NodeStats(String name,JSONObject o) { + this.name = name; + this.total = new NodeTotalDiskStats(o.getJSONObject("fs").getJSONObject("total")); + } + } + public static class NodeTotalDiskStats{ + public final long total; + public final long available; + public final long free; + + @Override + public String toString() { + return "NodeTotalDiskStats [total=" + total + ", available=" + available + ", free=" + free + + ", getUseDiskPercentage()=" + getUseDiskPercentage() + "]"; + } + + public float getUseDiskPercentage() { + return (total-available)*100.0f/(float)total; + } + public NodeTotalDiskStats(JSONObject o) { + this.total = o.getLong("total_in_bytes"); + this.available = o.getLong("available_in_bytes"); + this.free = o.getLong("free_in_bytes"); + } + } +} diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/responses/RefreshIndexResponse.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/responses/RefreshIndexResponse.java new file mode 100644 index 000000000..569bef4a8 --- /dev/null +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/responses/RefreshIndexResponse.java @@ -0,0 +1,44 @@ +/******************************************************************************* + * ============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.database.responses; + +import org.elasticsearch.client.Response; +import org.json.JSONObject; + +/** + * { "_shards": { "total": 10, "successful": 5, "failed": 0 } } + * + * @author jack + * + */ +public class RefreshIndexResponse extends BaseResponse { + + private boolean succeeded; + + public RefreshIndexResponse(Response response) { + super(response); + JSONObject o = this.getJson(response); + this.succeeded = o.getJSONObject("_shards").getInt("failed") == 0; + // {"_index":"historicalperformance24h","_type":"historicalperformance24h","_id":"CbZxvWwB4xjGPydc9ida","_version":1,"result":"created","_shards":{"total":4,"successful":1,"failed":0},"_seq_no":1,"_primary_term":1} + } + + public boolean succeeded() { + return this.succeeded; + } + +} diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/responses/SearchResponse.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/responses/SearchResponse.java new file mode 100644 index 000000000..f13ad7d34 --- /dev/null +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/responses/SearchResponse.java @@ -0,0 +1,81 @@ +/******************************************************************************* + * ============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.database.responses; + +import org.elasticsearch.client.Response; +import org.json.JSONArray; +import org.json.JSONObject; +import org.onap.ccsdk.features.sdnr.wt.common.database.SearchHit; + +public class SearchResponse extends BaseResponse { + + private long total; + private SearchHit[] searchHits; + private JSONObject aggregations; + + public SearchResponse(Response response) { + super(response); + this.handleResult(this.getJson(response)); + } + + public SearchResponse(String json) { + super(null); + this.handleResult(this.getJson(json)); + } + + private void handleResult(JSONObject result) { + if(result!=null && this.isResponseSucceeded()) { + JSONObject hitsouter=result.getJSONObject("hits"); + this.total = hitsouter.getLong("total"); + JSONArray a=hitsouter.getJSONArray("hits"); + SearchHit[] hits=new SearchHit[a.length()]; + for(int i=0;i<a.length();i++) { + hits[i]=new SearchHit(a.getJSONObject(i)); + } + this.searchHits=hits; + if(result.has("aggregations")) { + this.aggregations = result.getJSONObject("aggregations"); + } + else { + this.aggregations=null; + } + } + else { + this.searchHits=new SearchHit[0]; + } + } + public SearchHit[] getHits() { + return this.searchHits; + } + public long getTotal() { + return this.total; + } + public boolean hasAggregations() { + return this.aggregations!=null; + } + public AggregationEntries getAggregations(String property) { + AggregationEntries entries=new AggregationEntries(); + if(this.aggregations!=null && this.aggregations.has(property)) { + JSONArray a=this.aggregations.getJSONObject(property).getJSONArray("buckets"); + for(int i=0;i<a.length();i++) { + entries.put(a.getJSONObject(i).getString("key"),a.getJSONObject(i).getLong("doc_count") ); + } + } + return entries; + } +} diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/responses/UpdateByQueryResponse.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/responses/UpdateByQueryResponse.java new file mode 100644 index 000000000..6205df7cb --- /dev/null +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/responses/UpdateByQueryResponse.java @@ -0,0 +1,70 @@ +/******************************************************************************* + * ============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.database.responses; + +import org.elasticsearch.client.Response; +import org.json.JSONObject; + +/** + * { + * "took":47, + * "timed_out":false, + * "total":1, + * "updated":1, + * "deleted":0, + * "batches":1, + * "version_conflicts":0, + * "noops":0, + * "retries":{ + * "bulk":0, + * "search":0 + * }, + * "throttled_millis":0, + * "requests_per_second":-1.0, + * "throttled_until_millis":0, + * "failures":[]} + * @author jack + * + */ +public class UpdateByQueryResponse extends BaseResponse{ + + + private int isUpdated; + private int failures; + + public UpdateByQueryResponse(Response response) { + super(response); + JSONObject o = this.getJson(response); + + this.isUpdated=o.getInt("updated"); + this.failures = o.getJSONArray("failures").length(); + //{"_index":"historicalperformance24h","_type":"historicalperformance24h","_id":"CbZxvWwB4xjGPydc9ida","_version":1,"result":"created","_shards":{"total":4,"successful":1,"failed":0},"_seq_no":1,"_primary_term":1} + } + + + public boolean isUpdated() { + return this.isUpdated>0; + } + public boolean hasFailures() { + return this.failures>0; + } + @Override + public String toString() { + return "UpdateByQueryResponse [isUpdated=" + isUpdated + ", failures=" + failures + "]"; + } +} diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/responses/UpdateResponse.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/responses/UpdateResponse.java new file mode 100644 index 000000000..539969438 --- /dev/null +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/responses/UpdateResponse.java @@ -0,0 +1,65 @@ +/******************************************************************************* + * ============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.database.responses; + +import org.elasticsearch.client.Response; +import org.json.JSONObject; + +/** + { +"_index": "networkelement-connection-v1", +"_type": "networkelement-connection", +"_id": "sim2", +"_version": 2, +"result": "updated", +"_shards": { +"total": 2, +"successful": 1, +"failed": 0 +}, +"_seq_no": 5, +"_primary_term": 1 +} + * @author jack + * + */ +public class UpdateResponse extends BaseResponse{ + + private String result; + + public UpdateResponse(Response response) { + super(response); + JSONObject o = this.getJson(response); + + this.result=o.getString("result"); + } + + public boolean succeeded() { + if(this.result==null) { + return false; + } + String s=this.result.toLowerCase(); + return s.equals("updated") || s.equals("created"); + } + + @Override + public String toString() { + return "UpdateResponse [result=" + result + "]"; + } + +} diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/file/FileWatchdog.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/file/FileWatchdog.java new file mode 100644 index 000000000..89efc45e7 --- /dev/null +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/file/FileWatchdog.java @@ -0,0 +1,128 @@ +/******************************************************************************* + * ============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.file; + +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/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 new file mode 100644 index 000000000..d415d601f --- /dev/null +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/file/PomFile.java @@ -0,0 +1,100 @@ +/******************************************************************************* + * ============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.file; + +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.List; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.xml.sax.SAXException; + +public class PomFile { + + private Document xmlDoc; + + public PomFile(InputStream is) throws ParserConfigurationException, SAXException, IOException { + DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory + .newInstance(); +// documentBuilderFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); +// documentBuilderFactory.setFeature(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false); +// documentBuilderFactory.setFeature(XMLInputFactory.SUPPORT_DTD, false); + + DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); + this.xmlDoc = documentBuilder.parse(is); + } + + public String getProperty(String key) { + Node props = findChild("properties",this.xmlDoc.getDocumentElement()); + if(props!=null) { + Node prop = findChild(key, props); + if(prop!=null) { + return getTextValue(prop); + } + } + return null; + } + + public 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); + if (c.getNodeType() == Node.TEXT_NODE) { + textValue.append(c.getNodeValue()); + } + } + return textValue.toString().trim(); + } + + private Node findChild(String name,Node root) { + List<Element> childs = getChildElementNodes(root); + for(Element n : childs) { + if(name.equals(n.getNodeName())) { + return n; + } + } + return null; + } + /** + * get all child nodes with type ELEMENT_NODE back in a list + * @param node parent node + * @return List with child nodes + */ + private static List<Element> getChildElementNodes(Node node) { + List<Element> res = new ArrayList<>(); + NodeList childs = node.getChildNodes(); + Node item; + //System.out.println("Query node "+node.getNodeName()); + for (int n=0; n < childs.getLength(); n++) { + item = childs.item(n); + //System.out.println(node.getNodeName()+"-"+item.getNodeName()+" "+item.getNodeType()); + if (item.getNodeType() == Node.ELEMENT_NODE) { + res.add((Element)childs.item(n)); + } + } + return res; + } + +} diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/file/PomPropertiesFile.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/file/PomPropertiesFile.java new file mode 100644 index 000000000..c1cb984d0 --- /dev/null +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/file/PomPropertiesFile.java @@ -0,0 +1,92 @@ +/******************************************************************************* + * ============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.file; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.Locale; + +/** + * Example Content: + * #Generated by org.apache.felix.bundleplugin + * #Tue Nov 19 10:17:57 CET 2019 + * version=0.7.0-SNAPSHOT + * groupId=org.onap.ccsdk.features.sdnr.wt +* artifactId=sdnr-wt-data-provider-provider + * @author jack + * + */ +public class PomPropertiesFile { + + private Date buildDate; + private String version; + private String groupId; + private String artifactId; + + public Date getBuildDate() { + return this.buildDate; + } + public String getVersion() { + return this.version; + } + public String getGroupId() { + return this.groupId; + } + public String getArtifactId() { + return this.artifactId; + } + private final DateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US); + public PomPropertiesFile(InputStream is) throws IOException { + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + int intRead; + while ((intRead = is.read()) != -1) { + bos.write(intRead); + } + this.parse(new String(bos.toByteArray())); + } + + private void parse(String s) { + String[] lines = s.split("\n"); + for(String line:lines) { + + if(line.startsWith("#Generated")) { + + } else if( line.startsWith("groupId")) { + this.groupId = line.substring("groupId=".length()); + } + else if(line.startsWith("artifactId")) { + this.artifactId = line.substring("artifactId=".length()); + } + else if(line.startsWith("#")){ + try { + this.buildDate = sdf.parse(line.substring(1)); + } catch (ParseException e) { + + } + } + if(version!=null && this.buildDate!=null && this.groupId!=null && this.artifactId!=null) { + break; + } + } + } +} diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/http/BaseHTTPClient.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/http/BaseHTTPClient.java new file mode 100644 index 000000000..0604de3bf --- /dev/null +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/http/BaseHTTPClient.java @@ -0,0 +1,328 @@ +/******************************************************************************* + * ============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 BaseHTTPClient { + + private static Logger LOG = LoggerFactory.getLogger(BaseHTTPClient.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 BaseHTTPClient(String base) { + this(base, false); + } + + public BaseHTTPClient(String base, boolean trustAllCerts) { + this(base, trustAllCerts, null, null, SSLCERT_NONE); + } + + public BaseHTTPClient(String base, boolean trustAllCerts, String certFilename, String passphrase, int sslCertType) { + if(!base.endsWith("/")) { + base+="/"; + } + 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 BaseHTTPResponse 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 BaseHTTPResponse 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 BaseHTTPResponse(-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 BaseHTTPResponse(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/BaseHTTPResponse.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/http/BaseHTTPResponse.java new file mode 100644 index 000000000..2ef32fce1 --- /dev/null +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/http/BaseHTTPResponse.java @@ -0,0 +1,42 @@ +/******************************************************************************* + * ============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 BaseHTTPResponse { + + public static final int CODE404 = 404; + public static final int CODE200 = 200; + public static final BaseHTTPResponse UNKNOWN = new BaseHTTPResponse(-1, ""); + public final int code; + public final String body; + + public BaseHTTPResponse(int code,String body) + { + this.code=code; + this.body=body; + } + + @Override + public String toString() { + return "BaseHTTPResponse [code=" + code + ", body=" + body + "]"; + } + + public boolean isSuccess() { + return this.code==CODE200; + } +} diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/test/JSONAssert.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/test/JSONAssert.java new file mode 100644 index 000000000..17cc231a2 --- /dev/null +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/test/JSONAssert.java @@ -0,0 +1,196 @@ +/******************************************************************************* + * ============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 java.util.Comparator; +import java.util.Iterator; +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + +public class JSONAssert { + + /** + * nonstrict comparison means that json array items can be in different orders + */ + private static Comparator<JSONObject> nonStrictComarator = new Comparator<JSONObject>() { + + @Override + public int compare(JSONObject o1, JSONObject o2) { + if (o1.equals(o2)) { + return 0; + } + Iterator<?> keys = o1.keys(); + while (keys.hasNext()) { + String key = String.valueOf(keys.next()); + int x = this.test(o1.get(key), o2.get(key)); + if (x != 0) { + return x; + } + } + return 0; + } + + private int test(Object o1, Object o2) { + int x; + if ((o1 instanceof Double) && (o2 instanceof Double)) { + + return (((Double) o1).doubleValue() - ((Double) o2).doubleValue()) < 0 ? -1 : 1; + } else if ((o1 instanceof Boolean) && (o2 instanceof Boolean)) { + return ((Boolean) o1).booleanValue() == ((Boolean) o2).booleanValue() ? 0 : -1; + + } else if ((o1 instanceof String) && (o2 instanceof String)) { + + return ((String) o1).equals(((String) o2)) ? 0 : -1; + } else if ((o1 instanceof JSONObject) && (o2 instanceof JSONObject)) { + if (((JSONObject) o1).length() != ((JSONObject) o2).length()) { + return ((JSONObject) o1).length() - ((JSONObject) o2).length() < 0 ? -1 : 1; + } + Iterator<?> keys = ((JSONObject) o1).keys(); + while (keys.hasNext()) { + String key = String.valueOf(keys.next()); + if (!((JSONObject) o2).has(key)) { + return -1; + } + x = this.test(((JSONObject) o1).get(key), ((JSONObject) o2).get(key)); + if (x != 0) { + return x; + } + } + } else if ((o1 instanceof JSONArray) && (o2 instanceof JSONArray)) { + if (((JSONArray) o1).length() != ((JSONArray) o2).length()) { + return ((JSONArray) o1).length() - ((JSONArray) o2).length() < 0 ? -1 : 1; + } + for (int i = 0; i < ((JSONArray) o1).length(); i++) { + x = this.findInArray(((JSONArray) o1).get(i), (JSONArray) o2); + if (x != 0) { + return x; + } + } + for (int i = 0; i < ((JSONArray) o2).length(); i++) { + x = this.findInArray(((JSONArray) o2).get(i), (JSONArray) o1); + if (x != 0) { + return x; + } + } + } + return 0; + + } + + private int findInArray(Object node, JSONArray o) { + for (int i = 0; i < o.length(); i++) { + if (this.test(o.get(i), node) == 0) { + return 0; + } + } + return -1; + } + }; + /** + * strict comparison means that json array items have to be in the same order + */ + private static Comparator<JSONObject> strictComarator = new Comparator<JSONObject>() { + + @Override + public int compare(JSONObject o1, JSONObject o2) { + if (o1.equals(o2)) { + return 0; + } + Iterator<?> keys = o1.keys(); + while (keys.hasNext()) { + String key = String.valueOf(keys.next()); + int x = this.test(o1.get(key), o2.get(key)); + if (x != 0) { + return x; + } + } + return 0; + } + + private int test(Object o1, Object o2) { + int x; + if ((o1 instanceof Double) && (o2 instanceof Double)) { + + return (((Double) o1).doubleValue() - ((Double) o2).doubleValue()) < 0 ? -1 : 1; + } else if ((o1 instanceof Boolean) && (o2 instanceof Boolean)) { + return ((Boolean) o1).booleanValue() == ((Boolean) o2).booleanValue() ? 0 : -1; + + } else if ((o1 instanceof String) && (o2 instanceof String)) { + + return ((String) o1).equals(((String) o2)) ? 0 : -1; + } else if ((o1 instanceof JSONObject) && (o2 instanceof JSONObject)) { + if (((JSONObject) o1).length() == 0 && ((JSONObject) o2).length() == 0) { + return 0; + } + Iterator<?> keys = ((JSONObject) o1).keys(); + while (keys.hasNext()) { + String key = String.valueOf(keys.next()); + if (!((JSONObject) o2).has(key)) { + return -1; + } + x = this.test(((JSONObject) o1).get(key), ((JSONObject) o2).get(key)); + if (x != 0) { + return x; + } + } + } else if ((o1 instanceof JSONArray) && (o2 instanceof JSONArray)) { + if (((JSONArray) o1).length() != ((JSONArray) o2).length()) { + return ((JSONArray) o1).length() - ((JSONArray) o2).length() < 0 ? -1 : 1; + } + for (int i = 0; i < ((JSONArray) o1).length(); i++) { + x = this.test(((JSONArray) o1).get(i), ((JSONArray) o2).get(i)); + if (x != 0) { + return x; + } + } + } + return 0; + + } + }; + public static void assertEquals(String def, String toTest, boolean strict) throws JSONException { + assertEquals(null, def, toTest, strict); + } + + public static void assertEquals(String message, String def, String toTest, boolean strict) throws JSONException { + if (strict) { + assertEqualsStrict(message, def, toTest); + } else { + assertEqualsNonStrict(message, def, toTest); + } + } + + private static void assertEqualsNonStrict(String message, String def, String toTest) throws JSONException { + + JSONObject d1 = new JSONObject(def); + JSONObject d2 = new JSONObject(toTest); + if (nonStrictComarator.compare(d1, d2) != 0) { + throw new AssertionError(message); + } + } + + private static void assertEqualsStrict(String message, String def, String toTest) throws JSONException { + JSONObject d1 = new JSONObject(def); + JSONObject d2 = new JSONObject(toTest); + if (strictComarator.compare(d1, d2) != 0) { + throw new AssertionError(message); + } + } + +} |