From 59b90519eda3b95b0cfc49b6349a591c7d0c78e2 Mon Sep 17 00:00:00 2001 From: liamfallon Date: Mon, 7 Feb 2022 17:56:44 +0000 Subject: Remove JAXB and XML, use GSON for JSON This review converst apex-pdp to use GSON for JSON handling. In order to preserve backward compatibility with the JAXB format of JSON, custom handling of maps was required. Therefore, the policy-common StandardCoder could not be used. There are a lot of small changes, removing annotations from concepts and tweaking of test data. However, this cleans up the code base so it is worth doing. Issue-ID: POLICY-1820 Change-Id: I213fa64f6d7f3f1df8d10f111d9fbedbe80f9fe0 Signed-off-by: liamfallon --- .../service/engine/runtime/impl/EngineWorker.java | 3 +- .../service/parameters/ApexParameterHandler.java | 58 +++++++++++++++------- 2 files changed, 42 insertions(+), 19 deletions(-) (limited to 'services/services-engine/src/main') diff --git a/services/services-engine/src/main/java/org/onap/policy/apex/service/engine/runtime/impl/EngineWorker.java b/services/services-engine/src/main/java/org/onap/policy/apex/service/engine/runtime/impl/EngineWorker.java index f69eae664..ebb1cdf2d 100644 --- a/services/services-engine/src/main/java/org/onap/policy/apex/service/engine/runtime/impl/EngineWorker.java +++ b/services/services-engine/src/main/java/org/onap/policy/apex/service/engine/runtime/impl/EngineWorker.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2019-2020 Nordix Foundation. + * Modifications Copyright (C) 2019-2020,2022 Nordix Foundation. * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -449,7 +449,6 @@ final class EngineWorker implements EngineService { try { final var baOutputStream = new ByteArrayOutputStream(); final ApexModelWriter modelWriter = new ApexModelWriter<>(AxEngineModel.class); - modelWriter.setJsonOutput(true); modelWriter.write(apexEngineModel, baOutputStream); return baOutputStream.toString(); } catch (final Exception e) { diff --git a/services/services-engine/src/main/java/org/onap/policy/apex/service/parameters/ApexParameterHandler.java b/services/services-engine/src/main/java/org/onap/policy/apex/service/parameters/ApexParameterHandler.java index cc19080f9..2e97c54fc 100644 --- a/services/services-engine/src/main/java/org/onap/policy/apex/service/parameters/ApexParameterHandler.java +++ b/services/services-engine/src/main/java/org/onap/policy/apex/service/parameters/ApexParameterHandler.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2019-2021 Nordix Foundation. + * Modifications Copyright (C) 2019-2022 Nordix Foundation. * Modifications Copyright (C) 2020-2021 Bell Canada. All rights reserved. * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ @@ -41,6 +41,7 @@ import org.onap.policy.apex.service.parameters.eventprotocol.EventProtocolParame import org.onap.policy.common.parameters.ParameterException; import org.onap.policy.common.parameters.ParameterService; import org.onap.policy.common.parameters.ValidationResult; +import org.onap.policy.common.utils.coder.CoderException; import org.onap.policy.common.utils.coder.StandardCoder; import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; import org.slf4j.ext.XLogger; @@ -61,6 +62,8 @@ public class ApexParameterHandler { private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexParameterHandler.class); private static final String POLICY_TYPE_IMPL = "policy_type_impl"; + private static final String APEX_POLICY_MODEL = "apexPolicyModel"; + private String policyModel; private String apexConfig; @@ -81,13 +84,13 @@ public class ApexParameterHandler { // Register the adapters for our carrier technologies and event protocols with GSON // @formatter:off final var gson = new GsonBuilder() - .registerTypeAdapter(EngineParameters.class, - new EngineServiceParametersJsonAdapter()) - .registerTypeAdapter(CarrierTechnologyParameters.class, - new CarrierTechnologyParametersJsonAdapter()) - .registerTypeAdapter(EventProtocolParameters.class, - new EventProtocolParametersJsonAdapter()) - .create(); + .registerTypeAdapter(EngineParameters.class, + new EngineServiceParametersJsonAdapter()) + .registerTypeAdapter(CarrierTechnologyParameters.class, + new CarrierTechnologyParametersJsonAdapter()) + .registerTypeAdapter(EventProtocolParameters.class, + new EventProtocolParametersJsonAdapter()) + .create(); // @formatter:on parameters = gson.fromJson(apexConfig, ApexParameters.class); } catch (final Exception e) { @@ -135,13 +138,13 @@ public class ApexParameterHandler { ParameterService.register(parameters.getEngineServiceParameters().getEngineParameters()); ParameterService.register(parameters.getEngineServiceParameters().getEngineParameters().getContextParameters()); ParameterService.register(parameters.getEngineServiceParameters().getEngineParameters().getContextParameters() - .getSchemaParameters()); + .getSchemaParameters()); ParameterService.register(parameters.getEngineServiceParameters().getEngineParameters().getContextParameters() - .getDistributorParameters()); + .getDistributorParameters()); ParameterService.register(parameters.getEngineServiceParameters().getEngineParameters().getContextParameters() - .getLockManagerParameters()); + .getLockManagerParameters()); ParameterService.register(parameters.getEngineServiceParameters().getEngineParameters().getContextParameters() - .getPersistorParameters()); + .getPersistorParameters()); } private void parseConfigAndModel(final String toscaPolicyFilePath) throws ApexException { @@ -162,11 +165,7 @@ public class ApexParameterHandler { || EVENT_OUTPUT_PARAMETERS.equals(property.getKey())) { body = standardCoder.convert(property.getValue(), JsonObject.class); if (ENGINE_SERVICE_PARAMETERS.equals(property.getKey())) { - JsonElement policyModelObject = ((JsonObject) body).get(POLICY_TYPE_IMPL); - if (null != policyModelObject) { - policyModel = standardCoder.encode(policyModelObject); - } - ((JsonObject) body).remove(POLICY_TYPE_IMPL); + policyModel = extractPolicyModel(standardCoder, body); } } apexConfigJsonObject.add(property.getKey(), body); @@ -176,4 +175,29 @@ public class ApexParameterHandler { throw new ApexException("Parsing config and model from the tosca policy failed.", e); } } + + private String extractPolicyModel(StandardCoder standardCoder, JsonElement body) throws CoderException { + // Check for "policy_type_impl" + JsonElement policyTypeImplObject = ((JsonObject) body).get(POLICY_TYPE_IMPL); + if (null == policyTypeImplObject) { + return null; + } + + // "policy_type_impl" found + if (policyTypeImplObject instanceof JsonObject) { + + // Check for "apexPolicyModel", this is used to encapsulate policy models sometimes + JsonElement policyModelObject = ((JsonObject) policyTypeImplObject).get(APEX_POLICY_MODEL); + + if (policyModelObject != null) { + // Policy model encased in an "apexPolicyModel" object + return standardCoder.encode(policyModelObject); + } else { + // No encasement + return standardCoder.encode(policyTypeImplObject); + } + } else { + return policyTypeImplObject.getAsString(); + } + } } -- cgit 1.2.3-korg