aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLiam Fallon <liam.fallon@est.tech>2022-07-22 09:13:05 +0000
committerGerrit Code Review <gerrit@onap.org>2022-07-22 09:13:05 +0000
commitcfa642373420fa2bc429c94f17e4f0c0798e5791 (patch)
tree1b7a28cbb9f2259ce22bf3865dc9aba2610e27fe
parentc354fba3fb87ebe21a7fbb2a15268ccc9d033fbd (diff)
parent093d515e4f5e018dc33048b4f0d31c7ead6717ed (diff)
Merge "Support MetadataSet generation in APEX CLI Editor"
-rw-r--r--auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/tosca/ApexCliToscaEditorMain.java11
-rw-r--r--auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/tosca/ApexCliToscaParameterParser.java13
-rw-r--r--auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/tosca/ApexCliToscaParameters.java5
-rw-r--r--auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/utils/CliUtils.java81
-rw-r--r--auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/tosca/ApexCliToscaEditorTest.java30
-rw-r--r--auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/tosca/CommonTestData.java1
-rw-r--r--auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/utils/CliUtilsTest.java2
-rw-r--r--examples/examples-grpc/pom.xml23
8 files changed, 149 insertions, 17 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);
+ }
}
diff --git a/auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/tosca/ApexCliToscaEditorTest.java b/auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/tosca/ApexCliToscaEditorTest.java
index b75199ac3..51659c2ea 100644
--- a/auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/tosca/ApexCliToscaEditorTest.java
+++ b/auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/tosca/ApexCliToscaEditorTest.java
@@ -24,9 +24,12 @@ package org.onap.policy.apex.auth.clieditor.tosca;
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;
@@ -42,6 +45,7 @@ public class ApexCliToscaEditorTest {
private File tempOutputToscaFile;
private File tempLogFile;
String[] sampleArgs;
+ private File tempNodeTemplateFile;
/**
* Initialise args.
@@ -52,6 +56,7 @@ public class ApexCliToscaEditorTest {
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,
@@ -69,6 +74,7 @@ public class ApexCliToscaEditorTest {
public void removeGeneratedFiles() {
tempOutputToscaFile.delete();
tempLogFile.delete();
+ tempNodeTemplateFile.delete();
}
@Test
@@ -110,4 +116,28 @@ public class ApexCliToscaEditorTest {
};
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")));
+ }
}
diff --git a/auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/tosca/CommonTestData.java b/auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/tosca/CommonTestData.java
index 23d927e24..a3b4bcd44 100644
--- a/auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/tosca/CommonTestData.java
+++ b/auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/tosca/CommonTestData.java
@@ -36,4 +36,5 @@ public final class CommonTestData {
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";
}
diff --git a/auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/utils/CliUtilsTest.java b/auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/utils/CliUtilsTest.java
index 2ce34e148..ba3ab7627 100644
--- a/auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/utils/CliUtilsTest.java
+++ b/auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/utils/CliUtilsTest.java
@@ -79,7 +79,7 @@ public class CliUtilsTest {
@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();
diff --git a/examples/examples-grpc/pom.xml b/examples/examples-grpc/pom.xml
index b6b0a19a5..df4df5373 100644
--- a/examples/examples-grpc/pom.xml
+++ b/examples/examples-grpc/pom.xml
@@ -33,6 +33,8 @@
<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>
@@ -128,6 +130,27 @@
</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>