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.openecomp.policy.pap.xacml.rest.components;
23 import java.util.regex.Matcher;
24 import java.util.regex.Pattern;
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;
31 public class SafePolicyBuilder {
33 public static ControlLoopGuard loadYamlGuard(String specification) {
35 // Read the yaml into our Java Object
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;
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("$", "\\$");
51 xacmlFileContent=m.replaceAll(finalInput);
53 PolicyLogger.info("Generated XACML from the YAML Spec: \n" + xacmlFileContent);
55 return xacmlFileContent;