1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
|
/*-
* ============LICENSE_START=======================================================
* demo
* ================================================================================
* Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ============LICENSE_END=========================================================
*/
package org.onap.policy.controlloop.processor;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Instant;
import java.util.HashMap;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.junit.Ignore;
import org.junit.Test;
import org.kie.api.KieServices;
import org.kie.api.builder.KieBuilder;
import org.kie.api.builder.KieFileSystem;
import org.kie.api.builder.Message;
import org.kie.api.builder.ReleaseId;
import org.kie.api.builder.Results;
import org.kie.api.builder.model.KieModuleModel;
import org.kie.api.event.rule.AfterMatchFiredEvent;
import org.kie.api.event.rule.AgendaEventListener;
import org.kie.api.event.rule.AgendaGroupPoppedEvent;
import org.kie.api.event.rule.AgendaGroupPushedEvent;
import org.kie.api.event.rule.BeforeMatchFiredEvent;
import org.kie.api.event.rule.MatchCancelledEvent;
import org.kie.api.event.rule.MatchCreatedEvent;
import org.kie.api.event.rule.ObjectDeletedEvent;
import org.kie.api.event.rule.ObjectInsertedEvent;
import org.kie.api.event.rule.ObjectUpdatedEvent;
import org.kie.api.event.rule.RuleFlowGroupActivatedEvent;
import org.kie.api.event.rule.RuleFlowGroupDeactivatedEvent;
import org.kie.api.event.rule.RuleRuntimeEventListener;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.kie.api.runtime.rule.FactHandle;
import org.onap.policy.appc.Request;
import org.onap.policy.appc.Response;
import org.onap.policy.appc.ResponseCode;
import org.onap.policy.appc.ResponseValue;
import org.onap.policy.controlloop.ControlLoopEventStatus;
import org.onap.policy.controlloop.ControlLoopNotificationType;
import org.onap.policy.controlloop.VirtualControlLoopEvent;
import org.onap.policy.controlloop.VirtualControlLoopNotification;
import org.onap.policy.controlloop.ControlLoopLogger;
import org.onap.policy.controlloop.impl.ControlLoopLoggerStdOutImpl;
import org.onap.policy.controlloop.policy.ControlLoopPolicy;
import org.onap.policy.controlloop.policy.TargetType;
import org.onap.policy.drools.impl.PolicyEngineJUnitImpl;
import org.onap.policy.guard.PolicyGuard;
import org.onap.policy.guard.PolicyGuardYamlToXacml;
import com.att.research.xacml.api.pdp.PDPEngine;
import com.att.research.xacml.api.pdp.PDPEngineFactory;
import com.att.research.xacml.util.FactoryException;
import com.att.research.xacml.util.XACMLProperties;
import org.onap.policy.controlloop.policy.guard.ControlLoopGuard;
public class ControlLoopXacmlGuardTest {
@Ignore
@Test
public void test() {
try {
this.runTest("src/main/resources/ControlLoop_Template_1707_xacml_guard.drl",
"src/test/resources/yaml/policy_ControlLoop_vUSP_1707.yaml",
"service=vUSP;resource=vCTS;type=operational",
"CL_VUSP_8888",
"com.att.ecomp.closed_loop.vUSP:VNFS:0.0.1");
} catch (IOException e) {
e.printStackTrace();
fail(e.getMessage());
}
}
public void runTest(String droolsTemplate,
String yamlFile,
String policyScope,
String policyName,
String policyVersion) throws IOException {
//
// Pull info from the yaml
//
final Util.Pair<ControlLoopPolicy, String> pair = Util.loadYaml(yamlFile);
assertNotNull(pair);
assertNotNull(pair.a);
assertNotNull(pair.a.controlLoop);
assertNotNull(pair.a.controlLoop.controlLoopName);
assertTrue(pair.a.controlLoop.controlLoopName.length() > 0);
//
// Build a container
//
final KieSession kieSession = buildContainer(droolsTemplate,
pair.a.controlLoop.controlLoopName,
policyScope,
policyName,
policyVersion,
URLEncoder.encode(pair.b, "UTF-8"));
System.out.println("============");
System.out.println(URLEncoder.encode(pair.b, "UTF-8"));
System.out.println("============");
kieSession.addEventListener(new RuleRuntimeEventListener() {
@Override
public void objectInserted(ObjectInsertedEvent event) {
}
@Override
public void objectUpdated(ObjectUpdatedEvent event) {
}
@Override
public void objectDeleted(ObjectDeletedEvent event) {
}
});
kieSession.addEventListener(new AgendaEventListener() {
@Override
public void matchCreated(MatchCreatedEvent event) {
//System.out.println("matchCreated: " + event.getMatch().getRule());
}
@Override
public void matchCancelled(MatchCancelledEvent event) {
}
@Override
public void beforeMatchFired(BeforeMatchFiredEvent event) {
//System.out.println("beforeMatchFired: " + event.getMatch().getRule() + event.getMatch().getObjects());
}
@Override
public void afterMatchFired(AfterMatchFiredEvent event) {
}
@Override
public void agendaGroupPopped(AgendaGroupPoppedEvent event) {
}
@Override
public void agendaGroupPushed(AgendaGroupPushedEvent event) {
}
@Override
public void beforeRuleFlowGroupActivated(RuleFlowGroupActivatedEvent event) {
}
@Override
public void afterRuleFlowGroupActivated(RuleFlowGroupActivatedEvent event) {
}
@Override
public void beforeRuleFlowGroupDeactivated(RuleFlowGroupDeactivatedEvent event) {
}
@Override
public void afterRuleFlowGroupDeactivated(RuleFlowGroupDeactivatedEvent event) {
}
});
//
// Create XACML Guard policy from YAML
// We prepare 4 Guards. Notice that Rebuilds recipe has two Guards (for checking policy combining algorithm)
//
fromYamlToXacml("src/test/resources/yaml/policy_guard_vUSP_1707_appc_restart.yaml",
"src/main/resources/frequency_limiter_template.xml",
"src/test/resources/xacml/autogenerated_frequency_limiter_restart.xml");
fromYamlToXacml("src/test/resources/yaml/policy_guard_vUSP_1707_appc_rebuild.yaml",
"src/main/resources/frequency_limiter_template.xml",
"src/test/resources/xacml/autogenerated_frequency_limiter_rebuild.xml");
fromYamlToXacml("src/test/resources/yaml/policy_guard_vUSP_1707_appc_rebuild_1.yaml",
"src/main/resources/frequency_limiter_template.xml",
"src/test/resources/xacml/autogenerated_frequency_limiter_rebuild_1.xml");
fromYamlToXacml("src/test/resources/yaml/policy_guard_vUSP_1707_appc_migrate.yaml",
"src/main/resources/frequency_limiter_template.xml",
"src/test/resources/xacml/autogenerated_frequency_limiter_migrate.xml");
PolicyGuardYamlToXacml.fromYamlToXacmlBlacklist("src/test/resources/yaml/policy_guard_vUSP_1707_appc_restart_blacklist.yaml",
"src/main/resources/blacklist_template.xml",
"src/test/resources/xacml/autogenerated_blacklist.xml");
//
// Insert our globals
//
final ControlLoopLogger logger = new ControlLoopLoggerStdOutImpl();
kieSession.setGlobal("Logger", logger);
final PolicyEngineJUnitImpl engine = new PolicyEngineJUnitImpl();
kieSession.setGlobal("Engine", engine);
//
// Creating an embedded XACML PDP
//
final PDPEngine xacmlPdpEngine;
System.setProperty(XACMLProperties.XACML_PROPERTIES_NAME, "src/test/resources/xacml/xacml_guard.properties");
PDPEngineFactory factory;
try {
factory = PDPEngineFactory.newInstance();
xacmlPdpEngine = factory.newEngine();
kieSession.setGlobal("XacmlPdpEngine", xacmlPdpEngine);
} catch (FactoryException e1) {
e1.printStackTrace();
}
//
// Initial fire of rules
//
kieSession.fireAllRules();
//
// Kick a thread that starts testing
//
new Thread(new Runnable() {
@Override
public void run() {
try {
//
// Let's use a unique ID for the request and
// a unique trigger source.
//
UUID requestID = UUID.randomUUID();
String triggerSourceName = "foobartriggersource36";
Object obj = null;
sendGoodEvents(kieSession, pair.a, requestID, triggerSourceName);
obj = engine.subscribe("UEB", "POLICY-CL-MGT");
assertNotNull(obj);
assertTrue(obj instanceof VirtualControlLoopNotification);
assertTrue(((VirtualControlLoopNotification)obj).notification.equals(ControlLoopNotificationType.ACTIVE));
//
// Give the control loop a little time to acquire the lock and publish the request
//
Thread.sleep(2000);
// "About to query Guard" notification (Querying about Restart)
obj = engine.subscribe("UEB", "POLICY-CL-MGT");
assertNotNull(obj);
System.out.println("\n\n####################### GOING TO QUERY GUARD about Restart!!!!!!");
System.out.println("Rule: " + ((VirtualControlLoopNotification)obj).policyName +" Message: " + ((VirtualControlLoopNotification)obj).message);
assertTrue(obj instanceof VirtualControlLoopNotification);
assertTrue(((VirtualControlLoopNotification)obj).notification.equals(ControlLoopNotificationType.OPERATION));
Thread.sleep(2000);
// "Response from Guard" notification
obj = engine.subscribe("UEB", "POLICY-CL-MGT");
assertNotNull(obj);
System.out.println("Rule: " + ((VirtualControlLoopNotification)obj).policyName +" Message: " + ((VirtualControlLoopNotification)obj).message);
assertTrue(obj instanceof VirtualControlLoopNotification);
assertTrue(((VirtualControlLoopNotification)obj).notification.equals(ControlLoopNotificationType.OPERATION));
if(true == ((VirtualControlLoopNotification)obj).message.contains("Guard result: Deny")){
// "About to query Guard" notification (Querying about Rebuild)
obj = engine.subscribe("UEB", "POLICY-CL-MGT");
assertNotNull(obj);
System.out.println("\n\n####################### GOING TO QUERY GUARD about Rebuild!!!!!!");
System.out.println("Rule: " + ((VirtualControlLoopNotification)obj).policyName +" Message: " + ((VirtualControlLoopNotification)obj).message);
assertTrue(obj instanceof VirtualControlLoopNotification);
assertTrue(((VirtualControlLoopNotification)obj).notification.equals(ControlLoopNotificationType.OPERATION));
Thread.sleep(2000);
// "Response from Guard" notification
obj = engine.subscribe("UEB", "POLICY-CL-MGT");
assertNotNull(obj);
System.out.println("Rule: " + ((VirtualControlLoopNotification)obj).policyName +" Message: " + ((VirtualControlLoopNotification)obj).message);
assertTrue(obj instanceof VirtualControlLoopNotification);
assertTrue(((VirtualControlLoopNotification)obj).notification.equals(ControlLoopNotificationType.OPERATION));
if(true == ((VirtualControlLoopNotification)obj).message.contains("Guard result: Deny")){
// "About to query Guard" notification (Querying about Migrate)
obj = engine.subscribe("UEB", "POLICY-CL-MGT");
assertNotNull(obj);
System.out.println("\n\n####################### GOING TO QUERY GUARD!!!!!!");
System.out.println("Rule: " + ((VirtualControlLoopNotification)obj).policyName +" Message: " + ((VirtualControlLoopNotification)obj).message);
assertTrue(obj instanceof VirtualControlLoopNotification);
assertTrue(((VirtualControlLoopNotification)obj).notification.equals(ControlLoopNotificationType.OPERATION));
Thread.sleep(2000);
// "Response from Guard" notification
obj = engine.subscribe("UEB", "POLICY-CL-MGT");
assertNotNull(obj);
System.out.println("Rule: " + ((VirtualControlLoopNotification)obj).policyName +" Message: " + ((VirtualControlLoopNotification)obj).message);
assertTrue(obj instanceof VirtualControlLoopNotification);
assertTrue(((VirtualControlLoopNotification)obj).notification.equals(ControlLoopNotificationType.OPERATION));
if(true == ((VirtualControlLoopNotification)obj).message.contains("Guard result: Deny")){
//All the 3 operations were Denied by Guard
Thread.sleep(30000);
}
}
}
//
// In case one of the operations was permitted by Guard
//
if(true == ((VirtualControlLoopNotification)obj).message.contains("Guard result: Permit")){
obj = engine.subscribe("UEB", "POLICY-CL-MGT");
assertNotNull(obj);
System.out.println("Rule: " + ((VirtualControlLoopNotification)obj).policyName +" Message: " + ((VirtualControlLoopNotification)obj).message);
assertTrue(obj instanceof VirtualControlLoopNotification);
assertTrue(((VirtualControlLoopNotification)obj).notification.equals(ControlLoopNotificationType.OPERATION));
Thread.sleep(500);
obj = engine.subscribe("UEB", "APPC-CL");
assertNotNull(obj);
assertTrue(obj instanceof Request);
assertTrue(((Request)obj).CommonHeader.SubRequestID.equals("1"));
System.out.println("\n============ APP-C Got request!!! ===========\n");
//
// Ok - let's simulate ACCEPT
//
//
// now wait for it to finish
//
Thread.sleep(500);
//
// Now we are going to success it
//
Response response = new Response((Request) obj);
response.Status.Code = ResponseCode.SUCCESS.getValue();
response.Status.Value = ResponseValue.SUCCESS.toString();
response.Status.Description = "AppC success";
kieSession.insert(response);
//
// Give it some time to process
//
Thread.sleep(2000);
//
// Insert the abatement event
//
sendAbatement(kieSession, pair.a, requestID, triggerSourceName);
//
// now wait for it to finish
//
Thread.sleep(5000);
//
// Ensure they released the lock
//
assertFalse(PolicyGuard.isLocked(TargetType.VM, triggerSourceName, requestID));
}
} catch (InterruptedException e) {
System.err.println("Test thread got InterruptedException " + e.getLocalizedMessage());
} catch (AssertionError e) {
System.err.println("Test thread got AssertionError " + e.getLocalizedMessage());
e.printStackTrace();
} catch (Exception e) {
System.err.println("Test thread got Exception " + e.getLocalizedMessage());
e.printStackTrace();
}
kieSession.halt();
}
}).start();
//
// Start firing rules
//
kieSession.fireUntilHalt();
//
// Dump working memory
//
dumpFacts(kieSession);
//
// See if there is anything left in memory
//
assertEquals(1, kieSession.getFactCount());
for (FactHandle handle : kieSession.getFactHandles()) {
Object fact = kieSession.getObject(handle);
assertEquals("", "com.att.ecomp.policy.controlloop.Params", fact.getClass().getName());
}
}
public static void dumpFacts(KieSession kieSession) {
System.out.println("Fact Count: " + kieSession.getFactCount());
for (FactHandle handle : kieSession.getFactHandles()) {
System.out.println("FACT: " + handle);
}
}
protected void sendAbatement(KieSession kieSession, ControlLoopPolicy policy, UUID requestID, String triggerSourceName) throws InterruptedException {
VirtualControlLoopEvent event = new VirtualControlLoopEvent();
event.closedLoopControlName = policy.controlLoop.controlLoopName;
event.requestID = requestID;
event.target = "vserver.vserver-name";
event.closedLoopAlarmStart = Instant.now().minusSeconds(5);
event.closedLoopAlarmEnd = Instant.now();
event.AAI = new HashMap<String, String>();
event.AAI.put("cloud-region.identity-url", "foo");
event.AAI.put("vserver.selflink", "bar");
event.AAI.put("vserver.is-closed-loop-disabled", "false");
event.AAI.put("generic-vnf.vnf-name", "testGenericVnfName");
event.closedLoopEventStatus = ControlLoopEventStatus.ABATED;
kieSession.insert(event);
}
protected void sendGoodEvents(KieSession kieSession, ControlLoopPolicy policy, UUID requestID, String triggerSourceName) throws InterruptedException {
VirtualControlLoopEvent event = new VirtualControlLoopEvent();
event.closedLoopControlName = policy.controlLoop.controlLoopName;
event.requestID = requestID;
event.target = "vserver.vserver-name";
event.closedLoopAlarmStart = Instant.now();
event.AAI = new HashMap<String, String>();
event.AAI.put("cloud-region.identity-url", "foo");
event.AAI.put("vserver.selflink", "bar");
event.AAI.put("vserver.is-closed-loop-disabled", "false");
event.AAI.put("vserver.vserver-name", "testGenericVnfName");
event.closedLoopEventStatus = ControlLoopEventStatus.ONSET;
kieSession.insert(event);
Thread.sleep(1000);
/*
event = new ATTControlLoopEvent(event);
event.triggerID = "107.250.169.145_f5BigIP" + Instant.now().toEpochMilli();
kieSession.insert(event);
Thread.sleep(1000);
event = new ATTControlLoopEvent(event);
event.triggerID = "107.250.169.145_f5BigIP" + Instant.now().toEpochMilli();
kieSession.insert(event);
Thread.sleep(1000);
event = new ATTControlLoopEvent(event);
event.triggerID = "107.250.169.145_f5BigIP" + Instant.now().toEpochMilli();
kieSession.insert(event);
Thread.sleep(1000);
*/
}
protected void sendBadEvents(KieSession kieSession, ControlLoopPolicy policy, UUID requestID, String triggerSourceName) throws InterruptedException {
//
// Insert a bad Event
//
VirtualControlLoopEvent event = new VirtualControlLoopEvent();
event.closedLoopControlName = policy.controlLoop.controlLoopName;
kieSession.insert(event);
Thread.sleep(250);
//
// add the request id
//
event.requestID = requestID;
kieSession.insert(event);
Thread.sleep(250);
//
// add some aai
//
event.AAI = new HashMap<String, String>();
event.AAI.put("cloud-region.identity-url", "foo");
event.AAI.put("vserver.selflink", "bar");
event.AAI.put("vserver.vserver-name", "vmfoo");
kieSession.insert(event);
Thread.sleep(250);
//
// set a valid status
//
event.closedLoopEventStatus = ControlLoopEventStatus.ONSET;
kieSession.insert(event);
Thread.sleep(250);
//
// add a trigger sourcename
//
kieSession.insert(event);
Thread.sleep(250);
//
// add is closed-loop-disabled
//
event.AAI.put("vserver.is-closed-loop-disabled", "true");
kieSession.insert(event);
Thread.sleep(250);
//
// now enable
//
event.AAI.put("vserver.is-closed-loop-disabled", "false");
kieSession.insert(event);
Thread.sleep(250);
//
// Add target, but bad.
//
event.target = "VM_BLAH";
kieSession.insert(event);
Thread.sleep(250);
}
public static void fromYamlToXacml(String yamlFile, String xacmlTemplate, String xacmlPolicyOutput){
ControlLoopGuard yamlGuardObject = Util.loadYamlGuard(yamlFile);
System.out.println("actor: " + yamlGuardObject.guards.getFirst().actor);
System.out.println("recipe: " + yamlGuardObject.guards.getFirst().recipe);
System.out.println("num: " + yamlGuardObject.guards.getFirst().limit_constraints.getFirst().num);
System.out.println("duration: " + yamlGuardObject.guards.getFirst().limit_constraints.getFirst().duration);
System.out.println("time_in_range: " + yamlGuardObject.guards.getFirst().limit_constraints.getFirst().time_in_range);
Path xacmlTemplatePath = Paths.get(xacmlTemplate);
String xacmlTemplateContent;
try {
xacmlTemplateContent = new String(Files.readAllBytes(xacmlTemplatePath));
String xacmlPolicyContent = PolicyGuardYamlToXacml.generateXacmlGuard(xacmlTemplateContent,
yamlGuardObject.guards.getFirst().actor,
yamlGuardObject.guards.getFirst().recipe,
yamlGuardObject.guards.getFirst().limit_constraints.getFirst().num,
yamlGuardObject.guards.getFirst().limit_constraints.getFirst().duration,
yamlGuardObject.guards.getFirst().limit_constraints.getFirst().time_in_range.get("arg2"),
yamlGuardObject.guards.getFirst().limit_constraints.getFirst().time_in_range.get("arg3")
);
Files.write(Paths.get(xacmlPolicyOutput), xacmlPolicyContent.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
public static String generatePolicy(String ruleContents,
String closedLoopControlName,
String policyScope,
String policyName,
String policyVersion,
String controlLoopYaml) {
Pattern p = Pattern.compile("\\$\\{closedLoopControlName\\}");
Matcher m = p.matcher(ruleContents);
ruleContents = m.replaceAll(closedLoopControlName);
p = Pattern.compile("\\$\\{policyScope\\}");
m = p.matcher(ruleContents);
ruleContents = m.replaceAll(policyScope);
p = Pattern.compile("\\$\\{policyName\\}");
m = p.matcher(ruleContents);
ruleContents = m.replaceAll(policyName);
p = Pattern.compile("\\$\\{policyVersion\\}");
m = p.matcher(ruleContents);
ruleContents = m.replaceAll(policyVersion);
p = Pattern.compile("\\$\\{controlLoopYaml\\}");
m = p.matcher(ruleContents);
ruleContents = m.replaceAll(controlLoopYaml);
System.out.println(ruleContents);
return ruleContents;
}
public static KieSession buildContainer(String droolsTemplate, String closedLoopControlName, String policyScope, String policyName, String policyVersion, String yamlSpecification) throws IOException {
//
// Get our Drools Kie factory
//
KieServices ks = KieServices.Factory.get();
KieModuleModel kModule = ks.newKieModuleModel();
System.out.println("KMODULE:" + System.lineSeparator() + kModule.toXML());
//
// Generate our drools rule from our template
//
KieFileSystem kfs = ks.newKieFileSystem();
kfs.writeKModuleXML(kModule.toXML());
{
Path rule = Paths.get(droolsTemplate);
String ruleTemplate = new String(Files.readAllBytes(rule));
String drlContents = generatePolicy(ruleTemplate,
closedLoopControlName,
policyScope,
policyName,
policyVersion,
yamlSpecification);
kfs.write("src/main/resources/" + policyName + ".drl", ks.getResources().newByteArrayResource(drlContents.getBytes()));
}
//
// Compile the rule
//
KieBuilder builder = ks.newKieBuilder(kfs).buildAll();
Results results = builder.getResults();
if (results.hasMessages(Message.Level.ERROR)) {
for (Message msg : results.getMessages()) {
System.err.println(msg.toString());
}
throw new RuntimeException("Drools Rule has Errors");
}
for (Message msg : results.getMessages()) {
System.out.println(msg.toString());
}
//
// Create our kie Session and container
//
ReleaseId releaseId = ks.getRepository().getDefaultReleaseId();
System.out.println(releaseId);
KieContainer kContainer = ks.newKieContainer(releaseId);
return kContainer.newKieSession();
}
}
|