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
46 * Application: ONAP - Blueprint Generator
47 * Service for Policy Model to create Policy Models, Policy Group Names and Convert Policy to Yaml
51 @Service("onapPolicyModelService")
52 public class PolicyModelService {
54 @Qualifier("yamlObjectMapper")
56 protected ObjectMapper yamlObjectMapper;
59 private PolicyModelNodeService policyModelNodeService;
61 // Method to create Policy Models
62 public void createPolicyModels(Parameters[] params, String filePath) {
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);
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);
75 if (!"".equals(response.get("hasEntrySchema")))
76 model.setData_types(policyModelNodeService.createDataTypes((String) response.get("hasEntrySchema"), params));
77 policyModelToYaml(filePath, model, s);
79 } catch (Exception ex) {
80 throw new PolicyCreateException("Unable to create Policies from given input parameters", ex);
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());
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);