2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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=========================================================
20 package org.onap.policy.pap.xacml.rest.components;
22 import java.util.List;
24 import java.util.regex.Matcher;
25 import java.util.regex.Pattern;
27 import org.onap.policy.common.logging.eelf.PolicyLogger;
28 import org.onap.policy.controlloop.policy.guard.ControlLoopGuard;
29 import org.yaml.snakeyaml.Yaml;
30 import org.yaml.snakeyaml.constructor.Constructor;
32 public class SafePolicyBuilder {
34 private SafePolicyBuilder(){
35 //Private Constructor.
38 public static ControlLoopGuard loadYamlGuard(String specification) {
40 // Read the yaml into our Java Object
42 PolicyLogger.info("Requested YAML to convert : " + specification);
43 Yaml yaml = new Yaml(new Constructor(ControlLoopGuard.class));
44 Object obj = yaml.load(specification);
45 return (ControlLoopGuard) obj;
48 public static String generateXacmlGuard(String xacmlFileContent,Map<String, String> generateMap, List<String> blacklist, List<String> targets) {
49 //Setup default values and Targets.
50 StringBuilder targetRegex= new StringBuilder(".*|");
51 if(targets!=null && !targets.isEmpty()){
52 targetRegex = new StringBuilder();
53 for(String t : targets){
54 targetRegex.append(t + "|");
57 if(generateMap.get("clname")==null|| generateMap.get("clname").isEmpty()){
58 generateMap.put("clname",".*");
60 generateMap.put("targets", targetRegex.toString().substring(0, targetRegex.length()-1));
62 for(Map.Entry<String,String> map: generateMap.entrySet()){
63 Pattern p = Pattern.compile("\\$\\{" +map.getKey() +"\\}");
64 Matcher m = p.matcher(xacmlFileContent);
65 String finalInput = map.getValue();
66 if(finalInput.contains("$")){
67 finalInput = finalInput.replace("$", "\\$");
69 xacmlFileContent=m.replaceAll(finalInput);
71 if(blacklist!=null && !blacklist.isEmpty()){
72 StringBuilder rule = new StringBuilder();
73 for(String blackListName : blacklist){
74 if(blackListName.contains("$")){
75 blackListName = blackListName.replace("$", "\\$");
77 rule.append("<AttributeValue DataType=\"http://www.w3.org/2001/XMLSchema#string\">"+blackListName+"</AttributeValue>");
79 Pattern p = Pattern.compile("\\$\\{blackListElement\\}");
80 Matcher m = p.matcher(xacmlFileContent);
81 xacmlFileContent=m.replaceAll(rule.toString());
83 PolicyLogger.info("Generated XACML from the YAML Spec: \n" + xacmlFileContent);
84 return xacmlFileContent;