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=========================================================
21 package org.onap.policy.guard;
24 import java.io.IOException;
25 import java.nio.file.Files;
26 import java.nio.file.Path;
27 import java.nio.file.Paths;
28 import java.util.List;
30 import java.util.regex.Matcher;
31 import java.util.regex.Pattern;
33 import org.onap.policy.controlloop.policy.guard.ControlLoopGuard;
36 public class PolicyGuardYamlToXacml {
39 public static void fromYamlToXacml(String yamlFile, String xacmlTemplate, String xacmlPolicyOutput){
41 ControlLoopGuard yamlGuardObject = Util.loadYamlGuard(yamlFile);
42 System.out.println("actor: " + yamlGuardObject.guards.getFirst().actor);
43 System.out.println("recipe: " + yamlGuardObject.guards.getFirst().recipe);
44 System.out.println("num: " + yamlGuardObject.guards.getFirst().limit_constraints.getFirst().num);
45 System.out.println("duration: " + yamlGuardObject.guards.getFirst().limit_constraints.getFirst().duration);
46 System.out.println("time_in_range: " + yamlGuardObject.guards.getFirst().limit_constraints.getFirst().time_in_range);
48 Path xacmlTemplatePath = Paths.get(xacmlTemplate);
49 String xacmlTemplateContent;
52 xacmlTemplateContent = new String(Files.readAllBytes(xacmlTemplatePath));
54 String xacmlPolicyContent = generateXacmlGuard(xacmlTemplateContent,
55 yamlGuardObject.guards.getFirst().actor,
56 yamlGuardObject.guards.getFirst().recipe,
57 yamlGuardObject.guards.getFirst().limit_constraints.getFirst().num,
58 yamlGuardObject.guards.getFirst().limit_constraints.getFirst().duration,
59 yamlGuardObject.guards.getFirst().limit_constraints.getFirst().time_in_range.get("arg2"),
60 yamlGuardObject.guards.getFirst().limit_constraints.getFirst().time_in_range.get("arg3")
64 Files.write(Paths.get(xacmlPolicyOutput), xacmlPolicyContent.getBytes());
66 } catch (IOException e) {
67 // TODO Auto-generated catch block
75 public static String generateXacmlGuard(String xacmlFileContent,
79 Map<String,String> timeWindow,
80 String guardActiveStart,
81 String guardActiveEnd) {
83 Pattern p = Pattern.compile("\\$\\{actor\\}");
84 Matcher m = p.matcher(xacmlFileContent);
85 xacmlFileContent = m.replaceAll(actor);
87 p = Pattern.compile("\\$\\{recipe\\}");
88 m = p.matcher(xacmlFileContent);
89 xacmlFileContent = m.replaceAll(recipe);
91 p = Pattern.compile("\\$\\{limit\\}");
92 m = p.matcher(xacmlFileContent);
93 xacmlFileContent = m.replaceAll(limit.toString());
96 //p = Pattern.compile("\\$\\{timeWindow\\}");
97 //m = p.matcher(xacmlFileContent);
98 //xacmlFileContent = m.replaceAll("tw"+timeWindow);
100 p = Pattern.compile("\\$\\{twValue\\}");
101 m = p.matcher(xacmlFileContent);
102 xacmlFileContent = m.replaceAll(timeWindow.get("value"));
104 p = Pattern.compile("\\$\\{twUnits\\}");
105 m = p.matcher(xacmlFileContent);
106 xacmlFileContent = m.replaceAll(timeWindow.get("units"));
109 p = Pattern.compile("\\$\\{guardActiveStart\\}");
110 m = p.matcher(xacmlFileContent);
111 xacmlFileContent = m.replaceAll(guardActiveStart);
113 p = Pattern.compile("\\$\\{guardActiveEnd\\}");
114 m = p.matcher(xacmlFileContent);
115 xacmlFileContent = m.replaceAll(guardActiveEnd);
116 System.out.println(xacmlFileContent);
118 return xacmlFileContent;
123 public static void fromYamlToXacmlBlacklist(String yamlFile, String xacmlTemplate, String xacmlPolicyOutput){
125 ControlLoopGuard yamlGuardObject = Util.loadYamlGuard(yamlFile);
126 System.out.println("actor: " + yamlGuardObject.guards.getFirst().actor);
127 System.out.println("recipe: " + yamlGuardObject.guards.getFirst().recipe);
128 System.out.println("num: " + yamlGuardObject.guards.getFirst().limit_constraints.getFirst().num);
129 System.out.println("duration: " + yamlGuardObject.guards.getFirst().limit_constraints.getFirst().duration);
130 System.out.println("time_in_range: " + yamlGuardObject.guards.getFirst().limit_constraints.getFirst().time_in_range);
132 Path xacmlTemplatePath = Paths.get(xacmlTemplate);
133 String xacmlTemplateContent;
136 xacmlTemplateContent = new String(Files.readAllBytes(xacmlTemplatePath));
138 String xacmlPolicyContent = generateXacmlGuardBlacklist(xacmlTemplateContent,
139 yamlGuardObject.guards.getFirst().actor,
140 yamlGuardObject.guards.getFirst().recipe,
141 yamlGuardObject.guards.getFirst().limit_constraints.getFirst().blacklist,
142 yamlGuardObject.guards.getFirst().limit_constraints.getFirst().time_in_range.get("arg2"),
143 yamlGuardObject.guards.getFirst().limit_constraints.getFirst().time_in_range.get("arg3")
147 Files.write(Paths.get(xacmlPolicyOutput), xacmlPolicyContent.getBytes());
149 } catch (IOException e) {
150 // TODO Auto-generated catch block
156 public static String generateXacmlGuardBlacklist(String xacmlFileContent,
159 List<String> blacklist,
160 String guardActiveStart,
161 String guardActiveEnd) {
163 Pattern p = Pattern.compile("\\$\\{actor\\}");
164 Matcher m = p.matcher(xacmlFileContent);
165 xacmlFileContent = m.replaceAll(actor);
167 p = Pattern.compile("\\$\\{recipe\\}");
168 m = p.matcher(xacmlFileContent);
169 xacmlFileContent = m.replaceAll(recipe);
171 p = Pattern.compile("\\$\\{guardActiveStart\\}");
172 m = p.matcher(xacmlFileContent);
173 xacmlFileContent = m.replaceAll(guardActiveStart);
175 p = Pattern.compile("\\$\\{guardActiveEnd\\}");
176 m = p.matcher(xacmlFileContent);
177 xacmlFileContent = m.replaceAll(guardActiveEnd);
178 System.out.println(xacmlFileContent);
180 for(String target : blacklist){
181 p = Pattern.compile("\\$\\{blackListElement\\}");
182 m = p.matcher(xacmlFileContent);
183 xacmlFileContent = m.replaceAll("<AttributeValue DataType=\"http://www.w3.org/2001/XMLSchema#string\">"
185 + "</AttributeValue>"
186 + "\n\t\t\t\t\t\t\\$\\{blackListElement\\}");
189 p = Pattern.compile("\t\t\t\t\t\t\\$\\{blackListElement\\}\n");
190 m = p.matcher(xacmlFileContent);
191 xacmlFileContent = m.replaceAll("");
194 return xacmlFileContent;