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