aboutsummaryrefslogtreecommitdiffstats
path: root/policy-utils/src/main/java
diff options
context:
space:
mode:
authoradheli.tavares <adheli.tavares@est.tech>2024-07-24 14:52:55 +0100
committeradheli.tavares <adheli.tavares@est.tech>2024-07-24 14:53:10 +0100
commit4ec22933a69bcf5d2031d3c76fb32d1c50e0d7e1 (patch)
treec2207853a1eff88fd328de58d43b96485cd70174 /policy-utils/src/main/java
parent9133dabc8566458899d39a7223fcc9c43788febd (diff)
Isolating the usage of json-schema validator to drools-pdp
- only drools-pdp have this validation, so moving the class and its dependencies to there to avoid loading dependencies where it's not used. Issue-ID: POLICY-5084 Change-Id: I782e926aac7576c34401c9fef3cfcd3a98350a7f Signed-off-by: adheli.tavares <adheli.tavares@est.tech>
Diffstat (limited to 'policy-utils/src/main/java')
-rw-r--r--policy-utils/src/main/java/org/onap/policy/drools/exception/CoderRuntimeException.java32
-rw-r--r--policy-utils/src/main/java/org/onap/policy/drools/policies/DomainMaker.java1
-rw-r--r--policy-utils/src/main/java/org/onap/policy/drools/policies/StandardValCoder.java142
3 files changed, 174 insertions, 1 deletions
diff --git a/policy-utils/src/main/java/org/onap/policy/drools/exception/CoderRuntimeException.java b/policy-utils/src/main/java/org/onap/policy/drools/exception/CoderRuntimeException.java
new file mode 100644
index 00000000..f98c12ba
--- /dev/null
+++ b/policy-utils/src/main/java/org/onap/policy/drools/exception/CoderRuntimeException.java
@@ -0,0 +1,32 @@
+/*
+ * ============LICENSE_START=======================================================
+ * Modifications Copyright (C) 2024 Nordix Foundation.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.drools.exception;
+
+/**
+ * Exceptions generated by coders.
+ */
+public class CoderRuntimeException extends RuntimeException {
+
+ private static final long serialVersionUID = 1L;
+
+ public CoderRuntimeException(Throwable cause) {
+ super(cause);
+ }
+
+}
diff --git a/policy-utils/src/main/java/org/onap/policy/drools/policies/DomainMaker.java b/policy-utils/src/main/java/org/onap/policy/drools/policies/DomainMaker.java
index 506b0977..84c81cf0 100644
--- a/policy-utils/src/main/java/org/onap/policy/drools/policies/DomainMaker.java
+++ b/policy-utils/src/main/java/org/onap/policy/drools/policies/DomainMaker.java
@@ -28,7 +28,6 @@ import lombok.NonNull;
import org.apache.commons.lang3.StringUtils;
import org.onap.policy.common.utils.coder.CoderException;
import org.onap.policy.common.utils.coder.StandardCoder;
-import org.onap.policy.common.utils.coder.StandardValCoder;
import org.onap.policy.common.utils.resources.ResourceUtils;
import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
diff --git a/policy-utils/src/main/java/org/onap/policy/drools/policies/StandardValCoder.java b/policy-utils/src/main/java/org/onap/policy/drools/policies/StandardValCoder.java
new file mode 100644
index 00000000..26608f3e
--- /dev/null
+++ b/policy-utils/src/main/java/org/onap/policy/drools/policies/StandardValCoder.java
@@ -0,0 +1,142 @@
+/*--
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
+ * Modifications Copyright (C) 2024 Nordix Foundation.
+ * ================================================================================
+ * 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.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.drools.policies;
+
+import java.io.Reader;
+import java.io.StringReader;
+import java.io.StringWriter;
+import java.io.Writer;
+import lombok.NonNull;
+import lombok.ToString;
+import net.jimblackler.jsonschemafriend.GenerationException;
+import net.jimblackler.jsonschemafriend.Schema;
+import net.jimblackler.jsonschemafriend.SchemaStore;
+import net.jimblackler.jsonschemafriend.ValidationException;
+import net.jimblackler.jsonschemafriend.Validator;
+import org.onap.policy.common.utils.coder.CoderException;
+import org.onap.policy.common.utils.coder.StandardCoder;
+import org.onap.policy.drools.exception.CoderRuntimeException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Extension to the StandardCoder to support streaming validation against a Draft-07 Json schema specification.
+ * Extending specifically for drools policies - moved from common to here.
+ */
+
+@ToString
+public class StandardValCoder extends StandardCoder {
+
+ private static final Logger logger = LoggerFactory.getLogger(StandardValCoder.class);
+
+ private final Schema schema;
+
+ /**
+ * StandardCoder with validation.
+ */
+ public StandardValCoder(@NonNull String jsonSchema) {
+ try {
+ SchemaStore store = new SchemaStore();
+ this.schema = store.loadSchemaJson(jsonSchema);
+ } catch (GenerationException e) {
+ throw new CoderRuntimeException(e);
+ }
+ }
+
+ @Override
+ protected String toPrettyJson(Object object) {
+ try {
+ validate(gsonPretty.toJson(object));
+ } catch (CoderException e) {
+ throw new CoderRuntimeException(e);
+ }
+ return super.toPrettyJson(object);
+ }
+
+ @Override
+ protected String toJson(@NonNull Object object) {
+ var output = new StringWriter();
+ toJson(output, object);
+ return output.toString();
+ }
+
+ @Override
+ protected void toJson(@NonNull Writer target, @NonNull Object object) {
+ try {
+ validate(gson.toJson(object));
+ } catch (CoderException e) {
+ throw new CoderRuntimeException(e);
+ }
+ gson.toJson(object, object.getClass(), target);
+ }
+
+ @Override
+ protected <T> T fromJson(@NonNull Reader source, @NonNull Class<T> clazz) {
+ return convertFromDouble(clazz, gson.fromJson(source, clazz));
+ }
+
+ @Override
+ protected <T> T fromJson(String json, Class<T> clazz) {
+ try {
+ validate(json);
+ } catch (CoderException e) {
+ throw new CoderRuntimeException(e);
+ }
+ var reader = new StringReader(json);
+ return convertFromDouble(clazz, gson.fromJson(reader, clazz));
+ }
+
+ /**
+ * Is the json conformant?.
+ */
+ public boolean isConformant(@NonNull String json) {
+ try {
+ conformance(json);
+ return true;
+ } catch (Exception e) {
+ logger.error("JSON is not conformant to schema", e);
+ return false;
+ }
+ }
+
+ /**
+ * Check a json string for conformance against its schema definition.
+ */
+ public void conformance(@NonNull String json) throws CoderException {
+ validate(json);
+ }
+
+ private void validate(Object object) throws CoderException {
+ try {
+ final var validator = new Validator();
+ validator.validate(schema, object);
+ } catch (ValidationException exception) {
+ var error = String.format("JSON validation failed: %s", exception.getMessage());
+ logger.error(error);
+ throw new CoderException(error);
+ }
+ }
+
+ private void validate(String json) throws CoderException {
+ validate(gson.fromJson(json, Object.class));
+ }
+}