BpGen refactor Code Quality Issue-ID: DCAEGEN2-2502
[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 Application: ONAP - Blueprint Generator Service for Policy Model to create
46  * Policy Models, Policy Group Names and Convert Policy to Yaml
47  */
48 @Service("onapPolicyModelService")
49 public class PolicyModelService {
50
51     @Qualifier("yamlObjectMapper")
52     @Autowired
53     protected ObjectMapper yamlObjectMapper;
54
55     @Autowired
56     private PolicyModelNodeService policyModelNodeService;
57
58     /**
59      * Creates Policy Models
60      *
61      * @param params Parameters
62      * @param filePath File path
63      * @return
64      */
65     public void createPolicyModels(Parameters[] params, String filePath) {
66         try {
67             List<String> policyGroups = getPolicyGroupNames(params);
68             for (String s : policyGroups) {
69                 PolicyModel model = new PolicyModel();
70                 model.setTosca_definition_version(Constants.TOSCA_SIMPLE_YAML);
71
72                 Map<String, Object> response = policyModelNodeService.creatNodeType(s, params);
73                 String nodeTypeName = "onap.policy." + s;
74                 Map<String, PolicyModelNode> nodeType = new TreeMap<>();
75                 nodeType.put(nodeTypeName, (PolicyModelNode) response.get("policyModelNode"));
76                 model.setNode_types(nodeType);
77
78                 if (!"".equals(response.get("hasEntrySchema"))) {
79                     model.setData_types(
80                         policyModelNodeService.createDataTypes(
81                             (String) response.get("hasEntrySchema"), params));
82                 }
83                 policyModelToYaml(filePath, model, s);
84             }
85         } catch (Exception ex) {
86             throw new PolicyCreateException("Unable to create Policies from given input parameters",
87                 ex);
88         }
89     }
90
91     private List<String> getPolicyGroupNames(Parameters[] params) {
92         List<String> names = new ArrayList<>();
93         for (Parameters p : params) {
94             if (p.isPolicy_editable()) {
95                 if (names.isEmpty()) {
96                     names.add(p.getPolicy_group());
97                 } else if (!names.contains(p.getPolicy_group())) {
98                     names.add(p.getPolicy_group());
99                 }
100             }
101         }
102         return names;
103     }
104
105     private void policyModelToYaml(String path, PolicyModel model, String name) throws IOException {
106         File outputFile = new File(path, name + ".yml");
107         outputFile.getParentFile().mkdirs();
108         yamlObjectMapper.writeValue(outputFile, model);
109     }
110 }