summaryrefslogtreecommitdiffstats
path: root/blueprint-generator/src/main/java/org/onap/blueprintgenerator/models/policymodel/PolicyModel.java
blob: 9c9bd514b3fd6507487ed20cdb7fcb18d3b05f18 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
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<String, PolicyModelNode> node_types;
	private TreeMap<String, PolicyModelNode> data_types;
	
	public ArrayList<PolicyModel> createPolicyModels(ComponentSpec cs, String filePath) {
		ArrayList<PolicyModel> models = new ArrayList();
		Parameters[] params = cs.getParameters();
		
		ArrayList<String> groups = new ArrayList<String>();
		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<String> getModelGroups(Parameters[] params) {
		ArrayList<String> 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<String, PolicyModelNode> nodeType = new TreeMap();
		nodeType.put(nodeTypeName, node);
		model.setNode_types(nodeType);
		
		if(!hasEntryScheme.equals("")) {
			PolicyModelNode data = new PolicyModelNode();
			TreeMap<String, PolicyModelNode> 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");
	}
}