Adjust BP-gen to correctly support DFC component spec - types
[dcaegen2/platform.git] / mod / bpgenerator / src / main / java / org / onap / blueprintgenerator / models / policymodel / PolicyModel.java
1 /*============LICENSE_START=======================================================
2  org.onap.dcae
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
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
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=========================================================
20
21 */
22
23 package org.onap.blueprintgenerator.models.policymodel;
24
25 import static org.onap.blueprintgenerator.models.blueprint.BpConstants.TOSCA_SIMPLE_YAML;
26
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;
34 import java.io.File;
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;
40 import lombok.Getter;
41 import lombok.Setter;
42 import org.onap.blueprintgenerator.models.componentspec.ComponentSpec;
43 import org.onap.blueprintgenerator.models.componentspec.Parameters;
44
45 @Getter
46 @Setter
47 @JsonInclude(JsonInclude.Include.NON_NULL)
48
49 public class PolicyModel {
50
51     private String tosca_definition_version;
52     private TreeMap<String, PolicyModelNode> node_types;
53     private TreeMap<String, PolicyModelNode> data_types;
54
55     public ArrayList<PolicyModel> createPolicyModels(ComponentSpec cs, String filePath) {
56         ArrayList<PolicyModel> models = new ArrayList();
57         Parameters[] params = cs.getParameters();
58
59         ArrayList<String> groups = new ArrayList<String>();
60         groups = getModelGroups(params);
61
62         for (String s : groups) {
63             PolicyModel model = new PolicyModel();
64             model = model.createPolicyModel(s, params);
65             //models.add(model);
66             policyModelToYaml(filePath, model, s);
67         }
68
69 //              for(PolicyModel p: models) {
70 //                      policyModelToYaml(filePath, p);
71 //              }
72
73         return models;
74     }
75
76     public ArrayList<String> getModelGroups(Parameters[] params) {
77         ArrayList<String> groups = new ArrayList();
78
79         for (Parameters p : params) {
80             if (p.isPolicy_editable()) {
81                 if (groups.isEmpty()) {
82                     groups.add(p.getPolicy_group());
83                 } else {
84                     if (!groups.contains(p.getPolicy_group())) {
85                         groups.add(p.getPolicy_group());
86                     }
87                 }
88             }
89         }
90
91         return groups;
92     }
93
94     public PolicyModel createPolicyModel(String s, Parameters[] params) {
95         PolicyModel model = new PolicyModel();
96         model.setTosca_definition_version(TOSCA_SIMPLE_YAML);
97
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);
104
105         if (!hasEntryScheme.equals("")) {
106             PolicyModelNode data = new PolicyModelNode();
107             TreeMap<String, PolicyModelNode> dataType = data.createDataTypes(hasEntryScheme, params);
108             model.setData_types(dataType);
109         }
110
111         return model;
112     }
113
114     public void policyModelToYaml(String path, PolicyModel p, String name) {
115         File outputFile;
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();
122
123         try {
124             PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(outputFile, true)));
125         } catch (IOException e) {
126             e.printStackTrace();
127         }
128
129         try {
130             policyMapper.writeValue(outputFile, p);
131         } catch (IOException e) {
132             e.printStackTrace();
133         }
134         System.out.println("model " + name + " created");
135     }
136 }