From 4c5c31eb9a2513af080d60d0f537b8339856150d Mon Sep 17 00:00:00 2001 From: "a.sreekumar" Date: Wed, 2 Sep 2020 17:19:26 +0100 Subject: APEX standalone support for ToscaPolicy format Legacy format support is removed, and Tosca format support is added. Change-Id: I3cfc181ccb5471a5d224c0162af18c1fa0fdbc70 Issue-ID: POLICY-2812 Signed-off-by: a.sreekumar --- .../apex/service/engine/main/ApexActivator.java | 12 +- .../engine/main/ApexCommandLineArguments.java | 114 ++--------- .../service/parameters/ApexParameterHandler.java | 80 ++++++-- .../engineservice/EngineServiceParameters.java | 209 ++------------------- 4 files changed, 95 insertions(+), 320 deletions(-) (limited to 'services/services-engine/src/main') diff --git a/services/services-engine/src/main/java/org/onap/policy/apex/service/engine/main/ApexActivator.java b/services/services-engine/src/main/java/org/onap/policy/apex/service/engine/main/ApexActivator.java index 233fa1aa0..4d3349b83 100644 --- a/services/services-engine/src/main/java/org/onap/policy/apex/service/engine/main/ApexActivator.java +++ b/services/services-engine/src/main/java/org/onap/policy/apex/service/engine/main/ApexActivator.java @@ -22,7 +22,6 @@ package org.onap.policy.apex.service.engine.main; -import java.io.IOException; import java.util.AbstractMap; import java.util.ArrayList; import java.util.LinkedHashMap; @@ -51,7 +50,6 @@ import org.onap.policy.apex.service.parameters.engineservice.EngineServiceParame import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters; import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerPeeredMode; import org.onap.policy.common.parameters.ParameterService; -import org.onap.policy.common.utils.resources.TextFileUtils; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier; import org.slf4j.ext.XLogger; import org.slf4j.ext.XLoggerFactory; @@ -150,15 +148,13 @@ public class ApexActivator { inputParametersMap.putAll(apexParams.getEventInputParameters()); outputParametersMap.putAll(apexParams.getEventOutputParameters()); // Check if a policy model file has been specified - if (apexParams.getEngineServiceParameters().getPolicyModelFileName() != null) { - LOGGER.debug("deploying policy model in \"{}\" to the apex engines . . .", - apexParams.getEngineServiceParameters().getPolicyModelFileName()); + if (apexParams.getEngineServiceParameters().getPolicyModel() != null) { + LOGGER.debug("deploying policy model to the apex engines . . ."); try { - final String policyModelString = TextFileUtils - .getTextFileAsString(apexParams.getEngineServiceParameters().getPolicyModelFileName()); + final String policyModelString = apexParams.getEngineServiceParameters().getPolicyModel(); AxPolicyModel policyModel = EngineServiceImpl.createModel(engineKey, policyModelString); policyModelsMap.put(apexParamsEntry.getKey(), policyModel); - } catch (ApexException | IOException e) { + } catch (ApexException e) { throw new ApexRuntimeException("Failed to create the apex model.", e); } } diff --git a/services/services-engine/src/main/java/org/onap/policy/apex/service/engine/main/ApexCommandLineArguments.java b/services/services-engine/src/main/java/org/onap/policy/apex/service/engine/main/ApexCommandLineArguments.java index 89c47d6bc..31131b465 100644 --- a/services/services-engine/src/main/java/org/onap/policy/apex/service/engine/main/ApexCommandLineArguments.java +++ b/services/services-engine/src/main/java/org/onap/policy/apex/service/engine/main/ApexCommandLineArguments.java @@ -26,6 +26,8 @@ import java.io.PrintWriter; import java.io.StringWriter; import java.net.URL; import java.util.Arrays; +import lombok.Getter; +import lombok.Setter; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.DefaultParser; import org.apache.commons.cli.HelpFormatter; @@ -56,9 +58,9 @@ public class ApexCommandLineArguments { // Apache Commons CLI options private final Options options; - // The command line options - private String modelFilePath = null; - private String configurationFilePath = null; + @Getter + @Setter + private String toscaPolicyFilePath = null; private String relativeFileRoot = null; /** @@ -79,16 +81,6 @@ public class ApexCommandLineArguments { .required(false) .type(Boolean.class) .build()); - options.addOption(Option.builder("c") - .longOpt("config-file") - .desc("the full path to the configuration file to use, " - + "the configuration file must be a Json file " - + "containing the Apex configuration parameters") - .hasArg() - .argName("CONFIG_FILE") - .required(false) - .type(String.class) - .build()); options.addOption(Option.builder("rfr") .longOpt("relative-file-root") .desc("the root file path for relative file paths specified in the Apex configuration file, " @@ -98,11 +90,10 @@ public class ApexCommandLineArguments { .required(false) .type(String.class) .build()); - options.addOption(Option.builder("m").longOpt("model-file") - .desc("the full path to the model file to use, if set it overrides the model file set in the " - + "configuration file").hasArg().argName("MODEL_FILE") - .required(false) - .type(String.class).build()); + options.addOption(Option.builder("p").longOpt("tosca-policy-file") + .desc("the full path to the ToscaPolicy file to use.").hasArg().argName("TOSCA_POLICY_FILE") + .required(false) + .type(String.class).build()); //@formatter:on } @@ -133,9 +124,7 @@ public class ApexCommandLineArguments { */ public String parse(final String[] args) throws ApexException { // Clear all our arguments - setConfigurationFilePath(null); - setModelFilePath(null); - + setToscaPolicyFilePath(null); CommandLine commandLine = null; try { commandLine = new DefaultParser().parse(options, args); @@ -146,12 +135,12 @@ public class ApexCommandLineArguments { // Arguments left over after Commons CLI does its stuff final String[] remainingArgs = commandLine.getArgs(); - if (remainingArgs.length > 0 && commandLine.hasOption('c') || remainingArgs.length > 1) { + if (remainingArgs.length > 0 && commandLine.hasOption('p') || remainingArgs.length > 1) { throw new ApexException("too many command line arguments specified : " + Arrays.toString(args)); } if (remainingArgs.length == 1) { - configurationFilePath = remainingArgs[0]; + toscaPolicyFilePath = remainingArgs[0]; } if (commandLine.hasOption('h')) { @@ -162,20 +151,15 @@ public class ApexCommandLineArguments { return version(); } - if (commandLine.hasOption('c')) { - setConfigurationFilePath(commandLine.getOptionValue('c')); - } - if (commandLine.hasOption("rfr")) { setRelativeFileRoot(commandLine.getOptionValue("rfr")); } else { setRelativeFileRoot(null); } - if (commandLine.hasOption('m')) { - setModelFilePath(commandLine.getOptionValue('m')); + if (commandLine.hasOption('p')) { + toscaPolicyFilePath = commandLine.getOptionValue('p'); } - return null; } @@ -185,12 +169,7 @@ public class ApexCommandLineArguments { * @throws ApexException on command argument validation errors */ public void validate() throws ApexException { - validateReadableFile("Apex configuration", configurationFilePath); - - if (checkSetModelFilePath()) { - validateReadableFile("Apex model", modelFilePath); - } - + validateReadableFile("Tosca Policy", toscaPolicyFilePath); validateRelativeFileRoot(); } @@ -219,42 +198,6 @@ public class ApexCommandLineArguments { return stringWriter.toString(); } - /** - * Gets the model file path. - * - * @return the model file path - */ - public String getModelFilePath() { - return ResourceUtils.getFilePath4Resource(modelFilePath); - } - - /** - * Sets the model file path. - * - * @param modelFilePath the model file path - */ - public void setModelFilePath(final String modelFilePath) { - this.modelFilePath = modelFilePath; - } - - /** - * Check set model file path. - * - * @return true, if check set model file path - */ - public boolean checkSetModelFilePath() { - return modelFilePath != null && modelFilePath.length() > 0; - } - - /** - * Gets the configuration file path. - * - * @return the configuration file path - */ - public String getConfigurationFilePath() { - return configurationFilePath; - } - /** * Gets the root file path for relative file paths in the configuration file. * @@ -264,24 +207,6 @@ public class ApexCommandLineArguments { return relativeFileRoot; } - /** - * Gets the full expanded configuration file path. - * - * @return the configuration file path - */ - public String getFullConfigurationFilePath() { - return ResourceUtils.getFilePath4Resource(getConfigurationFilePath()); - } - - /** - * Sets the configuration file path. - * - * @param configurationFilePath the configuration file path - */ - public void setConfigurationFilePath(final String configurationFilePath) { - this.configurationFilePath = configurationFilePath; - - } /** * Sets the root file path for relative file paths in the configuration file. @@ -305,15 +230,6 @@ public class ApexCommandLineArguments { System.setProperty(APEX_RELATIVE_FILE_ROOT, relativeFileRootValue); } - /** - * Check set configuration file path. - * - * @return true, if check set configuration file path - */ - public boolean checkSetConfigurationFilePath() { - return configurationFilePath != null && configurationFilePath.length() > 0; - } - /** * Validate readable file. * 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 f88733f60..9a07b4ad5 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 @@ -24,8 +24,14 @@ package org.onap.policy.apex.service.parameters; import com.google.gson.Gson; import com.google.gson.GsonBuilder; -import java.io.FileReader; +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.Map.Entry; import org.onap.policy.apex.core.engine.EngineParameters; +import org.onap.policy.apex.model.basicmodel.concepts.ApexException; import org.onap.policy.apex.service.engine.main.ApexCommandLineArguments; import org.onap.policy.apex.service.parameters.carriertechnology.CarrierTechnologyParameters; import org.onap.policy.apex.service.parameters.carriertechnology.CarrierTechnologyParametersJsonAdapter; @@ -35,6 +41,8 @@ import org.onap.policy.apex.service.parameters.eventprotocol.EventProtocolParame import org.onap.policy.common.parameters.GroupValidationResult; import org.onap.policy.common.parameters.ParameterException; import org.onap.policy.common.parameters.ParameterService; +import org.onap.policy.common.utils.coder.StandardCoder; +import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; import org.slf4j.ext.XLogger; import org.slf4j.ext.XLoggerFactory; @@ -44,8 +52,18 @@ import org.slf4j.ext.XLoggerFactory; * @author Liam Fallon (liam.fallon@ericsson.com) */ public class ApexParameterHandler { + private static final String EVENT_OUTPUT_PARAMETERS = "eventOutputParameters"; + + private static final String EVENT_INPUT_PARAMETERS = "eventInputParameters"; + + private static final String ENGINE_SERVICE_PARAMETERS = "engineServiceParameters"; + private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexParameterHandler.class); + private static final String POLICY_TYPE_IMPL = "policy_type_impl"; + private String policyModel; + private String apexConfig; + /** * Read the parameters from the parameter file. * @@ -58,9 +76,10 @@ public class ApexParameterHandler { ParameterService.clear(); ApexParameters parameters = null; - + String toscaPolicyFilePath = arguments.getToscaPolicyFilePath(); // Read the parameters try { + parseConfigAndModel(toscaPolicyFilePath); // Register the adapters for our carrier technologies and event protocols with GSON // @formatter:off final Gson gson = new GsonBuilder() @@ -72,41 +91,33 @@ public class ApexParameterHandler { new EventProtocolParametersJsonAdapter()) .create(); // @formatter:on - parameters = gson.fromJson(new FileReader(arguments.getFullConfigurationFilePath()), ApexParameters.class); + parameters = gson.fromJson(apexConfig, ApexParameters.class); } catch (final Exception e) { - final String errorMessage = "error reading parameters from \"" + arguments.getConfigurationFilePath() - + "\"\n" + "(" + e.getClass().getSimpleName() + "):" + e.getMessage(); - LOGGER.error(errorMessage, e); + final String errorMessage = "error reading parameters from \"" + toscaPolicyFilePath + "\"\n" + "(" + + e.getClass().getSimpleName() + "):" + e.getMessage(); throw new ParameterException(errorMessage, e); } // The JSON processing returns null if there is an empty file if (parameters == null) { - final String errorMessage = "no parameters found in \"" + arguments.getConfigurationFilePath() + "\""; - LOGGER.error(errorMessage); + final String errorMessage = "no parameters found in \"" + toscaPolicyFilePath + "\""; throw new ParameterException(errorMessage); } - // Check if we should override the model file parameter - final String modelFilePath = arguments.getModelFilePath(); - if (modelFilePath != null && modelFilePath.replaceAll("\\s+", "").length() > 0) { - parameters.getEngineServiceParameters().setPolicyModelFileName(modelFilePath); + if (null != parameters.getEngineServiceParameters()) { + parameters.getEngineServiceParameters().setPolicyModel(policyModel); } // Validate the parameters final GroupValidationResult validationResult = parameters.validate(); if (!validationResult.isValid()) { - String returnMessage = "validation error(s) on parameters from \"" + arguments.getConfigurationFilePath() - + "\"\n"; + String returnMessage = "validation error(s) on parameters from \"" + toscaPolicyFilePath + "\"\n"; returnMessage += validationResult.getResult(); - - LOGGER.error(returnMessage); throw new ParameterException(returnMessage); } if (!validationResult.isClean()) { - String returnMessage = "validation messages(s) on parameters from \"" + arguments.getConfigurationFilePath() - + "\"\n"; + String returnMessage = "validation messages(s) on parameters from \"" + toscaPolicyFilePath + "\"\n"; returnMessage += validationResult.getResult(); LOGGER.info(returnMessage); @@ -134,4 +145,37 @@ public class ApexParameterHandler { ParameterService.register(parameters.getEngineServiceParameters().getEngineParameters().getContextParameters() .getPersistorParameters()); } + + private void parseConfigAndModel(final String toscaPolicyFilePath) throws ApexException { + policyModel = null; + apexConfig = null; + final StandardCoder standardCoder = new StandardCoder(); + JsonObject apexConfigJsonObject = new JsonObject(); + try { + ToscaServiceTemplate toscaServiceTemplate = standardCoder + .decode(Files.readString(Paths.get(toscaPolicyFilePath)), ToscaServiceTemplate.class); + for (Entry property : toscaServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0) + .entrySet().iterator().next().getValue().getProperties().entrySet()) { + JsonElement body = null; + if ("javaProperties".equals(property.getKey())) { + body = standardCoder.convert(property.getValue(), JsonArray.class); + } else if (EVENT_INPUT_PARAMETERS.equals(property.getKey()) + || ENGINE_SERVICE_PARAMETERS.equals(property.getKey()) + || 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); + } + } + apexConfigJsonObject.add(property.getKey(), body); + } + apexConfig = standardCoder.encode(apexConfigJsonObject); + } catch (Exception e) { + throw new ApexException("Parsing config and model from the tosca policy failed.", e); + } + } } diff --git a/services/services-engine/src/main/java/org/onap/policy/apex/service/parameters/engineservice/EngineServiceParameters.java b/services/services-engine/src/main/java/org/onap/policy/apex/service/parameters/engineservice/EngineServiceParameters.java index 52dcd3d3c..d67c44ef5 100644 --- a/services/services-engine/src/main/java/org/onap/policy/apex/service/parameters/engineservice/EngineServiceParameters.java +++ b/services/services-engine/src/main/java/org/onap/policy/apex/service/parameters/engineservice/EngineServiceParameters.java @@ -1,26 +1,29 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * Modifications Copyright (C) 2020 Bell Canada. All rights reserved. * ================================================================================ * 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.apex.service.parameters.engineservice; -import java.io.File; +import lombok.Getter; +import lombok.Setter; +import org.apache.commons.lang3.StringUtils; import org.onap.policy.apex.core.engine.EngineParameters; import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; import org.onap.policy.apex.model.basicmodel.concepts.AxKey; @@ -54,6 +57,8 @@ import org.onap.policy.common.parameters.ValidationStatus; * */ // @formatter:on +@Getter +@Setter public class EngineServiceParameters implements ParameterGroup { private static final int MAX_PORT = 65535; @@ -74,7 +79,6 @@ public class EngineServiceParameters implements ParameterGroup { public static final int DEFAULT_DEPLOYMENT_PORT = 34421; // Constants for repeated strings - private static final String POLICY_MODEL_FILE_NAME = "policyModelFileName"; // Apex engine service parameters private String name = DEFAULT_NAME; @@ -82,7 +86,7 @@ public class EngineServiceParameters implements ParameterGroup { private int id = DEFAULT_ID; private int instanceCount = DEFAULT_INSTANCE_COUNT; private int deploymentPort = DEFAULT_DEPLOYMENT_PORT; - private String policyModelFileName = null; + private String policyModel = null; private long periodicEventPeriod = 0; // @formatter:on @@ -111,7 +115,7 @@ public class EngineServiceParameters implements ParameterGroup { /** * Sets the key of the Apex engine service. - * + * * @param key the the Apex engine service key */ public void setEngineKey(final AxArtifactKey key) { @@ -119,151 +123,6 @@ public class EngineServiceParameters implements ParameterGroup { this.setVersion(key.getVersion()); } - /** - * Gets the name of the engine service. - * - * @return the name of the engine service - */ - public String getName() { - return name; - } - - /** - * Sets the name of the engine service. - * - * @param name the name of the engine service - */ - public void setName(final String name) { - this.name = name; - } - - /** - * Gets the version of the engine service. - * - * @return the version of the engine service - */ - public String getVersion() { - return version; - } - - /** - * Sets the version of the engine service. - * - * @param version the version of the engine service - */ - public void setVersion(final String version) { - this.version = version; - } - - /** - * Gets the id of the engine service. - * - * @return the id of the engine service - */ - public int getId() { - return id; - } - - /** - * Sets the id of the engine service. - * - * @param id the id of the engine service - */ - public void setId(final int id) { - this.id = id; - } - - /** - * Gets the instance count of the engine service. - * - * @return the instance count of the engine service - */ - public int getInstanceCount() { - return instanceCount; - } - - /** - * Sets the instance count of the engine service. - * - * @param instanceCount the instance count of the engine service - */ - public void setInstanceCount(final int instanceCount) { - this.instanceCount = instanceCount; - } - - /** - * Gets the deployment port of the engine service. - * - * @return the deployment port of the engine service - */ - public int getDeploymentPort() { - return deploymentPort; - } - - /** - * Sets the deployment port of the engine service. - * - * @param deploymentPort the deployment port of the engine service - */ - public void setDeploymentPort(final int deploymentPort) { - this.deploymentPort = deploymentPort; - } - - /** - * Gets the file name of the policy engine for deployment on the engine service. - * - * @return the file name of the policy engine for deployment on the engine service - */ - public String getPolicyModelFileName() { - return policyModelFileName; - } - - /** - * Sets the file name of the policy engine for deployment on the engine service. - * - * @param policyModelFileName the file name of the policy engine for deployment on the engine service - */ - public void setPolicyModelFileName(final String policyModelFileName) { - this.policyModelFileName = policyModelFileName; - } - - /** - * Get the period in milliseconds at which periodic events are sent, zero means no periodic events are being sent. - * - * @return the periodic period - */ - public long getPeriodicEventPeriod() { - return periodicEventPeriod; - } - - /** - * Set the period in milliseconds at which periodic events are sent, zero means no periodic events are to be sent, - * negative values are illegal. - * - * @param periodicEventPeriod the periodic period - */ - public void setPeriodicEventPeriod(final long periodicEventPeriod) { - this.periodicEventPeriod = periodicEventPeriod; - } - - /** - * Gets the engine parameters for engines in the engine service. - * - * @return the engine parameters for engines in the engine service - */ - public EngineParameters getEngineParameters() { - return engineParameters; - } - - /** - * Sets the engine parameters for engines in the engine service. - * - * @param engineParameters the engine parameters for engines in the engine service - */ - public void setEngineParameters(final EngineParameters engineParameters) { - this.engineParameters = engineParameters; - } - /** * {@inheritDoc}. */ @@ -275,8 +134,8 @@ public class EngineServiceParameters implements ParameterGroup { validateNumericParameters(result); - if (policyModelFileName != null) { - validatePolicyModelFileName(result); + if (StringUtils.isBlank(policyModel)) { + result.setResult("policyModel", ValidationStatus.INVALID, "must be specified"); } result.setResult("engineParameters", engineParameters.validate()); @@ -285,7 +144,7 @@ public class EngineServiceParameters implements ParameterGroup { /** * Validate string parameters. - * + * * @param result the result of string parameter validation */ private void validateStringParameters(final GroupValidationResult result) { @@ -327,44 +186,4 @@ public class EngineServiceParameters implements ParameterGroup { } } - /** - * Validate the policy model file name parameter. - * - * @param result the variable in which to store the result of the validation - */ - private void validatePolicyModelFileName(final GroupValidationResult result) { - if (policyModelFileName.trim().length() == 0) { - result.setResult(POLICY_MODEL_FILE_NAME, ValidationStatus.INVALID, - "\"" + policyModelFileName + "\" invalid, must be specified as a non-empty string"); - return; - } - - String absolutePolicyFileName = null; - - // Resolve the file name if it is a relative file name - File policyModelFile = new File(policyModelFileName); - if (policyModelFile.isAbsolute()) { - absolutePolicyFileName = policyModelFileName; - } else { - absolutePolicyFileName = System.getProperty("APEX_RELATIVE_FILE_ROOT") + File.separator - + policyModelFileName; - policyModelFile = new File(absolutePolicyFileName); - } - - // Check that the file exists - if (!policyModelFile.exists()) { - result.setResult(POLICY_MODEL_FILE_NAME, ValidationStatus.INVALID, "not found"); - } else if (!policyModelFile.isFile()) { - // Check that the file is a regular file - result.setResult(POLICY_MODEL_FILE_NAME, ValidationStatus.INVALID, "is not a plain file"); - } else { - // OK, we found the file and it's OK, so reset the file name - policyModelFileName = absolutePolicyFileName; - - // Check that the file is readable - if (!policyModelFile.canRead()) { - result.setResult(POLICY_MODEL_FILE_NAME, ValidationStatus.INVALID, "is not readable"); - } - } - } } -- cgit 1.2.3-korg