3 * * ============LICENSE_START=======================================================
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
12 * * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
24 package org.onap.policycreate.service;
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;
37 import java.io.IOException;
38 import java.util.ArrayList;
39 import java.util.List;
41 import java.util.TreeMap;
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
48 @Service("onapPolicyModelService")
49 public class PolicyModelService {
51 @Qualifier("yamlObjectMapper")
53 protected ObjectMapper yamlObjectMapper;
56 private PolicyModelNodeService policyModelNodeService;
59 * Creates Policy Models
61 * @param params Parameters
62 * @param filePath File path
65 public void createPolicyModels(Parameters[] params, String filePath) {
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);
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);
78 if (!"".equals(response.get("hasEntrySchema"))) {
80 policyModelNodeService.createDataTypes(
81 (String) response.get("hasEntrySchema"), params));
83 policyModelToYaml(filePath, model, s);
85 } catch (Exception ex) {
86 throw new PolicyCreateException("Unable to create Policies from given input parameters",
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());
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);