1 /*============LICENSE_START=======================================================
3 ================================================================================
4 Copyright (c) 2019 AT&T Intellectual Property. All rights reserved.
5 ================================================================================
6 Modifications Copyright (c) 2020 Nokia. All rights reserved.
7 ================================================================================
8 Licensed under the Apache License, Version 2.0 (the "License");
9 you may not use this file except in compliance with the License.
10 You may obtain a copy of the License at
12 http://www.apache.org/licenses/LICENSE-2.0
14 Unless required by applicable law or agreed to in writing, software
15 distributed under the License is distributed on an "AS IS" BASIS,
16 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 See the License for the specific language governing permissions and
18 limitations under the License.
19 ============LICENSE_END=========================================================
23 package org.onap.blueprintgenerator.models.policymodel;
25 import static org.onap.blueprintgenerator.models.blueprint.BpConstants.TOSCA_SIMPLE_YAML;
27 import com.fasterxml.jackson.annotation.JsonInclude;
28 import com.fasterxml.jackson.core.JsonGenerationException;
29 import com.fasterxml.jackson.databind.JsonMappingException;
30 import com.fasterxml.jackson.databind.ObjectMapper;
31 import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
32 import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator;
33 import java.io.BufferedWriter;
35 import java.io.FileWriter;
36 import java.io.IOException;
37 import java.io.PrintWriter;
38 import java.util.ArrayList;
39 import java.util.TreeMap;
42 import org.onap.blueprintgenerator.models.componentspec.ComponentSpec;
43 import org.onap.blueprintgenerator.models.componentspec.Parameters;
47 @JsonInclude(JsonInclude.Include.NON_NULL)
49 public class PolicyModel {
51 private String tosca_definition_version;
52 private TreeMap<String, PolicyModelNode> node_types;
53 private TreeMap<String, PolicyModelNode> data_types;
55 public ArrayList<PolicyModel> createPolicyModels(ComponentSpec cs, String filePath) {
56 ArrayList<PolicyModel> models = new ArrayList();
57 Parameters[] params = cs.getParameters();
59 ArrayList<String> groups = new ArrayList<String>();
60 groups = getModelGroups(params);
62 for (String s : groups) {
63 PolicyModel model = new PolicyModel();
64 model = model.createPolicyModel(s, params);
66 policyModelToYaml(filePath, model, s);
69 // for(PolicyModel p: models) {
70 // policyModelToYaml(filePath, p);
76 public ArrayList<String> getModelGroups(Parameters[] params) {
77 ArrayList<String> groups = new ArrayList();
79 for (Parameters p : params) {
80 if (p.isPolicy_editable()) {
81 if (groups.isEmpty()) {
82 groups.add(p.getPolicy_group());
84 if (!groups.contains(p.getPolicy_group())) {
85 groups.add(p.getPolicy_group());
94 public PolicyModel createPolicyModel(String s, Parameters[] params) {
95 PolicyModel model = new PolicyModel();
96 model.setTosca_definition_version(TOSCA_SIMPLE_YAML);
98 PolicyModelNode node = new PolicyModelNode();
99 String hasEntryScheme = node.createNodeType(s, params);
100 String nodeTypeName = "onap.policy." + s;
101 TreeMap<String, PolicyModelNode> nodeType = new TreeMap();
102 nodeType.put(nodeTypeName, node);
103 model.setNode_types(nodeType);
105 if (!hasEntryScheme.equals("")) {
106 PolicyModelNode data = new PolicyModelNode();
107 TreeMap<String, PolicyModelNode> dataType = data.createDataTypes(hasEntryScheme, params);
108 model.setData_types(dataType);
114 public void policyModelToYaml(String path, PolicyModel p, String name) {
116 String filePath = path + "/" + name + ".yml";
117 File policyFile = new File(filePath);
118 ObjectMapper policyMapper = new ObjectMapper(
119 new YAMLFactory().configure(YAMLGenerator.Feature.MINIMIZE_QUOTES, true));
120 outputFile = new File(path, name + ".yml");
121 outputFile.getParentFile().mkdirs();
124 PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(outputFile, true)));
125 } catch (IOException e) {
130 policyMapper.writeValue(outputFile, p);
131 } catch (IOException e) {
134 System.out.println("model " + name + " created");