From 7dfb26b3c3a35fe73684019fbcf68ab6c9cf091a Mon Sep 17 00:00:00 2001 From: highstreetherbert Date: Mon, 13 Jul 2020 19:05:55 +0200 Subject: SDN-R common Elasticsearch 7.x support Extend SDN-R common for Elasticsearch 7.x support Issue-ID: SDNC-1284 Signed-off-by: highstreetherbert Change-Id: I4c4f66f73f8adbdf2a9ba4f77d1e2d274ce49892 Signed-off-by: highstreetherbert --- .../ccsdk/features/sdnr/wt/common/HtAssert.java | 17 +++- .../wt/common/configuration/subtypes/Section.java | 41 ++++++-- .../sdnr/wt/common/database/DatabaseClient.java | 24 +++++ .../sdnr/wt/common/database/ExtRestClient.java | 57 +++++++++-- .../sdnr/wt/common/database/HtDatabaseClient.java | 108 ++++++++++++--------- .../common/database/HtDatabaseClientException.java | 45 +++++++++ .../sdnr/wt/common/database/Portstatus.java | 45 +++++---- .../sdnr/wt/common/database/config/HostInfo.java | 2 +- .../sdnr/wt/common/database/data/EsVersion.java | 5 + .../wt/common/database/requests/BaseRequest.java | 2 +- .../database/requests/ClusterSettingsRequest.java | 8 ++ .../database/requests/CreateIndexRequest.java | 3 +- .../common/database/requests/Delete7Request.java | 42 ++++++++ .../wt/common/database/requests/Get7Request.java | 41 ++++++++ .../wt/common/database/requests/GetRequest.java | 12 ++- .../wt/common/database/requests/Index7Request.java | 50 ++++++++++ .../common/database/requests/Search7Request.java | 46 +++++++++ .../wt/common/database/requests/SearchRequest.java | 14 +++ .../common/database/requests/Update7Request.java | 40 ++++++++ .../database/requests/UpdateByQuery7Request.java | 39 ++++++++ .../wt/common/database/responses/BaseResponse.java | 9 +- .../common/database/responses/DeleteResponse.java | 2 - .../common/database/responses/SearchResponse.java | 16 ++- .../common/database/responses/UpdateResponse.java | 2 +- .../sdnr/wt/common/http/BaseHTTPClient.java | 12 ++- .../features/sdnr/wt/common/test/TestConfig.java | 21 +--- .../features/sdnr/wt/common/test/TestDbClient.java | 10 +- .../sdnr/wt/common/test/TestDbRequests.java | 74 +++++++------- .../sdnr/wt/common/test/TestPortstatus.java | 29 +++--- 29 files changed, 649 insertions(+), 167 deletions(-) create mode 100644 sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/HtDatabaseClientException.java create mode 100644 sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/requests/Delete7Request.java create mode 100644 sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/requests/Get7Request.java create mode 100644 sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/requests/Index7Request.java create mode 100644 sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/requests/Search7Request.java create mode 100644 sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/requests/Update7Request.java create mode 100644 sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/requests/UpdateByQuery7Request.java (limited to 'sdnr/wt/common/src') diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/HtAssert.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/HtAssert.java index 99a0aaa71..976213f6f 100644 --- a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/HtAssert.java +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/HtAssert.java @@ -21,6 +21,8 @@ */ package org.onap.ccsdk.features.sdnr.wt.common; +import java.io.PrintWriter; +import java.io.StringWriter; import java.util.Arrays; /** @@ -45,7 +47,7 @@ public class HtAssert { }; - private static String whoCalledMe() { + public static String whoCalledMe() { StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace(); StackTraceElement caller = stackTraceElements[3]; String classname = caller.getClassName(); @@ -54,4 +56,17 @@ public class HtAssert { return classname + "." + methodName + ":" + lineNumber; } + public static String whoCalledMeAll() { + // StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace(); + // StringBuilder line =new StringBuilder(); + // for(StackTraceElement caller:stackTraceElements) { + // line.append(String.format("%s | %s:%d\n",caller.getClassName(),caller.getMethodName(),caller.getLineNumber())); + // } + // return line.toString(); + StringWriter sw = new StringWriter(); + PrintWriter pw = new PrintWriter(sw); + new Throwable().printStackTrace(pw); + return sw.toString(); + } + } diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/configuration/subtypes/Section.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/configuration/subtypes/Section.java index 54ebd03ac..7fb58a165 100644 --- a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/configuration/subtypes/Section.java +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/configuration/subtypes/Section.java @@ -25,29 +25,41 @@ import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map.Entry; +import java.util.Objects; import java.util.Optional; import java.util.regex.Matcher; import java.util.regex.Pattern; +import org.eclipse.jdt.annotation.NonNull; import org.onap.ccsdk.features.sdnr.wt.common.configuration.exception.ConversionException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** - * + * * @author Michael Dürre, Herbert Eiselt * * subset of configuration identified by its name */ public class Section { + //Interfaces + public interface EnvGetter { + String getenv(String substring); + } + // constants private static final Logger LOG = LoggerFactory.getLogger(Section.class); private static final String DELIMITER = "="; private static final String COMMENTCHARS[] = {"#", ";"}; + private static final String ENVVARIABLE = "${"; + private static final String REGEXENVVARIABLE = "(\\$\\{[A-Z0-9_-]+\\})"; // end of constants + private final Pattern pattern; + + // variables for test purpose + private static EnvGetter envGetter = (mkey) -> System.getenv(mkey); - // variables private final String name; private final List rawLines; private final LinkedHashMap values; @@ -59,6 +71,7 @@ public class Section { this.name = name; this.rawLines = new ArrayList<>(); this.values = new LinkedHashMap<>(); + this.pattern = Pattern.compile(REGEXENVVARIABLE); } //end of constructors @@ -96,11 +109,9 @@ public class Section { value = values.get(key).getValue(); } //try to read env var - if (value != null && value.contains("${")) { + if (value != null && value.contains(ENVVARIABLE)) { LOG.debug("try to find env var(s) for {}", value); - final String regex = "(\\$\\{[A-Z0-9_-]+\\})"; - final Pattern pattern = Pattern.compile(regex); final Matcher matcher = pattern.matcher(value); String tmp = new String(value); while (matcher.find() && matcher.groupCount() > 0) { @@ -108,7 +119,8 @@ public class Section { if (mkey != null) { try { LOG.debug("match found for v={} and env key={}", tmp, mkey); - String env = System.getenv(mkey.substring(2, mkey.length() - 1)); + //String env=System.getenv(mkey.substring(2,mkey.length()-1)); + String env = envGetter.getenv(mkey.substring(2, mkey.length() - 1)); tmp = tmp.replace(mkey, env == null ? "" : env); } catch (SecurityException e) { LOG.warn("unable to read env {}: {}", value, e); @@ -120,8 +132,6 @@ public class Section { return value; } - - public void setProperty(String key, String value) { boolean isuncommented = this.isCommentLine(key); if (isuncommented) { @@ -170,8 +180,6 @@ public class Section { } } - - public String[] toLines() { List lines = new ArrayList<>(); if (!this.name.isEmpty()) { @@ -242,6 +250,19 @@ public class Section { public String toString() { return "Section [name=" + name + ", rawLines=" + rawLines + ", values=" + values + "]"; } + + // static methods + public static void setEnvGetter(@NonNull EnvGetter newEnvGetter) { + if (Objects.nonNull(newEnvGetter)) { + envGetter = newEnvGetter; + } else { + throw new IllegalArgumentException("Null not allowed here"); + } + } + + public static EnvGetter getEnvGetter() { + return envGetter; + } // end of public methods } diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/DatabaseClient.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/DatabaseClient.java index 3a9c10514..fd7246605 100644 --- a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/DatabaseClient.java +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/DatabaseClient.java @@ -100,6 +100,30 @@ public interface DatabaseClient { */ public @Nullable String doWriteRaw(String indexName, String dataTypeName, @Nullable String esId, String json); + /** + * Write one object into Database + * + * @param indexName Name of index + * @param dataTypeName Name of datatype + * @param esId of object to be replaced or null for new entry. + * @param json String in JSON format. + * @param syncAfterRewrite trigger ES to sync after insert data + * @return esId String of the database object or null in case of write problems. + */ + public @Nullable String doWriteRaw(String indexName, String dataTypeName, String esId, String json, + boolean syncAfterWrite); + + /** + * 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. + * @param syncAfterRewrite trigger ES to sync after insert data + * @return esId String of the database object or null in case of write problems. + */ + public @Nullable String doWriteRaw(String dataTypeName, String esId, String json, boolean syncAfterWrite); + /** * Remove Object from database * diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/ExtRestClient.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/ExtRestClient.java index 9d5e9f648..836c0fae7 100644 --- a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/ExtRestClient.java +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/ExtRestClient.java @@ -46,23 +46,30 @@ import org.elasticsearch.client.RestClientBuilder.HttpClientConfigCallback; import org.json.JSONException; import org.onap.ccsdk.features.sdnr.wt.common.database.config.HostInfo; import org.onap.ccsdk.features.sdnr.wt.common.database.config.HostInfo.Protocol; +import org.onap.ccsdk.features.sdnr.wt.common.database.data.EsVersion; import org.onap.ccsdk.features.sdnr.wt.common.database.requests.ClusterHealthRequest; import org.onap.ccsdk.features.sdnr.wt.common.database.requests.ClusterSettingsRequest; import org.onap.ccsdk.features.sdnr.wt.common.database.requests.CreateAliasRequest; import org.onap.ccsdk.features.sdnr.wt.common.database.requests.CreateIndexRequest; +import org.onap.ccsdk.features.sdnr.wt.common.database.requests.Delete7Request; import org.onap.ccsdk.features.sdnr.wt.common.database.requests.DeleteAliasRequest; 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.Get7Request; import org.onap.ccsdk.features.sdnr.wt.common.database.requests.GetIndexRequest; import org.onap.ccsdk.features.sdnr.wt.common.database.requests.GetInfoRequest; import org.onap.ccsdk.features.sdnr.wt.common.database.requests.GetRequest; +import org.onap.ccsdk.features.sdnr.wt.common.database.requests.Index7Request; import org.onap.ccsdk.features.sdnr.wt.common.database.requests.IndexRequest; import org.onap.ccsdk.features.sdnr.wt.common.database.requests.ListAliasesRequest; import org.onap.ccsdk.features.sdnr.wt.common.database.requests.ListIndicesRequest; 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.Search7Request; import org.onap.ccsdk.features.sdnr.wt.common.database.requests.SearchRequest; +import org.onap.ccsdk.features.sdnr.wt.common.database.requests.Update7Request; +import org.onap.ccsdk.features.sdnr.wt.common.database.requests.UpdateByQuery7Request; import org.onap.ccsdk.features.sdnr.wt.common.database.requests.UpdateByQueryRequest; import org.onap.ccsdk.features.sdnr.wt.common.database.requests.UpdateRequest; import org.onap.ccsdk.features.sdnr.wt.common.database.responses.ClusterHealthResponse; @@ -84,6 +91,7 @@ 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.onap.ccsdk.features.sdnr.wt.common.http.BaseHTTPClient; +import org.osgi.framework.Version; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -186,15 +194,34 @@ public class ExtRestClient { // } private final RestClient client; + private final boolean isES7; - protected ExtRestClient(HostInfo[] hosts) { + protected ExtRestClient(HostInfo[] hosts) throws Exception { this(hosts, null, null, false); } - protected ExtRestClient(HostInfo[] hosts, String username, String password, boolean trustAll) { + protected ExtRestClient(HostInfo[] hosts, String username, String password, boolean trustAll) throws Exception { this.client = RestClient.builder(get(hosts)) .setHttpClientConfigCallback(new BasicAuthHttpClientConfigCallback(username, password, trustAll)) .build(); + EsVersion tmp = autoDetectVersion(); + LOG.info("working with sdnrdb version {}", tmp.toString()); + this.isES7 = tmp.isNewerOrEqualThan(new EsVersion(7, 0, 0)); + } + + /** + * @return + * @throws IOException + * @throws Exception + */ + private EsVersion autoDetectVersion() throws IOException, Exception { + GetInfoResponse infoResponse = this.getInfo(); + return infoResponse.getVersion(); + + } + + public boolean isVersion7() { + return this.isES7; } public ClusterHealthResponse health(ClusterHealthRequest request) @@ -207,7 +234,6 @@ public class ExtRestClient { } - // public boolean indicesExists(GetIndexRequest request) throws IOException { Response response = this.client.performRequest(request.getInner()); return response.getStatusLine().getStatusCode() == 200; @@ -238,11 +264,17 @@ public class ExtRestClient { } public IndexResponse index(IndexRequest request) throws IOException { + if (this.isES7 && !(request instanceof Index7Request)) { + request = new Index7Request(request); + } return new IndexResponse(this.client.performRequest(request.getInner())); } public DeleteResponse delete(DeleteRequest request) throws IOException { Response response = null; + if (this.isES7 && !(request instanceof Delete7Request)) { + request = new Delete7Request(request); + } try { response = this.client.performRequest(request.getInner()); } catch (ResponseException e) { @@ -274,6 +306,9 @@ public class ExtRestClient { * @throws IOException of client */ public SearchResponse search(SearchRequest request, boolean ignoreParseException) throws IOException { + if (this.isES7 && !(request instanceof Search7Request)) { + request = new Search7Request(request); + } if (ignoreParseException) { try { return new SearchResponse(this.client.performRequest(request.getInner())); @@ -287,6 +322,9 @@ public class ExtRestClient { } public GetResponse get(GetRequest request) throws IOException { + if (this.isES7 && !(request instanceof Get7Request)) { + request = new Get7Request(request); + } try { return new GetResponse(this.client.performRequest(request.getInner())); } catch (ResponseException e) { @@ -295,11 +333,17 @@ public class ExtRestClient { } public UpdateByQueryResponse update(UpdateByQueryRequest request) throws IOException { + if (this.isES7 && !(request instanceof UpdateByQuery7Request)) { + request = new UpdateByQuery7Request(request); + } return new UpdateByQueryResponse(this.client.performRequest(request.getInner())); } public UpdateResponse update(UpdateRequest request) throws IOException { + if (this.isES7 && !(request instanceof Update7Request)) { + request = new Update7Request(request); + } return new UpdateResponse(this.client.performRequest(request.getInner())); } @@ -356,15 +400,16 @@ public class ExtRestClient { return httphosts; } - public static ExtRestClient createInstance(HostInfo[] hosts) { + public static ExtRestClient createInstance(HostInfo[] hosts) throws Exception { return new ExtRestClient(hosts); } - public static ExtRestClient createInstance(HostInfo[] hosts, String username, String password, boolean trustAll) { + public static ExtRestClient createInstance(HostInfo[] hosts, String username, String password, boolean trustAll) + throws Exception { return new ExtRestClient(hosts, username, password, trustAll); } - public static ExtRestClient createInstance(String hostname, int port, Protocol protocol) { + public static ExtRestClient createInstance(String hostname, int port, Protocol protocol) throws Exception { return createInstance(new HostInfo[] {new HostInfo(hostname, port, protocol)}); } diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/HtDatabaseClient.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/HtDatabaseClient.java index e8d86a925..1ae9f1e6c 100644 --- a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/HtDatabaseClient.java +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/HtDatabaseClient.java @@ -25,7 +25,6 @@ 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; @@ -35,7 +34,6 @@ 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; @@ -43,7 +41,6 @@ import org.onap.ccsdk.features.sdnr.wt.common.database.responses.DeleteByQueryRe 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; @@ -59,34 +56,69 @@ import org.slf4j.LoggerFactory; public class HtDatabaseClient extends ExtRestClient implements DatabaseClient, AutoCloseable { private static final boolean REFRESH_AFTER_REWRITE_DEFAULT = true; - private static final boolean TRUSTALL_DEFAULT = false; + public static final boolean TRUSTALL_DEFAULT = false; + private static final long TIMOUT_MS_DEFAULT = 30000; + private static final long READ_MAX_SIZE = 9999; + private final static long SLEEPTIMEMS = 5000; private final Logger LOG = LoggerFactory.getLogger(HtDatabaseClient.class); private boolean doRefreshAfterWrite; - public HtDatabaseClient(HostInfo[] hosts) { - this(hosts, REFRESH_AFTER_REWRITE_DEFAULT); + private HtDatabaseClient(HostInfo[] hosts, boolean refreshAfterWrite, String username, String password, + boolean trustAll) throws Exception { + super(hosts, username, password, trustAll); + this.doRefreshAfterWrite = refreshAfterWrite; } - public HtDatabaseClient(HostInfo[] hosts, boolean refreshAfterWrite) { - this(hosts, refreshAfterWrite, null, null, TRUSTALL_DEFAULT); + /*---------------------------------- + * static factory function + */ + + static public HtDatabaseClient getClient(HostInfo[] hosts) throws HtDatabaseClientException { + return getClient(hosts, REFRESH_AFTER_REWRITE_DEFAULT, null, null, TRUSTALL_DEFAULT, TIMOUT_MS_DEFAULT); } - public HtDatabaseClient(HostInfo[] hosts, String username, String password) { - this(hosts, username, password, TRUSTALL_DEFAULT); + static public HtDatabaseClient getClient(HostInfo[] hosts, String username, String password) + throws HtDatabaseClientException { + return getClient(hosts, REFRESH_AFTER_REWRITE_DEFAULT, username, password, TRUSTALL_DEFAULT, TIMOUT_MS_DEFAULT); } - public HtDatabaseClient(HostInfo[] hosts, String username, String password, boolean trustAll) { - this(hosts, REFRESH_AFTER_REWRITE_DEFAULT, username, password, trustAll); + static public HtDatabaseClient getClient(HostInfo[] hosts, String username, String password, boolean trustAll) + throws HtDatabaseClientException { + return getClient(hosts, REFRESH_AFTER_REWRITE_DEFAULT, username, password, trustAll, TIMOUT_MS_DEFAULT); } - public HtDatabaseClient(HostInfo[] hosts, boolean refreshAfterWrite, String username, String password, - boolean trustAll) { - super(hosts, username, password, trustAll); - this.doRefreshAfterWrite = refreshAfterWrite; + static public HtDatabaseClient getClient(HostInfo[] hosts, String username, String password, boolean trustAll, + long timeoutms) throws HtDatabaseClientException { + return getClient(hosts, REFRESH_AFTER_REWRITE_DEFAULT, username, password, trustAll, TIMOUT_MS_DEFAULT); } + static public HtDatabaseClient getClient(HostInfo[] hosts, boolean refreshAfterWrite, String username, + String password, boolean trustAll, long timeoutms) throws HtDatabaseClientException { + long waiting = 0; + HtDatabaseClient client = null; + Exception exceptionWas = null; + + do { + try { + client = new HtDatabaseClient(hosts, refreshAfterWrite, username, password, trustAll); + exceptionWas = null; + } catch (Exception e) { + try { + Thread.sleep(SLEEPTIMEMS); + } catch (InterruptedException e1) { + Thread.currentThread().interrupt(); + } + waiting += SLEEPTIMEMS; + exceptionWas = e; + } + } while (client == null && waiting < timeoutms); + if (client == null) { + throw new HtDatabaseClientException("Can not reach database server. Client not started.", exceptionWas); + } + return client; + } /*---------------------------------- * Functions @@ -132,8 +164,18 @@ public class HtDatabaseClient extends ExtRestClient implements DatabaseClient, A return this.doWriteRaw(dataTypeName, dataTypeName, esId, json); } + @Override + public String doWriteRaw(String dataTypeName, String esId, String json, boolean syncAfterWrite) { + return this.doWriteRaw(dataTypeName, dataTypeName, esId, json, syncAfterWrite); + } + @Override public @Nullable String doWriteRaw(String indexName, String dataTypeName, @Nullable String esId, String json) { + return this.doWriteRaw(indexName, dataTypeName, esId, json, this.doRefreshAfterWrite); + } + + @Override + public String doWriteRaw(String indexName, String dataTypeName, String esId, String json, boolean syncAfterWrite) { IndexResponse response = null; IndexRequest indexRequest = new IndexRequest(indexName, dataTypeName, esId, this.doRefreshAfterWrite); @@ -148,24 +190,9 @@ public class HtDatabaseClient extends ExtRestClient implements DatabaseClient, A 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()); @@ -181,9 +208,6 @@ public class HtDatabaseClient extends ExtRestClient implements DatabaseClient, A } catch (IOException e) { LOG.warn("Problem deleting from db: {}", e.getMessage()); } - // if(this.doRefreshAfterWrite) { - // this.doRefresh(dataTypeName); - // } return response != null ? response.isDeleted() : false; } @@ -229,7 +253,6 @@ public class HtDatabaseClient extends ExtRestClient implements DatabaseClient, A long total = 0; LOG.debug("NetworkIndex query and read: {}", dataTypeName); - SearchRequest searchRequest = new SearchRequest(alias, dataTypeName); searchRequest.setQuery(queryBuilder); SearchResponse response = null; @@ -250,7 +273,8 @@ public class HtDatabaseClient extends ExtRestClient implements DatabaseClient, A @Override public @Nonnull SearchResult doReadAllJsonData(String dataTypeName, boolean ignoreException) { - return doReadByQueryJsonData(dataTypeName, QueryBuilders.matchAllQuery(), ignoreException); + return doReadByQueryJsonData(dataTypeName, QueryBuilders.matchAllQuery().size(READ_MAX_SIZE).from(0), + ignoreException); } public @Nonnull SearchResult doReadAllJsonData(String alias, String dataType, boolean ignoreException) { @@ -277,11 +301,8 @@ public class HtDatabaseClient extends ExtRestClient implements DatabaseClient, A UpdateResponse response = this.update(request); success = response.succeeded(); } catch (IOException e) { - LOG.warn("Problem updating {} with id {} and data {}: {}", dataTypeName, esId, json, e); + LOG.warn("Problem updating {} with id {} and data {}: '{}'", dataTypeName, esId, json, e.getMessage()); } - // if(this.doRefreshAfterWrite) { - // this.doRefresh(dataTypeName); - // } return success ? esId : null; } @@ -296,9 +317,6 @@ public class HtDatabaseClient extends ExtRestClient implements DatabaseClient, A } 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; } @@ -315,11 +333,9 @@ public class HtDatabaseClient extends ExtRestClient implements DatabaseClient, A } 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/HtDatabaseClientException.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/HtDatabaseClientException.java new file mode 100644 index 000000000..b1b3f7275 --- /dev/null +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/HtDatabaseClientException.java @@ -0,0 +1,45 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : ccsdk features + * ================================================================================ + * Copyright (C) 2020 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 HtDatabaseClientException extends Exception { + // constants + private static final long serialVersionUID = 1L; + // end of constants + + // variables + private Throwable rootCause; + // end of variables + + // constructors + public HtDatabaseClientException(String message, Throwable rootCause) { + super(message, rootCause); + this.rootCause = rootCause; + } + // end of constructors + + // getters and setters + public Throwable getRootCause() { + return rootCause; + } + // end of getters and setters +} diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/Portstatus.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/Portstatus.java index 250842e57..475178b4c 100644 --- a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/Portstatus.java +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/Portstatus.java @@ -1,25 +1,30 @@ /* - * ============LICENSE_START======================================================================== - * ONAP : ccsdk feature sdnr wt - * ================================================================================================= - * Copyright (C) Apache Camel project - * ================================================================================================= - * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at + * ============LICENSE_START======================================================= + * ONAP : ccsdk features + * ================================================================================ + * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property. + * All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * - * Unless required by applicable law or agreed to in writing, software distributed under the License - * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express - * or implied. See the License for the specific language governing permissions and limitations under - * the License. - * ============LICENSE_END========================================================================== + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= */ - package org.onap.ccsdk.features.sdnr.wt.common.database; import java.io.IOException; -import java.net.Socket; +import java.net.InetSocketAddress; +import java.net.SocketAddress; +import java.nio.channels.SocketChannel; + import org.onap.ccsdk.features.sdnr.wt.common.database.config.HostInfo; public class Portstatus { @@ -37,16 +42,16 @@ public class Portstatus { throw new IllegalArgumentException("Invalid start port: " + port); } - Socket ss = null; + SocketChannel channel = null; + SocketAddress socketAddress = new InetSocketAddress(dnsName, port); try { - ss = new Socket(dnsName, port); - ss.setReuseAddress(true); + channel = SocketChannel.open(socketAddress); return true; } catch (IOException e) { } finally { - if (ss != null) { + if (channel != null) { try { - ss.close(); + channel.close(); } catch (IOException e) { /* should not be thrown */ } 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 index cb0454361..2dd05c574 100644 --- 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 @@ -96,7 +96,7 @@ public class HostInfo { final Pattern pattern = Pattern.compile(regex); final Matcher matcher = pattern.matcher(dbUrl); if (!matcher.find() || matcher.groupCount() < 2) { - throw new ParseException("url " + dbUrl + " not parseable", 0); + throw new ParseException("url '" + dbUrl + "' not parseable. Expected http://xyz", 0); } Protocol p = Protocol.getValueOf(matcher.group(1)); String host = matcher.group(2); diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/data/EsVersion.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/data/EsVersion.java index 7d106dfec..1dd1c3d53 100644 --- a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/data/EsVersion.java +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/data/EsVersion.java @@ -48,6 +48,11 @@ public class EsVersion { this.revision = Integer.parseInt(hlp[2]); } + @Override + public String toString() { + return String.format("%d.%d.%d", this.major, this.minor, this.revision); + } + /** * @param major * @param minor 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 index 028de75d2..5e4467e74 100644 --- 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 @@ -35,7 +35,7 @@ public abstract class BaseRequest { private static final Logger LOG = LoggerFactory.getLogger(BaseRequest.class); - public static final int DEFAULT_RETRIES = 1; + public static final int DEFAULT_RETRIES = 3; protected final Request request; private String query; diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/requests/ClusterSettingsRequest.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/requests/ClusterSettingsRequest.java index 1379da5ad..a85227858 100644 --- a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/requests/ClusterSettingsRequest.java +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/requests/ClusterSettingsRequest.java @@ -41,6 +41,7 @@ public class ClusterSettingsRequest extends BaseRequest { this.persistent = new JSONObject(); this.data.put("persistent", this.persistent); this.allowAutoCreateIndex(autoCreateIndex); + } public ClusterSettingsRequest allowAutoCreateIndex(boolean allow) { @@ -48,4 +49,11 @@ public class ClusterSettingsRequest extends BaseRequest { this.setQuery(this.data); return this; } + + public ClusterSettingsRequest maxCompilationsPerMinute(long executions) { + //this.persistent.put("script.max_compilations_per_minute" ,executions); + this.persistent.put("script.max_compilations_rate", String.format("%d/1m", executions)); + this.setQuery(this.data); + return this; + } } 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 index 076032b29..2d14f2dca 100644 --- 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 @@ -54,9 +54,10 @@ public class CreateIndexRequest extends BaseRequest { return this; } - public void settings(JSONObject settings) { + public CreateIndexRequest settings(JSONObject settings) { this.settings = settings; this.setRequest(); + return this; } public boolean hasMappings() { diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/requests/Delete7Request.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/requests/Delete7Request.java new file mode 100644 index 000000000..923bb772a --- /dev/null +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/requests/Delete7Request.java @@ -0,0 +1,42 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : ccsdk features + * ================================================================================ + * Copyright (C) 2020 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; + +/** + * @author Michael Dürre + * + */ +public class Delete7Request extends DeleteRequest { + + public Delete7Request(String alias, String esId) { + super(alias, "_doc", esId); + } + + public Delete7Request(String alias, String esId, boolean refresh) { + super(alias, "_doc", esId, refresh); + } + + public Delete7Request(DeleteRequest request) { + this(request.getAlias(), request.getEsId(), request.doRefresh()); + } + +} diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/requests/Get7Request.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/requests/Get7Request.java new file mode 100644 index 000000000..3996a6fe7 --- /dev/null +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/requests/Get7Request.java @@ -0,0 +1,41 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : ccsdk features + * ================================================================================ + * Copyright (C) 2020 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; + +/** + * @author Michael Dürre + * + */ +public class Get7Request extends GetRequest { + + public Get7Request(String alias, String esId) { + super(alias, "_doc", esId); + } + + /** + * @param request + */ + public Get7Request(GetRequest request) { + this(request.getAlias(), request.getEsId()); + } + +} 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 index 6092d88b0..0ad74581b 100644 --- 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 @@ -25,10 +25,20 @@ import javax.annotation.Nonnull; public class GetRequest extends BaseRequest { + private final String alias; + private final String esId; + public GetRequest(String alias, String dataType, @Nonnull String esId) { super("GET", String.format("/%s/%s/%s", alias, dataType, BaseRequest.urlEncodeValue(esId))); + this.alias = alias; + this.esId = esId; } + protected String getAlias() { + return this.alias; + } - + protected String getEsId() { + return this.esId; + } } diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/requests/Index7Request.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/requests/Index7Request.java new file mode 100644 index 000000000..f399032d4 --- /dev/null +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/requests/Index7Request.java @@ -0,0 +1,50 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : ccsdk features + * ================================================================================ + * Copyright (C) 2020 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; + +/** + * @author Michael Dürre + * + */ +public class Index7Request extends IndexRequest { + + public Index7Request(String alias) { + super(alias, "_doc"); + } + + public Index7Request(String alias, String esId) { + super(alias, "_doc", esId); + } + + public Index7Request(String alias, String esId, boolean refresh) { + super(alias, "_doc", esId, refresh); + } + + /** + * @param request + */ + public Index7Request(IndexRequest request) { + this(request.getAlias(), request.getEsId(), request.doRefresh()); + this.setQuery(request.getQuery()); + } + +} diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/requests/Search7Request.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/requests/Search7Request.java new file mode 100644 index 000000000..9c4424fde --- /dev/null +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/requests/Search7Request.java @@ -0,0 +1,46 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : ccsdk features + * ================================================================================ + * Copyright (C) 2020 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; + +/** + * @author Michael Dürre + * + */ +public class Search7Request extends SearchRequest { + + /** + * @param alias + * @param dataType + */ + public Search7Request(String alias) { + super(String.format("/%s/_search", alias)); + } + + /** + * @param request + */ + public Search7Request(SearchRequest request) { + this(request.getAlias()); + this.setQuery(request.getQuery()); + } + +} 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 index 113cb4e1e..f90889418 100644 --- 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 @@ -25,8 +25,16 @@ import org.onap.ccsdk.features.sdnr.wt.common.database.queries.QueryBuilder; public class SearchRequest extends BaseRequest { + private final String alias; + + public SearchRequest(String uri) { + super("POST", uri); + this.alias = null; + } + public SearchRequest(String alias, String dataType) { super("POST", String.format("/%s/%s/_search", alias, dataType)); + this.alias = alias; } @Override @@ -34,4 +42,10 @@ public class SearchRequest extends BaseRequest { super.setQuery(query); } + public String getAlias() { + return this.alias; + } + + + } diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/requests/Update7Request.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/requests/Update7Request.java new file mode 100644 index 000000000..6cff93319 --- /dev/null +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/requests/Update7Request.java @@ -0,0 +1,40 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : ccsdk features + * ================================================================================ + * Copyright (C) 2020 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; + +/** + * @author Michael Dürre + * + * https://github.com/elastic/elasticsearch/blob/7.1/rest-api-spec/src/main/resources/rest-api-spec/api/update.json + */ +public class Update7Request extends UpdateRequest { + + public Update7Request(String alias, String esId, int retries, boolean refresh) { + super(String.format("/%s/_update/%s", alias, BaseRequest.urlEncodeValue(esId), retries), refresh); + } + + public Update7Request(UpdateRequest request) { + this(request.getAlias(), request.getEsId(), request.getRetries(), request.doRefresh()); + this.setQuery(request.getQuery()); + } + +} diff --git a/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/requests/UpdateByQuery7Request.java b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/requests/UpdateByQuery7Request.java new file mode 100644 index 000000000..14a45be6e --- /dev/null +++ b/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/database/requests/UpdateByQuery7Request.java @@ -0,0 +1,39 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : ccsdk features + * ================================================================================ + * Copyright (C) 2020 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; + +/** + * @author Michael Dürre + * + */ +public class UpdateByQuery7Request extends UpdateByQueryRequest { + + public UpdateByQuery7Request(String alias, boolean refresh) { + super(alias, "_doc", refresh); + } + + public UpdateByQuery7Request(UpdateByQueryRequest request) { + this(request.getAlias(), request.doRefresh()); + this.setQuery(request.getQuery()); + } + +} 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 index 2746be4a4..27916459f 100644 --- 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 @@ -32,6 +32,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class BaseResponse { + private static final Logger LOG = LoggerFactory.getLogger(BaseResponse.class); private final int responseCode; @@ -45,14 +46,10 @@ public class BaseResponse { } public boolean isResponseSucceeded() { - return this.responseCode < 300; + return this.responseCode < 300 && this.responseCode >= 200; } JSONObject getJson(Response response) { - if (response == null) { - LOG.warn("unable to parse response. response is null."); - return null; - } try { String sresponse = EntityUtils.toString(response.getEntity()); LOG.debug("parsing response={}", sresponse); @@ -95,4 +92,6 @@ public class BaseResponse { } } + + } 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 index f6202d009..46ef1b9f8 100644 --- 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 @@ -35,8 +35,6 @@ public class DeleteResponse extends BaseResponse { JSONObject o = this.getJson(response); if (o != null) { this.isDeleted = "deleted".equals(o.getString("result")); - } else { - this.isDeleted = false; } } else { this.isDeleted = false; 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 index 6a7ed0d06..88a14c6d1 100644 --- 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 @@ -45,7 +45,7 @@ public class SearchResponse extends BaseResponse { private void handleResult(JSONObject result) { if (result != null && this.isResponseSucceeded()) { JSONObject hitsouter = result.getJSONObject("hits"); - this.total = hitsouter.getLong("total"); + this.total = this.getTotalFromHits(hitsouter); JSONArray a = hitsouter.getJSONArray("hits"); SearchHit[] hits = new SearchHit[a.length()]; for (int i = 0; i < a.length(); i++) { @@ -84,4 +84,18 @@ public class SearchResponse extends BaseResponse { } return entries; } + + /** + * @param hits + * @return + */ + private long getTotalFromHits(JSONObject hits) { + Object o = hits.get("total"); + if (o instanceof Long || o instanceof Integer) { + return hits.getLong("total"); + } else if (o instanceof JSONObject) { + return hits.getJSONObject("total").getLong("value"); + } + return 0; + } } 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 index 80cb24872..2c9fee675 100644 --- 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 @@ -39,7 +39,7 @@ public class UpdateResponse extends BaseResponse { super(response); JSONObject o = this.getJson(response); - this.result = o.getString("result"); + this.result = o == null ? null : o.getString("result"); } public boolean succeeded() { 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 index fdba374c7..87a376538 100644 --- 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 @@ -72,7 +72,7 @@ public class BaseHTTPClient { private static final int DEFAULT_HTTP_TIMEOUT_MS = 30000; // in ms private final boolean trustAll; - private final String baseUrl; + private String baseUrl; private int timeout = DEFAULT_HTTP_TIMEOUT_MS; private SSLContext sc = null; @@ -99,7 +99,15 @@ public class BaseHTTPClient { } } - protected @Nonnull BaseHTTPResponse sendRequest(String uri, String method, String body, Map headers) + public void setBaseUrl(String base) { + if (!base.endsWith("/")) { + base += "/"; + } + this.baseUrl = base; + } + + @Nonnull + protected BaseHTTPResponse sendRequest(String uri, String method, String body, Map headers) throws IOException { return this.sendRequest(uri, method, body != null ? body.getBytes(CHARSET) : null, headers); } diff --git a/sdnr/wt/common/src/test/java/org/onap/ccsdk/features/sdnr/wt/common/test/TestConfig.java b/sdnr/wt/common/src/test/java/org/onap/ccsdk/features/sdnr/wt/common/test/TestConfig.java index 415620415..cbe0b9c4d 100644 --- a/sdnr/wt/common/src/test/java/org/onap/ccsdk/features/sdnr/wt/common/test/TestConfig.java +++ b/sdnr/wt/common/src/test/java/org/onap/ccsdk/features/sdnr/wt/common/test/TestConfig.java @@ -21,16 +21,16 @@ */ package org.onap.ccsdk.features.sdnr.wt.common.test; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; import java.io.File; import java.io.IOException; -import java.lang.reflect.Field; import java.net.MalformedURLException; import java.net.URL; import java.nio.file.Files; -import java.util.Collections; -import java.util.Map; import org.junit.After; import org.junit.Before; @@ -249,17 +249,4 @@ public class TestConfig { section.parseLines(); assertTrue(section.getProperty(KEY).length() > " in ".length()); } - - public static void setEnv(String key, String value) { - try { - Map env = System.getenv(); - Class cl = env.getClass(); - Field field = cl.getDeclaredField("m"); - field.setAccessible(true); - Map writableEnv = (Map) field.get(env); - writableEnv.put(key, value); - } catch (Exception e) { - throw new IllegalStateException("Failed to set environment variable", e); - } - } } diff --git a/sdnr/wt/common/src/test/java/org/onap/ccsdk/features/sdnr/wt/common/test/TestDbClient.java b/sdnr/wt/common/src/test/java/org/onap/ccsdk/features/sdnr/wt/common/test/TestDbClient.java index cf9d93052..421a2229c 100644 --- a/sdnr/wt/common/src/test/java/org/onap/ccsdk/features/sdnr/wt/common/test/TestDbClient.java +++ b/sdnr/wt/common/src/test/java/org/onap/ccsdk/features/sdnr/wt/common/test/TestDbClient.java @@ -21,10 +21,12 @@ */ package org.onap.ccsdk.features.sdnr.wt.common.test; -import static org.junit.Assert.*; - +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; import java.io.IOException; - import org.junit.BeforeClass; import org.junit.Test; import org.onap.ccsdk.features.sdnr.wt.common.database.HtDatabaseClient; @@ -47,7 +49,7 @@ public class TestDbClient { @BeforeClass public static void init() throws Exception { - dbClient = new HtDatabaseClient(hosts); + dbClient = HtDatabaseClient.getClient(hosts); dbClient.waitForYellowStatus(20000); } diff --git a/sdnr/wt/common/src/test/java/org/onap/ccsdk/features/sdnr/wt/common/test/TestDbRequests.java b/sdnr/wt/common/src/test/java/org/onap/ccsdk/features/sdnr/wt/common/test/TestDbRequests.java index 756d70c85..492dfa96e 100644 --- a/sdnr/wt/common/src/test/java/org/onap/ccsdk/features/sdnr/wt/common/test/TestDbRequests.java +++ b/sdnr/wt/common/src/test/java/org/onap/ccsdk/features/sdnr/wt/common/test/TestDbRequests.java @@ -21,6 +21,18 @@ */ package org.onap.ccsdk.features.sdnr.wt.common.test; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import java.io.IOException; +import java.text.ParseException; +import java.util.Arrays; +import java.util.List; +import org.json.JSONException; +import org.json.JSONObject; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; @@ -57,20 +69,6 @@ import org.onap.ccsdk.features.sdnr.wt.common.database.responses.SearchResponse; import org.onap.ccsdk.features.sdnr.wt.common.database.responses.UpdateByQueryResponse; import org.onap.ccsdk.features.sdnr.wt.common.database.responses.UpdateResponse; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; -import java.io.IOException; -import java.text.ParseException; -import java.util.Arrays; -import java.util.List; - -import org.json.JSONException; -import org.json.JSONObject; - public class TestDbRequests { private static HtDatabaseClient dbClient; @@ -80,7 +78,7 @@ public class TestDbRequests { @BeforeClass public static void init() throws Exception { - dbClient = new HtDatabaseClient(hosts); + dbClient = HtDatabaseClient.getClient(hosts); } @@ -200,7 +198,7 @@ public class TestDbRequests { } this.insert(IDX, ID, JSON); // delete data - DeleteRequest request2 = new DeleteRequest(IDX, IDX, ID); + DeleteRequest request2 = new DeleteRequest(IDX, dbClient.isVersion7() ? "_doc" : IDX, ID); DeleteResponse response2 = null; try { response2 = dbClient.delete(request2); @@ -215,7 +213,7 @@ public class TestDbRequests { fail(e.getMessage()); } // verify data deleted - GetRequest request4 = new GetRequest(IDX, IDX, ID); + GetRequest request4 = new GetRequest(IDX, dbClient.isVersion7() ? "_doc" : IDX, ID); GetResponse response4 = null; try { response4 = dbClient.get(request4); @@ -233,8 +231,11 @@ public class TestDbRequests { */ private JSONObject defaultMappings(String idx, boolean useStrict) { String mapping = "{}"; - return new JSONObject(String.format("{\"%s\":{%s\"properties\":%s}}", idx, - useStrict ? "\"dynamic\": false," : "\"dynamic\": true,", mapping)); + return dbClient.isVersion7() + ? new JSONObject(String.format("{%s\"properties\":%s}", + useStrict ? "\"dynamic\": false," : "\"dynamic\": true,", mapping)) + : new JSONObject(String.format("{\"%s\":{%s\"properties\":%s}}", idx, + useStrict ? "\"dynamic\": false," : "\"dynamic\": true,", mapping)); } @Test @@ -325,7 +326,7 @@ public class TestDbRequests { this.insert(IDX, ID, JSON); this.insert(IDX, ID2, JSON2); this.insert(IDX, ID3, JSON3); - SearchRequest request = new SearchRequest(IDX, IDX); + SearchRequest request = new SearchRequest(IDX, dbClient.isVersion7() ? "_doc" : IDX); request.setQuery(QueryBuilders.matchAllQuery()); SearchResponse response = null; try { @@ -336,7 +337,7 @@ public class TestDbRequests { assertNotNull(response); assertEquals("not all items found", 3, response.getHits().length); assertEquals("incorrect index", IDX, response.getHits()[0].getIndex()); - assertEquals("incorrect type", IDX, response.getHits()[0].getType()); + assertEquals("incorrect type", dbClient.isVersion7() ? "_doc" : IDX, response.getHits()[0].getType()); this.deleteIndex(IDX); } @@ -354,7 +355,7 @@ public class TestDbRequests { fail("unable to create index"); } this.insert(IDX, ID, JSON); - UpdateRequest request = new UpdateRequest(IDX, IDX, ID); + UpdateRequest request = new UpdateRequest(IDX, dbClient.isVersion7() ? "_doc" : IDX, ID); UpdateResponse response = null; try { request.source(new JSONObject(JSON2)); @@ -371,7 +372,7 @@ public class TestDbRequests { fail(e.getMessage()); } // verify update - GetRequest request3 = new GetRequest(IDX, IDX, ID); + GetRequest request3 = new GetRequest(IDX, dbClient.isVersion7() ? "_doc" : IDX, ID); GetResponse response3 = null; try { response3 = dbClient.get(request3); @@ -397,7 +398,7 @@ public class TestDbRequests { fail("unable to create index"); } this.insert(IDX, ID, JSON); - UpdateByQueryRequest request = new UpdateByQueryRequest(IDX, IDX); + UpdateByQueryRequest request = new UpdateByQueryRequest(IDX, dbClient.isVersion7() ? "_doc" : IDX); UpdateByQueryResponse response = null; try { request.source(ID, new JSONObject(JSON2)); @@ -414,7 +415,7 @@ public class TestDbRequests { fail(e.getMessage()); } // verify update - GetRequest request3 = new GetRequest(IDX, IDX, ID); + GetRequest request3 = new GetRequest(IDX, dbClient.isVersion7() ? "_doc" : IDX, ID); GetResponse response3 = null; try { response3 = dbClient.get(request3); @@ -434,9 +435,10 @@ public class TestDbRequests { final String JSON3 = "{ \"node-id\":\"sim3\",\"severity\":\"minor\"}"; final String JSON4 = "{ \"node-id\":\"sim4\",\"severity\":\"warning\"}"; final String JSON5 = "{ \"node-id\":\"sim5\",\"severity\":\"major\"}"; - final String MAPPINGS = String.format("{\"" + IDX + "\":{\"properties\":%s}}", - "{\"node-id\":{\"type\": \"keyword\"},\"severity\": {\"type\": \"keyword\"}}"); - // create index with mapping keyword + final String MAPPINGS = + String.format(dbClient.isVersion7() ? "{\"properties\":%s}" : "{\"" + IDX + "\":{\"properties\":%s}}", + "{\"node-id\":{\"type\": \"keyword\"},\"severity\": {\"type\": \"keyword\"}}"); + //create index with mapping keyword CreateIndexResponse iresponse = null; try { if (!dbClient.isExistsIndex(IDX)) { @@ -449,6 +451,7 @@ public class TestDbRequests { fail("unable to create index: " + e1.getMessage()); } + // fill index this.insert(IDX, null, JSON); this.insert(IDX, null, JSON2); @@ -462,7 +465,7 @@ public class TestDbRequests { fail(e.getMessage()); } - SearchRequest request = new SearchRequest(IDX, IDX); + SearchRequest request = new SearchRequest(IDX, dbClient.isVersion7() ? "_doc" : IDX); request.setQuery(QueryBuilders.matchAllQuery().aggregations("severity").size(0)); SearchResponse response = null; try { @@ -501,7 +504,7 @@ public class TestDbRequests { System.out.println(stats.getNodeStatistics()); } - // @Test + //@Test public void testPreventAutoCreateIndex() { final String IDX1 = "acidx1"; final String ID1 = "acid1"; @@ -511,7 +514,7 @@ public class TestDbRequests { ClusterSettingsResponse settingsResponse = null; String esId = null; - // set setting to allow autocreate + //set setting to allow autocreate try { settingsResponse = dbClient.setupClusterSettings(new ClusterSettingsRequest(true)); } catch (IOException e) { @@ -519,10 +522,10 @@ public class TestDbRequests { } assertNotNull(settingsResponse); assertTrue(settingsResponse.isAcknowledged()); - // test if something new can be created + //test if something new can be created esId = dbClient.doWriteRaw(IDX1, IDX1, ID1, OBJ); assertEquals(ID1, esId); - // set setting to deny autocreate + //set setting to deny autocreate try { settingsResponse = dbClient.setupClusterSettings(new ClusterSettingsRequest(false)); } catch (IOException e) { @@ -530,10 +533,10 @@ public class TestDbRequests { } assertNotNull(settingsResponse); assertTrue(settingsResponse.isAcknowledged()); - // test if something new cannot be created + //test if something new cannot be created esId = dbClient.doWriteRaw(IDX2, IDX2, ID2, OBJ); assertNull(esId); - // set setting to allow autocreate + //set setting to allow autocreate try { settingsResponse = dbClient.setupClusterSettings(new ClusterSettingsRequest(true)); } catch (IOException e) { @@ -560,4 +563,5 @@ public class TestDbRequests { } } + } diff --git a/sdnr/wt/common/src/test/java/org/onap/ccsdk/features/sdnr/wt/common/test/TestPortstatus.java b/sdnr/wt/common/src/test/java/org/onap/ccsdk/features/sdnr/wt/common/test/TestPortstatus.java index d073043c2..3ddbcebd0 100644 --- a/sdnr/wt/common/src/test/java/org/onap/ccsdk/features/sdnr/wt/common/test/TestPortstatus.java +++ b/sdnr/wt/common/src/test/java/org/onap/ccsdk/features/sdnr/wt/common/test/TestPortstatus.java @@ -1,19 +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 + * ============LICENSE_START======================================================= + * ONAP : ccsdk features + * ================================================================================ + * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property. + * All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * - * Unless required by applicable law or agreed to in writing, software distributed under the License - * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express - * or implied. See the License for the specific language governing permissions and limitations under - * the License. - * ============LICENSE_END========================================================================== + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= */ package org.onap.ccsdk.features.sdnr.wt.common.test; -- cgit 1.2.3-korg