[POLICY-73] replace openecomp for policy-engine
[policy/engine.git] / ONAP-PAP-REST / src / main / java / org / onap / policy / pap / xacml / rest / components / SafePolicyBuilder.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-PAP-REST
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20 package org.onap.policy.pap.xacml.rest.components;
21
22 import java.util.List;
23 import java.util.Map;
24 import java.util.regex.Matcher;
25 import java.util.regex.Pattern;
26
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;
31
32 public class SafePolicyBuilder {
33         
34         private SafePolicyBuilder(){
35                 //Private Constructor. 
36         }
37
38         public static ControlLoopGuard loadYamlGuard(String specification) {
39                 //
40                 // Read the yaml into our Java Object
41                 //
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;
46         }
47         
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 + "|");
55             }
56                 }
57                 if(generateMap.get("clname")==null|| generateMap.get("clname").isEmpty()){
58                         generateMap.put("clname",".*");
59                 }
60                 generateMap.put("targets", targetRegex.toString().substring(0, targetRegex.length()-1));
61                 // Replace values. 
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("$", "\\$");
68                         }
69                         xacmlFileContent=m.replaceAll(finalInput);
70                 }
71                 if(blacklist!=null && !blacklist.isEmpty()){
72                         StringBuilder rule = new StringBuilder();
73                         for(String blackListName : blacklist){
74                                 if(blackListName.contains("$")){
75                                         blackListName = blackListName.replace("$", "\\$");
76                                 }
77                                 rule.append("<AttributeValue DataType=\"http://www.w3.org/2001/XMLSchema#string\">"+blackListName+"</AttributeValue>");
78                         }
79                         Pattern p = Pattern.compile("\\$\\{blackListElement\\}");
80                         Matcher m = p.matcher(xacmlFileContent);
81                         xacmlFileContent=m.replaceAll(rule.toString());
82                 }
83                 PolicyLogger.info("Generated XACML from the YAML Spec: \n" + xacmlFileContent);
84                 return xacmlFileContent;
85         }
86 }