From dd3a1f88075c6e636574765852e9509ebc1af774 Mon Sep 17 00:00:00 2001 From: Andy Kwok Date: Thu, 14 Nov 2024 14:50:00 -0800 Subject: [PATCH 01/36] Common module Signed-off-by: Andy Kwok --- build.gradle | 1 + common/build.gradle | 35 +++++++++++++++++++ .../main/java/org/opensearch/CommonMain.java | 11 ++++++ settings.gradle | 5 ++- .../geospatial/plugin/GeospatialPlugin.java | 2 ++ 5 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 common/build.gradle create mode 100644 common/src/main/java/org/opensearch/CommonMain.java diff --git a/build.gradle b/build.gradle index d76333f3..c42b2854 100644 --- a/build.gradle +++ b/build.gradle @@ -162,6 +162,7 @@ configurations { dependencies { implementation "org.opensearch.plugin:geo:${opensearch_version}" api project(":libs:h3") + api project(":geospatial-common") yamlRestTestRuntimeOnly "org.apache.logging.log4j:log4j-core:${versions.log4j}" testImplementation "org.hamcrest:hamcrest:${versions.hamcrest}" testImplementation 'org.json:json:20231013' diff --git a/common/build.gradle b/common/build.gradle new file mode 100644 index 00000000..3fcb2566 --- /dev/null +++ b/common/build.gradle @@ -0,0 +1,35 @@ +plugins { + id 'java' + id 'maven-publish' + +} + +group = 'org.opensearch' +version = '3.0.0.0-SNAPSHOT' + +repositories { + mavenLocal() + maven { url "https://aws.oss.sonatype.org/content/repositories/snapshots" } + mavenCentral() + maven { url "https://plugins.gradle.org/m2/" } +} + +dependencies { + testImplementation platform('org.junit:junit-bom:5.10.0') + testImplementation 'org.junit.jupiter:junit-jupiter' +} + +publishing { + publications { + mavenJava(MavenPublication) { + from components.java + } + } + repositories { + mavenLocal() + } +} + +test { + useJUnitPlatform() +} \ No newline at end of file diff --git a/common/src/main/java/org/opensearch/CommonMain.java b/common/src/main/java/org/opensearch/CommonMain.java new file mode 100644 index 00000000..e32de89d --- /dev/null +++ b/common/src/main/java/org/opensearch/CommonMain.java @@ -0,0 +1,11 @@ +package org.opensearch; + +public class CommonMain { + + public final static String TEST_STR = "TEST"; + + public static void main(String[] args) { + + System.out.println("Hello world!"); + } +} \ No newline at end of file diff --git a/settings.gradle b/settings.gradle index 72251801..5a765915 100644 --- a/settings.gradle +++ b/settings.gradle @@ -10,4 +10,7 @@ rootProject.name = 'geospatial' include ":libs" -include ":libs:h3" \ No newline at end of file +include ":libs:h3" + +include 'common' +project(":common").name = rootProject.name + "-common" diff --git a/src/main/java/org/opensearch/geospatial/plugin/GeospatialPlugin.java b/src/main/java/org/opensearch/geospatial/plugin/GeospatialPlugin.java index d64f20b4..68a949b5 100644 --- a/src/main/java/org/opensearch/geospatial/plugin/GeospatialPlugin.java +++ b/src/main/java/org/opensearch/geospatial/plugin/GeospatialPlugin.java @@ -13,6 +13,7 @@ import java.util.Map; import java.util.function.Supplier; +import org.opensearch.CommonMain; import org.opensearch.action.ActionRequest; import org.opensearch.client.Client; import org.opensearch.cluster.metadata.IndexNameExpressionResolver; @@ -220,6 +221,7 @@ public List getRestHandlers( new ActionHandler<>(UpdateDatasourceAction.INSTANCE, UpdateDatasourceTransportAction.class), new ActionHandler<>(DeleteDatasourceAction.INSTANCE, DeleteDatasourceTransportAction.class) ); + String testStr = CommonMain.TEST_STR; List> allHandlers = new ArrayList<>(); allHandlers.addAll(geoJsonHandlers); From c5fc1542e97615fed203e1170a7bcb0bcfcd5161 Mon Sep 17 00:00:00 2001 From: Andy Kwok Date: Fri, 15 Nov 2024 16:13:39 -0800 Subject: [PATCH 02/36] POC Signed-off-by: Andy Kwok --- client/build.gradle | 37 +++++++++ .../opensearch/IpEnrichmentActionClient.java | 35 ++++++++ common/build.gradle | 3 +- .../geospatial/action/IpEnrichmentAction.java | 21 +++++ .../action/IpEnrichmentRequest.java | 79 +++++++++++++++++++ .../action/IpEnrichmentResponse.java | 69 ++++++++++++++++ settings.gradle | 2 + .../model/IpEnrichmentTransportAction.java | 46 +++++++++++ .../geospatial/plugin/GeospatialPlugin.java | 9 ++- 9 files changed, 299 insertions(+), 2 deletions(-) create mode 100644 client/build.gradle create mode 100644 client/src/main/java/org/opensearch/IpEnrichmentActionClient.java create mode 100644 common/src/main/java/org/opensearch/geospatial/action/IpEnrichmentAction.java create mode 100644 common/src/main/java/org/opensearch/geospatial/action/IpEnrichmentRequest.java create mode 100644 common/src/main/java/org/opensearch/geospatial/action/IpEnrichmentResponse.java create mode 100644 src/main/java/org/opensearch/geospatial/action/model/IpEnrichmentTransportAction.java diff --git a/client/build.gradle b/client/build.gradle new file mode 100644 index 00000000..11cbe094 --- /dev/null +++ b/client/build.gradle @@ -0,0 +1,37 @@ +plugins { + id 'java' + id 'maven-publish' +} + +group = 'org.opensearch' +version = '3.0.0.0-SNAPSHOT' + +repositories { + mavenLocal() + maven { url "https://aws.oss.sonatype.org/content/repositories/snapshots" } + mavenCentral() + maven { url "https://plugins.gradle.org/m2/" } +} + +dependencies { + compileOnly "org.opensearch:opensearch:${opensearch_version}" + implementation(project(":${rootProject.name}-common")) + + testImplementation platform('org.junit:junit-bom:5.10.0') + testImplementation 'org.junit.jupiter:junit-jupiter' +} + +publishing { + publications { + mavenJava(MavenPublication) { + from components.java + } + } + repositories { + mavenLocal() + } +} + +test { + useJUnitPlatform() +} \ No newline at end of file diff --git a/client/src/main/java/org/opensearch/IpEnrichmentActionClient.java b/client/src/main/java/org/opensearch/IpEnrichmentActionClient.java new file mode 100644 index 00000000..152cbd59 --- /dev/null +++ b/client/src/main/java/org/opensearch/IpEnrichmentActionClient.java @@ -0,0 +1,35 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.opensearch; + +import org.opensearch.client.node.NodeClient; +import org.opensearch.common.action.ActionFuture; +import org.opensearch.core.action.ActionResponse; +import org.opensearch.geospatial.action.IpEnrichmentAction; +import org.opensearch.geospatial.action.IpEnrichmentRequest; +import org.opensearch.geospatial.action.IpEnrichmentResponse; + +import java.util.concurrent.ExecutionException; + +/** + * Proxy for the node client operations. + */ +public class IpEnrichmentActionClient { + + NodeClient nodeClient; + + public IpEnrichmentActionClient(NodeClient nodeClient) { + this.nodeClient = nodeClient; + } + + public String enrichIp(String ipString) throws ExecutionException, InterruptedException { + ActionFuture responseActionFuture = nodeClient.execute(IpEnrichmentAction.INSTANCE, new IpEnrichmentRequest(ipString)); + ActionResponse genericActionResponse = responseActionFuture.get(); + IpEnrichmentResponse enrichmentResponse = IpEnrichmentResponse.fromActionResponse(genericActionResponse); + return enrichmentResponse.getAnswer(); + } + +} diff --git a/common/build.gradle b/common/build.gradle index 3fcb2566..6dff39b0 100644 --- a/common/build.gradle +++ b/common/build.gradle @@ -1,7 +1,6 @@ plugins { id 'java' id 'maven-publish' - } group = 'org.opensearch' @@ -15,6 +14,8 @@ repositories { } dependencies { + compileOnly "org.opensearch:opensearch:${opensearch_version}" + testImplementation platform('org.junit:junit-bom:5.10.0') testImplementation 'org.junit.jupiter:junit-jupiter' } diff --git a/common/src/main/java/org/opensearch/geospatial/action/IpEnrichmentAction.java b/common/src/main/java/org/opensearch/geospatial/action/IpEnrichmentAction.java new file mode 100644 index 00000000..16ba321e --- /dev/null +++ b/common/src/main/java/org/opensearch/geospatial/action/IpEnrichmentAction.java @@ -0,0 +1,21 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.opensearch.geospatial.action; + +import org.opensearch.action.ActionType; +import org.opensearch.core.action.ActionResponse; + +public class IpEnrichmentAction extends ActionType { + + + public static final IpEnrichmentAction INSTANCE = new IpEnrichmentAction(); + + public static final String NAME = "cluster:admin/geospatial/ipenrichment/get"; + + public IpEnrichmentAction() { + super(NAME, IpEnrichmentResponse::new); + } +} diff --git a/common/src/main/java/org/opensearch/geospatial/action/IpEnrichmentRequest.java b/common/src/main/java/org/opensearch/geospatial/action/IpEnrichmentRequest.java new file mode 100644 index 00000000..ff47cd70 --- /dev/null +++ b/common/src/main/java/org/opensearch/geospatial/action/IpEnrichmentRequest.java @@ -0,0 +1,79 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.opensearch.geospatial.action; + +import org.opensearch.action.ActionRequest; +import org.opensearch.action.ActionRequestValidationException; +import org.opensearch.core.common.io.stream.InputStreamStreamInput; +import org.opensearch.core.common.io.stream.OutputStreamStreamOutput; +import org.opensearch.core.common.io.stream.StreamInput; +import org.opensearch.core.common.io.stream.StreamOutput; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.UncheckedIOException; + +public class IpEnrichmentRequest extends ActionRequest { + + private String ipString; + + + public IpEnrichmentRequest() { + } + + public IpEnrichmentRequest(String ipString) { + this.ipString = ipString; + } + + /** + * Constructor for TransportAction. + * @param streamInput + */ + public IpEnrichmentRequest(StreamInput streamInput) throws IOException { + super(streamInput); + ipString = streamInput.readString(); + } + + @Override + public ActionRequestValidationException validate() { + ActionRequestValidationException errors = null; + if (ipString == null) { + errors = new ActionRequestValidationException(); + errors.addValidationError("ip string should not be null"); + } + return errors; + } + + @Override + public void writeTo(StreamOutput out) throws IOException { + super.writeTo(out); + out.writeString(ipString); + } + + public String getIpString() { + return ipString; + } + + public static IpEnrichmentRequest fromActionRequest(ActionRequest actionRequest) { + // From the same classloader + if (actionRequest instanceof IpEnrichmentRequest) { + return (IpEnrichmentRequest) actionRequest; + } + + // Or else convert it + try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); + OutputStreamStreamOutput osso = new OutputStreamStreamOutput(baos)) { + actionRequest.writeTo(osso); + try (StreamInput input = + new InputStreamStreamInput(new ByteArrayInputStream(baos.toByteArray()))) { + return new IpEnrichmentRequest(input); + } + } catch (IOException e) { + throw new UncheckedIOException("failed to parse ActionRequest into IpEnrichmentRequest", e); + } + } +} diff --git a/common/src/main/java/org/opensearch/geospatial/action/IpEnrichmentResponse.java b/common/src/main/java/org/opensearch/geospatial/action/IpEnrichmentResponse.java new file mode 100644 index 00000000..b9f14e9c --- /dev/null +++ b/common/src/main/java/org/opensearch/geospatial/action/IpEnrichmentResponse.java @@ -0,0 +1,69 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.opensearch.geospatial.action; + +import org.opensearch.core.action.ActionResponse; +import org.opensearch.core.common.io.stream.InputStreamStreamInput; +import org.opensearch.core.common.io.stream.OutputStreamStreamOutput; +import org.opensearch.core.common.io.stream.StreamInput; +import org.opensearch.core.common.io.stream.StreamOutput; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.UncheckedIOException; +import java.util.UUID; + + +public class IpEnrichmentResponse extends ActionResponse { + + + private String answer; + + public IpEnrichmentResponse(String answer) { + this.answer = answer; + } + + public IpEnrichmentResponse(StreamInput streamInput) throws IOException { + super(streamInput); + answer = streamInput.readString(); + } + + @Override + public void writeTo(StreamOutput streamOutput) throws IOException { + streamOutput.writeString(answer); + } + + public String getAnswer() { + return answer; + } + + public static IpEnrichmentResponse fromActionResponse(ActionResponse actionResponse) { + // From the same classloader + if (actionResponse instanceof IpEnrichmentResponse) { + return (IpEnrichmentResponse) actionResponse; + } + + // Or else convert it + try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); + OutputStreamStreamOutput osso = new OutputStreamStreamOutput(baos)) { + actionResponse.writeTo(osso); + try (StreamInput input = + new InputStreamStreamInput(new ByteArrayInputStream(baos.toByteArray()))) { + return new IpEnrichmentResponse(input); + } + } catch (IOException e) { + throw new UncheckedIOException("failed to parse ActionResponse into IpEnrichmentResponse", e); + } + } + + @Override + public String toString() { + return "IpEnrichmentResponse{" + + "answer='" + answer + '\'' + + '}'; + } +} diff --git a/settings.gradle b/settings.gradle index 5a765915..70918f70 100644 --- a/settings.gradle +++ b/settings.gradle @@ -14,3 +14,5 @@ include ":libs:h3" include 'common' project(":common").name = rootProject.name + "-common" +include 'client' +project(":client").name = rootProject.name + "-client" diff --git a/src/main/java/org/opensearch/geospatial/action/model/IpEnrichmentTransportAction.java b/src/main/java/org/opensearch/geospatial/action/model/IpEnrichmentTransportAction.java new file mode 100644 index 00000000..6b254466 --- /dev/null +++ b/src/main/java/org/opensearch/geospatial/action/model/IpEnrichmentTransportAction.java @@ -0,0 +1,46 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.opensearch.geospatial.action.model; + +import org.opensearch.action.ActionRequest; +import org.opensearch.action.support.ActionFilters; +import org.opensearch.action.support.HandledTransportAction; +import org.opensearch.common.inject.Inject; +import org.opensearch.core.action.ActionListener; +import org.opensearch.core.action.ActionResponse; +import org.opensearch.geospatial.action.IpEnrichmentAction; +import org.opensearch.geospatial.action.IpEnrichmentRequest; +import org.opensearch.geospatial.action.IpEnrichmentResponse; +import org.opensearch.tasks.Task; +import org.opensearch.transport.TransportService; + +public class IpEnrichmentTransportAction extends HandledTransportAction { + + + @Inject + public IpEnrichmentTransportAction( + TransportService transportService, + ActionFilters actionFilters) { + super(IpEnrichmentAction.NAME, transportService, actionFilters, IpEnrichmentRequest::new); + } + + @Override + protected void doExecute(Task task, ActionRequest request, ActionListener listener) { + IpEnrichmentRequest enrichmentRequest = IpEnrichmentRequest.fromActionRequest(request); + listener.onResponse(new IpEnrichmentResponse(enrichmentRequest.getIpString() + " Done!")); + } + + +// @Override +// protected void doExecute(Task task, ActionRequest request, ActionListener listener) { +// IpEnrichmentRequest enrichmentRequest = IpEnrichmentRequest.fromActionRequest(request); +// listener.onResponse(new IpEnrichmentResponse(enrichmentRequest.getIpString() + " Done!")); +// } + + + +} diff --git a/src/main/java/org/opensearch/geospatial/plugin/GeospatialPlugin.java b/src/main/java/org/opensearch/geospatial/plugin/GeospatialPlugin.java index 68a949b5..bfc8de3b 100644 --- a/src/main/java/org/opensearch/geospatial/plugin/GeospatialPlugin.java +++ b/src/main/java/org/opensearch/geospatial/plugin/GeospatialPlugin.java @@ -31,6 +31,8 @@ import org.opensearch.core.xcontent.NamedXContentRegistry; import org.opensearch.env.Environment; import org.opensearch.env.NodeEnvironment; +import org.opensearch.geospatial.action.IpEnrichmentAction; +import org.opensearch.geospatial.action.model.IpEnrichmentTransportAction; import org.opensearch.geospatial.action.upload.geojson.UploadGeoJSONAction; import org.opensearch.geospatial.action.upload.geojson.UploadGeoJSONTransportAction; import org.opensearch.geospatial.index.mapper.xypoint.XYPointFieldMapper; @@ -221,11 +223,16 @@ public List getRestHandlers( new ActionHandler<>(UpdateDatasourceAction.INSTANCE, UpdateDatasourceTransportAction.class), new ActionHandler<>(DeleteDatasourceAction.INSTANCE, DeleteDatasourceTransportAction.class) ); - String testStr = CommonMain.TEST_STR; + + // Inter-cluster IP enrichment request + List> ipEnrichmentHandlers = List.of( + new ActionHandler<>(IpEnrichmentAction.INSTANCE, IpEnrichmentTransportAction.class) + ); List> allHandlers = new ArrayList<>(); allHandlers.addAll(geoJsonHandlers); allHandlers.addAll(ip2geoHandlers); + allHandlers.addAll(ipEnrichmentHandlers); return allHandlers; } From 2c0210c2b9fbb53f75f6b458cd673ab055b81a72 Mon Sep 17 00:00:00 2001 From: Andy Kwok Date: Wed, 20 Nov 2024 15:42:42 -0800 Subject: [PATCH 03/36] Consolidate interface Signed-off-by: Andy Kwok --- .../geospatial/action/IpEnrichmentAction.java | 0 .../action}/IpEnrichmentActionClient.java | 0 .../geospatial/action/IpEnrichmentRequest.java | 0 .../geospatial/action/IpEnrichmentResponse.java | 0 common/src/main/java/org/opensearch/CommonMain.java | 11 ----------- .../action}/IpEnrichmentTransportAction.java | 13 ++++++++++++- 6 files changed, 12 insertions(+), 12 deletions(-) rename {common => client}/src/main/java/org/opensearch/geospatial/action/IpEnrichmentAction.java (100%) rename client/src/main/java/org/opensearch/{ => geospatial/action}/IpEnrichmentActionClient.java (100%) rename {common => client}/src/main/java/org/opensearch/geospatial/action/IpEnrichmentRequest.java (100%) rename {common => client}/src/main/java/org/opensearch/geospatial/action/IpEnrichmentResponse.java (100%) delete mode 100644 common/src/main/java/org/opensearch/CommonMain.java rename src/main/java/org/opensearch/geospatial/{action/model => ip2geo/action}/IpEnrichmentTransportAction.java (76%) diff --git a/common/src/main/java/org/opensearch/geospatial/action/IpEnrichmentAction.java b/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentAction.java similarity index 100% rename from common/src/main/java/org/opensearch/geospatial/action/IpEnrichmentAction.java rename to client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentAction.java diff --git a/client/src/main/java/org/opensearch/IpEnrichmentActionClient.java b/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentActionClient.java similarity index 100% rename from client/src/main/java/org/opensearch/IpEnrichmentActionClient.java rename to client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentActionClient.java diff --git a/common/src/main/java/org/opensearch/geospatial/action/IpEnrichmentRequest.java b/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentRequest.java similarity index 100% rename from common/src/main/java/org/opensearch/geospatial/action/IpEnrichmentRequest.java rename to client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentRequest.java diff --git a/common/src/main/java/org/opensearch/geospatial/action/IpEnrichmentResponse.java b/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentResponse.java similarity index 100% rename from common/src/main/java/org/opensearch/geospatial/action/IpEnrichmentResponse.java rename to client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentResponse.java diff --git a/common/src/main/java/org/opensearch/CommonMain.java b/common/src/main/java/org/opensearch/CommonMain.java deleted file mode 100644 index e32de89d..00000000 --- a/common/src/main/java/org/opensearch/CommonMain.java +++ /dev/null @@ -1,11 +0,0 @@ -package org.opensearch; - -public class CommonMain { - - public final static String TEST_STR = "TEST"; - - public static void main(String[] args) { - - System.out.println("Hello world!"); - } -} \ No newline at end of file diff --git a/src/main/java/org/opensearch/geospatial/action/model/IpEnrichmentTransportAction.java b/src/main/java/org/opensearch/geospatial/ip2geo/action/IpEnrichmentTransportAction.java similarity index 76% rename from src/main/java/org/opensearch/geospatial/action/model/IpEnrichmentTransportAction.java rename to src/main/java/org/opensearch/geospatial/ip2geo/action/IpEnrichmentTransportAction.java index 6b254466..c4051b8c 100644 --- a/src/main/java/org/opensearch/geospatial/action/model/IpEnrichmentTransportAction.java +++ b/src/main/java/org/opensearch/geospatial/ip2geo/action/IpEnrichmentTransportAction.java @@ -14,23 +14,34 @@ import org.opensearch.geospatial.action.IpEnrichmentAction; import org.opensearch.geospatial.action.IpEnrichmentRequest; import org.opensearch.geospatial.action.IpEnrichmentResponse; +import org.opensearch.geospatial.ip2geo.dao.Ip2GeoCachedDao; import org.opensearch.tasks.Task; import org.opensearch.transport.TransportService; +import java.util.Map; + public class IpEnrichmentTransportAction extends HandledTransportAction { + private Ip2GeoCachedDao ip2GeoCachedDao; + + @Inject public IpEnrichmentTransportAction( TransportService transportService, - ActionFilters actionFilters) { + ActionFilters actionFilters, + Ip2GeoCachedDao cachedDao) { super(IpEnrichmentAction.NAME, transportService, actionFilters, IpEnrichmentRequest::new); + this.ip2GeoCachedDao = cachedDao; } @Override protected void doExecute(Task task, ActionRequest request, ActionListener listener) { IpEnrichmentRequest enrichmentRequest = IpEnrichmentRequest.fromActionRequest(request); + String ipString = enrichmentRequest.getIpString(); + Map testResult = ip2GeoCachedDao.getGeoData(".geospatial-ip2geo-data.my-datasource.ef3486f8-401b-4d77-b89b-3a4cd19eda04", ipString); + System.out.println(testResult); listener.onResponse(new IpEnrichmentResponse(enrichmentRequest.getIpString() + " Done!")); } From e60b14aa5f9aa538fdfbe4455fff222b2fb6c07b Mon Sep 17 00:00:00 2001 From: Andy Kwok Date: Wed, 20 Nov 2024 15:42:51 -0800 Subject: [PATCH 04/36] Consolidate interface Signed-off-by: Andy Kwok --- build.gradle | 2 +- .../geospatial/action/IpEnrichmentActionClient.java | 5 +---- .../opensearch/geospatial/action/IpEnrichmentResponse.java | 1 - .../ip2geo/action/IpEnrichmentTransportAction.java | 2 +- .../org/opensearch/geospatial/plugin/GeospatialPlugin.java | 3 +-- 5 files changed, 4 insertions(+), 9 deletions(-) diff --git a/build.gradle b/build.gradle index c42b2854..d4ec0bbb 100644 --- a/build.gradle +++ b/build.gradle @@ -162,7 +162,7 @@ configurations { dependencies { implementation "org.opensearch.plugin:geo:${opensearch_version}" api project(":libs:h3") - api project(":geospatial-common") + api project(":geospatial-client") yamlRestTestRuntimeOnly "org.apache.logging.log4j:log4j-core:${versions.log4j}" testImplementation "org.hamcrest:hamcrest:${versions.hamcrest}" testImplementation 'org.json:json:20231013' diff --git a/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentActionClient.java b/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentActionClient.java index 152cbd59..bac42314 100644 --- a/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentActionClient.java +++ b/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentActionClient.java @@ -3,14 +3,11 @@ * SPDX-License-Identifier: Apache-2.0 */ -package org.opensearch; +package org.opensearch.geospatial.action; import org.opensearch.client.node.NodeClient; import org.opensearch.common.action.ActionFuture; import org.opensearch.core.action.ActionResponse; -import org.opensearch.geospatial.action.IpEnrichmentAction; -import org.opensearch.geospatial.action.IpEnrichmentRequest; -import org.opensearch.geospatial.action.IpEnrichmentResponse; import java.util.concurrent.ExecutionException; diff --git a/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentResponse.java b/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentResponse.java index b9f14e9c..ca25a111 100644 --- a/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentResponse.java +++ b/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentResponse.java @@ -15,7 +15,6 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.UncheckedIOException; -import java.util.UUID; public class IpEnrichmentResponse extends ActionResponse { diff --git a/src/main/java/org/opensearch/geospatial/ip2geo/action/IpEnrichmentTransportAction.java b/src/main/java/org/opensearch/geospatial/ip2geo/action/IpEnrichmentTransportAction.java index c4051b8c..2b9b4b1d 100644 --- a/src/main/java/org/opensearch/geospatial/ip2geo/action/IpEnrichmentTransportAction.java +++ b/src/main/java/org/opensearch/geospatial/ip2geo/action/IpEnrichmentTransportAction.java @@ -3,7 +3,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -package org.opensearch.geospatial.action.model; +package org.opensearch.geospatial.ip2geo.action; import org.opensearch.action.ActionRequest; import org.opensearch.action.support.ActionFilters; diff --git a/src/main/java/org/opensearch/geospatial/plugin/GeospatialPlugin.java b/src/main/java/org/opensearch/geospatial/plugin/GeospatialPlugin.java index bfc8de3b..6307967e 100644 --- a/src/main/java/org/opensearch/geospatial/plugin/GeospatialPlugin.java +++ b/src/main/java/org/opensearch/geospatial/plugin/GeospatialPlugin.java @@ -13,7 +13,6 @@ import java.util.Map; import java.util.function.Supplier; -import org.opensearch.CommonMain; import org.opensearch.action.ActionRequest; import org.opensearch.client.Client; import org.opensearch.cluster.metadata.IndexNameExpressionResolver; @@ -32,7 +31,7 @@ import org.opensearch.env.Environment; import org.opensearch.env.NodeEnvironment; import org.opensearch.geospatial.action.IpEnrichmentAction; -import org.opensearch.geospatial.action.model.IpEnrichmentTransportAction; +import org.opensearch.geospatial.ip2geo.action.IpEnrichmentTransportAction; import org.opensearch.geospatial.action.upload.geojson.UploadGeoJSONAction; import org.opensearch.geospatial.action.upload.geojson.UploadGeoJSONTransportAction; import org.opensearch.geospatial.index.mapper.xypoint.XYPointFieldMapper; From 389256447e39dfb727d34fc4bb2071ad1c4c13fb Mon Sep 17 00:00:00 2001 From: Andy Kwok Date: Wed, 20 Nov 2024 16:05:00 -0800 Subject: [PATCH 05/36] Remove common module Signed-off-by: Andy Kwok --- common/build.gradle | 36 ------------------------------------ settings.gradle | 2 -- 2 files changed, 38 deletions(-) delete mode 100644 common/build.gradle diff --git a/common/build.gradle b/common/build.gradle deleted file mode 100644 index 6dff39b0..00000000 --- a/common/build.gradle +++ /dev/null @@ -1,36 +0,0 @@ -plugins { - id 'java' - id 'maven-publish' -} - -group = 'org.opensearch' -version = '3.0.0.0-SNAPSHOT' - -repositories { - mavenLocal() - maven { url "https://aws.oss.sonatype.org/content/repositories/snapshots" } - mavenCentral() - maven { url "https://plugins.gradle.org/m2/" } -} - -dependencies { - compileOnly "org.opensearch:opensearch:${opensearch_version}" - - testImplementation platform('org.junit:junit-bom:5.10.0') - testImplementation 'org.junit.jupiter:junit-jupiter' -} - -publishing { - publications { - mavenJava(MavenPublication) { - from components.java - } - } - repositories { - mavenLocal() - } -} - -test { - useJUnitPlatform() -} \ No newline at end of file diff --git a/settings.gradle b/settings.gradle index 70918f70..e4103866 100644 --- a/settings.gradle +++ b/settings.gradle @@ -12,7 +12,5 @@ rootProject.name = 'geospatial' include ":libs" include ":libs:h3" -include 'common' -project(":common").name = rootProject.name + "-common" include 'client' project(":client").name = rootProject.name + "-client" From ed41a808ee8931bedfa19948513bd519980ce269 Mon Sep 17 00:00:00 2001 From: Andy Kwok Date: Wed, 20 Nov 2024 18:47:02 -0800 Subject: [PATCH 06/36] Gradle cleanup Signed-off-by: Andy Kwok --- client/build.gradle | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/client/build.gradle b/client/build.gradle index 11cbe094..f81afe5b 100644 --- a/client/build.gradle +++ b/client/build.gradle @@ -1,10 +1,19 @@ -plugins { - id 'java' - id 'maven-publish' -} +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +apply plugin: 'java' +apply plugin: 'maven-publish' +apply plugin: 'jacoco' +apply plugin: 'io.freefair.lombok' -group = 'org.opensearch' -version = '3.0.0.0-SNAPSHOT' +allprojects { + group = opensearch_group + version = "${opensearch_build}" + targetCompatibility = JavaVersion.VERSION_11 + sourceCompatibility = JavaVersion.VERSION_11 +} repositories { mavenLocal() @@ -13,12 +22,16 @@ repositories { maven { url "https://plugins.gradle.org/m2/" } } +compileJava { + options.compilerArgs.addAll(["-processor", 'lombok.launch.AnnotationProcessorHider$AnnotationProcessor']) +} +compileTestJava { + options.compilerArgs.addAll(["-processor", 'lombok.launch.AnnotationProcessorHider$AnnotationProcessor']) +} + + dependencies { compileOnly "org.opensearch:opensearch:${opensearch_version}" - implementation(project(":${rootProject.name}-common")) - - testImplementation platform('org.junit:junit-bom:5.10.0') - testImplementation 'org.junit.jupiter:junit-jupiter' } publishing { From f5e970cc1d681dcab960eeaa5c398d2a0f875d38 Mon Sep 17 00:00:00 2001 From: Andy Kwok Date: Wed, 20 Nov 2024 18:55:51 -0800 Subject: [PATCH 07/36] Lombok Signed-off-by: Andy Kwok --- client/build.gradle | 2 +- .../geospatial/action/IpEnrichmentRequest.java | 14 ++++++-------- .../action/IpEnrichmentResponse.java | 18 ++++++++---------- .../action/IpEnrichmentTransportAction.java | 4 ++-- 4 files changed, 17 insertions(+), 21 deletions(-) diff --git a/client/build.gradle b/client/build.gradle index f81afe5b..f14416b4 100644 --- a/client/build.gradle +++ b/client/build.gradle @@ -31,7 +31,7 @@ compileTestJava { dependencies { - compileOnly "org.opensearch:opensearch:${opensearch_version}" + compileOnly "${group}:opensearch:${opensearch_version}" } publishing { diff --git a/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentRequest.java b/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentRequest.java index ff47cd70..19f495df 100644 --- a/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentRequest.java +++ b/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentRequest.java @@ -5,6 +5,9 @@ package org.opensearch.geospatial.action; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.Setter; import org.opensearch.action.ActionRequest; import org.opensearch.action.ActionRequestValidationException; import org.opensearch.core.common.io.stream.InputStreamStreamInput; @@ -17,6 +20,9 @@ import java.io.IOException; import java.io.UncheckedIOException; +@Getter +@Setter +@AllArgsConstructor public class IpEnrichmentRequest extends ActionRequest { private String ipString; @@ -25,10 +31,6 @@ public class IpEnrichmentRequest extends ActionRequest { public IpEnrichmentRequest() { } - public IpEnrichmentRequest(String ipString) { - this.ipString = ipString; - } - /** * Constructor for TransportAction. * @param streamInput @@ -54,10 +56,6 @@ public void writeTo(StreamOutput out) throws IOException { out.writeString(ipString); } - public String getIpString() { - return ipString; - } - public static IpEnrichmentRequest fromActionRequest(ActionRequest actionRequest) { // From the same classloader if (actionRequest instanceof IpEnrichmentRequest) { diff --git a/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentResponse.java b/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentResponse.java index ca25a111..b237e1c9 100644 --- a/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentResponse.java +++ b/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentResponse.java @@ -5,6 +5,10 @@ package org.opensearch.geospatial.action; +import lombok.AllArgsConstructor; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; import org.opensearch.core.action.ActionResponse; import org.opensearch.core.common.io.stream.InputStreamStreamInput; import org.opensearch.core.common.io.stream.OutputStreamStreamOutput; @@ -16,16 +20,14 @@ import java.io.IOException; import java.io.UncheckedIOException; - +@Getter +@Setter +@AllArgsConstructor +@EqualsAndHashCode(callSuper = false) public class IpEnrichmentResponse extends ActionResponse { - private String answer; - public IpEnrichmentResponse(String answer) { - this.answer = answer; - } - public IpEnrichmentResponse(StreamInput streamInput) throws IOException { super(streamInput); answer = streamInput.readString(); @@ -36,10 +38,6 @@ public void writeTo(StreamOutput streamOutput) throws IOException { streamOutput.writeString(answer); } - public String getAnswer() { - return answer; - } - public static IpEnrichmentResponse fromActionResponse(ActionResponse actionResponse) { // From the same classloader if (actionResponse instanceof IpEnrichmentResponse) { diff --git a/src/main/java/org/opensearch/geospatial/ip2geo/action/IpEnrichmentTransportAction.java b/src/main/java/org/opensearch/geospatial/ip2geo/action/IpEnrichmentTransportAction.java index 2b9b4b1d..6daed929 100644 --- a/src/main/java/org/opensearch/geospatial/ip2geo/action/IpEnrichmentTransportAction.java +++ b/src/main/java/org/opensearch/geospatial/ip2geo/action/IpEnrichmentTransportAction.java @@ -5,6 +5,7 @@ package org.opensearch.geospatial.ip2geo.action; +import lombok.extern.log4j.Log4j2; import org.opensearch.action.ActionRequest; import org.opensearch.action.support.ActionFilters; import org.opensearch.action.support.HandledTransportAction; @@ -20,13 +21,12 @@ import java.util.Map; +@Log4j2 public class IpEnrichmentTransportAction extends HandledTransportAction { - private Ip2GeoCachedDao ip2GeoCachedDao; - @Inject public IpEnrichmentTransportAction( TransportService transportService, From 4266e3715b2374d49741839fc70dd636031123b1 Mon Sep 17 00:00:00 2001 From: Andy Kwok Date: Wed, 20 Nov 2024 19:00:26 -0800 Subject: [PATCH 08/36] Java doc - 1 Signed-off-by: Andy Kwok --- .../ip2geo/action/IpEnrichmentTransportAction.java | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/opensearch/geospatial/ip2geo/action/IpEnrichmentTransportAction.java b/src/main/java/org/opensearch/geospatial/ip2geo/action/IpEnrichmentTransportAction.java index 6daed929..16a27ea1 100644 --- a/src/main/java/org/opensearch/geospatial/ip2geo/action/IpEnrichmentTransportAction.java +++ b/src/main/java/org/opensearch/geospatial/ip2geo/action/IpEnrichmentTransportAction.java @@ -21,12 +21,21 @@ import java.util.Map; +/** + * Transport action to convert provided IP address String into GeoLocation data. + */ @Log4j2 public class IpEnrichmentTransportAction extends HandledTransportAction { private Ip2GeoCachedDao ip2GeoCachedDao; + /** + * Constructor + * @param transportService the transport service + * @param actionFilters the action filters + * @param cachedDao the cached datasource facade + */ @Inject public IpEnrichmentTransportAction( TransportService transportService, @@ -46,11 +55,6 @@ protected void doExecute(Task task, ActionRequest request, ActionListener listener) { -// IpEnrichmentRequest enrichmentRequest = IpEnrichmentRequest.fromActionRequest(request); -// listener.onResponse(new IpEnrichmentResponse(enrichmentRequest.getIpString() + " Done!")); -// } From b0b96d04af89efe9b510e5d0476d822b08ca138f Mon Sep 17 00:00:00 2001 From: Andy Kwok Date: Thu, 21 Nov 2024 12:58:58 -0800 Subject: [PATCH 09/36] Java doc Signed-off-by: Andy Kwok --- .../geospatial/action/IpEnrichmentAction.java | 5 ++- .../action/IpEnrichmentActionClient.java | 30 ++++++++++----- .../action/IpEnrichmentRequest.java | 23 +++++++++--- .../action/IpEnrichmentResponse.java | 37 ++++++++++++++----- .../action/IpEnrichmentTransportAction.java | 15 +++++--- 5 files changed, 79 insertions(+), 31 deletions(-) diff --git a/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentAction.java b/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentAction.java index 16ba321e..963221bf 100644 --- a/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentAction.java +++ b/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentAction.java @@ -8,9 +8,12 @@ import org.opensearch.action.ActionType; import org.opensearch.core.action.ActionResponse; +/** + * An ActionType registered on OpenSearch registry, for inter-cluster transportAction call, + * to resolve GeoLocation for IP String. + */ public class IpEnrichmentAction extends ActionType { - public static final IpEnrichmentAction INSTANCE = new IpEnrichmentAction(); public static final String NAME = "cluster:admin/geospatial/ipenrichment/get"; diff --git a/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentActionClient.java b/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentActionClient.java index bac42314..b17e1b9a 100644 --- a/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentActionClient.java +++ b/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentActionClient.java @@ -5,28 +5,38 @@ package org.opensearch.geospatial.action; +import lombok.AllArgsConstructor; +import lombok.extern.log4j.Log4j2; import org.opensearch.client.node.NodeClient; import org.opensearch.common.action.ActionFuture; import org.opensearch.core.action.ActionResponse; +import java.util.Map; import java.util.concurrent.ExecutionException; /** - * Proxy for the node client operations. + * Facade to provide GeoLocation enrichment for other plugin. */ +@Log4j2 +@AllArgsConstructor public class IpEnrichmentActionClient { NodeClient nodeClient; - public IpEnrichmentActionClient(NodeClient nodeClient) { - this.nodeClient = nodeClient; - } - - public String enrichIp(String ipString) throws ExecutionException, InterruptedException { + /** + * Client facing method, which read an IP in String form and return a map instance which contain the associated GeoLocation data. + * @param ipString IP v4 || v6 address in String form. + * @return A map instance which contain GeoLocation data for the given Ip address. + */ + public Map getGeoLocationData (String ipString) { ActionFuture responseActionFuture = nodeClient.execute(IpEnrichmentAction.INSTANCE, new IpEnrichmentRequest(ipString)); - ActionResponse genericActionResponse = responseActionFuture.get(); - IpEnrichmentResponse enrichmentResponse = IpEnrichmentResponse.fromActionResponse(genericActionResponse); - return enrichmentResponse.getAnswer(); + try { + ActionResponse genericActionResponse = responseActionFuture.get(); + IpEnrichmentResponse enrichmentResponse = IpEnrichmentResponse.fromActionResponse(genericActionResponse); + return enrichmentResponse.getGeoLocationData(); + } catch (Exception e) { + log.error("GeoSpatial IP Enrichment call failure, with detail: ", e); + return null; + } } - } diff --git a/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentRequest.java b/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentRequest.java index 19f495df..1c653226 100644 --- a/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentRequest.java +++ b/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentRequest.java @@ -20,6 +20,9 @@ import java.io.IOException; import java.io.UncheckedIOException; +/** + * Wrapper for the IP 2 GeoLocation action request. + */ @Getter @Setter @AllArgsConstructor @@ -27,19 +30,19 @@ public class IpEnrichmentRequest extends ActionRequest { private String ipString; - - public IpEnrichmentRequest() { - } - /** * Constructor for TransportAction. - * @param streamInput + * @param streamInput the streamInput. */ public IpEnrichmentRequest(StreamInput streamInput) throws IOException { super(streamInput); ipString = streamInput.readString(); } + /** + * Perform validation on the request, before GetSpatial processing it. + * @return Exception which contain validation errors, if any. + */ @Override public ActionRequestValidationException validate() { ActionRequestValidationException errors = null; @@ -50,12 +53,22 @@ public ActionRequestValidationException validate() { return errors; } + /** + * Overrided method to populate convert object's payload into StreamOutput form. + * @param out the StreamOutput object. + * @throws IOException If given StreamOutput is not compatible. + */ @Override public void writeTo(StreamOutput out) throws IOException { super.writeTo(out); out.writeString(ipString); } + /** + * Static method get around the cast exception happen for cross plugin communication. + * @param actionRequest An casted-up version of IpEnrichmentRequest. + * @return IpEnrichmentRequest object which can be used within the scope of the caller. + */ public static IpEnrichmentRequest fromActionRequest(ActionRequest actionRequest) { // From the same classloader if (actionRequest instanceof IpEnrichmentRequest) { diff --git a/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentResponse.java b/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentResponse.java index b237e1c9..0b03ddad 100644 --- a/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentResponse.java +++ b/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentResponse.java @@ -8,6 +8,7 @@ import lombok.AllArgsConstructor; import lombok.EqualsAndHashCode; import lombok.Getter; +import lombok.NoArgsConstructor; import lombok.Setter; import org.opensearch.core.action.ActionResponse; import org.opensearch.core.common.io.stream.InputStreamStreamInput; @@ -19,25 +20,47 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.UncheckedIOException; +import java.util.Map; +/** + * Wrapper class to encapsulate the IP enrichment result for IpEnrichmentTransportAction. + */ @Getter @Setter @AllArgsConstructor @EqualsAndHashCode(callSuper = false) public class IpEnrichmentResponse extends ActionResponse { - private String answer; + private Map geoLocationData; + + /** + * Private method to be used by fromActionResponse call to populate this Response class. + * @param streamInput Stream object which contain the geoLocationData. + * @throws IOException Exception being thrown when given stremInput doesn't contain what IpEnrichmentResponse is expecting. + */ public IpEnrichmentResponse(StreamInput streamInput) throws IOException { super(streamInput); - answer = streamInput.readString(); + geoLocationData = streamInput.readMap(); } + /** + * Overrided method used by OpenSearch runtime to write result into listener. + * @param streamOutput the streamOutput used to construct this response object. + * @throws IOException the IOException. + */ @Override public void writeTo(StreamOutput streamOutput) throws IOException { - streamOutput.writeString(answer); + streamOutput.writeMap(geoLocationData); } + /** + * Static method to convert a given ActionResponse to IpEnrichmentResponse by serialisation with streamOuput. + * This will be required for cross plugin communication scenario, as multiple class definition will be loaded + * by respective Plugin's classloader. + * @param actionResponse An IpEnrichmentResponse in casted-up form. + * @return An IpEnrichmentResponse object which contain the same payload as the incoming object. + */ public static IpEnrichmentResponse fromActionResponse(ActionResponse actionResponse) { // From the same classloader if (actionResponse instanceof IpEnrichmentResponse) { @@ -53,14 +76,8 @@ public static IpEnrichmentResponse fromActionResponse(ActionResponse actionRespo return new IpEnrichmentResponse(input); } } catch (IOException e) { - throw new UncheckedIOException("failed to parse ActionResponse into IpEnrichmentResponse", e); + throw new UncheckedIOException("Failed to parse ActionResponse into IpEnrichmentResponse", e); } } - @Override - public String toString() { - return "IpEnrichmentResponse{" + - "answer='" + answer + '\'' + - '}'; - } } diff --git a/src/main/java/org/opensearch/geospatial/ip2geo/action/IpEnrichmentTransportAction.java b/src/main/java/org/opensearch/geospatial/ip2geo/action/IpEnrichmentTransportAction.java index 16a27ea1..59cf7c60 100644 --- a/src/main/java/org/opensearch/geospatial/ip2geo/action/IpEnrichmentTransportAction.java +++ b/src/main/java/org/opensearch/geospatial/ip2geo/action/IpEnrichmentTransportAction.java @@ -45,17 +45,22 @@ public IpEnrichmentTransportAction( this.ip2GeoCachedDao = cachedDao; } + + /** + * Overrided method to extract IP String from IpEnrichmentRequest object and return the enrichment result + * in the form of IpEnrichmentResponse which contains the GeoLocation data for given IP String. + * @param task the task. + * @param request request object in the form of IpEnrichmentRequest which contain the IP String to resolve + * @param listener a container which encapsulate IpEnrichmentResponse object with the GeoLocation data for given IP. + */ @Override protected void doExecute(Task task, ActionRequest request, ActionListener listener) { IpEnrichmentRequest enrichmentRequest = IpEnrichmentRequest.fromActionRequest(request); String ipString = enrichmentRequest.getIpString(); Map testResult = ip2GeoCachedDao.getGeoData(".geospatial-ip2geo-data.my-datasource.ef3486f8-401b-4d77-b89b-3a4cd19eda04", ipString); System.out.println(testResult); - listener.onResponse(new IpEnrichmentResponse(enrichmentRequest.getIpString() + " Done!")); + log.debug("GeoSpatial IP lookup on IP: [{}], and result [{}]", ipString, testResult); + listener.onResponse(new IpEnrichmentResponse(testResult)); } - - - - } From 2daa6353dbf1373b937de29c052f6c1fd08f0da6 Mon Sep 17 00:00:00 2001 From: Andy Kwok Date: Thu, 21 Nov 2024 13:19:38 -0800 Subject: [PATCH 10/36] API Signature Signed-off-by: Andy Kwok --- .../action/IpEnrichmentActionClient.java | 17 +++++++++++++++-- .../geospatial/action/IpEnrichmentRequest.java | 3 +++ .../action/IpEnrichmentTransportAction.java | 9 +++++---- 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentActionClient.java b/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentActionClient.java index b17e1b9a..83c7ec7f 100644 --- a/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentActionClient.java +++ b/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentActionClient.java @@ -12,6 +12,7 @@ import org.opensearch.core.action.ActionResponse; import java.util.Map; +import java.util.Optional; import java.util.concurrent.ExecutionException; /** @@ -23,13 +24,25 @@ public class IpEnrichmentActionClient { NodeClient nodeClient; + + /** + * IpEnrichment with default datasource. + * @param ipString Ip String to resolve. + * @return A map instance which contain GeoLocation data for the given Ip address. + */ + public Map getGeoLocationData (String ipString) { + return getGeoLocationData(ipString, "defaultDataSource"); + } + /** * Client facing method, which read an IP in String form and return a map instance which contain the associated GeoLocation data. * @param ipString IP v4 || v6 address in String form. + * @param datasourceName datasourceName in String form. * @return A map instance which contain GeoLocation data for the given Ip address. */ - public Map getGeoLocationData (String ipString) { - ActionFuture responseActionFuture = nodeClient.execute(IpEnrichmentAction.INSTANCE, new IpEnrichmentRequest(ipString)); + public Map getGeoLocationData (String ipString, String datasourceName) { + ActionFuture responseActionFuture = nodeClient.execute( + IpEnrichmentAction.INSTANCE, new IpEnrichmentRequest(ipString, datasourceName)); try { ActionResponse genericActionResponse = responseActionFuture.get(); IpEnrichmentResponse enrichmentResponse = IpEnrichmentResponse.fromActionResponse(genericActionResponse); diff --git a/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentRequest.java b/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentRequest.java index 1c653226..20c2a035 100644 --- a/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentRequest.java +++ b/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentRequest.java @@ -19,6 +19,7 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.UncheckedIOException; +import java.util.Optional; /** * Wrapper for the IP 2 GeoLocation action request. @@ -30,6 +31,8 @@ public class IpEnrichmentRequest extends ActionRequest { private String ipString; + private String datasourceName; + /** * Constructor for TransportAction. * @param streamInput the streamInput. diff --git a/src/main/java/org/opensearch/geospatial/ip2geo/action/IpEnrichmentTransportAction.java b/src/main/java/org/opensearch/geospatial/ip2geo/action/IpEnrichmentTransportAction.java index 59cf7c60..bf380656 100644 --- a/src/main/java/org/opensearch/geospatial/ip2geo/action/IpEnrichmentTransportAction.java +++ b/src/main/java/org/opensearch/geospatial/ip2geo/action/IpEnrichmentTransportAction.java @@ -57,10 +57,11 @@ public IpEnrichmentTransportAction( protected void doExecute(Task task, ActionRequest request, ActionListener listener) { IpEnrichmentRequest enrichmentRequest = IpEnrichmentRequest.fromActionRequest(request); String ipString = enrichmentRequest.getIpString(); - Map testResult = ip2GeoCachedDao.getGeoData(".geospatial-ip2geo-data.my-datasource.ef3486f8-401b-4d77-b89b-3a4cd19eda04", ipString); - System.out.println(testResult); - log.debug("GeoSpatial IP lookup on IP: [{}], and result [{}]", ipString, testResult); - listener.onResponse(new IpEnrichmentResponse(testResult)); + String indexName = ip2GeoCachedDao.getIndexName(enrichmentRequest.getDatasourceName()); + Map geoLocationData = ip2GeoCachedDao.getGeoData(indexName, ipString); + System.out.println(geoLocationData); + log.debug("GeoSpatial IP lookup on IP: [{}], and result [{}]", ipString, geoLocationData); + listener.onResponse(new IpEnrichmentResponse(geoLocationData)); } } From 4f51eace991fa7f22fe7ec7f48ac83749a30e3ea Mon Sep 17 00:00:00 2001 From: Andy Kwok Date: Thu, 21 Nov 2024 15:38:54 -0800 Subject: [PATCH 11/36] Fetch default data source Signed-off-by: Andy Kwok --- .../action/IpEnrichmentActionClient.java | 2 +- .../action/IpEnrichmentRequest.java | 2 ++ .../action/IpEnrichmentTransportAction.java | 31 +++++++++++++++---- 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentActionClient.java b/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentActionClient.java index 83c7ec7f..7a92f573 100644 --- a/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentActionClient.java +++ b/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentActionClient.java @@ -31,7 +31,7 @@ public class IpEnrichmentActionClient { * @return A map instance which contain GeoLocation data for the given Ip address. */ public Map getGeoLocationData (String ipString) { - return getGeoLocationData(ipString, "defaultDataSource"); + return getGeoLocationData(ipString, null); } /** diff --git a/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentRequest.java b/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentRequest.java index 20c2a035..487c5823 100644 --- a/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentRequest.java +++ b/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentRequest.java @@ -40,6 +40,7 @@ public class IpEnrichmentRequest extends ActionRequest { public IpEnrichmentRequest(StreamInput streamInput) throws IOException { super(streamInput); ipString = streamInput.readString(); + datasourceName= streamInput.readOptionalString(); } /** @@ -65,6 +66,7 @@ public ActionRequestValidationException validate() { public void writeTo(StreamOutput out) throws IOException { super.writeTo(out); out.writeString(ipString); + out.writeOptionalString(datasourceName); } /** diff --git a/src/main/java/org/opensearch/geospatial/ip2geo/action/IpEnrichmentTransportAction.java b/src/main/java/org/opensearch/geospatial/ip2geo/action/IpEnrichmentTransportAction.java index bf380656..dde58f8f 100644 --- a/src/main/java/org/opensearch/geospatial/ip2geo/action/IpEnrichmentTransportAction.java +++ b/src/main/java/org/opensearch/geospatial/ip2geo/action/IpEnrichmentTransportAction.java @@ -15,11 +15,15 @@ import org.opensearch.geospatial.action.IpEnrichmentAction; import org.opensearch.geospatial.action.IpEnrichmentRequest; import org.opensearch.geospatial.action.IpEnrichmentResponse; +import org.opensearch.geospatial.ip2geo.dao.DatasourceDao; import org.opensearch.geospatial.ip2geo.dao.Ip2GeoCachedDao; +import org.opensearch.geospatial.ip2geo.jobscheduler.Datasource; import org.opensearch.tasks.Task; import org.opensearch.transport.TransportService; +import java.util.List; import java.util.Map; +import java.util.Optional; /** * Transport action to convert provided IP address String into GeoLocation data. @@ -30,6 +34,10 @@ public class IpEnrichmentTransportAction extends HandledTransportAction allDatasources = datasourceDao.getAllDatasources(); + this.defaultDataSourceName = (!allDatasources.isEmpty()) ? allDatasources.get(0).getName() : null; } @@ -57,11 +69,18 @@ public IpEnrichmentTransportAction( protected void doExecute(Task task, ActionRequest request, ActionListener listener) { IpEnrichmentRequest enrichmentRequest = IpEnrichmentRequest.fromActionRequest(request); String ipString = enrichmentRequest.getIpString(); - String indexName = ip2GeoCachedDao.getIndexName(enrichmentRequest.getDatasourceName()); - Map geoLocationData = ip2GeoCachedDao.getGeoData(indexName, ipString); - System.out.println(geoLocationData); - log.debug("GeoSpatial IP lookup on IP: [{}], and result [{}]", ipString, geoLocationData); - listener.onResponse(new IpEnrichmentResponse(geoLocationData)); + if (enrichmentRequest.getDatasourceName() == null && + defaultDataSourceName == null) { + log.error("No data source available, IpEnrichmentTransportAction aborted."); + listener.onFailure(new IllegalArgumentException()); + } else { + String dataSourceName = Optional.ofNullable(enrichmentRequest.getDatasourceName()) + .orElse(defaultDataSourceName); + String indexName = ip2GeoCachedDao.getIndexName(dataSourceName); + Map geoLocationData = ip2GeoCachedDao.getGeoData(indexName, ipString); + log.debug("GeoSpatial IP lookup on IP: [{}], and result [{}]", ipString, geoLocationData); + listener.onResponse(new IpEnrichmentResponse(geoLocationData)); + } } } From e6ae54495b1b0a13a2766579d2dd9741a19ed8b4 Mon Sep 17 00:00:00 2001 From: Andy Kwok Date: Fri, 22 Nov 2024 11:36:32 -0800 Subject: [PATCH 12/36] Logger Signed-off-by: Andy Kwok --- .../geospatial/action/IpEnrichmentActionClient.java | 5 +++-- .../opensearch/geospatial/action/IpEnrichmentRequest.java | 5 ++++- .../opensearch/geospatial/action/IpEnrichmentResponse.java | 6 ++++-- .../ip2geo/action/IpEnrichmentTransportAction.java | 5 +---- 4 files changed, 12 insertions(+), 9 deletions(-) diff --git a/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentActionClient.java b/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentActionClient.java index 7a92f573..6a3bc477 100644 --- a/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentActionClient.java +++ b/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentActionClient.java @@ -16,7 +16,7 @@ import java.util.concurrent.ExecutionException; /** - * Facade to provide GeoLocation enrichment for other plugin. + * Facade to provide GeoLocation enrichment for other plugins. */ @Log4j2 @AllArgsConstructor @@ -24,7 +24,6 @@ public class IpEnrichmentActionClient { NodeClient nodeClient; - /** * IpEnrichment with default datasource. * @param ipString Ip String to resolve. @@ -41,8 +40,10 @@ public Map getGeoLocationData (String ipString) { * @return A map instance which contain GeoLocation data for the given Ip address. */ public Map getGeoLocationData (String ipString, String datasourceName) { + // Composite the request object. ActionFuture responseActionFuture = nodeClient.execute( IpEnrichmentAction.INSTANCE, new IpEnrichmentRequest(ipString, datasourceName)); + // Send out the request and process the response. try { ActionResponse genericActionResponse = responseActionFuture.get(); IpEnrichmentResponse enrichmentResponse = IpEnrichmentResponse.fromActionResponse(genericActionResponse); diff --git a/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentRequest.java b/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentRequest.java index 487c5823..6d3a1e4d 100644 --- a/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentRequest.java +++ b/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentRequest.java @@ -8,6 +8,7 @@ import lombok.AllArgsConstructor; import lombok.Getter; import lombok.Setter; +import lombok.extern.log4j.Log4j2; import org.opensearch.action.ActionRequest; import org.opensearch.action.ActionRequestValidationException; import org.opensearch.core.common.io.stream.InputStreamStreamInput; @@ -26,6 +27,7 @@ */ @Getter @Setter +@Log4j2 @AllArgsConstructor public class IpEnrichmentRequest extends ActionRequest { @@ -41,6 +43,7 @@ public IpEnrichmentRequest(StreamInput streamInput) throws IOException { super(streamInput); ipString = streamInput.readString(); datasourceName= streamInput.readOptionalString(); + log.trace("Constructing IP Enrichment request with values: [{}, {}]", ipString, datasourceName); } /** @@ -58,7 +61,7 @@ public ActionRequestValidationException validate() { } /** - * Overrided method to populate convert object's payload into StreamOutput form. + * Overridden method to populate object's payload into StreamOutput form. * @param out the StreamOutput object. * @throws IOException If given StreamOutput is not compatible. */ diff --git a/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentResponse.java b/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentResponse.java index 0b03ddad..0a84ff8f 100644 --- a/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentResponse.java +++ b/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentResponse.java @@ -10,6 +10,7 @@ import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; +import lombok.extern.log4j.Log4j2; import org.opensearch.core.action.ActionResponse; import org.opensearch.core.common.io.stream.InputStreamStreamInput; import org.opensearch.core.common.io.stream.OutputStreamStreamOutput; @@ -27,13 +28,13 @@ */ @Getter @Setter +@Log4j2 @AllArgsConstructor @EqualsAndHashCode(callSuper = false) public class IpEnrichmentResponse extends ActionResponse { private Map geoLocationData; - /** * Private method to be used by fromActionResponse call to populate this Response class. * @param streamInput Stream object which contain the geoLocationData. @@ -45,13 +46,14 @@ public IpEnrichmentResponse(StreamInput streamInput) throws IOException { } /** - * Overrided method used by OpenSearch runtime to write result into listener. + * Overridden method used by OpenSearch runtime to write result into listener. * @param streamOutput the streamOutput used to construct this response object. * @throws IOException the IOException. */ @Override public void writeTo(StreamOutput streamOutput) throws IOException { streamOutput.writeMap(geoLocationData); + log.trace("Constructing IP Enrichment response with values: [{}]", geoLocationData); } /** diff --git a/src/main/java/org/opensearch/geospatial/ip2geo/action/IpEnrichmentTransportAction.java b/src/main/java/org/opensearch/geospatial/ip2geo/action/IpEnrichmentTransportAction.java index dde58f8f..01c7bb40 100644 --- a/src/main/java/org/opensearch/geospatial/ip2geo/action/IpEnrichmentTransportAction.java +++ b/src/main/java/org/opensearch/geospatial/ip2geo/action/IpEnrichmentTransportAction.java @@ -34,8 +34,6 @@ public class IpEnrichmentTransportAction extends HandledTransportAction allDatasources = datasourceDao.getAllDatasources(); this.defaultDataSourceName = (!allDatasources.isEmpty()) ? allDatasources.get(0).getName() : null; } /** - * Overrided method to extract IP String from IpEnrichmentRequest object and return the enrichment result + * Overridden method to extract IP String from IpEnrichmentRequest object and return the enrichment result * in the form of IpEnrichmentResponse which contains the GeoLocation data for given IP String. * @param task the task. * @param request request object in the form of IpEnrichmentRequest which contain the IP String to resolve From 569e8119dcf03b53756a699c53819b8635a9acc2 Mon Sep 17 00:00:00 2001 From: Andy Kwok Date: Fri, 22 Nov 2024 12:33:08 -0800 Subject: [PATCH 13/36] Initial test cases Signed-off-by: Andy Kwok --- client/build.gradle | 5 ++ .../action/IpEnrichmentRequest.java | 3 +- .../action/IpEnrichmentRequestTest.java | 70 +++++++++++++++++++ 3 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentRequestTest.java diff --git a/client/build.gradle b/client/build.gradle index f14416b4..d19e241b 100644 --- a/client/build.gradle +++ b/client/build.gradle @@ -32,6 +32,11 @@ compileTestJava { dependencies { compileOnly "${group}:opensearch:${opensearch_version}" + + testImplementation 'org.junit.jupiter:junit-jupiter:5.11.3' + testImplementation 'org.junit.jupiter:junit-jupiter-api:5.11.3' + + testImplementation "${group}:opensearch:${opensearch_version}" } publishing { diff --git a/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentRequest.java b/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentRequest.java index 6d3a1e4d..3c507eb6 100644 --- a/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentRequest.java +++ b/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentRequest.java @@ -20,7 +20,6 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.UncheckedIOException; -import java.util.Optional; /** * Wrapper for the IP 2 GeoLocation action request. @@ -74,7 +73,7 @@ public void writeTo(StreamOutput out) throws IOException { /** * Static method get around the cast exception happen for cross plugin communication. - * @param actionRequest An casted-up version of IpEnrichmentRequest. + * @param actionRequest A casted-up version of IpEnrichmentRequest. * @return IpEnrichmentRequest object which can be used within the scope of the caller. */ public static IpEnrichmentRequest fromActionRequest(ActionRequest actionRequest) { diff --git a/client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentRequestTest.java b/client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentRequestTest.java new file mode 100644 index 00000000..03a07fff --- /dev/null +++ b/client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentRequestTest.java @@ -0,0 +1,70 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.opensearch.geospatial.action; + + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class IpEnrichmentRequestTest { + + + // Test Validate + // Success + // Missing IP String + // Missing DataSource is ok + + + /** + * Test validate() against a valid record. + */ + @Test + public void testValidateValidRequest() { + IpEnrichmentRequest request = new IpEnrichmentRequest( + "192.168.1.1", "ValidDataSourceName"); + Assertions.assertNull(request.validate()); + } + + /** + * Test validate() against a valid record, + * no error expected, because dataSourceName is optional. + */ + @Test + public void testValidateNullDataSourceName() { + IpEnrichmentRequest request = new IpEnrichmentRequest( + "192.168.1.1", null); + Assertions.assertNull(request.validate()); + } + + /** + * Test validate() against a valid record, + * no error expected, because dataSourceName is optional. + */ + @Test + public void testValidateNullIpStringAndDataSourceName() { + IpEnrichmentRequest request = new IpEnrichmentRequest( + null, null); + Assertions.assertEquals(1, request.validate().validationErrors().size()); + } + + + // Test StreamInput + // Successful case + // Junk input + // Partial missing output + + + // Test WriteTo + // Write a valid record + // Write with some junk data + + + // Test FromActionRequest + // Test valid record + // Test non IPEnrichment request + // Test with some junk. + +} \ No newline at end of file From 081fac5a7ab1b44b50d2fcf32904d28d676f79dc Mon Sep 17 00:00:00 2001 From: Andy Kwok Date: Fri, 22 Nov 2024 15:20:40 -0800 Subject: [PATCH 14/36] Add test-cases Signed-off-by: Andy Kwok --- client/build.gradle | 3 + .../action/IpEnrichmentActionClientTest.java | 62 +++++++++++++++++++ .../action/IpEnrichmentRequestTest.java | 37 +++++------ .../action/IpEnrichmentResponseTest.java | 26 ++++++++ 4 files changed, 108 insertions(+), 20 deletions(-) create mode 100644 client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentActionClientTest.java create mode 100644 client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentResponseTest.java diff --git a/client/build.gradle b/client/build.gradle index d19e241b..bff7fe79 100644 --- a/client/build.gradle +++ b/client/build.gradle @@ -35,8 +35,11 @@ dependencies { testImplementation 'org.junit.jupiter:junit-jupiter:5.11.3' testImplementation 'org.junit.jupiter:junit-jupiter-api:5.11.3' + testImplementation 'org.mockito:mockito-core:5.14.2' + testImplementation 'org.mockito:mockito-junit-jupiter:5.14.2' testImplementation "${group}:opensearch:${opensearch_version}" + } publishing { diff --git a/client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentActionClientTest.java b/client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentActionClientTest.java new file mode 100644 index 00000000..6c44971a --- /dev/null +++ b/client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentActionClientTest.java @@ -0,0 +1,62 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.opensearch.geospatial.action; + +import lombok.SneakyThrows; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.opensearch.client.node.NodeClient; +import org.opensearch.common.action.ActionFuture; +import org.opensearch.core.action.ActionResponse; + +import java.util.Map; +import java.util.concurrent.ExecutionException; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +class IpEnrichmentActionClientTest { + + // Test with happy path + + // Test with exception path + + @Mock + private NodeClient mockNodeClient; + + @Mock + private ActionFuture mockResult; + + @SneakyThrows + @Test + void testWithValidResponse() { + Map dummyPayload = Map.of("k1", "v1"); + String dummyIpString = "192.168.1.1"; + when(mockResult.get()).thenReturn(new IpEnrichmentResponse(dummyPayload)); + when(mockNodeClient.execute(eq(IpEnrichmentAction.INSTANCE), any())).thenReturn(mockResult); + + IpEnrichmentActionClient ipClient = new IpEnrichmentActionClient(mockNodeClient); + Map actualPayload = ipClient.getGeoLocationData(dummyIpString); + Assertions.assertEquals(dummyPayload, actualPayload); + } + + @SneakyThrows + @Test + void testWithException() { + Map dummyPayload = Map.of("k1", "v1"); + String dummyIpString = "192.168.1.1"; + when(mockResult.get()).thenThrow(new ExecutionException(new Throwable())); + when(mockNodeClient.execute(eq(IpEnrichmentAction.INSTANCE), any())).thenReturn(mockResult); + + IpEnrichmentActionClient ipClient = new IpEnrichmentActionClient(mockNodeClient); + Assertions.assertNull(ipClient.getGeoLocationData(dummyIpString)); + } +} \ No newline at end of file diff --git a/client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentRequestTest.java b/client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentRequestTest.java index 03a07fff..1b0b7b58 100644 --- a/client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentRequestTest.java +++ b/client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentRequestTest.java @@ -9,15 +9,11 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +/** + * Test cases for IpEnrichmentRequest. + */ public class IpEnrichmentRequestTest { - - // Test Validate - // Success - // Missing IP String - // Missing DataSource is ok - - /** * Test validate() against a valid record. */ @@ -51,20 +47,21 @@ public void testValidateNullIpStringAndDataSourceName() { } - // Test StreamInput - // Successful case - // Junk input - // Partial missing output - - - // Test WriteTo - // Write a valid record - // Write with some junk data + /** + * Test validate() against a valid record, + * no error expected, because dataSourceName is optional. + */ + @Test + public void testFromActionRequestOnValidRecord() { + String ipString = "192.168.1.1"; + String dsName = "demo"; + IpEnrichmentRequest request = new IpEnrichmentRequest( + ipString, dsName); + IpEnrichmentRequest requestAfterStream = IpEnrichmentRequest.fromActionRequest(request); - // Test FromActionRequest - // Test valid record - // Test non IPEnrichment request - // Test with some junk. + Assertions.assertEquals(request.getIpString(), requestAfterStream.getIpString()); + Assertions.assertEquals(request.getDatasourceName(), requestAfterStream.getDatasourceName()); + } } \ No newline at end of file diff --git a/client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentResponseTest.java b/client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentResponseTest.java new file mode 100644 index 00000000..83591104 --- /dev/null +++ b/client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentResponseTest.java @@ -0,0 +1,26 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.opensearch.geospatial.action; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.Map; + +class IpEnrichmentResponseTest { + + /** + * To simulate when Response class being passed from one plugin to the other. + */ + @Test + void testFromActionResponseWithValidPayload() { + + Map payload = Map.of("k1", "v1"); + IpEnrichmentResponse response = new IpEnrichmentResponse(payload); + IpEnrichmentResponse castedResponse = IpEnrichmentResponse.fromActionResponse(response); + Assertions.assertEquals(response.getGeoLocationData(), castedResponse.getGeoLocationData()); + } +} \ No newline at end of file From 171fee6bbb3831ead8c4713e9b64f625cec5e4e3 Mon Sep 17 00:00:00 2001 From: Andy Kwok Date: Fri, 22 Nov 2024 15:33:34 -0800 Subject: [PATCH 15/36] Test-cases: Client Signed-off-by: Andy Kwok --- .../geospatial/action/IpEnrichmentActionClientTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentActionClientTest.java b/client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentActionClientTest.java index 6c44971a..2a23ae5f 100644 --- a/client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentActionClientTest.java +++ b/client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentActionClientTest.java @@ -51,7 +51,6 @@ void testWithValidResponse() { @SneakyThrows @Test void testWithException() { - Map dummyPayload = Map.of("k1", "v1"); String dummyIpString = "192.168.1.1"; when(mockResult.get()).thenThrow(new ExecutionException(new Throwable())); when(mockNodeClient.execute(eq(IpEnrichmentAction.INSTANCE), any())).thenReturn(mockResult); From 3b0fb1d7dedff002139c7d66c9940f1a22b99810 Mon Sep 17 00:00:00 2001 From: Andy Kwok Date: Fri, 22 Nov 2024 17:02:05 -0800 Subject: [PATCH 16/36] Update tests Signed-off-by: Andy Kwok --- .../action/IpEnrichmentActionClientTest.java | 4 - .../action/IpEnrichmentTransportAction.java | 15 +++- .../IpEnrichmentTransportActionTests.java | 89 +++++++++++++++++++ 3 files changed, 100 insertions(+), 8 deletions(-) create mode 100644 src/test/java/org/opensearch/geospatial/ip2geo/action/IpEnrichmentTransportActionTests.java diff --git a/client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentActionClientTest.java b/client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentActionClientTest.java index 2a23ae5f..597dfcc9 100644 --- a/client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentActionClientTest.java +++ b/client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentActionClientTest.java @@ -25,10 +25,6 @@ @ExtendWith(MockitoExtension.class) class IpEnrichmentActionClientTest { - // Test with happy path - - // Test with exception path - @Mock private NodeClient mockNodeClient; diff --git a/src/main/java/org/opensearch/geospatial/ip2geo/action/IpEnrichmentTransportAction.java b/src/main/java/org/opensearch/geospatial/ip2geo/action/IpEnrichmentTransportAction.java index 01c7bb40..c361b0b2 100644 --- a/src/main/java/org/opensearch/geospatial/ip2geo/action/IpEnrichmentTransportAction.java +++ b/src/main/java/org/opensearch/geospatial/ip2geo/action/IpEnrichmentTransportAction.java @@ -32,9 +32,11 @@ public class IpEnrichmentTransportAction extends HandledTransportAction { - private Ip2GeoCachedDao ip2GeoCachedDao; + private final Ip2GeoCachedDao ip2GeoCachedDao; - private String defaultDataSourceName; +// private final String defaultDataSourceName; + + private final DatasourceDao datasourceDao; /** * Constructor @@ -50,8 +52,7 @@ public IpEnrichmentTransportAction( DatasourceDao datasourceDao) { super(IpEnrichmentAction.NAME, transportService, actionFilters, IpEnrichmentRequest::new); this.ip2GeoCachedDao = cachedDao; - List allDatasources = datasourceDao.getAllDatasources(); - this.defaultDataSourceName = (!allDatasources.isEmpty()) ? allDatasources.get(0).getName() : null; + this.datasourceDao = datasourceDao; } @@ -66,6 +67,7 @@ public IpEnrichmentTransportAction( protected void doExecute(Task task, ActionRequest request, ActionListener listener) { IpEnrichmentRequest enrichmentRequest = IpEnrichmentRequest.fromActionRequest(request); String ipString = enrichmentRequest.getIpString(); + String defaultDataSourceName = getDefaultDataSourceName(); if (enrichmentRequest.getDatasourceName() == null && defaultDataSourceName == null) { log.error("No data source available, IpEnrichmentTransportAction aborted."); @@ -80,4 +82,9 @@ protected void doExecute(Task task, ActionRequest request, ActionListener allDatasources = datasourceDao.getAllDatasources(); + return (!allDatasources.isEmpty()) ? allDatasources.get(0).getName() : null; + } + } diff --git a/src/test/java/org/opensearch/geospatial/ip2geo/action/IpEnrichmentTransportActionTests.java b/src/test/java/org/opensearch/geospatial/ip2geo/action/IpEnrichmentTransportActionTests.java new file mode 100644 index 00000000..198246eb --- /dev/null +++ b/src/test/java/org/opensearch/geospatial/ip2geo/action/IpEnrichmentTransportActionTests.java @@ -0,0 +1,89 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.opensearch.geospatial.ip2geo.action; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.opensearch.core.action.ActionListener; +import org.opensearch.core.action.ActionResponse; +import org.opensearch.geospatial.action.IpEnrichmentRequest; +import org.opensearch.geospatial.action.IpEnrichmentResponse; +import org.opensearch.geospatial.ip2geo.Ip2GeoTestCase; +import org.opensearch.geospatial.ip2geo.jobscheduler.Datasource; +import org.opensearch.tasks.Task; + +import java.util.Collections; +import java.util.List; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +public class IpEnrichmentTransportActionTests extends Ip2GeoTestCase { + + private IpEnrichmentTransportAction action; + + @Mock + Task task; + + @Mock + ActionListener listener; + + @Mock + Datasource mockDataSource; + + @Before + public void init() { + action = new IpEnrichmentTransportAction( + transportService, actionFilters, ip2GeoCachedDao, datasourceDao); + } + + + /** + * When dataSource is provided. + */ + @Test + public void testDoExecute_All_Succeed() { + IpEnrichmentRequest request = new IpEnrichmentRequest("192.168.1.1", "testSource"); + action.doExecute(task, request, listener); + + verify(listener, times(1)).onResponse(any(IpEnrichmentResponse.class)); + } + + /** + * When dataSource is absent, but default is valid. + */ + @Test + public void testDoExecute_WithDefaultDataSource() { + when(mockDataSource.getName()).thenReturn("defaultDataSourceName"); + when(datasourceDao.getAllDatasources()).thenReturn(List.of(mockDataSource)); + when(ip2GeoCachedDao.getIndexName(eq("defaultDataSourceName"))).thenReturn("defaultIndexName"); + when(ip2GeoCachedDao.getGeoData(eq("defaultIndexName"), any())).thenReturn(Collections.emptyMap()); + + IpEnrichmentRequest request = new IpEnrichmentRequest("192.168.1.1", null); + action.doExecute(task, request, listener); + verify(listener, times(1)).onResponse(any(IpEnrichmentResponse.class)); + } + + + /** + * No alternative dataSource, exception being thrown to indicate this. + */ + @Test + public void testDoExecute_WithNoAlternativeDataSource() { + IpEnrichmentRequest request = new IpEnrichmentRequest( + "192.168.1.1", null); + action.doExecute(task, request, listener); + + verify(listener, times(1)) + .onFailure(any(IllegalArgumentException.class)); + } + + +} \ No newline at end of file From b3c9d7b8fd565cd60c1d4ae3c27649f82c0a7e9d Mon Sep 17 00:00:00 2001 From: Andy Kwok Date: Fri, 22 Nov 2024 17:04:26 -0800 Subject: [PATCH 17/36] Update code style Signed-off-by: Andy Kwok --- .../action/IpEnrichmentTransportAction.java | 30 ++++++++--------- .../geospatial/plugin/GeospatialPlugin.java | 2 +- .../IpEnrichmentTransportActionTests.java | 32 ++++++++----------- 3 files changed, 28 insertions(+), 36 deletions(-) diff --git a/src/main/java/org/opensearch/geospatial/ip2geo/action/IpEnrichmentTransportAction.java b/src/main/java/org/opensearch/geospatial/ip2geo/action/IpEnrichmentTransportAction.java index c361b0b2..ff80c51d 100644 --- a/src/main/java/org/opensearch/geospatial/ip2geo/action/IpEnrichmentTransportAction.java +++ b/src/main/java/org/opensearch/geospatial/ip2geo/action/IpEnrichmentTransportAction.java @@ -5,7 +5,10 @@ package org.opensearch.geospatial.ip2geo.action; -import lombok.extern.log4j.Log4j2; +import java.util.List; +import java.util.Map; +import java.util.Optional; + import org.opensearch.action.ActionRequest; import org.opensearch.action.support.ActionFilters; import org.opensearch.action.support.HandledTransportAction; @@ -21,20 +24,17 @@ import org.opensearch.tasks.Task; import org.opensearch.transport.TransportService; -import java.util.List; -import java.util.Map; -import java.util.Optional; +import lombok.extern.log4j.Log4j2; /** * Transport action to convert provided IP address String into GeoLocation data. */ @Log4j2 -public class IpEnrichmentTransportAction extends HandledTransportAction { +public class IpEnrichmentTransportAction extends HandledTransportAction { private final Ip2GeoCachedDao ip2GeoCachedDao; -// private final String defaultDataSourceName; + // private final String defaultDataSourceName; private final DatasourceDao datasourceDao; @@ -46,16 +46,16 @@ public class IpEnrichmentTransportAction extends HandledTransportAction geoLocationData = ip2GeoCachedDao.getGeoData(indexName, ipString); log.debug("GeoSpatial IP lookup on IP: [{}], and result [{}]", ipString, geoLocationData); diff --git a/src/main/java/org/opensearch/geospatial/plugin/GeospatialPlugin.java b/src/main/java/org/opensearch/geospatial/plugin/GeospatialPlugin.java index 6307967e..f0f45e1e 100644 --- a/src/main/java/org/opensearch/geospatial/plugin/GeospatialPlugin.java +++ b/src/main/java/org/opensearch/geospatial/plugin/GeospatialPlugin.java @@ -31,7 +31,6 @@ import org.opensearch.env.Environment; import org.opensearch.env.NodeEnvironment; import org.opensearch.geospatial.action.IpEnrichmentAction; -import org.opensearch.geospatial.ip2geo.action.IpEnrichmentTransportAction; import org.opensearch.geospatial.action.upload.geojson.UploadGeoJSONAction; import org.opensearch.geospatial.action.upload.geojson.UploadGeoJSONTransportAction; import org.opensearch.geospatial.index.mapper.xypoint.XYPointFieldMapper; @@ -43,6 +42,7 @@ import org.opensearch.geospatial.ip2geo.action.DeleteDatasourceTransportAction; import org.opensearch.geospatial.ip2geo.action.GetDatasourceAction; import org.opensearch.geospatial.ip2geo.action.GetDatasourceTransportAction; +import org.opensearch.geospatial.ip2geo.action.IpEnrichmentTransportAction; import org.opensearch.geospatial.ip2geo.action.PutDatasourceAction; import org.opensearch.geospatial.ip2geo.action.PutDatasourceTransportAction; import org.opensearch.geospatial.ip2geo.action.RestDeleteDatasourceHandler; diff --git a/src/test/java/org/opensearch/geospatial/ip2geo/action/IpEnrichmentTransportActionTests.java b/src/test/java/org/opensearch/geospatial/ip2geo/action/IpEnrichmentTransportActionTests.java index 198246eb..bcf9c596 100644 --- a/src/test/java/org/opensearch/geospatial/ip2geo/action/IpEnrichmentTransportActionTests.java +++ b/src/test/java/org/opensearch/geospatial/ip2geo/action/IpEnrichmentTransportActionTests.java @@ -5,6 +5,15 @@ package org.opensearch.geospatial.ip2geo.action; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.util.Collections; +import java.util.List; + import org.junit.Before; import org.junit.Test; import org.mockito.Mock; @@ -16,15 +25,6 @@ import org.opensearch.geospatial.ip2geo.jobscheduler.Datasource; import org.opensearch.tasks.Task; -import java.util.Collections; -import java.util.List; - -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - public class IpEnrichmentTransportActionTests extends Ip2GeoTestCase { private IpEnrichmentTransportAction action; @@ -40,11 +40,9 @@ public class IpEnrichmentTransportActionTests extends Ip2GeoTestCase { @Before public void init() { - action = new IpEnrichmentTransportAction( - transportService, actionFilters, ip2GeoCachedDao, datasourceDao); + action = new IpEnrichmentTransportAction(transportService, actionFilters, ip2GeoCachedDao, datasourceDao); } - /** * When dataSource is provided. */ @@ -71,19 +69,15 @@ public void testDoExecute_WithDefaultDataSource() { verify(listener, times(1)).onResponse(any(IpEnrichmentResponse.class)); } - /** * No alternative dataSource, exception being thrown to indicate this. */ @Test public void testDoExecute_WithNoAlternativeDataSource() { - IpEnrichmentRequest request = new IpEnrichmentRequest( - "192.168.1.1", null); + IpEnrichmentRequest request = new IpEnrichmentRequest("192.168.1.1", null); action.doExecute(task, request, listener); - verify(listener, times(1)) - .onFailure(any(IllegalArgumentException.class)); + verify(listener, times(1)).onFailure(any(IllegalArgumentException.class)); } - -} \ No newline at end of file +} From 1dd42b793891889f3df40d76c52923c32442eb3f Mon Sep 17 00:00:00 2001 From: Andy Kwok Date: Mon, 25 Nov 2024 10:10:35 -0800 Subject: [PATCH 18/36] Style fix Signed-off-by: Andy Kwok --- .../opensearch/geospatial/action/IpEnrichmentResponse.java | 6 +++--- .../ip2geo/action/IpEnrichmentTransportAction.java | 3 +-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentResponse.java b/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentResponse.java index 0a84ff8f..826e8249 100644 --- a/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentResponse.java +++ b/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentResponse.java @@ -36,24 +36,24 @@ public class IpEnrichmentResponse extends ActionResponse { private Map geoLocationData; /** - * Private method to be used by fromActionResponse call to populate this Response class. + * Public method to be called by fromActionResponse( ) to populate this Response class. * @param streamInput Stream object which contain the geoLocationData. * @throws IOException Exception being thrown when given stremInput doesn't contain what IpEnrichmentResponse is expecting. */ public IpEnrichmentResponse(StreamInput streamInput) throws IOException { super(streamInput); geoLocationData = streamInput.readMap(); + log.trace("Constructing IP Enrichment response with values: [{}]", geoLocationData); } /** - * Overridden method used by OpenSearch runtime to write result into listener. + * Overridden method used by OpenSearch runtime to serialise this class content into stream. * @param streamOutput the streamOutput used to construct this response object. * @throws IOException the IOException. */ @Override public void writeTo(StreamOutput streamOutput) throws IOException { streamOutput.writeMap(geoLocationData); - log.trace("Constructing IP Enrichment response with values: [{}]", geoLocationData); } /** diff --git a/src/main/java/org/opensearch/geospatial/ip2geo/action/IpEnrichmentTransportAction.java b/src/main/java/org/opensearch/geospatial/ip2geo/action/IpEnrichmentTransportAction.java index ff80c51d..aed46866 100644 --- a/src/main/java/org/opensearch/geospatial/ip2geo/action/IpEnrichmentTransportAction.java +++ b/src/main/java/org/opensearch/geospatial/ip2geo/action/IpEnrichmentTransportAction.java @@ -34,8 +34,6 @@ public class IpEnrichmentTransportAction extends HandledTransportAction Date: Mon, 25 Nov 2024 20:27:50 -0800 Subject: [PATCH 19/36] Update deps Signed-off-by: Andy Kwok --- client/LICENSE.txt | 204 ++++++++++++++++++ client/NOTICE.txt | 2 + client/build.gradle | 17 +- .../action/IpEnrichmentActionClientTest.java | 20 +- .../action/IpEnrichmentRequestTest.java | 14 +- .../action/IpEnrichmentResponseTest.java | 10 +- 6 files changed, 234 insertions(+), 33 deletions(-) create mode 100644 client/LICENSE.txt create mode 100644 client/NOTICE.txt diff --git a/client/LICENSE.txt b/client/LICENSE.txt new file mode 100644 index 00000000..3ab280eb --- /dev/null +++ b/client/LICENSE.txt @@ -0,0 +1,204 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + 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. + +This project is based on a modification of https://github.com/uber/h3 which is licensed under the Apache 2.0 License. diff --git a/client/NOTICE.txt b/client/NOTICE.txt new file mode 100644 index 00000000..76223dfd --- /dev/null +++ b/client/NOTICE.txt @@ -0,0 +1,2 @@ +OpenSearch (https://opensearch.org) +Copyright OpenSearch Contributors \ No newline at end of file diff --git a/client/build.gradle b/client/build.gradle index bff7fe79..3760582a 100644 --- a/client/build.gradle +++ b/client/build.gradle @@ -3,16 +3,13 @@ * SPDX-License-Identifier: Apache-2.0 */ -apply plugin: 'java' -apply plugin: 'maven-publish' -apply plugin: 'jacoco' +//apply plugin: 'java' +apply plugin: 'opensearch.build' apply plugin: 'io.freefair.lombok' allprojects { group = opensearch_group version = "${opensearch_build}" - targetCompatibility = JavaVersion.VERSION_11 - sourceCompatibility = JavaVersion.VERSION_11 } repositories { @@ -33,15 +30,15 @@ compileTestJava { dependencies { compileOnly "${group}:opensearch:${opensearch_version}" - testImplementation 'org.junit.jupiter:junit-jupiter:5.11.3' - testImplementation 'org.junit.jupiter:junit-jupiter-api:5.11.3' - testImplementation 'org.mockito:mockito-core:5.14.2' - testImplementation 'org.mockito:mockito-junit-jupiter:5.14.2' - + testImplementation "junit:junit:${versions.junit}" + testImplementation "org.mockito:mockito-core:${versions.mockito}" testImplementation "${group}:opensearch:${opensearch_version}" } +licenseFile = "LICENSE.txt" +noticeFile = "NOTICE.txt" + publishing { publications { mavenJava(MavenPublication) { diff --git a/client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentActionClientTest.java b/client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentActionClientTest.java index 597dfcc9..feab90f3 100644 --- a/client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentActionClientTest.java +++ b/client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentActionClientTest.java @@ -6,11 +6,9 @@ package org.opensearch.geospatial.action; import lombok.SneakyThrows; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.Assert; +import org.junit.Test; import org.mockito.Mock; -import org.mockito.junit.jupiter.MockitoExtension; import org.opensearch.client.node.NodeClient; import org.opensearch.common.action.ActionFuture; import org.opensearch.core.action.ActionResponse; @@ -22,8 +20,8 @@ import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.when; -@ExtendWith(MockitoExtension.class) -class IpEnrichmentActionClientTest { + +public class IpEnrichmentActionClientTest { @Mock private NodeClient mockNodeClient; @@ -33,7 +31,7 @@ class IpEnrichmentActionClientTest { @SneakyThrows @Test - void testWithValidResponse() { + public void testWithValidResponse() { Map dummyPayload = Map.of("k1", "v1"); String dummyIpString = "192.168.1.1"; when(mockResult.get()).thenReturn(new IpEnrichmentResponse(dummyPayload)); @@ -41,17 +39,17 @@ void testWithValidResponse() { IpEnrichmentActionClient ipClient = new IpEnrichmentActionClient(mockNodeClient); Map actualPayload = ipClient.getGeoLocationData(dummyIpString); - Assertions.assertEquals(dummyPayload, actualPayload); + Assert.assertEquals(dummyPayload, actualPayload); } - @SneakyThrows @Test - void testWithException() { + @SneakyThrows + public void testWithException() { String dummyIpString = "192.168.1.1"; when(mockResult.get()).thenThrow(new ExecutionException(new Throwable())); when(mockNodeClient.execute(eq(IpEnrichmentAction.INSTANCE), any())).thenReturn(mockResult); IpEnrichmentActionClient ipClient = new IpEnrichmentActionClient(mockNodeClient); - Assertions.assertNull(ipClient.getGeoLocationData(dummyIpString)); + Assert.assertNull(ipClient.getGeoLocationData(dummyIpString)); } } \ No newline at end of file diff --git a/client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentRequestTest.java b/client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentRequestTest.java index 1b0b7b58..40c4158b 100644 --- a/client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentRequestTest.java +++ b/client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentRequestTest.java @@ -6,8 +6,8 @@ package org.opensearch.geospatial.action; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Test; /** * Test cases for IpEnrichmentRequest. @@ -21,7 +21,7 @@ public class IpEnrichmentRequestTest { public void testValidateValidRequest() { IpEnrichmentRequest request = new IpEnrichmentRequest( "192.168.1.1", "ValidDataSourceName"); - Assertions.assertNull(request.validate()); + Assert.assertNull(request.validate()); } /** @@ -32,7 +32,7 @@ public void testValidateValidRequest() { public void testValidateNullDataSourceName() { IpEnrichmentRequest request = new IpEnrichmentRequest( "192.168.1.1", null); - Assertions.assertNull(request.validate()); + Assert.assertNull(request.validate()); } /** @@ -43,7 +43,7 @@ public void testValidateNullDataSourceName() { public void testValidateNullIpStringAndDataSourceName() { IpEnrichmentRequest request = new IpEnrichmentRequest( null, null); - Assertions.assertEquals(1, request.validate().validationErrors().size()); + Assert.assertEquals(1, request.validate().validationErrors().size()); } @@ -60,8 +60,8 @@ public void testFromActionRequestOnValidRecord() { IpEnrichmentRequest requestAfterStream = IpEnrichmentRequest.fromActionRequest(request); - Assertions.assertEquals(request.getIpString(), requestAfterStream.getIpString()); - Assertions.assertEquals(request.getDatasourceName(), requestAfterStream.getDatasourceName()); + Assert.assertEquals(request.getIpString(), requestAfterStream.getIpString()); + Assert.assertEquals(request.getDatasourceName(), requestAfterStream.getDatasourceName()); } } \ No newline at end of file diff --git a/client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentResponseTest.java b/client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentResponseTest.java index 83591104..a5f0cde1 100644 --- a/client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentResponseTest.java +++ b/client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentResponseTest.java @@ -5,22 +5,22 @@ package org.opensearch.geospatial.action; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Test; import java.util.Map; -class IpEnrichmentResponseTest { +public class IpEnrichmentResponseTest { /** * To simulate when Response class being passed from one plugin to the other. */ @Test - void testFromActionResponseWithValidPayload() { + public void testFromActionResponseWithValidPayload() { Map payload = Map.of("k1", "v1"); IpEnrichmentResponse response = new IpEnrichmentResponse(payload); IpEnrichmentResponse castedResponse = IpEnrichmentResponse.fromActionResponse(response); - Assertions.assertEquals(response.getGeoLocationData(), castedResponse.getGeoLocationData()); + Assert.assertEquals(response.getGeoLocationData(), castedResponse.getGeoLocationData()); } } \ No newline at end of file From a6725b241c80ff64157d0d39f92eed6f1d95e8d9 Mon Sep 17 00:00:00 2001 From: Andy Kwok Date: Mon, 25 Nov 2024 21:03:38 -0800 Subject: [PATCH 20/36] Spotless Signed-off-by: Andy Kwok --- client/build.gradle | 14 +++++++++- .../action/IpEnrichmentActionClient.java | 17 +++++------ .../action/IpEnrichmentRequest.java | 25 ++++++++--------- .../action/IpEnrichmentResponse.java | 28 +++++++++---------- .../action/IpEnrichmentActionClientTest.java | 18 ++++++------ .../action/IpEnrichmentRequestTest.java | 16 ++++------- .../action/IpEnrichmentResponseTest.java | 6 ++-- 7 files changed, 64 insertions(+), 60 deletions(-) diff --git a/client/build.gradle b/client/build.gradle index 3760582a..fdca15de 100644 --- a/client/build.gradle +++ b/client/build.gradle @@ -3,9 +3,9 @@ * SPDX-License-Identifier: Apache-2.0 */ -//apply plugin: 'java' apply plugin: 'opensearch.build' apply plugin: 'io.freefair.lombok' +apply plugin: "com.diffplug.spotless" allprojects { group = opensearch_group @@ -36,9 +36,21 @@ dependencies { } + + licenseFile = "LICENSE.txt" noticeFile = "NOTICE.txt" +spotless { + java { + removeUnusedImports() + importOrder 'java', 'javax', 'org', 'com' + eclipse().configFile rootProject.file('formatterConfig.xml') + trimTrailingWhitespace() + endWithNewline() + } +} + publishing { publications { mavenJava(MavenPublication) { diff --git a/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentActionClient.java b/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentActionClient.java index 6a3bc477..2cd98917 100644 --- a/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentActionClient.java +++ b/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentActionClient.java @@ -5,15 +5,14 @@ package org.opensearch.geospatial.action; -import lombok.AllArgsConstructor; -import lombok.extern.log4j.Log4j2; +import java.util.Map; + import org.opensearch.client.node.NodeClient; import org.opensearch.common.action.ActionFuture; import org.opensearch.core.action.ActionResponse; -import java.util.Map; -import java.util.Optional; -import java.util.concurrent.ExecutionException; +import lombok.AllArgsConstructor; +import lombok.extern.log4j.Log4j2; /** * Facade to provide GeoLocation enrichment for other plugins. @@ -22,7 +21,7 @@ @AllArgsConstructor public class IpEnrichmentActionClient { - NodeClient nodeClient; + final private NodeClient nodeClient; /** * IpEnrichment with default datasource. @@ -39,10 +38,12 @@ public Map getGeoLocationData (String ipString) { * @param datasourceName datasourceName in String form. * @return A map instance which contain GeoLocation data for the given Ip address. */ - public Map getGeoLocationData (String ipString, String datasourceName) { + public Map getGeoLocationData(String ipString, String datasourceName) { // Composite the request object. ActionFuture responseActionFuture = nodeClient.execute( - IpEnrichmentAction.INSTANCE, new IpEnrichmentRequest(ipString, datasourceName)); + IpEnrichmentAction.INSTANCE, + new IpEnrichmentRequest(ipString, datasourceName) + ); // Send out the request and process the response. try { ActionResponse genericActionResponse = responseActionFuture.get(); diff --git a/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentRequest.java b/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentRequest.java index 3c507eb6..28dc9da6 100644 --- a/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentRequest.java +++ b/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentRequest.java @@ -5,10 +5,11 @@ package org.opensearch.geospatial.action; -import lombok.AllArgsConstructor; -import lombok.Getter; -import lombok.Setter; -import lombok.extern.log4j.Log4j2; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.UncheckedIOException; + import org.opensearch.action.ActionRequest; import org.opensearch.action.ActionRequestValidationException; import org.opensearch.core.common.io.stream.InputStreamStreamInput; @@ -16,10 +17,10 @@ import org.opensearch.core.common.io.stream.StreamInput; import org.opensearch.core.common.io.stream.StreamOutput; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.UncheckedIOException; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.Setter; +import lombok.extern.log4j.Log4j2; /** * Wrapper for the IP 2 GeoLocation action request. @@ -41,7 +42,7 @@ public class IpEnrichmentRequest extends ActionRequest { public IpEnrichmentRequest(StreamInput streamInput) throws IOException { super(streamInput); ipString = streamInput.readString(); - datasourceName= streamInput.readOptionalString(); + datasourceName = streamInput.readOptionalString(); log.trace("Constructing IP Enrichment request with values: [{}, {}]", ipString, datasourceName); } @@ -83,11 +84,9 @@ public static IpEnrichmentRequest fromActionRequest(ActionRequest actionRequest) } // Or else convert it - try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); - OutputStreamStreamOutput osso = new OutputStreamStreamOutput(baos)) { + try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); OutputStreamStreamOutput osso = new OutputStreamStreamOutput(baos)) { actionRequest.writeTo(osso); - try (StreamInput input = - new InputStreamStreamInput(new ByteArrayInputStream(baos.toByteArray()))) { + try (StreamInput input = new InputStreamStreamInput(new ByteArrayInputStream(baos.toByteArray()))) { return new IpEnrichmentRequest(input); } } catch (IOException e) { diff --git a/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentResponse.java b/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentResponse.java index 826e8249..7f5c3007 100644 --- a/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentResponse.java +++ b/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentResponse.java @@ -5,23 +5,23 @@ package org.opensearch.geospatial.action; -import lombok.AllArgsConstructor; -import lombok.EqualsAndHashCode; -import lombok.Getter; -import lombok.NoArgsConstructor; -import lombok.Setter; -import lombok.extern.log4j.Log4j2; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.UncheckedIOException; +import java.util.Map; + import org.opensearch.core.action.ActionResponse; import org.opensearch.core.common.io.stream.InputStreamStreamInput; import org.opensearch.core.common.io.stream.OutputStreamStreamOutput; import org.opensearch.core.common.io.stream.StreamInput; import org.opensearch.core.common.io.stream.StreamOutput; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.UncheckedIOException; -import java.util.Map; +import lombok.AllArgsConstructor; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; +import lombok.extern.log4j.Log4j2; /** * Wrapper class to encapsulate the IP enrichment result for IpEnrichmentTransportAction. @@ -70,11 +70,9 @@ public static IpEnrichmentResponse fromActionResponse(ActionResponse actionRespo } // Or else convert it - try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); - OutputStreamStreamOutput osso = new OutputStreamStreamOutput(baos)) { + try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); OutputStreamStreamOutput osso = new OutputStreamStreamOutput(baos)) { actionResponse.writeTo(osso); - try (StreamInput input = - new InputStreamStreamInput(new ByteArrayInputStream(baos.toByteArray()))) { + try (StreamInput input = new InputStreamStreamInput(new ByteArrayInputStream(baos.toByteArray()))) { return new IpEnrichmentResponse(input); } } catch (IOException e) { diff --git a/client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentActionClientTest.java b/client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentActionClientTest.java index feab90f3..aee3b4d5 100644 --- a/client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentActionClientTest.java +++ b/client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentActionClientTest.java @@ -5,7 +5,13 @@ package org.opensearch.geospatial.action; -import lombok.SneakyThrows; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.when; + +import java.util.Map; +import java.util.concurrent.ExecutionException; + import org.junit.Assert; import org.junit.Test; import org.mockito.Mock; @@ -13,13 +19,7 @@ import org.opensearch.common.action.ActionFuture; import org.opensearch.core.action.ActionResponse; -import java.util.Map; -import java.util.concurrent.ExecutionException; - -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.when; - +import lombok.SneakyThrows; public class IpEnrichmentActionClientTest { @@ -52,4 +52,4 @@ public void testWithException() { IpEnrichmentActionClient ipClient = new IpEnrichmentActionClient(mockNodeClient); Assert.assertNull(ipClient.getGeoLocationData(dummyIpString)); } -} \ No newline at end of file +} diff --git a/client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentRequestTest.java b/client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentRequestTest.java index 40c4158b..5a2486f2 100644 --- a/client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentRequestTest.java +++ b/client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentRequestTest.java @@ -5,7 +5,6 @@ package org.opensearch.geospatial.action; - import org.junit.Assert; import org.junit.Test; @@ -19,8 +18,7 @@ public class IpEnrichmentRequestTest { */ @Test public void testValidateValidRequest() { - IpEnrichmentRequest request = new IpEnrichmentRequest( - "192.168.1.1", "ValidDataSourceName"); + IpEnrichmentRequest request = new IpEnrichmentRequest("192.168.1.1", "ValidDataSourceName"); Assert.assertNull(request.validate()); } @@ -30,8 +28,7 @@ public void testValidateValidRequest() { */ @Test public void testValidateNullDataSourceName() { - IpEnrichmentRequest request = new IpEnrichmentRequest( - "192.168.1.1", null); + IpEnrichmentRequest request = new IpEnrichmentRequest("192.168.1.1", null); Assert.assertNull(request.validate()); } @@ -41,12 +38,10 @@ public void testValidateNullDataSourceName() { */ @Test public void testValidateNullIpStringAndDataSourceName() { - IpEnrichmentRequest request = new IpEnrichmentRequest( - null, null); + IpEnrichmentRequest request = new IpEnrichmentRequest(null, null); Assert.assertEquals(1, request.validate().validationErrors().size()); } - /** * Test validate() against a valid record, * no error expected, because dataSourceName is optional. @@ -55,8 +50,7 @@ public void testValidateNullIpStringAndDataSourceName() { public void testFromActionRequestOnValidRecord() { String ipString = "192.168.1.1"; String dsName = "demo"; - IpEnrichmentRequest request = new IpEnrichmentRequest( - ipString, dsName); + IpEnrichmentRequest request = new IpEnrichmentRequest(ipString, dsName); IpEnrichmentRequest requestAfterStream = IpEnrichmentRequest.fromActionRequest(request); @@ -64,4 +58,4 @@ public void testFromActionRequestOnValidRecord() { Assert.assertEquals(request.getDatasourceName(), requestAfterStream.getDatasourceName()); } -} \ No newline at end of file +} diff --git a/client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentResponseTest.java b/client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentResponseTest.java index a5f0cde1..6bb88c9b 100644 --- a/client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentResponseTest.java +++ b/client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentResponseTest.java @@ -5,11 +5,11 @@ package org.opensearch.geospatial.action; +import java.util.Map; + import org.junit.Assert; import org.junit.Test; -import java.util.Map; - public class IpEnrichmentResponseTest { /** @@ -23,4 +23,4 @@ public void testFromActionResponseWithValidPayload() { IpEnrichmentResponse castedResponse = IpEnrichmentResponse.fromActionResponse(response); Assert.assertEquals(response.getGeoLocationData(), castedResponse.getGeoLocationData()); } -} \ No newline at end of file +} From 030d100dc39ba3caf815728eade34f46d60c7f50 Mon Sep 17 00:00:00 2001 From: Andy Kwok Date: Mon, 25 Nov 2024 21:11:33 -0800 Subject: [PATCH 21/36] Spotless Signed-off-by: Andy Kwok --- client/build.gradle | 2 -- .../opensearch/geospatial/action/IpEnrichmentActionClient.java | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/client/build.gradle b/client/build.gradle index fdca15de..3b00b093 100644 --- a/client/build.gradle +++ b/client/build.gradle @@ -36,8 +36,6 @@ dependencies { } - - licenseFile = "LICENSE.txt" noticeFile = "NOTICE.txt" diff --git a/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentActionClient.java b/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentActionClient.java index 2cd98917..8209e10a 100644 --- a/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentActionClient.java +++ b/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentActionClient.java @@ -28,7 +28,7 @@ public class IpEnrichmentActionClient { * @param ipString Ip String to resolve. * @return A map instance which contain GeoLocation data for the given Ip address. */ - public Map getGeoLocationData (String ipString) { + public Map getGeoLocationData(String ipString) { return getGeoLocationData(ipString, null); } From 7cad58e8fbcc8f93f8f79e4ba3f58932c12687ff Mon Sep 17 00:00:00 2001 From: Andy Kwok Date: Tue, 26 Nov 2024 13:03:23 -0800 Subject: [PATCH 22/36] Code refactor Signed-off-by: Andy Kwok --- .../action/IpEnrichmentActionClient.java | 23 ++++------------ .../action/IpEnrichmentRequest.java | 2 +- .../action/IpEnrichmentActionClientTest.java | 17 ++++++------ .../action/IpEnrichmentTransportAction.java | 26 +++---------------- .../IpEnrichmentTransportActionTests.java | 2 +- 5 files changed, 19 insertions(+), 51 deletions(-) diff --git a/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentActionClient.java b/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentActionClient.java index 8209e10a..2898e3fc 100644 --- a/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentActionClient.java +++ b/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentActionClient.java @@ -6,6 +6,7 @@ package org.opensearch.geospatial.action; import java.util.Map; +import java.util.concurrent.ExecutionException; import org.opensearch.client.node.NodeClient; import org.opensearch.common.action.ActionFuture; @@ -23,35 +24,21 @@ public class IpEnrichmentActionClient { final private NodeClient nodeClient; - /** - * IpEnrichment with default datasource. - * @param ipString Ip String to resolve. - * @return A map instance which contain GeoLocation data for the given Ip address. - */ - public Map getGeoLocationData(String ipString) { - return getGeoLocationData(ipString, null); - } - /** * Client facing method, which read an IP in String form and return a map instance which contain the associated GeoLocation data. * @param ipString IP v4 || v6 address in String form. * @param datasourceName datasourceName in String form. * @return A map instance which contain GeoLocation data for the given Ip address. */ - public Map getGeoLocationData(String ipString, String datasourceName) { + public Map getGeoLocationData(String ipString, String datasourceName) throws ExecutionException, InterruptedException { // Composite the request object. ActionFuture responseActionFuture = nodeClient.execute( IpEnrichmentAction.INSTANCE, new IpEnrichmentRequest(ipString, datasourceName) ); // Send out the request and process the response. - try { - ActionResponse genericActionResponse = responseActionFuture.get(); - IpEnrichmentResponse enrichmentResponse = IpEnrichmentResponse.fromActionResponse(genericActionResponse); - return enrichmentResponse.getGeoLocationData(); - } catch (Exception e) { - log.error("GeoSpatial IP Enrichment call failure, with detail: ", e); - return null; - } + ActionResponse genericActionResponse = responseActionFuture.get(); + IpEnrichmentResponse enrichmentResponse = IpEnrichmentResponse.fromActionResponse(genericActionResponse); + return enrichmentResponse.getGeoLocationData(); } } diff --git a/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentRequest.java b/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentRequest.java index 28dc9da6..c029ca13 100644 --- a/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentRequest.java +++ b/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentRequest.java @@ -90,7 +90,7 @@ public static IpEnrichmentRequest fromActionRequest(ActionRequest actionRequest) return new IpEnrichmentRequest(input); } } catch (IOException e) { - throw new UncheckedIOException("failed to parse ActionRequest into IpEnrichmentRequest", e); + throw new UncheckedIOException("Failed to parse ActionRequest into IpEnrichmentRequest", e); } } } diff --git a/client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentActionClientTest.java b/client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentActionClientTest.java index aee3b4d5..458b9f34 100644 --- a/client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentActionClientTest.java +++ b/client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentActionClientTest.java @@ -29,27 +29,28 @@ public class IpEnrichmentActionClientTest { @Mock private ActionFuture mockResult; + String dummyIpString = "192.168.1.1"; + + String dummyDataSourceName = "testDataSource"; + + Map dummyPayload = Map.of("k1", "v1"); + @SneakyThrows @Test public void testWithValidResponse() { - Map dummyPayload = Map.of("k1", "v1"); - String dummyIpString = "192.168.1.1"; when(mockResult.get()).thenReturn(new IpEnrichmentResponse(dummyPayload)); when(mockNodeClient.execute(eq(IpEnrichmentAction.INSTANCE), any())).thenReturn(mockResult); - IpEnrichmentActionClient ipClient = new IpEnrichmentActionClient(mockNodeClient); - Map actualPayload = ipClient.getGeoLocationData(dummyIpString); + Map actualPayload = ipClient.getGeoLocationData(dummyIpString, dummyDataSourceName); Assert.assertEquals(dummyPayload, actualPayload); } - @Test @SneakyThrows + @Test(expected = ExecutionException.class) public void testWithException() { - String dummyIpString = "192.168.1.1"; when(mockResult.get()).thenThrow(new ExecutionException(new Throwable())); when(mockNodeClient.execute(eq(IpEnrichmentAction.INSTANCE), any())).thenReturn(mockResult); - IpEnrichmentActionClient ipClient = new IpEnrichmentActionClient(mockNodeClient); - Assert.assertNull(ipClient.getGeoLocationData(dummyIpString)); + ipClient.getGeoLocationData(dummyIpString, dummyDataSourceName); } } diff --git a/src/main/java/org/opensearch/geospatial/ip2geo/action/IpEnrichmentTransportAction.java b/src/main/java/org/opensearch/geospatial/ip2geo/action/IpEnrichmentTransportAction.java index aed46866..3719fb93 100644 --- a/src/main/java/org/opensearch/geospatial/ip2geo/action/IpEnrichmentTransportAction.java +++ b/src/main/java/org/opensearch/geospatial/ip2geo/action/IpEnrichmentTransportAction.java @@ -5,9 +5,7 @@ package org.opensearch.geospatial.ip2geo.action; -import java.util.List; import java.util.Map; -import java.util.Optional; import org.opensearch.action.ActionRequest; import org.opensearch.action.support.ActionFilters; @@ -18,9 +16,7 @@ import org.opensearch.geospatial.action.IpEnrichmentAction; import org.opensearch.geospatial.action.IpEnrichmentRequest; import org.opensearch.geospatial.action.IpEnrichmentResponse; -import org.opensearch.geospatial.ip2geo.dao.DatasourceDao; import org.opensearch.geospatial.ip2geo.dao.Ip2GeoCachedDao; -import org.opensearch.geospatial.ip2geo.jobscheduler.Datasource; import org.opensearch.tasks.Task; import org.opensearch.transport.TransportService; @@ -34,25 +30,16 @@ public class IpEnrichmentTransportAction extends HandledTransportAction listener) { IpEnrichmentRequest enrichmentRequest = IpEnrichmentRequest.fromActionRequest(request); String ipString = enrichmentRequest.getIpString(); - String defaultDataSourceName = getDefaultDataSourceName(); - if (enrichmentRequest.getDatasourceName() == null && defaultDataSourceName == null) { + if (enrichmentRequest.getDatasourceName() == null) { log.error("No data source available, IpEnrichmentTransportAction aborted."); listener.onFailure(new IllegalArgumentException()); } else { - String dataSourceName = Optional.ofNullable(enrichmentRequest.getDatasourceName()).orElse(defaultDataSourceName); + String dataSourceName = enrichmentRequest.getDatasourceName(); String indexName = ip2GeoCachedDao.getIndexName(dataSourceName); Map geoLocationData = ip2GeoCachedDao.getGeoData(indexName, ipString); log.debug("GeoSpatial IP lookup on IP: [{}], and result [{}]", ipString, geoLocationData); listener.onResponse(new IpEnrichmentResponse(geoLocationData)); } } - - private String getDefaultDataSourceName() { - List allDatasources = datasourceDao.getAllDatasources(); - return (!allDatasources.isEmpty()) ? allDatasources.get(0).getName() : null; - } - } diff --git a/src/test/java/org/opensearch/geospatial/ip2geo/action/IpEnrichmentTransportActionTests.java b/src/test/java/org/opensearch/geospatial/ip2geo/action/IpEnrichmentTransportActionTests.java index bcf9c596..22d8acbc 100644 --- a/src/test/java/org/opensearch/geospatial/ip2geo/action/IpEnrichmentTransportActionTests.java +++ b/src/test/java/org/opensearch/geospatial/ip2geo/action/IpEnrichmentTransportActionTests.java @@ -40,7 +40,7 @@ public class IpEnrichmentTransportActionTests extends Ip2GeoTestCase { @Before public void init() { - action = new IpEnrichmentTransportAction(transportService, actionFilters, ip2GeoCachedDao, datasourceDao); + action = new IpEnrichmentTransportAction(transportService, actionFilters, ip2GeoCachedDao); } /** From fb07b879fa8b414c83f9e57a30f501de414c79cc Mon Sep 17 00:00:00 2001 From: Andy Kwok Date: Tue, 26 Nov 2024 17:51:51 -0800 Subject: [PATCH 23/36] Serialisation attempt Signed-off-by: Andy Kwok --- .../geospatial/action/IpEnrichmentRequest.java | 9 ++++++++- .../geospatial/action/IpEnrichmentResponse.java | 11 ++++++++++- .../action/IpEnrichmentTransportActionTests.java | 16 +--------------- 3 files changed, 19 insertions(+), 17 deletions(-) diff --git a/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentRequest.java b/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentRequest.java index c029ca13..1f3cbe3c 100644 --- a/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentRequest.java +++ b/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentRequest.java @@ -31,6 +31,8 @@ @AllArgsConstructor public class IpEnrichmentRequest extends ActionRequest { + private static final String VERSION = "2.18.0.0"; + private String ipString; private String datasourceName; @@ -87,7 +89,12 @@ public static IpEnrichmentRequest fromActionRequest(ActionRequest actionRequest) try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); OutputStreamStreamOutput osso = new OutputStreamStreamOutput(baos)) { actionRequest.writeTo(osso); try (StreamInput input = new InputStreamStreamInput(new ByteArrayInputStream(baos.toByteArray()))) { - return new IpEnrichmentRequest(input); + String objectVersion = input.readString(); + if (VERSION.equals(objectVersion)) { + return new IpEnrichmentRequest(input); + } else { + throw new IllegalArgumentException("Fail to serialise IpEnrichmentRequest due to version mismatch"); + } } } catch (IOException e) { throw new UncheckedIOException("Failed to parse ActionRequest into IpEnrichmentRequest", e); diff --git a/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentResponse.java b/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentResponse.java index 7f5c3007..a1f34e55 100644 --- a/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentResponse.java +++ b/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentResponse.java @@ -33,6 +33,8 @@ @EqualsAndHashCode(callSuper = false) public class IpEnrichmentResponse extends ActionResponse { + private static final String VERSION = "2.18.0.0"; + private Map geoLocationData; /** @@ -72,8 +74,15 @@ public static IpEnrichmentResponse fromActionResponse(ActionResponse actionRespo // Or else convert it try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); OutputStreamStreamOutput osso = new OutputStreamStreamOutput(baos)) { actionResponse.writeTo(osso); + + try (StreamInput input = new InputStreamStreamInput(new ByteArrayInputStream(baos.toByteArray()))) { - return new IpEnrichmentResponse(input); + String objectVersion = input.readString(); + if (VERSION.equals(objectVersion)) { + return new IpEnrichmentResponse(input); + } else { + throw new IllegalArgumentException("Fail to serialise IpEnrichmentResponse due to version mismatch"); + } } } catch (IOException e) { throw new UncheckedIOException("Failed to parse ActionResponse into IpEnrichmentResponse", e); diff --git a/src/test/java/org/opensearch/geospatial/ip2geo/action/IpEnrichmentTransportActionTests.java b/src/test/java/org/opensearch/geospatial/ip2geo/action/IpEnrichmentTransportActionTests.java index 22d8acbc..641bd9c2 100644 --- a/src/test/java/org/opensearch/geospatial/ip2geo/action/IpEnrichmentTransportActionTests.java +++ b/src/test/java/org/opensearch/geospatial/ip2geo/action/IpEnrichmentTransportActionTests.java @@ -53,21 +53,7 @@ public void testDoExecute_All_Succeed() { verify(listener, times(1)).onResponse(any(IpEnrichmentResponse.class)); } - - /** - * When dataSource is absent, but default is valid. - */ - @Test - public void testDoExecute_WithDefaultDataSource() { - when(mockDataSource.getName()).thenReturn("defaultDataSourceName"); - when(datasourceDao.getAllDatasources()).thenReturn(List.of(mockDataSource)); - when(ip2GeoCachedDao.getIndexName(eq("defaultDataSourceName"))).thenReturn("defaultIndexName"); - when(ip2GeoCachedDao.getGeoData(eq("defaultIndexName"), any())).thenReturn(Collections.emptyMap()); - - IpEnrichmentRequest request = new IpEnrichmentRequest("192.168.1.1", null); - action.doExecute(task, request, listener); - verify(listener, times(1)).onResponse(any(IpEnrichmentResponse.class)); - } + /** * No alternative dataSource, exception being thrown to indicate this. From 7b6ed6daa0d5c54ab105a2ddad77bc4f40a4a626 Mon Sep 17 00:00:00 2001 From: Andy Kwok Date: Wed, 27 Nov 2024 10:49:02 -0800 Subject: [PATCH 24/36] Remove custom serialisation support Signed-off-by: Andy Kwok --- .../geospatial/action/IpEnrichmentRequest.java | 9 +-------- .../geospatial/action/IpEnrichmentResponse.java | 11 +---------- 2 files changed, 2 insertions(+), 18 deletions(-) diff --git a/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentRequest.java b/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentRequest.java index 1f3cbe3c..c029ca13 100644 --- a/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentRequest.java +++ b/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentRequest.java @@ -31,8 +31,6 @@ @AllArgsConstructor public class IpEnrichmentRequest extends ActionRequest { - private static final String VERSION = "2.18.0.0"; - private String ipString; private String datasourceName; @@ -89,12 +87,7 @@ public static IpEnrichmentRequest fromActionRequest(ActionRequest actionRequest) try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); OutputStreamStreamOutput osso = new OutputStreamStreamOutput(baos)) { actionRequest.writeTo(osso); try (StreamInput input = new InputStreamStreamInput(new ByteArrayInputStream(baos.toByteArray()))) { - String objectVersion = input.readString(); - if (VERSION.equals(objectVersion)) { - return new IpEnrichmentRequest(input); - } else { - throw new IllegalArgumentException("Fail to serialise IpEnrichmentRequest due to version mismatch"); - } + return new IpEnrichmentRequest(input); } } catch (IOException e) { throw new UncheckedIOException("Failed to parse ActionRequest into IpEnrichmentRequest", e); diff --git a/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentResponse.java b/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentResponse.java index a1f34e55..7f5c3007 100644 --- a/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentResponse.java +++ b/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentResponse.java @@ -33,8 +33,6 @@ @EqualsAndHashCode(callSuper = false) public class IpEnrichmentResponse extends ActionResponse { - private static final String VERSION = "2.18.0.0"; - private Map geoLocationData; /** @@ -74,15 +72,8 @@ public static IpEnrichmentResponse fromActionResponse(ActionResponse actionRespo // Or else convert it try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); OutputStreamStreamOutput osso = new OutputStreamStreamOutput(baos)) { actionResponse.writeTo(osso); - - try (StreamInput input = new InputStreamStreamInput(new ByteArrayInputStream(baos.toByteArray()))) { - String objectVersion = input.readString(); - if (VERSION.equals(objectVersion)) { - return new IpEnrichmentResponse(input); - } else { - throw new IllegalArgumentException("Fail to serialise IpEnrichmentResponse due to version mismatch"); - } + return new IpEnrichmentResponse(input); } } catch (IOException e) { throw new UncheckedIOException("Failed to parse ActionResponse into IpEnrichmentResponse", e); From 03de489705e7b8abc437368d97b386a5f828cc5a Mon Sep 17 00:00:00 2001 From: Andy Kwok Date: Wed, 27 Nov 2024 11:15:34 -0800 Subject: [PATCH 25/36] Address code comments Signed-off-by: Andy Kwok --- .../org/opensearch/geospatial/action/IpEnrichmentRequest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentRequest.java b/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentRequest.java index c029ca13..ed8a8f94 100644 --- a/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentRequest.java +++ b/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentRequest.java @@ -42,7 +42,7 @@ public class IpEnrichmentRequest extends ActionRequest { public IpEnrichmentRequest(StreamInput streamInput) throws IOException { super(streamInput); ipString = streamInput.readString(); - datasourceName = streamInput.readOptionalString(); + datasourceName = streamInput.readString(); log.trace("Constructing IP Enrichment request with values: [{}, {}]", ipString, datasourceName); } From 5207f7f3487d211a1d6bc2215f092db1a3116ebf Mon Sep 17 00:00:00 2001 From: Andy Kwok Date: Wed, 27 Nov 2024 12:54:01 -0800 Subject: [PATCH 26/36] Update test infra Signed-off-by: Andy Kwok --- client/build.gradle | 7 ++--- .../action/IpEnrichmentRequest.java | 10 ++++--- ...ava => IpEnrichmentActionClientTests.java} | 8 ++++- ...est.java => IpEnrichmentRequestTests.java} | 30 +++++++++++++------ ...st.java => IpEnrichmentResponseTests.java} | 2 +- 5 files changed, 38 insertions(+), 19 deletions(-) rename client/src/test/java/org/opensearch/geospatial/action/{IpEnrichmentActionClientTest.java => IpEnrichmentActionClientTests.java} (86%) rename client/src/test/java/org/opensearch/geospatial/action/{IpEnrichmentRequestTest.java => IpEnrichmentRequestTests.java} (63%) rename client/src/test/java/org/opensearch/geospatial/action/{IpEnrichmentResponseTest.java => IpEnrichmentResponseTests.java} (94%) diff --git a/client/build.gradle b/client/build.gradle index 3b00b093..464dc0e5 100644 --- a/client/build.gradle +++ b/client/build.gradle @@ -32,6 +32,9 @@ dependencies { testImplementation "junit:junit:${versions.junit}" testImplementation "org.mockito:mockito-core:${versions.mockito}" + testImplementation "org.hamcrest:hamcrest:${versions.hamcrest}" + testImplementation "net.bytebuddy:byte-buddy:${versions.bytebuddy}" + testImplementation "net.bytebuddy:byte-buddy-agent:${versions.bytebuddy}" testImplementation "${group}:opensearch:${opensearch_version}" } @@ -59,7 +62,3 @@ publishing { mavenLocal() } } - -test { - useJUnitPlatform() -} \ No newline at end of file diff --git a/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentRequest.java b/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentRequest.java index ed8a8f94..4ad92392 100644 --- a/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentRequest.java +++ b/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentRequest.java @@ -52,12 +52,14 @@ public IpEnrichmentRequest(StreamInput streamInput) throws IOException { */ @Override public ActionRequestValidationException validate() { - ActionRequestValidationException errors = null; + ActionRequestValidationException errors = new ActionRequestValidationException(); if (ipString == null) { - errors = new ActionRequestValidationException(); errors.addValidationError("ip string should not be null"); } - return errors; + if (datasourceName == null) { + errors.addValidationError("DateSource should not be null"); + } + return errors.validationErrors().isEmpty() ? null : errors; } /** @@ -69,7 +71,7 @@ public ActionRequestValidationException validate() { public void writeTo(StreamOutput out) throws IOException { super.writeTo(out); out.writeString(ipString); - out.writeOptionalString(datasourceName); + out.writeString(datasourceName); } /** diff --git a/client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentActionClientTest.java b/client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentActionClientTests.java similarity index 86% rename from client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentActionClientTest.java rename to client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentActionClientTests.java index 458b9f34..73016c18 100644 --- a/client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentActionClientTest.java +++ b/client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentActionClientTests.java @@ -13,15 +13,21 @@ import java.util.concurrent.ExecutionException; import org.junit.Assert; +import org.junit.Before; import org.junit.Test; +import org.junit.runner.RunWith; import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.mockito.junit.MockitoJUnitRunner; +import org.opensearch.action.support.PlainActionFuture; import org.opensearch.client.node.NodeClient; import org.opensearch.common.action.ActionFuture; import org.opensearch.core.action.ActionResponse; import lombok.SneakyThrows; -public class IpEnrichmentActionClientTest { +@RunWith(MockitoJUnitRunner.class) +public class IpEnrichmentActionClientTests { @Mock private NodeClient mockNodeClient; diff --git a/client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentRequestTest.java b/client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentRequestTests.java similarity index 63% rename from client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentRequestTest.java rename to client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentRequestTests.java index 5a2486f2..63740f9a 100644 --- a/client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentRequestTest.java +++ b/client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentRequestTests.java @@ -11,40 +11,52 @@ /** * Test cases for IpEnrichmentRequest. */ -public class IpEnrichmentRequestTest { +public class IpEnrichmentRequestTests { /** * Test validate() against a valid record. */ @Test public void testValidateValidRequest() { + System.out.println("Test"); IpEnrichmentRequest request = new IpEnrichmentRequest("192.168.1.1", "ValidDataSourceName"); Assert.assertNull(request.validate()); } /** - * Test validate() against a valid record, - * no error expected, because dataSourceName is optional. + * Test validate() against an invalid record, + * Expecting an error being thrown as dataSource being null. */ @Test public void testValidateNullDataSourceName() { IpEnrichmentRequest request = new IpEnrichmentRequest("192.168.1.1", null); - Assert.assertNull(request.validate()); + Assert.assertEquals(1, request.validate().validationErrors().size()); } + /** - * Test validate() against a valid record, - * no error expected, because dataSourceName is optional. + * Test validate() against an invalid record, + * Expecting an error being thrown as ipString being null. + */ + @Test + public void testValidateNullIpString() { + IpEnrichmentRequest request = new IpEnrichmentRequest(null, "dataSource"); + Assert.assertEquals(1, request.validate().validationErrors().size()); + } + + + /** + * Test validate() against an invalid record, + * Expecting an error with size in 2, because both fields are null. */ @Test public void testValidateNullIpStringAndDataSourceName() { IpEnrichmentRequest request = new IpEnrichmentRequest(null, null); - Assert.assertEquals(1, request.validate().validationErrors().size()); + Assert.assertEquals(2, request.validate().validationErrors().size()); } /** - * Test validate() against a valid record, - * no error expected, because dataSourceName is optional. + * Test fromActionRequest( ) to make sure the serialisation works. */ @Test public void testFromActionRequestOnValidRecord() { diff --git a/client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentResponseTest.java b/client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentResponseTests.java similarity index 94% rename from client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentResponseTest.java rename to client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentResponseTests.java index 6bb88c9b..103b37af 100644 --- a/client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentResponseTest.java +++ b/client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentResponseTests.java @@ -10,7 +10,7 @@ import org.junit.Assert; import org.junit.Test; -public class IpEnrichmentResponseTest { +public class IpEnrichmentResponseTests { /** * To simulate when Response class being passed from one plugin to the other. From 517bffbbe71ca2d47064c2aa8c078f455432a50d Mon Sep 17 00:00:00 2001 From: Andy Kwok Date: Wed, 27 Nov 2024 14:21:44 -0800 Subject: [PATCH 27/36] Remove unused test-cases Signed-off-by: Andy Kwok --- .../action/IpEnrichmentTransportActionTests.java | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/src/test/java/org/opensearch/geospatial/ip2geo/action/IpEnrichmentTransportActionTests.java b/src/test/java/org/opensearch/geospatial/ip2geo/action/IpEnrichmentTransportActionTests.java index 641bd9c2..9605377b 100644 --- a/src/test/java/org/opensearch/geospatial/ip2geo/action/IpEnrichmentTransportActionTests.java +++ b/src/test/java/org/opensearch/geospatial/ip2geo/action/IpEnrichmentTransportActionTests.java @@ -53,17 +53,5 @@ public void testDoExecute_All_Succeed() { verify(listener, times(1)).onResponse(any(IpEnrichmentResponse.class)); } - - - /** - * No alternative dataSource, exception being thrown to indicate this. - */ - @Test - public void testDoExecute_WithNoAlternativeDataSource() { - IpEnrichmentRequest request = new IpEnrichmentRequest("192.168.1.1", null); - action.doExecute(task, request, listener); - - verify(listener, times(1)).onFailure(any(IllegalArgumentException.class)); - } } From 4da99e3d11db973b6446f1f25f19e0a98da151e1 Mon Sep 17 00:00:00 2001 From: Andy Kwok Date: Wed, 27 Nov 2024 14:50:26 -0800 Subject: [PATCH 28/36] Add lombok config Signed-off-by: Andy Kwok --- client/lombok.config | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 client/lombok.config diff --git a/client/lombok.config b/client/lombok.config new file mode 100644 index 00000000..9745d1ed --- /dev/null +++ b/client/lombok.config @@ -0,0 +1,5 @@ +# tell lombok this is your root directory +config.stopBubbling = true +# add @lombok.Generated annotations to all generated nodes where possible +# to skip code coverage for auto generated code +lombok.addLombokGeneratedAnnotation = true From dab68edb7172cf43097f6621f2bafb85f62815a7 Mon Sep 17 00:00:00 2001 From: Andy Kwok Date: Wed, 27 Nov 2024 15:40:57 -0800 Subject: [PATCH 29/36] Update test Signed-off-by: Andy Kwok --- .../geospatial/action/IpEnrichmentActionClientTests.java | 3 --- .../geospatial/action/IpEnrichmentRequestTests.java | 2 -- .../ip2geo/action/IpEnrichmentTransportActionTests.java | 9 +-------- 3 files changed, 1 insertion(+), 13 deletions(-) diff --git a/client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentActionClientTests.java b/client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentActionClientTests.java index 73016c18..e91f7aa3 100644 --- a/client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentActionClientTests.java +++ b/client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentActionClientTests.java @@ -13,13 +13,10 @@ import java.util.concurrent.ExecutionException; import org.junit.Assert; -import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; -import org.mockito.MockitoAnnotations; import org.mockito.junit.MockitoJUnitRunner; -import org.opensearch.action.support.PlainActionFuture; import org.opensearch.client.node.NodeClient; import org.opensearch.common.action.ActionFuture; import org.opensearch.core.action.ActionResponse; diff --git a/client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentRequestTests.java b/client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentRequestTests.java index 63740f9a..af815ca4 100644 --- a/client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentRequestTests.java +++ b/client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentRequestTests.java @@ -33,7 +33,6 @@ public void testValidateNullDataSourceName() { Assert.assertEquals(1, request.validate().validationErrors().size()); } - /** * Test validate() against an invalid record, * Expecting an error being thrown as ipString being null. @@ -44,7 +43,6 @@ public void testValidateNullIpString() { Assert.assertEquals(1, request.validate().validationErrors().size()); } - /** * Test validate() against an invalid record, * Expecting an error with size in 2, because both fields are null. diff --git a/src/test/java/org/opensearch/geospatial/ip2geo/action/IpEnrichmentTransportActionTests.java b/src/test/java/org/opensearch/geospatial/ip2geo/action/IpEnrichmentTransportActionTests.java index 9605377b..ca431f5a 100644 --- a/src/test/java/org/opensearch/geospatial/ip2geo/action/IpEnrichmentTransportActionTests.java +++ b/src/test/java/org/opensearch/geospatial/ip2geo/action/IpEnrichmentTransportActionTests.java @@ -6,16 +6,10 @@ package org.opensearch.geospatial.ip2geo.action; import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -import java.util.Collections; -import java.util.List; import org.junit.Before; -import org.junit.Test; import org.mockito.Mock; import org.opensearch.core.action.ActionListener; import org.opensearch.core.action.ActionResponse; @@ -46,8 +40,7 @@ public void init() { /** * When dataSource is provided. */ - @Test - public void testDoExecute_All_Succeed() { + public void testDoExecuteAllSucceed() { IpEnrichmentRequest request = new IpEnrichmentRequest("192.168.1.1", "testSource"); action.doExecute(task, request, listener); From 0ef987a2b387b2a7e7a99b9133b86e2e302557fa Mon Sep 17 00:00:00 2001 From: Andy Kwok Date: Wed, 27 Nov 2024 16:00:48 -0800 Subject: [PATCH 30/36] Update changelog Signed-off-by: Andy Kwok --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8d9c48da..48527ae0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ See the [CONTRIBUTING guide](./CONTRIBUTING.md#Changelog) for instructions on ho ## [Unreleased 2.x](https://github.com/opensearch-project/geospatial/compare/2.17...2.x) ### Features +- Introduce new Java artifact geospatial-client to facilitate cross plugin communication. ### Enhancements ### Bug Fixes ### Infrastructure From c17b81877104b1f284074ccbcc8b280e1bbf2435 Mon Sep 17 00:00:00 2001 From: Andy Kwok Date: Wed, 27 Nov 2024 17:59:23 -0800 Subject: [PATCH 31/36] Update gradle Signed-off-by: Andy Kwok --- client/build.gradle | 40 ++++++++++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/client/build.gradle b/client/build.gradle index 464dc0e5..f8c8b446 100644 --- a/client/build.gradle +++ b/client/build.gradle @@ -7,10 +7,17 @@ apply plugin: 'opensearch.build' apply plugin: 'io.freefair.lombok' apply plugin: "com.diffplug.spotless" -allprojects { - group = opensearch_group - version = "${opensearch_build}" -} + +group = opensearch_group +version = "${opensearch_build}" +description = 'OpenSearch Geospatial client' + +project.dependencyLicenses.enabled = false +project.thirdPartyAudit.enabled = false +project.loggerUsageCheck.enabled = false +project.forbiddenApis.ignoreFailures = true +project.testingConventions.enabled = false +project.forbiddenApisTest.enabled = false repositories { mavenLocal() @@ -35,7 +42,6 @@ dependencies { testImplementation "org.hamcrest:hamcrest:${versions.hamcrest}" testImplementation "net.bytebuddy:byte-buddy:${versions.bytebuddy}" testImplementation "net.bytebuddy:byte-buddy-agent:${versions.bytebuddy}" - testImplementation "${group}:opensearch:${opensearch_version}" } @@ -54,11 +60,25 @@ spotless { publishing { publications { - mavenJava(MavenPublication) { - from components.java + pluginZip(MavenPublication) { publication -> + pom { + name = "opensearch-geospatial-client" + description = 'OpenSearch Geospatial client' + licenses { + license { + name = "The Apache License, Version 2.0" + url = "http://www.apache.org/licenses/LICENSE-2.0.txt" + } + } + developers { + developer { + name = "OpenSearch" + url = "https://github.com/opensearch-project/geospatial/libs/h3" + } + } + } } } - repositories { - mavenLocal() - } } + + From b043c7cc2c83bb0213cfd42cdb04c1d41b9b4a75 Mon Sep 17 00:00:00 2001 From: Andy Kwok Date: Wed, 27 Nov 2024 18:03:31 -0800 Subject: [PATCH 32/36] Update Gradle build Signed-off-by: Andy Kwok --- client/build.gradle | 4 ---- 1 file changed, 4 deletions(-) diff --git a/client/build.gradle b/client/build.gradle index f8c8b446..ce786ab4 100644 --- a/client/build.gradle +++ b/client/build.gradle @@ -12,10 +12,7 @@ group = opensearch_group version = "${opensearch_build}" description = 'OpenSearch Geospatial client' -project.dependencyLicenses.enabled = false -project.thirdPartyAudit.enabled = false project.loggerUsageCheck.enabled = false -project.forbiddenApis.ignoreFailures = true project.testingConventions.enabled = false project.forbiddenApisTest.enabled = false @@ -42,7 +39,6 @@ dependencies { testImplementation "org.hamcrest:hamcrest:${versions.hamcrest}" testImplementation "net.bytebuddy:byte-buddy:${versions.bytebuddy}" testImplementation "net.bytebuddy:byte-buddy-agent:${versions.bytebuddy}" - } licenseFile = "LICENSE.txt" From 507c6f189b64ccc3502149e908b1c6112a5730a3 Mon Sep 17 00:00:00 2001 From: Andy Kwok Date: Thu, 28 Nov 2024 12:46:12 -0800 Subject: [PATCH 33/36] Update artifact info Signed-off-by: Andy Kwok --- client/build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/build.gradle b/client/build.gradle index ce786ab4..582c6640 100644 --- a/client/build.gradle +++ b/client/build.gradle @@ -69,7 +69,7 @@ publishing { developers { developer { name = "OpenSearch" - url = "https://github.com/opensearch-project/geospatial/libs/h3" + url = "https://github.com/opensearch-project/geospatial" } } } From 92d352b4bda8a60d58007db4d33ba0679d4523f1 Mon Sep 17 00:00:00 2001 From: Andy Kwok Date: Thu, 28 Nov 2024 13:50:26 -0800 Subject: [PATCH 34/36] Minimise diff Signed-off-by: Andy Kwok --- .../ip2geo/action/IpEnrichmentTransportActionTests.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/test/java/org/opensearch/geospatial/ip2geo/action/IpEnrichmentTransportActionTests.java b/src/test/java/org/opensearch/geospatial/ip2geo/action/IpEnrichmentTransportActionTests.java index ca431f5a..98391a05 100644 --- a/src/test/java/org/opensearch/geospatial/ip2geo/action/IpEnrichmentTransportActionTests.java +++ b/src/test/java/org/opensearch/geospatial/ip2geo/action/IpEnrichmentTransportActionTests.java @@ -29,9 +29,6 @@ public class IpEnrichmentTransportActionTests extends Ip2GeoTestCase { @Mock ActionListener listener; - @Mock - Datasource mockDataSource; - @Before public void init() { action = new IpEnrichmentTransportAction(transportService, actionFilters, ip2GeoCachedDao); From a00ca45918e32496d9e34161de25959df66168a3 Mon Sep 17 00:00:00 2001 From: Andy Kwok Date: Fri, 29 Nov 2024 16:00:37 -0800 Subject: [PATCH 35/36] Style fix Signed-off-by: Andy Kwok --- .../ip2geo/action/IpEnrichmentTransportActionTests.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/test/java/org/opensearch/geospatial/ip2geo/action/IpEnrichmentTransportActionTests.java b/src/test/java/org/opensearch/geospatial/ip2geo/action/IpEnrichmentTransportActionTests.java index 98391a05..85745b11 100644 --- a/src/test/java/org/opensearch/geospatial/ip2geo/action/IpEnrichmentTransportActionTests.java +++ b/src/test/java/org/opensearch/geospatial/ip2geo/action/IpEnrichmentTransportActionTests.java @@ -16,7 +16,6 @@ import org.opensearch.geospatial.action.IpEnrichmentRequest; import org.opensearch.geospatial.action.IpEnrichmentResponse; import org.opensearch.geospatial.ip2geo.Ip2GeoTestCase; -import org.opensearch.geospatial.ip2geo.jobscheduler.Datasource; import org.opensearch.tasks.Task; public class IpEnrichmentTransportActionTests extends Ip2GeoTestCase { From 2a1b2e791fc471b7cef59dc3e764d06f1794d5c7 Mon Sep 17 00:00:00 2001 From: Andy Kwok Date: Mon, 2 Dec 2024 11:39:30 -0800 Subject: [PATCH 36/36] Code comments Signed-off-by: Andy Kwok --- CHANGELOG.md | 2 +- .../action/IpEnrichmentTransportAction.java | 15 +++++---------- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 48527ae0..f9674cdf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,7 +17,7 @@ See the [CONTRIBUTING guide](./CONTRIBUTING.md#Changelog) for instructions on ho ## [Unreleased 2.x](https://github.com/opensearch-project/geospatial/compare/2.17...2.x) ### Features -- Introduce new Java artifact geospatial-client to facilitate cross plugin communication. +- Introduce new Java artifact geospatial-client to facilitate cross plugin communication. ([#700](https://github.com/opensearch-project/geospatial/pull/700)) ### Enhancements ### Bug Fixes ### Infrastructure diff --git a/src/main/java/org/opensearch/geospatial/ip2geo/action/IpEnrichmentTransportAction.java b/src/main/java/org/opensearch/geospatial/ip2geo/action/IpEnrichmentTransportAction.java index 3719fb93..5932835c 100644 --- a/src/main/java/org/opensearch/geospatial/ip2geo/action/IpEnrichmentTransportAction.java +++ b/src/main/java/org/opensearch/geospatial/ip2geo/action/IpEnrichmentTransportAction.java @@ -53,15 +53,10 @@ public IpEnrichmentTransportAction(TransportService transportService, ActionFilt protected void doExecute(Task task, ActionRequest request, ActionListener listener) { IpEnrichmentRequest enrichmentRequest = IpEnrichmentRequest.fromActionRequest(request); String ipString = enrichmentRequest.getIpString(); - if (enrichmentRequest.getDatasourceName() == null) { - log.error("No data source available, IpEnrichmentTransportAction aborted."); - listener.onFailure(new IllegalArgumentException()); - } else { - String dataSourceName = enrichmentRequest.getDatasourceName(); - String indexName = ip2GeoCachedDao.getIndexName(dataSourceName); - Map geoLocationData = ip2GeoCachedDao.getGeoData(indexName, ipString); - log.debug("GeoSpatial IP lookup on IP: [{}], and result [{}]", ipString, geoLocationData); - listener.onResponse(new IpEnrichmentResponse(geoLocationData)); - } + String dataSourceName = enrichmentRequest.getDatasourceName(); + String indexName = ip2GeoCachedDao.getIndexName(dataSourceName); + Map geoLocationData = ip2GeoCachedDao.getGeoData(indexName, ipString); + log.debug("GeoSpatial IP lookup on IP: [{}], and result [{}]", ipString, geoLocationData); + listener.onResponse(new IpEnrichmentResponse(geoLocationData)); } }