From da6aff9cb4074c07081c524dd8945797d32e767b Mon Sep 17 00:00:00 2001 From: Varun Jain Date: Wed, 30 Nov 2022 17:29:28 -0800 Subject: [PATCH 1/9] Validate Detector Action Handle request Signed-off-by: Varun Jain --- .../ad/AnomalyDetectorExtension.java | 2 +- .../ad/rest/RestValidateDetectorAction.java | 114 +++++++++++++++--- 2 files changed, 101 insertions(+), 15 deletions(-) diff --git a/src/main/java/org/opensearch/ad/AnomalyDetectorExtension.java b/src/main/java/org/opensearch/ad/AnomalyDetectorExtension.java index 633bcd381..ce3257d8b 100644 --- a/src/main/java/org/opensearch/ad/AnomalyDetectorExtension.java +++ b/src/main/java/org/opensearch/ad/AnomalyDetectorExtension.java @@ -44,7 +44,7 @@ public AnomalyDetectorExtension() { @Override public List getExtensionRestHandlers() { - return List.of(new RestCreateDetectorAction(extensionsRunner, this), new RestGetDetectorAction(), new RestValidateDetectorAction()); + return List.of(new RestCreateDetectorAction(extensionsRunner, this), new RestGetDetectorAction(), new RestValidateDetectorAction(extensionsRunner,this)); } @Override diff --git a/src/main/java/org/opensearch/ad/rest/RestValidateDetectorAction.java b/src/main/java/org/opensearch/ad/rest/RestValidateDetectorAction.java index 611333ed2..b9c765b49 100644 --- a/src/main/java/org/opensearch/ad/rest/RestValidateDetectorAction.java +++ b/src/main/java/org/opensearch/ad/rest/RestValidateDetectorAction.java @@ -1,44 +1,130 @@ package org.opensearch.ad.rest; import static org.opensearch.rest.RestRequest.Method.POST; -import static org.opensearch.rest.RestStatus.NOT_FOUND; -import static org.opensearch.rest.RestStatus.OK; -import java.util.List; +import java.io.IOException; +import java.util.*; +import java.util.stream.Collectors; +import org.apache.commons.lang3.StringUtils; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import org.opensearch.ad.AnomalyDetectorExtension; +import org.opensearch.ad.common.exception.ADValidationException; import org.opensearch.ad.constant.CommonErrorMessages; +import org.opensearch.ad.model.AnomalyDetector; +import org.opensearch.ad.model.DetectorValidationIssue; +import org.opensearch.ad.model.ValidationAspect; +import org.opensearch.ad.settings.AnomalyDetectorSettings; import org.opensearch.ad.settings.EnabledSetting; +import org.opensearch.ad.transport.ValidateAnomalyDetectorAction; +import org.opensearch.ad.transport.ValidateAnomalyDetectorRequest; +import org.opensearch.client.opensearch.OpenSearchClient; +import org.opensearch.common.unit.TimeValue; +import org.opensearch.common.xcontent.NamedXContentRegistry; +import org.opensearch.common.xcontent.XContentBuilder; +import org.opensearch.common.xcontent.XContentParser; +import org.opensearch.common.xcontent.XContentType; import org.opensearch.extensions.rest.ExtensionRestRequest; import org.opensearch.extensions.rest.ExtensionRestResponse; import org.opensearch.rest.RestHandler.Route; import org.opensearch.rest.RestRequest.Method; +import org.opensearch.rest.action.RestToXContentListener; import org.opensearch.sdk.ExtensionRestHandler; +import org.opensearch.sdk.ExtensionsRunner; + +import static org.opensearch.common.xcontent.XContentParserUtils.ensureExpectedToken; +import static org.opensearch.ad.util.RestHandlerUtils.TYPE; +import static org.opensearch.ad.util.RestHandlerUtils.VALIDATE; +import static org.opensearch.rest.RestStatus.*; public class RestValidateDetectorAction implements ExtensionRestHandler { private final Logger logger = LogManager.getLogger(RestValidateDetectorAction.class); + private final OpenSearchClient sdkClient; + private final NamedXContentRegistry xContentRegistry; + public static final Set ALL_VALIDATION_ASPECTS_STRS = Arrays + .asList(ValidationAspect.values()) + .stream() + .map(aspect -> aspect.getName()) + .collect(Collectors.toSet()); + public RestValidateDetectorAction(ExtensionsRunner runner, AnomalyDetectorExtension extension){ + this.xContentRegistry = runner.getNamedXContentRegistry().getRegistry(); + this.sdkClient = extension.getClient(); + } @Override public List routes() { return List.of(new Route(POST, "/detectors/_validate"), new Route(POST, "/detectors/_validate/{type}")); } @Override - public ExtensionRestResponse handleRequest(ExtensionRestRequest request) { + public ExtensionRestResponse handleRequest(ExtensionRestRequest request){ if (!EnabledSetting.isADPluginEnabled()) { throw new IllegalStateException(CommonErrorMessages.DISABLED_ERR_MSG); } - Method method = request.method(); - - if (!Method.POST.equals(method)) { - return new ExtensionRestResponse( - request, - NOT_FOUND, - "Extension REST action improperly configured to handle " + request.toString() - ); + AnomalyDetector detector; + XContentParser parser; + XContentBuilder builder = null; + ValidateAnomalyDetectorRequest validateAnomalyDetectorRequest; + try { + parser= request.contentParser(this.xContentRegistry); + ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser); + String typesStr = validateTypeString(request); + DetectorValidationIssue issue=null; + try{ + detector= AnomalyDetector.parse(parser); + //TODO Dependent on https://github.com/opensearch-project/opensearch-sdk-java/issues/211 +// validateAnomalyDetectorRequest= new ValidateAnomalyDetectorRequest( +// detector, +// typesStr, +// extension, +// maxMultiEntityDetectors, +// maxAnomalyFeatures, +// requestTimeout +// ); + }catch (Exception e){ + if(e instanceof ADValidationException){ + ADValidationException ADException = (ADValidationException) e; + issue = new DetectorValidationIssue( + ADException.getAspect(), + ADException.getType(), + ADException.getMessage() + ); + } + } + + try { + builder = XContentBuilder.builder(XContentType.JSON.xContent()); + builder.startObject(); + builder.field("issue", issue); + builder.endObject(); + }catch (IOException e){ + e.printStackTrace(); + } + + + + }catch (Exception e){ + return new ExtensionRestResponse(request, BAD_REQUEST, builder); } - // do things with request - return new ExtensionRestResponse(request, OK, "placeholder"); + return new ExtensionRestResponse(request, OK, builder); } + + private Boolean validationTypesAreAccepted(String validationType) { + Set typesInRequest = new HashSet<>(Arrays.asList(validationType.split(","))); + return (!Collections.disjoint(typesInRequest, ALL_VALIDATION_ASPECTS_STRS)); + } + + private String validateTypeString(ExtensionRestRequest request){ + String typesStr = request.param(TYPE); + + // if type param isn't blank and isn't a part of possible validation types throws exception + if (!StringUtils.isBlank(typesStr)) { + if (!validationTypesAreAccepted(typesStr)) { + throw new IllegalStateException(CommonErrorMessages.NOT_EXISTENT_VALIDATION_TYPE); + } + } + return typesStr; + } + } From e035dd847e77f42c039e6823697e8e8f85bd0df4 Mon Sep 17 00:00:00 2001 From: Varun Jain Date: Thu, 1 Dec 2022 12:32:32 -0800 Subject: [PATCH 2/9] Validate Detector Action Handle request Signed-off-by: Varun Jain --- .../ad/rest/RestValidateDetectorAction.java | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/opensearch/ad/rest/RestValidateDetectorAction.java b/src/main/java/org/opensearch/ad/rest/RestValidateDetectorAction.java index b9c765b49..27c66246c 100644 --- a/src/main/java/org/opensearch/ad/rest/RestValidateDetectorAction.java +++ b/src/main/java/org/opensearch/ad/rest/RestValidateDetectorAction.java @@ -3,8 +3,12 @@ import static org.opensearch.rest.RestRequest.Method.POST; import java.io.IOException; -import java.util.*; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Set; import java.util.stream.Collectors; +import java.util.Collections; import org.apache.commons.lang3.StringUtils; import org.apache.logging.log4j.LogManager; @@ -29,6 +33,7 @@ import org.opensearch.extensions.rest.ExtensionRestResponse; import org.opensearch.rest.RestHandler.Route; import org.opensearch.rest.RestRequest.Method; +import org.opensearch.rest.RestStatus; import org.opensearch.rest.action.RestToXContentListener; import org.opensearch.sdk.ExtensionRestHandler; import org.opensearch.sdk.ExtensionsRunner; @@ -36,7 +41,6 @@ import static org.opensearch.common.xcontent.XContentParserUtils.ensureExpectedToken; import static org.opensearch.ad.util.RestHandlerUtils.TYPE; import static org.opensearch.ad.util.RestHandlerUtils.VALIDATE; -import static org.opensearch.rest.RestStatus.*; public class RestValidateDetectorAction implements ExtensionRestHandler { private final Logger logger = LogManager.getLogger(RestValidateDetectorAction.class); @@ -48,6 +52,7 @@ public class RestValidateDetectorAction implements ExtensionRestHandler { .stream() .map(aspect -> aspect.getName()) .collect(Collectors.toSet()); + public RestValidateDetectorAction(ExtensionsRunner runner, AnomalyDetectorExtension extension){ this.xContentRegistry = runner.getNamedXContentRegistry().getRegistry(); this.sdkClient = extension.getClient(); @@ -59,6 +64,15 @@ public List routes() { @Override public ExtensionRestResponse handleRequest(ExtensionRestRequest request){ + Method method = request.method(); + + if (!Method.POST.equals(method)) { + return new ExtensionRestResponse( + request, + RestStatus.NOT_FOUND, + "Extension REST action improperly configured to handle " + request.toString() + ); + } if (!EnabledSetting.isADPluginEnabled()) { throw new IllegalStateException(CommonErrorMessages.DISABLED_ERR_MSG); } @@ -105,9 +119,9 @@ public ExtensionRestResponse handleRequest(ExtensionRestRequest request){ }catch (Exception e){ - return new ExtensionRestResponse(request, BAD_REQUEST, builder); + return new ExtensionRestResponse(request, RestStatus.BAD_REQUEST, builder); } - return new ExtensionRestResponse(request, OK, builder); + return new ExtensionRestResponse(request, RestStatus.OK, "placeholder"); } private Boolean validationTypesAreAccepted(String validationType) { From a4dece03a2a9b862720573c3083db43c3230592b Mon Sep 17 00:00:00 2001 From: Varun Jain Date: Fri, 2 Dec 2022 10:05:21 -0800 Subject: [PATCH 3/9] Validate Detector Action Handle request Signed-off-by: Varun Jain --- .../ad/rest/RestValidateDetectorAction.java | 20 +++++++------------ 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/src/main/java/org/opensearch/ad/rest/RestValidateDetectorAction.java b/src/main/java/org/opensearch/ad/rest/RestValidateDetectorAction.java index 27c66246c..0e4a2e501 100644 --- a/src/main/java/org/opensearch/ad/rest/RestValidateDetectorAction.java +++ b/src/main/java/org/opensearch/ad/rest/RestValidateDetectorAction.java @@ -35,14 +35,16 @@ import org.opensearch.rest.RestRequest.Method; import org.opensearch.rest.RestStatus; import org.opensearch.rest.action.RestToXContentListener; +import org.opensearch.sdk.BaseExtensionRestHandler; import org.opensearch.sdk.ExtensionRestHandler; import org.opensearch.sdk.ExtensionsRunner; +import org.opensearch.sdk.RouteHandler; import static org.opensearch.common.xcontent.XContentParserUtils.ensureExpectedToken; import static org.opensearch.ad.util.RestHandlerUtils.TYPE; import static org.opensearch.ad.util.RestHandlerUtils.VALIDATE; -public class RestValidateDetectorAction implements ExtensionRestHandler { +public class RestValidateDetectorAction extends BaseExtensionRestHandler { private final Logger logger = LogManager.getLogger(RestValidateDetectorAction.class); private final OpenSearchClient sdkClient; private final NamedXContentRegistry xContentRegistry; @@ -57,22 +59,15 @@ public RestValidateDetectorAction(ExtensionsRunner runner, AnomalyDetectorExtens this.xContentRegistry = runner.getNamedXContentRegistry().getRegistry(); this.sdkClient = extension.getClient(); } + @Override - public List routes() { - return List.of(new Route(POST, "/detectors/_validate"), new Route(POST, "/detectors/_validate/{type}")); + protected List routeHandlers() { + //return List.of(new Route(POST, "/detectors/_validate"), new Route(POST, "/detectors/_validate/{type}")); + return List.of(new RouteHandler(POST, "/detectors/_validate",(r)->handleRequest(r))); } @Override public ExtensionRestResponse handleRequest(ExtensionRestRequest request){ - Method method = request.method(); - - if (!Method.POST.equals(method)) { - return new ExtensionRestResponse( - request, - RestStatus.NOT_FOUND, - "Extension REST action improperly configured to handle " + request.toString() - ); - } if (!EnabledSetting.isADPluginEnabled()) { throw new IllegalStateException(CommonErrorMessages.DISABLED_ERR_MSG); } @@ -87,7 +82,6 @@ public ExtensionRestResponse handleRequest(ExtensionRestRequest request){ DetectorValidationIssue issue=null; try{ detector= AnomalyDetector.parse(parser); - //TODO Dependent on https://github.com/opensearch-project/opensearch-sdk-java/issues/211 // validateAnomalyDetectorRequest= new ValidateAnomalyDetectorRequest( // detector, // typesStr, From 32b6632cd427795b8a821228fc4bbd386102dc1b Mon Sep 17 00:00:00 2001 From: Varun Jain Date: Fri, 2 Dec 2022 10:07:23 -0800 Subject: [PATCH 4/9] Validate Detector Action Handle request Signed-off-by: Varun Jain --- .../opensearch/ad/rest/RestValidateDetectorAction.java | 9 --------- 1 file changed, 9 deletions(-) diff --git a/src/main/java/org/opensearch/ad/rest/RestValidateDetectorAction.java b/src/main/java/org/opensearch/ad/rest/RestValidateDetectorAction.java index 0e4a2e501..49bfd3e9e 100644 --- a/src/main/java/org/opensearch/ad/rest/RestValidateDetectorAction.java +++ b/src/main/java/org/opensearch/ad/rest/RestValidateDetectorAction.java @@ -19,30 +19,22 @@ import org.opensearch.ad.model.AnomalyDetector; import org.opensearch.ad.model.DetectorValidationIssue; import org.opensearch.ad.model.ValidationAspect; -import org.opensearch.ad.settings.AnomalyDetectorSettings; import org.opensearch.ad.settings.EnabledSetting; -import org.opensearch.ad.transport.ValidateAnomalyDetectorAction; import org.opensearch.ad.transport.ValidateAnomalyDetectorRequest; import org.opensearch.client.opensearch.OpenSearchClient; -import org.opensearch.common.unit.TimeValue; import org.opensearch.common.xcontent.NamedXContentRegistry; import org.opensearch.common.xcontent.XContentBuilder; import org.opensearch.common.xcontent.XContentParser; import org.opensearch.common.xcontent.XContentType; import org.opensearch.extensions.rest.ExtensionRestRequest; import org.opensearch.extensions.rest.ExtensionRestResponse; -import org.opensearch.rest.RestHandler.Route; -import org.opensearch.rest.RestRequest.Method; import org.opensearch.rest.RestStatus; -import org.opensearch.rest.action.RestToXContentListener; import org.opensearch.sdk.BaseExtensionRestHandler; -import org.opensearch.sdk.ExtensionRestHandler; import org.opensearch.sdk.ExtensionsRunner; import org.opensearch.sdk.RouteHandler; import static org.opensearch.common.xcontent.XContentParserUtils.ensureExpectedToken; import static org.opensearch.ad.util.RestHandlerUtils.TYPE; -import static org.opensearch.ad.util.RestHandlerUtils.VALIDATE; public class RestValidateDetectorAction extends BaseExtensionRestHandler { private final Logger logger = LogManager.getLogger(RestValidateDetectorAction.class); @@ -62,7 +54,6 @@ public RestValidateDetectorAction(ExtensionsRunner runner, AnomalyDetectorExtens @Override protected List routeHandlers() { - //return List.of(new Route(POST, "/detectors/_validate"), new Route(POST, "/detectors/_validate/{type}")); return List.of(new RouteHandler(POST, "/detectors/_validate",(r)->handleRequest(r))); } From be0bf5e2568b9e2b02d9187829cd5f918264841c Mon Sep 17 00:00:00 2001 From: Varun Jain Date: Fri, 2 Dec 2022 12:08:17 -0800 Subject: [PATCH 5/9] Validate Detector Action Handle request Signed-off-by: Varun Jain --- .../ad/AnomalyDetectorExtension.java | 7 ++- .../ad/rest/RestValidateDetectorAction.java | 63 +++++++++---------- 2 files changed, 34 insertions(+), 36 deletions(-) diff --git a/src/main/java/org/opensearch/ad/AnomalyDetectorExtension.java b/src/main/java/org/opensearch/ad/AnomalyDetectorExtension.java index ce3257d8b..9312459ea 100644 --- a/src/main/java/org/opensearch/ad/AnomalyDetectorExtension.java +++ b/src/main/java/org/opensearch/ad/AnomalyDetectorExtension.java @@ -44,7 +44,12 @@ public AnomalyDetectorExtension() { @Override public List getExtensionRestHandlers() { - return List.of(new RestCreateDetectorAction(extensionsRunner, this), new RestGetDetectorAction(), new RestValidateDetectorAction(extensionsRunner,this)); + return List + .of( + new RestCreateDetectorAction(extensionsRunner, this), + new RestGetDetectorAction(), + new RestValidateDetectorAction(extensionsRunner, this) + ); } @Override diff --git a/src/main/java/org/opensearch/ad/rest/RestValidateDetectorAction.java b/src/main/java/org/opensearch/ad/rest/RestValidateDetectorAction.java index 49bfd3e9e..67044e839 100644 --- a/src/main/java/org/opensearch/ad/rest/RestValidateDetectorAction.java +++ b/src/main/java/org/opensearch/ad/rest/RestValidateDetectorAction.java @@ -1,14 +1,16 @@ package org.opensearch.ad.rest; +import static org.opensearch.ad.util.RestHandlerUtils.TYPE; +import static org.opensearch.common.xcontent.XContentParserUtils.ensureExpectedToken; import static org.opensearch.rest.RestRequest.Method.POST; import java.io.IOException; import java.util.Arrays; +import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.stream.Collectors; -import java.util.Collections; import org.apache.commons.lang3.StringUtils; import org.apache.logging.log4j.LogManager; @@ -33,32 +35,29 @@ import org.opensearch.sdk.ExtensionsRunner; import org.opensearch.sdk.RouteHandler; -import static org.opensearch.common.xcontent.XContentParserUtils.ensureExpectedToken; -import static org.opensearch.ad.util.RestHandlerUtils.TYPE; - public class RestValidateDetectorAction extends BaseExtensionRestHandler { private final Logger logger = LogManager.getLogger(RestValidateDetectorAction.class); private final OpenSearchClient sdkClient; private final NamedXContentRegistry xContentRegistry; public static final Set ALL_VALIDATION_ASPECTS_STRS = Arrays - .asList(ValidationAspect.values()) - .stream() - .map(aspect -> aspect.getName()) - .collect(Collectors.toSet()); + .asList(ValidationAspect.values()) + .stream() + .map(aspect -> aspect.getName()) + .collect(Collectors.toSet()); - public RestValidateDetectorAction(ExtensionsRunner runner, AnomalyDetectorExtension extension){ + public RestValidateDetectorAction(ExtensionsRunner runner, AnomalyDetectorExtension extension) { this.xContentRegistry = runner.getNamedXContentRegistry().getRegistry(); this.sdkClient = extension.getClient(); } @Override protected List routeHandlers() { - return List.of(new RouteHandler(POST, "/detectors/_validate",(r)->handleRequest(r))); + return List.of(new RouteHandler(POST, "/detectors/_validate", (r) -> handleRequest(r))); } @Override - public ExtensionRestResponse handleRequest(ExtensionRestRequest request){ + public ExtensionRestResponse handleRequest(ExtensionRestRequest request) { if (!EnabledSetting.isADPluginEnabled()) { throw new IllegalStateException(CommonErrorMessages.DISABLED_ERR_MSG); } @@ -67,28 +66,24 @@ public ExtensionRestResponse handleRequest(ExtensionRestRequest request){ XContentBuilder builder = null; ValidateAnomalyDetectorRequest validateAnomalyDetectorRequest; try { - parser= request.contentParser(this.xContentRegistry); + parser = request.contentParser(this.xContentRegistry); ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser); String typesStr = validateTypeString(request); - DetectorValidationIssue issue=null; - try{ - detector= AnomalyDetector.parse(parser); -// validateAnomalyDetectorRequest= new ValidateAnomalyDetectorRequest( -// detector, -// typesStr, -// extension, -// maxMultiEntityDetectors, -// maxAnomalyFeatures, -// requestTimeout -// ); - }catch (Exception e){ - if(e instanceof ADValidationException){ + DetectorValidationIssue issue = null; + try { + detector = AnomalyDetector.parse(parser); + // validateAnomalyDetectorRequest= new ValidateAnomalyDetectorRequest( + // detector, + // typesStr, + // extension, + // maxMultiEntityDetectors, + // maxAnomalyFeatures, + // requestTimeout + // ); + } catch (Exception e) { + if (e instanceof ADValidationException) { ADValidationException ADException = (ADValidationException) e; - issue = new DetectorValidationIssue( - ADException.getAspect(), - ADException.getType(), - ADException.getMessage() - ); + issue = new DetectorValidationIssue(ADException.getAspect(), ADException.getType(), ADException.getMessage()); } } @@ -97,13 +92,11 @@ public ExtensionRestResponse handleRequest(ExtensionRestRequest request){ builder.startObject(); builder.field("issue", issue); builder.endObject(); - }catch (IOException e){ + } catch (IOException e) { e.printStackTrace(); } - - - }catch (Exception e){ + } catch (Exception e) { return new ExtensionRestResponse(request, RestStatus.BAD_REQUEST, builder); } return new ExtensionRestResponse(request, RestStatus.OK, "placeholder"); @@ -114,7 +107,7 @@ private Boolean validationTypesAreAccepted(String validationType) { return (!Collections.disjoint(typesInRequest, ALL_VALIDATION_ASPECTS_STRS)); } - private String validateTypeString(ExtensionRestRequest request){ + private String validateTypeString(ExtensionRestRequest request) { String typesStr = request.param(TYPE); // if type param isn't blank and isn't a part of possible validation types throws exception From b4c693c03a373b4dd5cb3a5bfc03be0aa90552aa Mon Sep 17 00:00:00 2001 From: Varun Jain Date: Sun, 4 Dec 2022 15:17:20 -0800 Subject: [PATCH 6/9] Validate Detector handle request Signed-off-by: Varun Jain --- .../org/opensearch/ad/model/ModelProfile.java | 4 +- .../ad/model/ModelProfileTests.java | 39 +++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 src/test/java/org/opensearch/ad/model/ModelProfileTests.java diff --git a/src/main/java/org/opensearch/ad/model/ModelProfile.java b/src/main/java/org/opensearch/ad/model/ModelProfile.java index 953c4e164..79ef58c5b 100644 --- a/src/main/java/org/opensearch/ad/model/ModelProfile.java +++ b/src/main/java/org/opensearch/ad/model/ModelProfile.java @@ -78,13 +78,15 @@ public long getModelSizeInBytes() { @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - builder.field(CommonName.MODEL_ID_KEY, modelId); + builder.startObject(); + builder.field(CommonName.MODEL_ID_KEY, "vscduiskcnl"); if (entity != null) { builder.field(CommonName.ENTITY_KEY, entity); } if (modelSizeInBytes > 0) { builder.field(CommonName.MODEL_SIZE_IN_BYTES, modelSizeInBytes); } + builder.endObject(); return builder; } diff --git a/src/test/java/org/opensearch/ad/model/ModelProfileTests.java b/src/test/java/org/opensearch/ad/model/ModelProfileTests.java new file mode 100644 index 000000000..862f42cff --- /dev/null +++ b/src/test/java/org/opensearch/ad/model/ModelProfileTests.java @@ -0,0 +1,39 @@ +package org.opensearch.ad.model; + +import static org.opensearch.common.xcontent.XContentFactory.jsonBuilder; + +import java.io.IOException; + +import org.opensearch.ad.AbstractADTest; +import org.opensearch.ad.constant.CommonName; +import org.opensearch.common.Strings; +import org.opensearch.common.xcontent.ToXContent; +import org.opensearch.common.xcontent.XContentBuilder; + +import test.org.opensearch.ad.util.JsonDeserializer; + +public class ModelProfileTests extends AbstractADTest { + + public void testToXContent() throws IOException { + ModelProfile profile1 = new ModelProfile( + randomAlphaOfLength(5), + Entity.createSingleAttributeEntity(randomAlphaOfLength(5), randomAlphaOfLength(5)), + 0 + ); + XContentBuilder builder = jsonBuilder(); + profile1.toXContent(builder, ToXContent.EMPTY_PARAMS); + String json = Strings.toString(builder); + assertTrue(JsonDeserializer.hasChildNode(json, CommonName.ENTITY_KEY)); + assertFalse(JsonDeserializer.hasChildNode(json, CommonName.MODEL_SIZE_IN_BYTES)); + + ModelProfile profile2 = new ModelProfile(randomAlphaOfLength(5), null, 1); + + builder = jsonBuilder(); + profile2.toXContent(builder, ToXContent.EMPTY_PARAMS); + json = Strings.toString(builder); + + assertFalse(JsonDeserializer.hasChildNode(json, CommonName.ENTITY_KEY)); + assertTrue(JsonDeserializer.hasChildNode(json, CommonName.MODEL_SIZE_IN_BYTES)); + + } +} From a2c196c971f083bf226f844184508fc47db181e9 Mon Sep 17 00:00:00 2001 From: Varun Jain Date: Sun, 4 Dec 2022 16:06:32 -0800 Subject: [PATCH 7/9] Validate Detector Handle Request Signed-off-by: Varun Jain --- src/main/java/org/opensearch/ad/model/ModelProfile.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/opensearch/ad/model/ModelProfile.java b/src/main/java/org/opensearch/ad/model/ModelProfile.java index 79ef58c5b..b91f4b9e3 100644 --- a/src/main/java/org/opensearch/ad/model/ModelProfile.java +++ b/src/main/java/org/opensearch/ad/model/ModelProfile.java @@ -79,7 +79,7 @@ public long getModelSizeInBytes() { @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(); - builder.field(CommonName.MODEL_ID_KEY, "vscduiskcnl"); + builder.field(CommonName.MODEL_ID_KEY, modelId); if (entity != null) { builder.field(CommonName.ENTITY_KEY, entity); } From 88771f04116fbf593b70613ebb50c8d098f6f74f Mon Sep 17 00:00:00 2001 From: Varun Jain Date: Sun, 4 Dec 2022 16:21:10 -0800 Subject: [PATCH 8/9] Validate Detector Handle Request Signed-off-by: Varun Jain --- src/main/java/org/opensearch/ad/model/ModelProfile.java | 2 -- src/test/java/org/opensearch/ad/model/ModelProfileTests.java | 4 ++++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/opensearch/ad/model/ModelProfile.java b/src/main/java/org/opensearch/ad/model/ModelProfile.java index b91f4b9e3..953c4e164 100644 --- a/src/main/java/org/opensearch/ad/model/ModelProfile.java +++ b/src/main/java/org/opensearch/ad/model/ModelProfile.java @@ -78,7 +78,6 @@ public long getModelSizeInBytes() { @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - builder.startObject(); builder.field(CommonName.MODEL_ID_KEY, modelId); if (entity != null) { builder.field(CommonName.ENTITY_KEY, entity); @@ -86,7 +85,6 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws if (modelSizeInBytes > 0) { builder.field(CommonName.MODEL_SIZE_IN_BYTES, modelSizeInBytes); } - builder.endObject(); return builder; } diff --git a/src/test/java/org/opensearch/ad/model/ModelProfileTests.java b/src/test/java/org/opensearch/ad/model/ModelProfileTests.java index 862f42cff..a1122e565 100644 --- a/src/test/java/org/opensearch/ad/model/ModelProfileTests.java +++ b/src/test/java/org/opensearch/ad/model/ModelProfileTests.java @@ -21,7 +21,9 @@ public void testToXContent() throws IOException { 0 ); XContentBuilder builder = jsonBuilder(); + builder.startObject(); profile1.toXContent(builder, ToXContent.EMPTY_PARAMS); + builder.endObject(); String json = Strings.toString(builder); assertTrue(JsonDeserializer.hasChildNode(json, CommonName.ENTITY_KEY)); assertFalse(JsonDeserializer.hasChildNode(json, CommonName.MODEL_SIZE_IN_BYTES)); @@ -29,7 +31,9 @@ public void testToXContent() throws IOException { ModelProfile profile2 = new ModelProfile(randomAlphaOfLength(5), null, 1); builder = jsonBuilder(); + builder.startObject(); profile2.toXContent(builder, ToXContent.EMPTY_PARAMS); + builder.endObject(); json = Strings.toString(builder); assertFalse(JsonDeserializer.hasChildNode(json, CommonName.ENTITY_KEY)); From cd105d9bc2e84ed388669edd99760ced39e421ac Mon Sep 17 00:00:00 2001 From: Varun Jain Date: Sun, 4 Dec 2022 17:04:48 -0800 Subject: [PATCH 9/9] Validate Detector Handle Request Signed-off-by: Varun Jain --- .../org/opensearch/ad/rest/RestValidateDetectorAction.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/opensearch/ad/rest/RestValidateDetectorAction.java b/src/main/java/org/opensearch/ad/rest/RestValidateDetectorAction.java index 67044e839..4baa74f45 100644 --- a/src/main/java/org/opensearch/ad/rest/RestValidateDetectorAction.java +++ b/src/main/java/org/opensearch/ad/rest/RestValidateDetectorAction.java @@ -53,11 +53,10 @@ public RestValidateDetectorAction(ExtensionsRunner runner, AnomalyDetectorExtens @Override protected List routeHandlers() { - return List.of(new RouteHandler(POST, "/detectors/_validate", (r) -> handleRequest(r))); + return List.of(new RouteHandler(POST, "/detectors/_validate", (r) -> handleValidateDetectorRequest(r))); } - @Override - public ExtensionRestResponse handleRequest(ExtensionRestRequest request) { + private ExtensionRestResponse handleValidateDetectorRequest(ExtensionRestRequest request) { if (!EnabledSetting.isADPluginEnabled()) { throw new IllegalStateException(CommonErrorMessages.DISABLED_ERR_MSG); }