Fixed the Policy API issues and Bugfixes
[policy/engine.git] / ECOMP-PAP-REST / src / main / java / org / openecomp / policy / pap / xacml / rest / components / SafePolicyBuilder.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ECOMP-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.openecomp.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.openecomp.policy.common.logging.eelf.PolicyLogger;
28 import org.openecomp.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) {
49                 for(Map.Entry<String,String> map: generateMap.entrySet()){
50                         Pattern p = Pattern.compile("\\$\\{" +map.getKey() +"\\}");
51                         Matcher m = p.matcher(xacmlFileContent);
52                         String finalInput = map.getValue();
53                         if(finalInput.contains("$")){
54                                 finalInput = finalInput.replace("$", "\\$");
55                         }
56                         xacmlFileContent=m.replaceAll(finalInput);
57                 }
58                 if(blacklist!=null && !blacklist.isEmpty()){
59                         StringBuilder rule = new StringBuilder();
60                         for(String blackListName : blacklist){
61                                 if(blackListName.contains("$")){
62                                         blackListName = blackListName.replace("$", "\\$");
63                                 }
64                                 rule.append("<AttributeValue DataType=\"http://www.w3.org/2001/XMLSchema#string\">"+blackListName+"</AttributeValue>");
65                         }
66                         Pattern p = Pattern.compile("\\$\\{blackListElement\\}");
67                         Matcher m = p.matcher(xacmlFileContent);
68                         xacmlFileContent=m.replaceAll(rule.toString());
69                 }
70                 PolicyLogger.info("Generated XACML from the YAML Spec: \n" + xacmlFileContent);
71                 return xacmlFileContent;
72         }
73 }