[POLICY-22] Reorganizing drools-apps
[policy/drools-applications.git] / controlloop / common / guard / src / main / java / org / onap / policy / guard / PolicyGuardYamlToXacml.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * guard
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
21 package org.onap.policy.guard;
22
23
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;
29 import java.util.Map;
30 import java.util.regex.Matcher;
31 import java.util.regex.Pattern;
32
33 import org.onap.policy.controlloop.policy.guard.ControlLoopGuard;
34
35
36 public class PolicyGuardYamlToXacml {
37         
38         
39         public static void fromYamlToXacml(String yamlFile, String xacmlTemplate, String xacmlPolicyOutput){
40                 
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);
47                 
48                 Path xacmlTemplatePath = Paths.get(xacmlTemplate);
49         String xacmlTemplateContent;
50                 
51         try {
52                         xacmlTemplateContent = new String(Files.readAllBytes(xacmlTemplatePath));
53                         
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")
61                                 );
62                 
63         
64                 Files.write(Paths.get(xacmlPolicyOutput), xacmlPolicyContent.getBytes());
65         
66                 } catch (IOException e) {
67                         // TODO Auto-generated catch block
68                         e.printStackTrace();
69                 }
70                 
71         }
72         
73         
74         
75         public static String    generateXacmlGuard(String xacmlFileContent, 
76                         String actor, 
77                         String recipe, 
78                         Integer limit,
79                         Map<String,String> timeWindow,
80                         String guardActiveStart, 
81                         String guardActiveEnd) {
82
83                 Pattern p = Pattern.compile("\\$\\{actor\\}");
84                 Matcher m = p.matcher(xacmlFileContent);
85                 xacmlFileContent = m.replaceAll(actor);
86
87                 p = Pattern.compile("\\$\\{recipe\\}");
88                 m = p.matcher(xacmlFileContent);
89                 xacmlFileContent = m.replaceAll(recipe);
90
91                 p = Pattern.compile("\\$\\{limit\\}");
92                 m = p.matcher(xacmlFileContent);
93                 xacmlFileContent = m.replaceAll(limit.toString());
94                 
95                 
96                 //p = Pattern.compile("\\$\\{timeWindow\\}");
97                 //m = p.matcher(xacmlFileContent);
98                 //xacmlFileContent = m.replaceAll("tw"+timeWindow);
99                 
100                 p = Pattern.compile("\\$\\{twValue\\}");
101                 m = p.matcher(xacmlFileContent);
102                 xacmlFileContent = m.replaceAll(timeWindow.get("value"));
103                 
104                 p = Pattern.compile("\\$\\{twUnits\\}");
105                 m = p.matcher(xacmlFileContent);
106                 xacmlFileContent = m.replaceAll(timeWindow.get("units"));
107                 
108
109                 p = Pattern.compile("\\$\\{guardActiveStart\\}");
110                 m = p.matcher(xacmlFileContent);
111                 xacmlFileContent = m.replaceAll(guardActiveStart);
112
113                 p = Pattern.compile("\\$\\{guardActiveEnd\\}");
114                 m = p.matcher(xacmlFileContent);
115                 xacmlFileContent = m.replaceAll(guardActiveEnd);
116                 System.out.println(xacmlFileContent);
117
118                 return xacmlFileContent;
119         }
120         
121
122         
123         public static void fromYamlToXacmlBlacklist(String yamlFile, String xacmlTemplate, String xacmlPolicyOutput){
124                 
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);
131                 
132                 Path xacmlTemplatePath = Paths.get(xacmlTemplate);
133         String xacmlTemplateContent;
134                 
135         try {
136                         xacmlTemplateContent = new String(Files.readAllBytes(xacmlTemplatePath));
137                         
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")
144                                 );
145                 
146         
147                 Files.write(Paths.get(xacmlPolicyOutput), xacmlPolicyContent.getBytes());
148         
149                 } catch (IOException e) {
150                         // TODO Auto-generated catch block
151                         e.printStackTrace();
152                 }
153                 
154         }
155         
156         public static String    generateXacmlGuardBlacklist(String xacmlFileContent, 
157                         String actor, 
158                         String recipe, 
159                         List<String> blacklist,
160                         String guardActiveStart, 
161                         String guardActiveEnd) {
162
163                 Pattern p = Pattern.compile("\\$\\{actor\\}");
164                 Matcher m = p.matcher(xacmlFileContent);
165                 xacmlFileContent = m.replaceAll(actor);
166
167                 p = Pattern.compile("\\$\\{recipe\\}");
168                 m = p.matcher(xacmlFileContent);
169                 xacmlFileContent = m.replaceAll(recipe);
170                 
171                 p = Pattern.compile("\\$\\{guardActiveStart\\}");
172                 m = p.matcher(xacmlFileContent);
173                 xacmlFileContent = m.replaceAll(guardActiveStart);
174
175                 p = Pattern.compile("\\$\\{guardActiveEnd\\}");
176                 m = p.matcher(xacmlFileContent);
177                 xacmlFileContent = m.replaceAll(guardActiveEnd);
178                 System.out.println(xacmlFileContent);
179                 
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\">" 
184                                                                                         + target
185                                                                                         + "</AttributeValue>"
186                                                                                         + "\n\t\t\t\t\t\t\\$\\{blackListElement\\}");
187                 }
188                 
189                 p = Pattern.compile("\t\t\t\t\t\t\\$\\{blackListElement\\}\n");
190                 m = p.matcher(xacmlFileContent);
191                 xacmlFileContent = m.replaceAll("");
192                 
193                 
194                 return xacmlFileContent;
195         }
196         
197         
198 }