7637719b177fd184c1e6142ce61af11e3d90cc57
[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.Map;
23 import java.util.regex.Matcher;
24 import java.util.regex.Pattern;
25
26 import org.openecomp.policy.common.logging.eelf.PolicyLogger;
27 import org.openecomp.policy.controlloop.policy.guard.ControlLoopGuard;
28 import org.yaml.snakeyaml.Yaml;
29 import org.yaml.snakeyaml.constructor.Constructor;
30
31 public class SafePolicyBuilder {
32
33         public static ControlLoopGuard loadYamlGuard(String specification) {
34                 //
35                 // Read the yaml into our Java Object
36                 //
37                 PolicyLogger.info("Requested YAML to convert : " + specification);
38                 Yaml yaml = new Yaml(new Constructor(ControlLoopGuard.class));
39                 Object obj = yaml.load(specification);
40                 return (ControlLoopGuard) obj;
41         }
42         
43         public static String    generateXacmlGuard(String xacmlFileContent,Map<String, String> generateMap) {
44                 for(String key: generateMap.keySet()){
45                         Pattern p = Pattern.compile("\\$\\{" +key +"\\}");
46                         Matcher m = p.matcher(xacmlFileContent);
47                         String finalInput = generateMap.get(key);
48                         if(finalInput.contains("$")){
49                                 finalInput = finalInput.replace("$", "\\$");
50                         }
51                         xacmlFileContent=m.replaceAll(finalInput);
52                 }
53                 PolicyLogger.info("Generated XACML from the YAML Spec: \n" + xacmlFileContent);
54
55                 return xacmlFileContent;
56         }
57 }