bp-gen code clone from cli repo
[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  Licensed under the Apache License, Version 2.0 (the "License"); 
7  you may not use this file except in compliance with the License. 
8  You may obtain a copy of the License at 
9
10       http://www.apache.org/licenses/LICENSE-2.0 
11
12  Unless required by applicable law or agreed to in writing, software 
13  distributed under the License is distributed on an "AS IS" BASIS, 
14  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
15  See the License for the specific language governing permissions and 
16  limitations under the License. 
17  ============LICENSE_END========================================================= 
18
19 */
20
21 package org.onap.blueprintgenerator.models.policymodel;
22
23 import java.io.BufferedWriter;
24 import java.io.File;
25 import java.io.FileWriter;
26 import java.io.IOException;
27 import java.io.PrintWriter;
28 import java.util.ArrayList;
29 import java.util.LinkedHashMap;
30 import java.util.TreeMap;
31
32 import org.onap.blueprintgenerator.models.blueprint.Node;
33 import org.onap.blueprintgenerator.models.componentspec.ComponentSpec;
34 import org.onap.blueprintgenerator.models.componentspec.Parameters;
35
36 import com.fasterxml.jackson.annotation.JsonInclude;
37 import com.fasterxml.jackson.core.JsonGenerationException;
38 import com.fasterxml.jackson.databind.JsonMappingException;
39 import com.fasterxml.jackson.databind.ObjectMapper;
40 import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
41 import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator;
42
43 import lombok.Getter;
44 import lombok.Setter;
45
46 @Getter @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_1_0_0");
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(new YAMLFactory().configure(YAMLGenerator.Feature.MINIMIZE_QUOTES, true));
119                         outputFile = new File(path, name + ".yml");
120                         outputFile.getParentFile().mkdirs();
121
122                         try {
123                                 PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(outputFile, true)));
124                         } catch (IOException e) {
125                                 e.printStackTrace();
126                         }
127
128                         try {
129                                 policyMapper.writeValue(outputFile, p);
130                         } catch (JsonGenerationException e) {
131                                 e.printStackTrace();
132                         } catch (JsonMappingException e) {
133                                 e.printStackTrace();
134                         } catch (IOException e) {
135                                 e.printStackTrace();
136                         }
137                         System.out.println("model " + name + " created");
138         }
139 }