bbc6259b7edeee46b810016cae8ae6e9d05ac9f3
[dcaegen2/platform.git] / mod / bpgenerator / onap / src / main / java / org / onap / policycreate / service / PolicyModelService.java
1 /*
2  *
3  *  * ============LICENSE_START=======================================================
4  *  *  org.onap.dcae
5  *  *  ================================================================================
6  *  *  Copyright (c) 2020  AT&T Intellectual Property. 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
24 package org.onap.policycreate.service;
25
26 import org.onap.blueprintgenerator.constants.Constants;
27 import org.onap.blueprintgenerator.model.componentspec.common.Parameters;
28 import org.onap.policycreate.exception.PolicyCreateException;
29 import org.onap.policycreate.model.PolicyModel;
30 import org.onap.policycreate.model.PolicyModelNode;
31 import com.fasterxml.jackson.databind.ObjectMapper;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.beans.factory.annotation.Qualifier;
34 import org.springframework.stereotype.Service;
35
36 import java.io.File;
37 import java.io.IOException;
38 import java.util.ArrayList;
39 import java.util.List;
40 import java.util.Map;
41 import java.util.TreeMap;
42
43 /**
44  * @author : Ravi Mantena
45  * @date 10/16/2020
46  * Application: ONAP - Blueprint Generator
47  * Service for Policy Model to create Policy Models, Policy Group Names and Convert Policy to Yaml
48  */
49
50
51 @Service("onapPolicyModelService")
52 public class PolicyModelService {
53
54     @Qualifier("yamlObjectMapper")
55     @Autowired
56     protected ObjectMapper yamlObjectMapper;
57
58     @Autowired
59     private PolicyModelNodeService policyModelNodeService;
60
61     // Method to create Policy Models
62     public void createPolicyModels(Parameters[] params, String filePath) {
63         try {
64             List<String> policyGroups = getPolicyGroupNames(params);
65             for (String s : policyGroups) {
66                 PolicyModel model = new PolicyModel();
67                 model.setTosca_definition_version(Constants.TOSCA_SIMPLE_YAML);
68
69                 Map<String, Object> response = policyModelNodeService.creatNodeType(s, params);
70                 String nodeTypeName = "onap.policy." + s;
71                 Map<String, PolicyModelNode> nodeType = new TreeMap<>();
72                 nodeType.put(nodeTypeName, (PolicyModelNode) response.get("policyModelNode"));
73                 model.setNode_types(nodeType);
74
75                 if (!"".equals(response.get("hasEntrySchema")))
76                     model.setData_types(policyModelNodeService.createDataTypes((String) response.get("hasEntrySchema"), params));
77                 policyModelToYaml(filePath, model, s);
78             }
79         } catch (Exception ex) {
80             throw new PolicyCreateException("Unable to create Policies from given input parameters", ex);
81         }
82     }
83
84     private List<String> getPolicyGroupNames(Parameters[] params) {
85         List<String> names = new ArrayList<>();
86         for (Parameters p : params) {
87             if (p.isPolicy_editable()) {
88                 if (names.isEmpty()) {
89                     names.add(p.getPolicy_group());
90                 } else if (!names.contains(p.getPolicy_group())) {
91                         names.add(p.getPolicy_group());
92                 }
93             }
94         }
95         return names;
96     }
97
98     private void policyModelToYaml(String path, PolicyModel model, String name) throws IOException {
99         File outputFile = new File(path, name + ".yml");
100         outputFile.getParentFile().mkdirs();
101         yamlObjectMapper.writeValue(outputFile, model);
102     }
103
104 }
105