diff options
author | 2022-07-08 12:29:25 +0100 | |
---|---|---|
committer | 2022-07-18 11:03:46 +0100 | |
commit | 093d515e4f5e018dc33048b4f0d31c7ead6717ed (patch) | |
tree | 41e643642e3c68ced3b05df1a7dee0dc343eef8b /auth/cli-editor/src/main/java/org | |
parent | 3b9e1d6eff05e0523c43f066a3e0a7e70025352f (diff) |
Support MetadataSet generation in APEX CLI Editor
In addition to the existing capabilities of CLI editor, new arguments added for generating
Tosca policy template excluding the policy model entity.
Also generates independent json file for node template comprising the policy model data.
Issue-ID: POLICY-4084
Signed-off-by: zrrmmua <ramesh.murugan.iyer@est.tech>
Change-Id: I7c6a9570846e3ebfaa3f4ebc8d60faa94b533240
Diffstat (limited to 'auth/cli-editor/src/main/java/org')
4 files changed, 94 insertions, 16 deletions
diff --git a/auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/tosca/ApexCliToscaEditorMain.java b/auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/tosca/ApexCliToscaEditorMain.java index 0926c80e0..5f7bad116 100644 --- a/auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/tosca/ApexCliToscaEditorMain.java +++ b/auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/tosca/ApexCliToscaEditorMain.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019-2021 Nordix Foundation. + * Copyright (C) 2019-2022 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -64,6 +64,7 @@ public class ApexCliToscaEditorMain { parameters.validate(); String policyModelFilePath = null; + String nodeType = parameters.getNodeType(); try { final var tempModelFile = TextFileUtils.createTempFile("policyModel", ".json"); policyModelFilePath = tempModelFile.getAbsolutePath(); @@ -84,19 +85,19 @@ public class ApexCliToscaEditorMain { // Create the ToscaPolicy using the tosca template skeleton file, config file, and policy model created. try { - CliUtils.createToscaServiceTemplate(parameters, policyModelFilePath); + CliUtils.createToscaPolicy(parameters, policyModelFilePath, nodeType); LOGGER.info("Apex CLI Tosca editor completed execution."); } catch (IOException | CoderException e) { failure = true; - LOGGER.error("Failed to create the ToscaPolicy using the generated policy model, apex config file and" - + " the tosca template skeleton file."); + LOGGER.error("Failed to create the Tosca template using the generated policy model," + + "apex config file and the tosca template skeleton file. " + e); } + } else { failure = true; LOGGER.error("execution of Apex command line editor failed: {} command execution failure(s) occurred", apexCliEditor.getErrorCount()); } - } /** diff --git a/auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/tosca/ApexCliToscaParameterParser.java b/auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/tosca/ApexCliToscaParameterParser.java index 5a35499c7..6cdb1a728 100644 --- a/auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/tosca/ApexCliToscaParameterParser.java +++ b/auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/tosca/ApexCliToscaParameterParser.java @@ -43,6 +43,12 @@ public class ApexCliToscaParameterParser extends CommandLineParameterParser { getOptions().addOption(Option.builder("ot").longOpt("output-tosca-file") .desc("name of a file that will contain the output model for the editor").hasArg() .argName("OUTPUT_TOSCA_FILE").required(false).type(String.class).build()); + getOptions().addOption(Option.builder("nt").longOpt("node-type") + .desc("name of the node type to associate node template").hasArg() + .argName("NODE_TYPE").required(false).type(String.class).build()); + getOptions().addOption(Option.builder("on").longOpt("output-node-template-file") + .desc("name of the node template file that will contain the output metadataSet").hasArg() + .argName("OUTPUT_NODE_TEMPLATE_FILE").required(false).type(String.class).build()); } /** @@ -67,6 +73,13 @@ public class ApexCliToscaParameterParser extends CommandLineParameterParser { if (commandLine.hasOption("ot")) { parameters.setOutputToscaPolicyFileName(commandLine.getOptionValue("ot")); } + if (commandLine.hasOption("nt")) { + parameters.setNodeType(commandLine.getOptionValue("nt")); + } + if (commandLine.hasOption("on")) { + parameters.setOutputNodeTemplateFileName(commandLine.getOptionValue("on")); + } + return parameters; } diff --git a/auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/tosca/ApexCliToscaParameters.java b/auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/tosca/ApexCliToscaParameters.java index 49f790c5d..84f7e6e74 100644 --- a/auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/tosca/ApexCliToscaParameters.java +++ b/auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/tosca/ApexCliToscaParameters.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. + * Copyright (C) 2019,2022 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -40,6 +40,8 @@ public class ApexCliToscaParameters extends CommandLineParameters { private String apexConfigFileName = null; private String inputToscaTemplateFileName = null; private String outputToscaPolicyFileName = null; + private String nodeType = null; + private String outputNodeTemplateFileName = null; /** * Validates the command line parameters. @@ -53,5 +55,6 @@ public class ApexCliToscaParameters extends CommandLineParameters { CliUtils.validateReadableFile("Apex Config File", apexConfigFileName); CliUtils.validateReadableFile("Input Tosca Template File", inputToscaTemplateFileName); CliUtils.validateWritableFile("Output Tosca Policy File", outputToscaPolicyFileName); + CliUtils.validateWritableFile("Output Tosca Node Template File", outputNodeTemplateFileName); } } diff --git a/auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/utils/CliUtils.java b/auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/utils/CliUtils.java index 74147dd41..7dd0ca277 100644 --- a/auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/utils/CliUtils.java +++ b/auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/utils/CliUtils.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019-2021 Nordix Foundation. + * Copyright (C) 2019-2022 Nordix Foundation. * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -62,24 +62,35 @@ public final class CliUtils { * * @param parameters containing paths to the apex config and tosca template skeleton file * @param policyModelFilePath path of apex policy model + * @param nodeType node type name if node template is generated, null by default */ - public static void createToscaServiceTemplate(ApexCliToscaParameters parameters, String policyModelFilePath) + public static void createToscaPolicy(ApexCliToscaParameters parameters, String policyModelFilePath, String nodeType) throws IOException, CoderException { final var standardCoder = new StandardCoder(); - var apexConfig = TextFileUtils.getTextFileAsString(parameters.getApexConfigFileName()); - JsonObject apexConfigJson = standardCoder.decode(apexConfig, JsonObject.class); - var policyModel = TextFileUtils.getTextFileAsString(policyModelFilePath); - JsonObject policyModelJson = standardCoder.decode(policyModel, JsonObject.class); - var toscaTemplate = TextFileUtils.getTextFileAsString(parameters.getInputToscaTemplateFileName()); - JsonObject toscaTemplateJson = standardCoder.decode(toscaTemplate, JsonObject.class); + var apexConfigJson = getJsonObject(parameters.getApexConfigFileName()); + var policyModelJson = getJsonObject(policyModelFilePath); + var toscaTemplateJson = getJsonObject(parameters.getInputToscaTemplateFileName()); var toscaPolicyProperties = toscaTemplateJson.get("topology_template").getAsJsonObject(); var toscaPolicy = toscaPolicyProperties.get("policies").getAsJsonArray().get(0).getAsJsonObject(); var toscaProperties = toscaPolicy.get(toscaPolicy.keySet().toArray()[0].toString()).getAsJsonObject() .get("properties").getAsJsonObject(); + // Populate metadataSet reference in Tosca policy and create node template file if nodeType is provided + if (nodeType != null) { + var metadataSetName = toscaPolicy.get(toscaPolicy.keySet().toArray()[0].toString()).getAsJsonObject() + .get("name").getAsString() + ".metadataSet"; + var metadataSetVersion = toscaPolicy.get(toscaPolicy.keySet().toArray()[0].toString()) + .getAsJsonObject().get("version").getAsString(); + JsonObject metadata = new JsonObject(); + metadata.addProperty("metadataSetName", metadataSetName); + metadata.addProperty("metadataSetVersion", metadataSetVersion); + toscaPolicy.add("metadata", metadata); + + createToscaNodeTemplate(parameters, policyModelJson, nodeType, metadataSetName, metadataSetVersion); + } apexConfigJson.entrySet().forEach(entry -> { - if ("engineServiceParameters".equals(entry.getKey())) { + if ("engineServiceParameters".equals(entry.getKey()) && nodeType == null) { entry.getValue().getAsJsonObject().add("policy_type_impl", policyModelJson); } toscaProperties.add(entry.getKey(), entry.getValue()); @@ -94,6 +105,50 @@ public final class CliUtils { } /** + * Method to create tosca node template with metadataSet. + * + * @param parameters containing tosca node template skeleton file + * @param policyModelJson path of apex policy model + * @param nodeType node type name for the node template + * @param metadataSetName name of the node template + * @param metadataSetVersion version of the node template + */ + public static void createToscaNodeTemplate(ApexCliToscaParameters parameters, JsonObject policyModelJson, + String nodeType, String metadataSetName, String metadataSetVersion) + throws IOException, CoderException { + final var standardCoder = new StandardCoder(); + + JsonObject nodeTemplateFile = new JsonObject(); + nodeTemplateFile.addProperty("tosca_definitions_version", "tosca_simple_yaml_1_1_0"); + + JsonObject metadata = new JsonObject(); + metadata.add("policyModel", policyModelJson); + + JsonObject nodeTemplate = new JsonObject(); + nodeTemplate.addProperty("type", nodeType); + nodeTemplate.addProperty("type_version", "1.0.0"); + nodeTemplate.addProperty("version", metadataSetVersion); + nodeTemplate.addProperty("description", "MetadataSet for policy containing policy model"); + nodeTemplate.add("metadata", metadata); + + JsonObject metadataSet = new JsonObject(); + metadataSet.add(metadataSetName, nodeTemplate); + + JsonObject nodeTemplates = new JsonObject(); + nodeTemplates.add("node_templates", metadataSet); + + nodeTemplateFile.add("topology_template", nodeTemplates); + + final var toscaNodeTemplateString = standardCoder.encode(nodeTemplateFile); + final String toscaNodeTemplateFileName = parameters.getOutputNodeTemplateFileName(); + if (StringUtils.isNotBlank(toscaNodeTemplateFileName)) { + TextFileUtils.putStringAsTextFile(toscaNodeTemplateString, toscaNodeTemplateFileName); + } else { + LOGGER.debug("Output file name not specified. Resulting tosca node template: {}", toscaNodeTemplateString); + } + } + + /** * Validate that a file is readable. * * @param fileTag the file tag, a tag used for information and error messages @@ -113,7 +168,7 @@ public final class CliUtils { throw new CommandLineException(prefixExceptionMessage + " is not a normal file"); } if (!theFile.canRead()) { - throw new CommandLineException(prefixExceptionMessage + " is ureadable"); + throw new CommandLineException(prefixExceptionMessage + " is unreadable"); } } @@ -223,4 +278,10 @@ public final class CliUtils { } return cliArgsList; } + + private static JsonObject getJsonObject(String filePath) throws IOException, CoderException { + final var standardCoder = new StandardCoder(); + var contentString = TextFileUtils.getTextFileAsString(filePath); + return standardCoder.decode(contentString, JsonObject.class); + } } |