2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017, 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.pap.xacml.rest.components;
23 import java.util.List;
25 import java.util.regex.Matcher;
26 import java.util.regex.Pattern;
28 import org.onap.policy.common.logging.eelf.PolicyLogger;
29 import org.onap.policy.controlloop.policy.guard.ControlLoopGuard;
30 import org.yaml.snakeyaml.Yaml;
31 import org.yaml.snakeyaml.constructor.Constructor;
33 public class SafePolicyBuilder {
35 private SafePolicyBuilder() {
36 // Private Constructor.
39 public static ControlLoopGuard loadYamlGuard(String specification) {
41 // Read the yaml into our Java Object
43 PolicyLogger.info("Requested YAML to convert : " + specification);
44 Yaml yaml = new Yaml(new Constructor(ControlLoopGuard.class));
45 Object obj = yaml.load(specification);
46 return (ControlLoopGuard) obj;
49 public static String generateXacmlGuard(String xacmlFileContent, Map<String, String> generateMap,
50 List<String> blacklist, List<String> targets) {
51 // Setup default values and Targets.
52 StringBuilder targetRegex = new StringBuilder(".*|");
53 if (targets != null && !targets.isEmpty()) {
54 targetRegex = new StringBuilder();
55 for (String t : targets) {
56 targetRegex.append(t + "|");
59 if (generateMap.get("clname") == null || generateMap.get("clname").isEmpty()) {
60 generateMap.put("clname", ".*");
62 generateMap.put("targets", targetRegex.toString().substring(0, targetRegex.length() - 1));
64 for (Map.Entry<String, String> map : generateMap.entrySet()) {
65 Pattern p = Pattern.compile("\\$\\{" + map.getKey() + "\\}");
66 Matcher m = p.matcher(xacmlFileContent);
67 String finalInput = map.getValue();
68 if (finalInput.contains("$")) {
69 finalInput = finalInput.replace("$", "\\$");
71 xacmlFileContent = m.replaceAll(finalInput);
73 if (blacklist != null && !blacklist.isEmpty()) {
74 StringBuilder rule = new StringBuilder();
75 for (String blackListName : blacklist) {
76 if (blackListName.contains("$")) {
77 blackListName = blackListName.replace("$", "\\$");
79 rule.append("<AttributeValue DataType=\"http://www.w3.org/2001/XMLSchema#string\">" + blackListName
80 + "</AttributeValue>");
82 Pattern p = Pattern.compile("\\$\\{blackListElement\\}");
83 Matcher m = p.matcher(xacmlFileContent);
84 xacmlFileContent = m.replaceAll(rule.toString());
86 PolicyLogger.info("Generated XACML from the YAML Spec: \n" + xacmlFileContent);
87 return xacmlFileContent;