Consolidate PolicyRestAdapter setup
[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, 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
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
21 package org.onap.policy.pap.xacml.rest.components;
22
23 import java.util.List;
24 import java.util.Map;
25 import java.util.regex.Matcher;
26 import java.util.regex.Pattern;
27
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;
32
33 public class SafePolicyBuilder {
34
35     private SafePolicyBuilder() {
36         // Private Constructor.
37     }
38
39     public static ControlLoopGuard loadYamlGuard(String specification) {
40         //
41         // Read the yaml into our Java Object
42         //
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;
47     }
48
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 + "|");
57             }
58         }
59         if (generateMap.get("clname") == null || generateMap.get("clname").isEmpty()) {
60             generateMap.put("clname", ".*");
61         }
62         generateMap.put("targets", targetRegex.toString().substring(0, targetRegex.length() - 1));
63         // Replace values.
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("$", "\\$");
70             }
71             xacmlFileContent = m.replaceAll(finalInput);
72         }
73         if (blacklist != null && !blacklist.isEmpty()) {
74             StringBuilder rule = new StringBuilder();
75             for (String blackListName : blacklist) {
76                 if (blackListName.contains("$")) {
77                     blackListName = blackListName.replace("$", "\\$");
78                 }
79                 rule.append("<AttributeValue DataType=\"http://www.w3.org/2001/XMLSchema#string\">" + blackListName
80                         + "</AttributeValue>");
81             }
82             Pattern p = Pattern.compile("\\$\\{blackListElement\\}");
83             Matcher m = p.matcher(xacmlFileContent);
84             xacmlFileContent = m.replaceAll(rule.toString());
85         }
86         PolicyLogger.info("Generated XACML from the YAML Spec: \n" + xacmlFileContent);
87         return xacmlFileContent;
88     }
89 }