Skip to content

Commit

Permalink
Added Changes on SDK Side to register its Implemented Interfaces (#508)
Browse files Browse the repository at this point in the history
* added required changes from the sdk part
Signed-off-by: varuntumbe <[email protected]>

* SpotlessApply Done. Replaced Wildcard Imports.
Signed-off-by: varuntumbe <[email protected]>

* Add required javadoc

Signed-off-by: Daniel Widdis <[email protected]>

---------

Signed-off-by: Daniel Widdis <[email protected]>
Co-authored-by: Daniel Widdis <[email protected]>
  • Loading branch information
varuntumbe and dbwiddis authored Mar 7, 2023
1 parent 9ab3e2a commit 30759d2
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
31 changes: 31 additions & 0 deletions src/main/java/org/opensearch/sdk/ExtensionsRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,15 @@

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeoutException;
import java.util.function.Consumer;
import java.util.stream.Collectors;

/**
* The primary class to run an extension.
Expand Down Expand Up @@ -388,6 +392,33 @@ public void startTransportService(TransportService transportService) {

}

/**
* Returns a list of interfaces implemented by the corresponding {@link Extension}.
*
* @return A list of strings matching the interface name.
*/
public List<String> getExtensionImplementedInterfaces() {
Extension extension = getExtension();

Set<Class<?>> interfaceSet = new HashSet<>();
Class<?> extensionClass = extension.getClass();
do {
interfaceSet.addAll(Arrays.stream(extensionClass.getInterfaces()).collect(Collectors.toSet()));
extensionClass = extensionClass.getSuperclass();
} while (extensionClass != null);

List<String> interfacesOfOpenSearch = new ArrayList<String>();
// we are making an assumption here that all the other Interfaces will be in the same package ( or will be in subpackage ) in which
// ActionExtension Interface belongs.
String packageNameOfActionExtension = ActionExtension.class.getPackageName();
for (Class<?> anInterface : interfaceSet) {
if (anInterface.getPackageName().startsWith(packageNameOfActionExtension)) {
interfacesOfOpenSearch.add(anInterface.getSimpleName());
}
}
return interfacesOfOpenSearch;
}

/**
* Requests that OpenSearch register the REST Actions for this extension.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ public InitializeExtensionResponse handleExtensionInitRequest(InitializeExtensio
extensionsRunner.setUniqueId(extensionInitRequest.getExtension().getId());
// Successfully initialized. Send the response.
try {
return new InitializeExtensionResponse(extensionsRunner.getSettings().get(NODE_NAME_SETTING));
return new InitializeExtensionResponse(
extensionsRunner.getSettings().get(NODE_NAME_SETTING),
extensionsRunner.getExtensionImplementedInterfaces()
);
} finally {
// After sending successful response to initialization, send the REST API and Settings
extensionsRunner.setOpensearchNode(extensionsRunner.opensearchNode);
Expand Down
10 changes: 10 additions & 0 deletions src/test/java/org/opensearch/sdk/TestExtensionsRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -235,4 +236,13 @@ public void testGettersAndSetters() throws IOException {
assertEquals(ExtensionsRunnerForTest.NODE_HOST, settings.get(TransportSettings.BIND_HOST.getKey()));
assertEquals(ExtensionsRunnerForTest.NODE_PORT, settings.get(TransportSettings.PORT.getKey()));
}

@Test
public void testGetExtensionImplementedInterfaces() {
List<String> implementedInterfaces = extensionsRunner.getExtensionImplementedInterfaces();
assertTrue(!implementedInterfaces.isEmpty());
assertTrue(implementedInterfaces.contains("Extension"));
assertTrue(implementedInterfaces.contains("ActionExtension"));
}

}

0 comments on commit 30759d2

Please sign in to comment.