forked from opensearch-project/ml-commons
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
connector transport actions, requests and responses (opensearch-proje…
…ct#1053) Signed-off-by: Xun Zhang <[email protected]>
- Loading branch information
1 parent
fab5c46
commit 8e819af
Showing
10 changed files
with
642 additions
and
0 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
...n/src/main/java/org/opensearch/ml/common/transport/connector/MLConnectorDeleteAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.ml.common.transport.connector; | ||
|
||
import org.opensearch.action.ActionType; | ||
import org.opensearch.action.delete.DeleteResponse; | ||
|
||
public class MLConnectorDeleteAction extends ActionType<DeleteResponse> { | ||
public static final MLConnectorDeleteAction INSTANCE = new MLConnectorDeleteAction(); | ||
public static final String NAME = "cluster:admin/opensearch/ml/connectors/delete"; | ||
|
||
private MLConnectorDeleteAction() { super(NAME, DeleteResponse::new);} | ||
} |
71 changes: 71 additions & 0 deletions
71
.../src/main/java/org/opensearch/ml/common/transport/connector/MLConnectorDeleteRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.ml.common.transport.connector; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
import org.opensearch.action.ActionRequest; | ||
import org.opensearch.action.ActionRequestValidationException; | ||
import org.opensearch.common.io.stream.InputStreamStreamInput; | ||
import org.opensearch.common.io.stream.OutputStreamStreamOutput; | ||
import org.opensearch.common.io.stream.StreamInput; | ||
import org.opensearch.common.io.stream.StreamOutput; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.UncheckedIOException; | ||
|
||
import static org.opensearch.action.ValidateActions.addValidationError; | ||
|
||
public class MLConnectorDeleteRequest extends ActionRequest { | ||
@Getter | ||
String connectorId; | ||
|
||
@Builder | ||
public MLConnectorDeleteRequest(String connectorId) { | ||
this.connectorId = connectorId; | ||
} | ||
|
||
public MLConnectorDeleteRequest(StreamInput input) throws IOException { | ||
super(input); | ||
this.connectorId = input.readString(); | ||
} | ||
|
||
@Override | ||
public void writeTo(StreamOutput output) throws IOException { | ||
super.writeTo(output); | ||
output.writeString(connectorId); | ||
} | ||
|
||
@Override | ||
public ActionRequestValidationException validate() { | ||
ActionRequestValidationException exception = null; | ||
|
||
if (this.connectorId == null) { | ||
exception = addValidationError("ML connector id can't be null", exception); | ||
} | ||
|
||
return exception; | ||
} | ||
|
||
public static MLConnectorDeleteRequest fromActionRequest(ActionRequest actionRequest) { | ||
if (actionRequest instanceof MLConnectorDeleteRequest) { | ||
return (MLConnectorDeleteRequest)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 MLConnectorDeleteRequest(input); | ||
} | ||
} catch (IOException e) { | ||
throw new UncheckedIOException("failed to parse ActionRequest into MLConnectorDeleteRequest", e); | ||
} | ||
} | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
common/src/main/java/org/opensearch/ml/common/transport/connector/MLConnectorGetAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.ml.common.transport.connector; | ||
|
||
import org.opensearch.action.ActionType; | ||
|
||
public class MLConnectorGetAction extends ActionType<MLConnectorGetResponse> { | ||
public static final MLConnectorGetAction INSTANCE = new MLConnectorGetAction(); | ||
public static final String NAME = "cluster:admin/opensearch/ml/connectors/get"; | ||
|
||
private MLConnectorGetAction() { super(NAME, MLConnectorGetResponse::new);} | ||
|
||
} |
75 changes: 75 additions & 0 deletions
75
common/src/main/java/org/opensearch/ml/common/transport/connector/MLConnectorGetRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.ml.common.transport.connector; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
import org.opensearch.action.ActionRequest; | ||
import org.opensearch.action.ActionRequestValidationException; | ||
import org.opensearch.common.io.stream.InputStreamStreamInput; | ||
import org.opensearch.common.io.stream.OutputStreamStreamOutput; | ||
import org.opensearch.common.io.stream.StreamInput; | ||
import org.opensearch.common.io.stream.StreamOutput; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.UncheckedIOException; | ||
|
||
import static org.opensearch.action.ValidateActions.addValidationError; | ||
|
||
@Getter | ||
public class MLConnectorGetRequest extends ActionRequest { | ||
|
||
String connectorId; | ||
boolean returnContent; | ||
|
||
@Builder | ||
public MLConnectorGetRequest(String connectorId, boolean returnContent) { | ||
this.connectorId = connectorId; | ||
this.returnContent = returnContent; | ||
} | ||
|
||
public MLConnectorGetRequest(StreamInput in) throws IOException { | ||
super(in); | ||
this.connectorId = in.readString(); | ||
this.returnContent = in.readBoolean(); | ||
} | ||
|
||
@Override | ||
public void writeTo(StreamOutput out) throws IOException { | ||
super.writeTo(out); | ||
out.writeString(this.connectorId); | ||
out.writeBoolean(returnContent); | ||
} | ||
|
||
@Override | ||
public ActionRequestValidationException validate() { | ||
ActionRequestValidationException exception = null; | ||
|
||
if (this.connectorId == null) { | ||
exception = addValidationError("ML connector id can't be null", exception); | ||
} | ||
|
||
return exception; | ||
} | ||
|
||
public static MLConnectorGetRequest fromActionRequest(ActionRequest actionRequest) { | ||
if (actionRequest instanceof MLConnectorGetRequest) { | ||
return (MLConnectorGetRequest) 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 MLConnectorGetRequest(input); | ||
} | ||
} catch (IOException e) { | ||
throw new UncheckedIOException("failed to parse ActionRequest into MLConnectorGetRequest", e); | ||
} | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
...on/src/main/java/org/opensearch/ml/common/transport/connector/MLConnectorGetResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.ml.common.transport.connector; | ||
|
||
import lombok.Builder; | ||
import org.opensearch.action.ActionResponse; | ||
import org.opensearch.common.io.stream.InputStreamStreamInput; | ||
import org.opensearch.common.io.stream.OutputStreamStreamOutput; | ||
import org.opensearch.common.io.stream.StreamInput; | ||
import org.opensearch.common.io.stream.StreamOutput; | ||
import org.opensearch.core.xcontent.ToXContent; | ||
import org.opensearch.core.xcontent.ToXContentObject; | ||
import org.opensearch.core.xcontent.XContentBuilder; | ||
import org.opensearch.ml.common.connector.Connector; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.UncheckedIOException; | ||
|
||
public class MLConnectorGetResponse extends ActionResponse implements ToXContentObject { | ||
Connector mlConnector; | ||
|
||
@Builder | ||
public MLConnectorGetResponse(Connector mlConnector) { | ||
this.mlConnector = mlConnector; | ||
} | ||
|
||
public MLConnectorGetResponse(StreamInput in) throws IOException { | ||
super(in); | ||
mlConnector = Connector.fromStream(in); | ||
} | ||
|
||
@Override | ||
public void writeTo(StreamOutput out) throws IOException{ | ||
mlConnector.writeTo(out); | ||
} | ||
|
||
@Override | ||
public XContentBuilder toXContent(XContentBuilder xContentBuilder, ToXContent.Params params) throws IOException { | ||
return mlConnector.toXContent(xContentBuilder, params); | ||
} | ||
|
||
public static MLConnectorGetResponse fromActionResponse(ActionResponse actionResponse) { | ||
if (actionResponse instanceof MLConnectorGetResponse) { | ||
return (MLConnectorGetResponse) actionResponse; | ||
} | ||
|
||
try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); | ||
OutputStreamStreamOutput osso = new OutputStreamStreamOutput(baos)) { | ||
actionResponse.writeTo(osso); | ||
try (StreamInput input = new InputStreamStreamInput(new ByteArrayInputStream(baos.toByteArray()))) { | ||
return new MLConnectorGetResponse(input); | ||
} | ||
} catch (IOException e) { | ||
throw new UncheckedIOException("failed to parse ActionResponse into MLConnectorGetResponse", e); | ||
} | ||
} | ||
|
||
} |
19 changes: 19 additions & 0 deletions
19
...n/src/main/java/org/opensearch/ml/common/transport/connector/MLConnectorSearchAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.ml.common.transport.connector; | ||
|
||
import org.opensearch.action.ActionType; | ||
import org.opensearch.action.search.SearchResponse; | ||
|
||
public class MLConnectorSearchAction extends ActionType<SearchResponse> { | ||
// External Action which used for public facing RestAPIs. | ||
public static final String NAME = "cluster:admin/opensearch/ml/connectors/search"; | ||
public static final MLConnectorSearchAction INSTANCE = new MLConnectorSearchAction(); | ||
|
||
private MLConnectorSearchAction() { | ||
super(NAME, SearchResponse::new); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...n/src/main/java/org/opensearch/ml/common/transport/connector/MLCreateConnectorAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.ml.common.transport.connector; | ||
|
||
import org.opensearch.action.ActionType; | ||
|
||
public class MLCreateConnectorAction extends ActionType<MLCreateConnectorResponse> { | ||
public static MLCreateConnectorAction INSTANCE = new MLCreateConnectorAction(); | ||
public static final String NAME = "cluster:admin/opensearch/ml/create_connector"; | ||
|
||
private MLCreateConnectorAction() { | ||
super(NAME, MLCreateConnectorResponse::new); | ||
} | ||
} |
Oops, something went wrong.