From 85fe98b96d6097369a107b7df79f75c0c2a10bc0 Mon Sep 17 00:00:00 2001 From: ms236b Date: Mon, 26 Aug 2019 09:41:54 -0400 Subject: Added the policy models to onap version Added Policy model support to onap version, added header Issue-ID: DCAEGEN2-1684 Change-Id: Id41c3c72c07613ef68e33ba2c34ad62b84944e8d Signed-off-by: ms236b --- blueprint-generator/README.md | 22 +++ .../TestCases/testComponentSpec.json | 17 ++- blueprint-generator/pom.xml | 2 +- .../onap/blueprintgenerator/core/PolicyCreate.java | 85 ++++++++++++ .../models/policymodel/PolicyModel.java | 139 +++++++++++++++++++ .../models/policymodel/PolicyModelNode.java | 147 +++++++++++++++++++++ .../models/policymodel/PolicyProperties.java | 41 ++++++ .../core/BlueprintGeneratorTest.java | 13 +- blueprint-generator/version.properties | 2 +- 9 files changed, 464 insertions(+), 4 deletions(-) create mode 100644 blueprint-generator/src/main/java/org/onap/blueprintgenerator/core/PolicyCreate.java create mode 100644 blueprint-generator/src/main/java/org/onap/blueprintgenerator/models/policymodel/PolicyModel.java create mode 100644 blueprint-generator/src/main/java/org/onap/blueprintgenerator/models/policymodel/PolicyModelNode.java create mode 100644 blueprint-generator/src/main/java/org/onap/blueprintgenerator/models/policymodel/PolicyProperties.java diff --git a/blueprint-generator/README.md b/blueprint-generator/README.md index 1016bd8..55ffad9 100644 --- a/blueprint-generator/README.md +++ b/blueprint-generator/README.md @@ -58,3 +58,25 @@ This command will create a blueprint from the component spec TestComponentSpec. - If you do not add a -n tag the blueprint name will default to what it is in the component spec - If the directory you specified in the -p tag does not already exist the directory will be created for you - The -t flag will override the default imports set for the blueprints. To see an example of how the import yaml file should be structured see the testImports.yaml file under the folder TestCases. + + +#Instructions for policy models + +-Run the program on the command line with the following tags: +OPTIONS: +- -i: The path to the JSON spec file (required) +- -p: The output path for all of the models (required) + +If you're on windows it will look like this: + +```bash +java -cp "lib/blueprint-generator-onap-0.0.1-SNAPSHOT.jar;lib/*" org.onap.blueprintgenerator.core.PolicyCreate -p models -i ComponentSpecs/TestComponentSpec.json +``` + +If you're on linux it will look like this + +```bash +java -cp blueprint-generator/lib/blueprint-generator-0.0.1-SNAPSHOT.jar:blueprint-generator/lib/* org.onap.blueprintgenerator.core.PolicyCreate -i ComponentSpecs/TestComponentSpec.json +``` + +This command will create a directory called models and put the policy models created from the component spec given in that directory. (A component spec may generate multiple policy models) \ No newline at end of file diff --git a/blueprint-generator/TestCases/testComponentSpec.json b/blueprint-generator/TestCases/testComponentSpec.json index a0a3489..d6b617b 100644 --- a/blueprint-generator/TestCases/testComponentSpec.json +++ b/blueprint-generator/TestCases/testComponentSpec.json @@ -54,7 +54,22 @@ "designer_editable": true, "policy_editable": true, "policy_group": "Test_Parameters", - "required": true + "required": true, + "policy_schema": [ + { + "name": "PolicySchemaTest", + "description": "List of objects for vnf type monitorng", + "type": "String", + "entry_schema": [ + { + "name": "TestEntrySchema", + "description": "entry", + "type": "string", + "value": "None" + } + ] + } + ] } ], diff --git a/blueprint-generator/pom.xml b/blueprint-generator/pom.xml index 96a0c97..cb798d2 100644 --- a/blueprint-generator/pom.xml +++ b/blueprint-generator/pom.xml @@ -29,7 +29,7 @@ org.onap.dcaegen2.platform.cli blueprint-generator - 1.1.0-SNAPSHOT + 1.2.0-SNAPSHOT 1.6 1.6 diff --git a/blueprint-generator/src/main/java/org/onap/blueprintgenerator/core/PolicyCreate.java b/blueprint-generator/src/main/java/org/onap/blueprintgenerator/core/PolicyCreate.java new file mode 100644 index 0000000..3210a2f --- /dev/null +++ b/blueprint-generator/src/main/java/org/onap/blueprintgenerator/core/PolicyCreate.java @@ -0,0 +1,85 @@ +/**============LICENSE_START======================================================= + org.onap.dcae + ================================================================================ + Copyright (c) 2019 AT&T Intellectual Property. 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. + ============LICENSE_END========================================================= + +*/ + +package org.onap.blueprintgenerator.core; + +import java.util.ArrayList; + +import org.apache.commons.cli.BasicParser; +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.Options; +import org.onap.blueprintgenerator.models.componentspec.ComponentSpec; +import org.onap.blueprintgenerator.models.policymodel.PolicyModel; + +public class PolicyCreate { + + private static String componentSpecPath = ""; + + private static String outputPath = ""; + + + + public static void main(String[] args) throws Exception { + printInstructions(); + parseInputs(args); + + ComponentSpec cs = new ComponentSpec(); + cs.createComponentSpecFromFile(componentSpecPath); + + PolicyModel p = new PolicyModel(); + ArrayList models = p.createPolicyModels(cs, outputPath); + } + + public static void printInstructions() { + System.out.println("OPTIONS:"); + System.out.println("-i: The path to the JSON spec file (required)"); + System.out.println("-p: The output path for all of the models (required)"); + } + + public static void parseInputs(String[] args) throws Exception { + //convert the arguments array to a string to make it easier + String commands = ""; + for(String s: args) { + if(commands.length() == 0) { + commands = s; + } + else { + commands = commands + " " + s; + } + } + + //check if it has the required inputs + if(!commands.contains("-p") || !commands.contains("-i")) { + System.out.println("did not enter the required inputs"); + System.exit(0); + } + else { + BasicParser parser = new BasicParser(); + Options options = new Options(); + options.addOption("i", "Spec", true, "ComponentSpec import file"); + options.addOption("p", "Path", true, "Path to the final blueprint"); + + CommandLine commandLine = parser.parse(options, args); + componentSpecPath = commandLine.getOptionValue("i"); + outputPath = commandLine.getOptionValue("p"); + + } + } +} diff --git a/blueprint-generator/src/main/java/org/onap/blueprintgenerator/models/policymodel/PolicyModel.java b/blueprint-generator/src/main/java/org/onap/blueprintgenerator/models/policymodel/PolicyModel.java new file mode 100644 index 0000000..9c9bd51 --- /dev/null +++ b/blueprint-generator/src/main/java/org/onap/blueprintgenerator/models/policymodel/PolicyModel.java @@ -0,0 +1,139 @@ +/**============LICENSE_START======================================================= + org.onap.dcae + ================================================================================ + Copyright (c) 2019 AT&T Intellectual Property. 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. + ============LICENSE_END========================================================= + +*/ + +package org.onap.blueprintgenerator.models.policymodel; + +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.io.PrintWriter; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.TreeMap; + +import org.onap.blueprintgenerator.models.blueprint.Node; +import org.onap.blueprintgenerator.models.componentspec.ComponentSpec; +import org.onap.blueprintgenerator.models.componentspec.Parameters; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.core.JsonGenerationException; +import com.fasterxml.jackson.databind.JsonMappingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; +import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator; + +import lombok.Getter; +import lombok.Setter; + +@Getter @Setter +@JsonInclude(JsonInclude.Include.NON_NULL) + +public class PolicyModel { + + private String tosca_definition_version; + private TreeMap node_types; + private TreeMap data_types; + + public ArrayList createPolicyModels(ComponentSpec cs, String filePath) { + ArrayList models = new ArrayList(); + Parameters[] params = cs.getParameters(); + + ArrayList groups = new ArrayList(); + groups = getModelGroups(params); + + for(String s: groups) { + PolicyModel model = new PolicyModel(); + model = model.createPolicyModel(s, params); + //models.add(model); + policyModelToYaml(filePath, model, s); + } + +// for(PolicyModel p: models) { +// policyModelToYaml(filePath, p); +// } + + return models; + } + + public ArrayList getModelGroups(Parameters[] params) { + ArrayList groups = new ArrayList(); + + for(Parameters p: params) { + if(p.isPolicy_editable()) { + if(groups.isEmpty()) { + groups.add(p.getPolicy_group()); + } else { + if(!groups.contains(p.getPolicy_group())) { + groups.add(p.getPolicy_group()); + } + } + } + } + + return groups; + } + + public PolicyModel createPolicyModel(String s, Parameters[] params) { + PolicyModel model = new PolicyModel(); + model.setTosca_definition_version("tosca_simple_yaml_1_0_0"); + + PolicyModelNode node = new PolicyModelNode(); + String hasEntryScheme = node.createNodeType(s, params); + String nodeTypeName = "onap.policy." + s; + TreeMap nodeType = new TreeMap(); + nodeType.put(nodeTypeName, node); + model.setNode_types(nodeType); + + if(!hasEntryScheme.equals("")) { + PolicyModelNode data = new PolicyModelNode(); + TreeMap dataType = data.createDataTypes(hasEntryScheme, params); + model.setData_types(dataType); + } + + return model; + } + + public void policyModelToYaml(String path, PolicyModel p, String name) { + File outputFile; + String filePath = path + "/" + name + ".yml"; + File policyFile = new File(filePath); + ObjectMapper policyMapper = new ObjectMapper(new YAMLFactory().configure(YAMLGenerator.Feature.MINIMIZE_QUOTES, true)); + outputFile = new File(path, name + ".yml"); + outputFile.getParentFile().mkdirs(); + + try { + PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(outputFile, true))); + } catch (IOException e) { + e.printStackTrace(); + } + + try { + policyMapper.writeValue(outputFile, p); + } catch (JsonGenerationException e) { + e.printStackTrace(); + } catch (JsonMappingException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + System.out.println("model " + name + " created"); + } +} diff --git a/blueprint-generator/src/main/java/org/onap/blueprintgenerator/models/policymodel/PolicyModelNode.java b/blueprint-generator/src/main/java/org/onap/blueprintgenerator/models/policymodel/PolicyModelNode.java new file mode 100644 index 0000000..56d1d13 --- /dev/null +++ b/blueprint-generator/src/main/java/org/onap/blueprintgenerator/models/policymodel/PolicyModelNode.java @@ -0,0 +1,147 @@ +/**============LICENSE_START======================================================= + org.onap.dcae + ================================================================================ + Copyright (c) 2019 AT&T Intellectual Property. 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. + ============LICENSE_END========================================================= + +*/ + +package org.onap.blueprintgenerator.models.policymodel; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.TreeMap; + +import org.onap.blueprintgenerator.models.blueprint.Node; +import org.onap.blueprintgenerator.models.componentspec.EntrySchemaObj; +import org.onap.blueprintgenerator.models.componentspec.Parameters; +import org.onap.blueprintgenerator.models.componentspec.PolicySchemaObj; + +import com.fasterxml.jackson.annotation.JsonInclude; + +import lombok.Getter; +import lombok.Setter; + +@Getter @Setter +@JsonInclude(JsonInclude.Include.NON_NULL) +public class PolicyModelNode { + + private String derived_from; + private TreeMap properties; + + public String createNodeType(String policyName, Parameters[] params) { + String hasEntrySchema = ""; + + TreeMap props = new TreeMap(); + for(Parameters p: params) { + if(p.getPolicy_group() != null) { + if(p.getPolicy_group().equals(policyName)) { + String name = p.getName(); + String type = p.getType(); + PolicyProperties polProps = new PolicyProperties(); + if(p.getPolicy_schema() != null) { + polProps.setType("map"); + HashMap entrySchema = new HashMap(); + entrySchema.put("type", "onap.datatypes." + name); + //ArrayList entrySchema = new ArrayList(); + //entrySchema.add("type: onap.data." + name); + polProps.setEntry_schema(entrySchema); + hasEntrySchema = name; + props.put(name, polProps); + } + else { + polProps.setType(type); + props.put(name, polProps); + } + } + } + } + + this.setDerived_from("tosca.datatypes.Root"); + this.setProperties(props); + return hasEntrySchema; + } + + public TreeMap createDataTypes(String param, Parameters[] parameters) { + TreeMap dataType = new TreeMap(); + + PolicyModelNode node = new PolicyModelNode(); + node.setDerived_from("tosca.datatypes.Root"); + + TreeMap properties = new TreeMap(); + + Parameters par = new Parameters(); + for(Parameters p: parameters) { + if(p.getName().equals(param)) { + par = p; + break; + } + } + + for(PolicySchemaObj pol: par.getPolicy_schema()) { + if(pol.getEntry_schema() != null) { + PolicyProperties prop = new PolicyProperties(); + prop.setType("map"); + HashMap schema = new HashMap(); + schema.put("type", "onap.datatypes." + pol.getName()); +// prop.setType("list"); +// ArrayList schema = new ArrayList(); +// schema.add("type: onap.data." + pol.getName()); + prop.setEntry_schema(schema); + properties.put(pol.getName(), prop); + dataType = translateEntrySchema(dataType, pol.getEntry_schema(), pol.getName()); + } + else { + PolicyProperties prop = new PolicyProperties(); + prop.setType(pol.getType()); + properties.put(pol.getName(), prop); + } + } + + node.setProperties(properties); + dataType.put("onap.datatypes." + param, node); + return dataType; + } + + private TreeMap translateEntrySchema(TreeMap dataType, EntrySchemaObj[] entry, String name){ + TreeMap data = dataType; + PolicyModelNode node = new PolicyModelNode(); + node.setDerived_from("tosca.nodes.Root"); + TreeMap properties = new TreeMap(); + + for(EntrySchemaObj e: entry) { + if(e.getEntry_schema() != null) { + PolicyProperties prop = new PolicyProperties(); + prop.setType("list"); + ArrayList schema = new ArrayList(); + schema.add("type: onap.datatypes." + e.getName()); + prop.setEntry_schema(schema); + properties.put(e.getName(), prop); + data = translateEntrySchema(data, e.getEntry_schema(), e.getName()); + node.setProperties(properties); + } else { + PolicyProperties prop = new PolicyProperties(); + prop.setType(e.getType()); + properties.put(e.getName(), prop); + node.setProperties(properties); + } + } + + dataType.put("onap.datatypes." + name, node); + return data; + } + +} diff --git a/blueprint-generator/src/main/java/org/onap/blueprintgenerator/models/policymodel/PolicyProperties.java b/blueprint-generator/src/main/java/org/onap/blueprintgenerator/models/policymodel/PolicyProperties.java new file mode 100644 index 0000000..6faf78d --- /dev/null +++ b/blueprint-generator/src/main/java/org/onap/blueprintgenerator/models/policymodel/PolicyProperties.java @@ -0,0 +1,41 @@ +/**============LICENSE_START======================================================= + org.onap.dcae + ================================================================================ + Copyright (c) 2019 AT&T Intellectual Property. 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. + ============LICENSE_END========================================================= + +*/ + +package org.onap.blueprintgenerator.models.policymodel; + +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.TreeMap; + +import org.onap.blueprintgenerator.models.blueprint.Node; + +import com.fasterxml.jackson.annotation.JsonInclude; + +import lombok.Getter; +import lombok.Setter; + +@Getter @Setter +@JsonInclude(JsonInclude.Include.NON_NULL) +public class PolicyProperties { + + private String type; + private Object entry_schema; + +} diff --git a/blueprint-generator/src/test/java/org/onap/blueprintgenerator/core/BlueprintGeneratorTest.java b/blueprint-generator/src/test/java/org/onap/blueprintgenerator/core/BlueprintGeneratorTest.java index 91dec26..3eeffec 100644 --- a/blueprint-generator/src/test/java/org/onap/blueprintgenerator/core/BlueprintGeneratorTest.java +++ b/blueprint-generator/src/test/java/org/onap/blueprintgenerator/core/BlueprintGeneratorTest.java @@ -56,7 +56,7 @@ import org.onap.blueprintgenerator.models.componentspec.Subscribes; import org.onap.blueprintgenerator.models.componentspec.Volumes; import org.onap.blueprintgenerator.models.dmaapbp.DmaapNode; import org.onap.blueprintgenerator.models.onapbp.OnapNode; - +import org.onap.blueprintgenerator.models.policymodel.PolicyModel; import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.databind.JsonMappingException; @@ -421,4 +421,15 @@ public class BlueprintGeneratorTest { boolean t = true; assertEquals(true, t); } + + @Test + public void testPolicyModels() { + ComponentSpec cs = new ComponentSpec(); + cs.createComponentSpecFromFile("TestCases/testComponentSpec.json"); + + PolicyModel p = new PolicyModel(); + p.createPolicyModels(cs, "TestModels"); + + assertEquals(true, true); + } } diff --git a/blueprint-generator/version.properties b/blueprint-generator/version.properties index 7b8b963..00ef564 100644 --- a/blueprint-generator/version.properties +++ b/blueprint-generator/version.properties @@ -1,5 +1,5 @@ major=1 -minor=1 +minor=2 patch=0 base_version=${major}.${minor}.${patch} release_version=${base_version} -- cgit 1.2.3-korg