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
/*-
* ============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.
parameters.validate();
String policyModelFilePath = null;
+ String nodeType = parameters.getNodeType();
try {
final var tempModelFile = TextFileUtils.createTempFile("policyModel", ".json");
policyModelFilePath = tempModelFile.getAbsolutePath();
// 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());
}
-
}
/**
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());
}
/**
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;
}
/*-
* ============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.
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.
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);
}
}
/*-
* ============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");
*
* @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());
}
}
+ /**
+ * 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.
*
throw new CommandLineException(prefixExceptionMessage + " is not a normal file");
}
if (!theFile.canRead()) {
- throw new CommandLineException(prefixExceptionMessage + " is ureadable");
+ throw new CommandLineException(prefixExceptionMessage + " is unreadable");
}
}
}
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);
+ }
}
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
import java.io.File;
import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Paths;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
private File tempOutputToscaFile;
private File tempLogFile;
String[] sampleArgs;
+ private File tempNodeTemplateFile;
/**
* Initialise args.
public void initialiseArgs() throws IOException {
tempOutputToscaFile = File.createTempFile("ToscaPolicyOutput", ".json");
+ tempNodeTemplateFile = File.createTempFile("ToscaNodeTemplate", ".json");
tempLogFile = File.createTempFile("ApexCliTosca", ".log");
sampleArgs = new String[] {
"-c", CommonTestData.COMMAND_FILE_NAME,
public void removeGeneratedFiles() {
tempOutputToscaFile.delete();
tempLogFile.delete();
+ tempNodeTemplateFile.delete();
}
@Test
};
assertThatThrownBy(() -> new ApexCliToscaEditorMain(sampleArgs)).hasMessage("Insufficient arguments provided.");
}
+
+ @Test
+ public void testGenerateToscaPolicyMetadataSet() throws Exception {
+ // @formatter:off
+ final String[] cliArgs = new String[] {
+ "-c", CommonTestData.COMMAND_FILE_NAME,
+ "-l", tempLogFile.getAbsolutePath(),
+ "-ac", CommonTestData.APEX_CONFIG_FILE_NAME,
+ "-t", CommonTestData.INPUT_TOSCA_TEMPLATE_FILE_NAME,
+ "-ot", tempOutputToscaFile.getAbsolutePath(),
+ "-on", tempNodeTemplateFile.getAbsolutePath(),
+ "-nt", CommonTestData.NODE_TYPE
+ };
+ // @formatter:on
+
+ new ApexCliToscaEditorMain(cliArgs);
+
+ assertTrue(tempOutputToscaFile.length() > 0);
+ assertTrue(Files.lines(Paths.get(tempOutputToscaFile.toString()))
+ .noneMatch(l -> l.contains("policy_type_impl")));
+ assertTrue(tempNodeTemplateFile.length() > 0);
+ assertTrue(Files.lines(Paths.get(tempNodeTemplateFile.toString()))
+ .anyMatch(l -> l.contains("policyModel")));
+ }
}
public static final String APEX_CONFIG_FILE_NAME = "src/test/resources/tosca/ApexConfig.json";
public static final String COMMAND_FILE_NAME = "src/test/resources/tosca/PolicyModel.apex";
public static final String POLICY_MODEL_FILE_NAME = "src/test/resources/tosca/PolicyModel.json";
+ public static final String NODE_TYPE = "org.onap.nodetypes.policy.MetadataSet";
}
@Test
public void testCreateToscaServiceTemplate() throws IOException, CoderException {
ApexCliToscaParameters params = new ApexCliToscaParameterParser().parse(sampleArgs);
- CliUtils.createToscaServiceTemplate(params, policyModelFilePath);
+ CliUtils.createToscaPolicy(params, policyModelFilePath, null);
String outputTosca = TextFileUtils.getTextFileAsString(tempOutputToscaFile.getAbsolutePath());
String outputToscaCompare =
TextFileUtils.getTextFileAsString("src/test/resources/tosca/ToscaPolicyOutput_compare_1.json").trim();
<properties>
<policymodel.name>APEXgRPCPolicy</policymodel.name>
<toscapolicy.name>APEXgRPCToscaPolicy</toscapolicy.name>
+ <!-- Update the required node type value for generating a node template -->
+ <nodeType.name>org.onap.nodetypes.policy.MetadataSet</nodeType.name>
</properties>
<dependencies>
<dependency>
</arguments>
</configuration>
</execution>
+ <!-- Generate Tosca policy with metadataSet reference and a node template json file with policy model -->
+ <execution>
+ <id>generate-tosca-policy-metadataSet</id>
+ <phase>compile</phase>
+ <goals>
+ <goal>java</goal>
+ </goals>
+ <configuration>
+ <mainClass>org.onap.policy.apex.auth.clieditor.tosca.ApexCliToscaEditorMain</mainClass>
+ <classpathScope>compile</classpathScope>
+ <arguments>
+ <argument>--command-file=${project.basedir}/src/main/resources/policy/${policymodel.name}.apex</argument>
+ <argument>--output-tosca-file=${project.build.directory}/classes/${toscapolicy.name}.metadataSet.json</argument>
+ <argument>--log-file=${project.build.directory}/${policymodel.name}_policygeneration.log</argument>
+ <argument>--apex-config-file=${project.basedir}/src/main/resources/examples/config/APEXgRPC/ApexConfig.json</argument>
+ <argument>--tosca-template-file=${project.basedir}/src/main/resources/tosca/ToscaTemplate.json</argument>
+ <argument>--node-type=${nodeType.name}</argument>
+ <argument>--output-node-template-file=${project.build.directory}/classes/${toscapolicy.name}.nodeTemplate.json</argument>
+ </arguments>
+ </configuration>
+ </execution>
</executions>
</plugin>
</plugins>