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.template.demo;
23 import static org.junit.Assert.assertEquals;
25 import java.io.IOException;
26 import java.nio.file.Files;
27 import java.nio.file.Path;
28 import java.nio.file.Paths;
29 import java.time.Instant;
30 import java.util.HashMap;
31 import java.util.UUID;
32 import java.util.regex.Matcher;
33 import java.util.regex.Pattern;
35 import org.junit.Test;
36 import org.kie.api.KieServices;
37 import org.kie.api.builder.KieBuilder;
38 import org.kie.api.builder.KieFileSystem;
39 import org.kie.api.builder.Message;
40 import org.kie.api.builder.ReleaseId;
41 import org.kie.api.builder.Results;
42 import org.kie.api.builder.model.KieModuleModel;
43 import org.kie.api.runtime.KieContainer;
44 import org.kie.api.runtime.KieSession;
45 import org.kie.api.runtime.rule.FactHandle;
46 import org.onap.policy.appc.CommonHeader;
47 import org.onap.policy.appc.Response;
48 import org.onap.policy.appc.ResponseStatus;
49 import org.onap.policy.controlloop.ControlLoopEventStatus;
50 import org.onap.policy.controlloop.ControlLoopTargetType;
51 import org.onap.policy.controlloop.VirtualControlLoopEvent;
52 import org.onap.policy.appc.util.Serialization;
56 public class TestFirewallDemo {
60 public void testvDNS() throws IOException {
64 final String closedLoopControlName = "CL-DNS-LOW-TRAFFIC-SIG-d925ed73-8231-4d02-9545-db4e101f88f8";
65 final KieSession kieSession = buildContainer("src/main/resources/archetype-resources/src/main/resources/ControlLoopDemo__closedLoopControlName__.drl",
66 closedLoopControlName,
68 "myFirewallDemoPolicy",
71 "http://localhost:8080/TestREST/Test",
74 "http://localhost:8080/TestREST/Test",
77 "4ff56a54-9e3f-46b7-a337-07a1d3c6b469",
83 // Initial fire of rules
85 kieSession.fireAllRules();
87 // Kick a thread that starts testing
89 new Thread(new Runnable() {
94 // Generate an invalid DCAE Event with requestID=null
96 VirtualControlLoopEvent invalidEvent = new VirtualControlLoopEvent();
97 invalidEvent.closedLoopControlName = closedLoopControlName;
98 invalidEvent.requestID = null;
99 invalidEvent.closedLoopEventClient = "tca.instance00001";
100 invalidEvent.target_type = ControlLoopTargetType.VF;
101 invalidEvent.target = "generic-vnf.vnf-id";
102 invalidEvent.from = "DCAE";
103 invalidEvent.closedLoopAlarmStart = Instant.now();
104 invalidEvent.AAI = new HashMap<String, String>();
105 invalidEvent.AAI.put("vserver.vserver-name", "vserver-name-16102016-aai3255-data-11-1");
106 invalidEvent.closedLoopEventStatus = ControlLoopEventStatus.ONSET;
108 System.out.println("----- Invalid ONSET -----");
109 System.out.println(Serialization.gsonPretty.toJson(invalidEvent));
112 // Insert invalid DCAE Event into memory
114 kieSession.insert(invalidEvent);
117 } catch (InterruptedException e) {
120 // Generate first DCAE ONSET Event
122 VirtualControlLoopEvent onsetEvent = new VirtualControlLoopEvent();
123 onsetEvent.closedLoopControlName = closedLoopControlName;
124 onsetEvent.requestID = UUID.randomUUID();
125 onsetEvent.closedLoopEventClient = "tca.instance00001";
126 onsetEvent.target_type = ControlLoopTargetType.VF;
127 onsetEvent.target = "generic-vnf.vnf-id";
128 onsetEvent.from = "DCAE";
129 onsetEvent.closedLoopAlarmStart = Instant.now();
130 onsetEvent.AAI = new HashMap<String, String>();
131 onsetEvent.AAI.put("vserver.vserver-name", "vserver-name-16102016-aai3255-data-11-1");
132 onsetEvent.closedLoopEventStatus = ControlLoopEventStatus.ONSET;
134 System.out.println("----- ONSET -----");
135 System.out.println(Serialization.gsonPretty.toJson(onsetEvent));
138 // Insert first DCAE ONSET Event into memory
140 kieSession.insert(onsetEvent);
142 // We have test for subsequent ONSET Events in testvFirewall()
143 // So no need to test it again here
147 } catch (InterruptedException e) {
150 // Test is finished, so stop the kieSession
157 // Start firing rules
159 kieSession.fireUntilHalt();
161 // Dump working memory
163 dumpFacts(kieSession);
165 // See if there is anything left in memory, there SHOULD only be
168 assertEquals("There should only be 1 Fact left in memory.", 1, kieSession.getFactCount());
169 for (FactHandle handle : kieSession.getFactHandles()) {
170 Object fact = kieSession.getObject(handle);
171 assertEquals("Non-Param Fact left in working memory", "org.onap.policy.controlloop.Params", fact.getClass().getName());
176 public void testvFirewall() throws IOException {
180 final String closedLoopControlName = "CL-FRWL-LOW-TRAFFIC-SIG-d925ed73-8231-4d02-9545-db4e101f88f8";
181 final KieSession kieSession = buildContainer("src/main/resources/archetype-resources/src/main/resources/ControlLoopDemo__closedLoopControlName__.drl",
182 closedLoopControlName,
184 "myFirewallDemoPolicy",
187 "http://localhost:8080/TestREST/Test",
199 // Initial fire of rules
201 kieSession.fireAllRules();
203 // Kick a thread that starts testing
205 new Thread(new Runnable() {
210 // Generate an invalid DCAE Event with requestID=null
212 VirtualControlLoopEvent invalidEvent = new VirtualControlLoopEvent();
213 invalidEvent.closedLoopControlName = closedLoopControlName;
214 invalidEvent.requestID = null;
215 invalidEvent.closedLoopEventClient = "tca.instance00001";
216 invalidEvent.target_type = ControlLoopTargetType.VF;
217 invalidEvent.target = "generic-vnf.vnf-id";
218 invalidEvent.from = "DCAE";
219 invalidEvent.closedLoopAlarmStart = Instant.now();
220 invalidEvent.AAI = new HashMap<String, String>();
221 invalidEvent.AAI.put("generic-vnf.vnf-id", "foo");
222 invalidEvent.closedLoopEventStatus = ControlLoopEventStatus.ONSET;
224 System.out.println("----- Invalid ONSET -----");
225 System.out.println(Serialization.gsonPretty.toJson(invalidEvent));
228 // Insert invalid DCAE Event into memory
230 kieSession.insert(invalidEvent);
233 } catch (InterruptedException e) {
236 // Generate first DCAE ONSET Event
238 VirtualControlLoopEvent onsetEvent = new VirtualControlLoopEvent();
239 onsetEvent.closedLoopControlName = closedLoopControlName;
240 onsetEvent.requestID = UUID.randomUUID();
241 onsetEvent.closedLoopEventClient = "tca.instance00001";
242 onsetEvent.target_type = ControlLoopTargetType.VF;
243 onsetEvent.target = "generic-vnf.vnf-id";
244 onsetEvent.from = "DCAE";
245 onsetEvent.closedLoopAlarmStart = Instant.now();
246 onsetEvent.AAI = new HashMap<String, String>();
247 onsetEvent.AAI.put("generic-vnf.vnf-id", "fw0001vm001fw001");
248 //onsetEvent.AAI.put("vserver.vserver-name", "vserver-name-16102016-aai3255-data-11-1");
249 onsetEvent.closedLoopEventStatus = ControlLoopEventStatus.ONSET;
251 System.out.println("----- ONSET -----");
252 System.out.println(Serialization.gsonPretty.toJson(onsetEvent));
255 // Insert first DCAE ONSET Event into memory
257 kieSession.insert(onsetEvent);
260 } catch (InterruptedException e) {
264 Thread thread = new Thread(new Runnable() {
270 // Generate subsequent DCAE ONSET Event
272 VirtualControlLoopEvent subOnsetEvent = new VirtualControlLoopEvent();
273 subOnsetEvent.closedLoopControlName = closedLoopControlName;
274 subOnsetEvent.requestID = UUID.randomUUID();
275 subOnsetEvent.closedLoopEventClient = "tca.instance00001";
276 subOnsetEvent.target_type = ControlLoopTargetType.VF;
277 subOnsetEvent.target = "generic-vnf.vnf-id";
278 subOnsetEvent.from = "DCAE";
279 subOnsetEvent.closedLoopAlarmStart = Instant.now();
280 subOnsetEvent.AAI = new HashMap<String, String>();
281 subOnsetEvent.AAI.put("generic-vnf.vnf-id", "fw0001vm001fw001");
282 //subOnsetEvent.AAI.put("vserver.vserver-name", "vserver-name-16102016-aai3255-data-11-1");
283 subOnsetEvent.closedLoopEventStatus = ControlLoopEventStatus.ONSET;
285 System.out.println("----- Subsequent ONSET -----");
286 System.out.println(Serialization.gsonPretty.toJson(subOnsetEvent));
289 // Insert subsequent DCAE ONSET Event into memory
291 kieSession.insert(subOnsetEvent);
294 } catch (InterruptedException e) {
304 } catch (InterruptedException e) {
311 // Generate APPC ACCEPT Response
313 Response response1 = new Response();
315 CommonHeader commonHeader1 = new CommonHeader();
316 commonHeader1.RequestID = onsetEvent.requestID;
317 response1.CommonHeader = commonHeader1;
319 ResponseStatus responseStatus1 = new ResponseStatus();
320 responseStatus1.Code = 100;
321 response1.Status = responseStatus1;
323 System.out.println("----- APP-C RESPONSE 100 -----");
324 System.out.println(Serialization.gsonPretty.toJson(response1));
326 // Insert APPC Response into memory
328 kieSession.insert(response1);
330 // Simulating APPC takes some time for processing the recipe
331 // and then gives response
335 } catch (InterruptedException e) {
338 // Generate APPC SUCCESS Response
340 Response response2 = new Response();
342 CommonHeader commonHeader2 = new CommonHeader();
343 commonHeader2.RequestID = onsetEvent.requestID;
344 response2.CommonHeader = commonHeader2;
346 ResponseStatus responseStatus2 = new ResponseStatus();
347 responseStatus2.Code = 400;
348 response2.Status = responseStatus2;
350 System.out.println("----- APP-C RESPONSE 400 -----");
351 System.out.println(Serialization.gsonPretty.toJson(response2));
353 // Insert APPC Response into memory
355 kieSession.insert(response2);
359 } catch (InterruptedException e) {
362 // Test is finished, so stop the kieSession
369 // Start firing rules
371 kieSession.fireUntilHalt();
373 // Dump working memory
375 dumpFacts(kieSession);
377 // See if there is anything left in memory, there SHOULD only be
380 assertEquals("There should only be 1 Fact left in memory.", 1, kieSession.getFactCount());
381 for (FactHandle handle : kieSession.getFactHandles()) {
382 Object fact = kieSession.getObject(handle);
383 assertEquals("Non-Param Fact left in working memory", "org.onap.policy.controlloop.Params", fact.getClass().getName());
387 public static void dumpFacts(KieSession kieSession) {
388 System.out.println("Fact Count: " + kieSession.getFactCount());
389 for (FactHandle handle : kieSession.getFactHandles()) {
390 System.out.println("FACT: " + handle);
394 public static KieSession buildContainer(String droolsTemplate,
395 String closedLoopControlName,
398 String policyVersion,
406 String aaiNamedQuery,
408 String notificationTopic,
409 String appcTopic ) throws IOException {
411 // Get our Drools Kie factory
413 KieServices ks = KieServices.Factory.get();
415 KieModuleModel kModule = ks.newKieModuleModel();
417 System.out.println("KMODULE:" + System.lineSeparator() + kModule.toXML());
420 // Generate our drools rule from our template
422 KieFileSystem kfs = ks.newKieFileSystem();
424 kfs.writeKModuleXML(kModule.toXML());
426 Path rule = Paths.get(droolsTemplate);
427 String ruleTemplate = new String(Files.readAllBytes(rule));
428 String drlContents = generatePolicy(ruleTemplate,
429 closedLoopControlName,
446 kfs.write("src/main/resources/" + policyName + ".drl", ks.getResources().newByteArrayResource(drlContents.getBytes()));
451 KieBuilder builder = ks.newKieBuilder(kfs).buildAll();
452 Results results = builder.getResults();
453 if (results.hasMessages(Message.Level.ERROR)) {
454 for (Message msg : results.getMessages()) {
455 System.err.println(msg.toString());
457 throw new RuntimeException("Drools Rule has Errors");
459 for (Message msg : results.getMessages()) {
460 System.out.println(msg.toString());
463 // Create our kie Session and container
465 ReleaseId releaseId = ks.getRepository().getDefaultReleaseId();
466 System.out.println(releaseId);
467 KieContainer kContainer = ks.newKieContainer(releaseId);
469 return kContainer.newKieSession();
471 public static String generatePolicy(String ruleContents,
472 String closedLoopControlName,
475 String policyVersion,
483 String aaiNamedQueryUUID,
485 String notificationTopic,
488 Pattern p = Pattern.compile("\\$\\{closedLoopControlName\\}");
489 Matcher m = p.matcher(ruleContents);
490 ruleContents = m.replaceAll(closedLoopControlName);
492 p = Pattern.compile("\\$\\{policyScope\\}");
493 m = p.matcher(ruleContents);
494 ruleContents = m.replaceAll(policyScope);
496 p = Pattern.compile("\\$\\{policyName\\}");
497 m = p.matcher(ruleContents);
498 ruleContents = m.replaceAll(policyName);
500 p = Pattern.compile("\\$\\{policyVersion\\}");
501 m = p.matcher(ruleContents);
502 ruleContents = m.replaceAll(policyVersion);
504 p = Pattern.compile("\\$\\{actor\\}");
505 m = p.matcher(ruleContents);
506 ruleContents = m.replaceAll(actor);
508 p = Pattern.compile("\\$\\{aaiURL\\}");
509 m = p.matcher(ruleContents);
510 if (aaiURL == null) {
511 ruleContents = m.replaceAll("null");
513 ruleContents = m.replaceAll(aaiURL);
516 p = Pattern.compile("\\$\\{aaiUsername\\}");
517 m = p.matcher(ruleContents);
518 if (aaiUsername == null) {
519 ruleContents = m.replaceAll("null");
521 ruleContents = m.replaceAll(aaiUsername);
524 p = Pattern.compile("\\$\\{aaiPassword\\}");
525 m = p.matcher(ruleContents);
526 if (aaiPassword == null) {
527 ruleContents = m.replaceAll("null");
529 ruleContents = m.replaceAll(aaiPassword);
532 p = Pattern.compile("\\$\\{msoURL\\}");
533 m = p.matcher(ruleContents);
534 if (msoURL == null) {
535 ruleContents = m.replaceAll("null");
537 ruleContents = m.replaceAll(msoURL);
540 p = Pattern.compile("\\$\\{msoUsername\\}");
541 m = p.matcher(ruleContents);
542 if (msoUsername == null) {
543 ruleContents = m.replaceAll("null");
545 ruleContents = m.replaceAll(msoUsername);
548 p = Pattern.compile("\\$\\{msoPassword\\}");
549 m = p.matcher(ruleContents);
550 if (msoPassword == null) {
551 ruleContents = m.replaceAll("null");
553 ruleContents = m.replaceAll(msoPassword);
556 p = Pattern.compile("\\$\\{aaiNamedQueryUUID\\}");
557 m = p.matcher(ruleContents);
558 if (aaiNamedQueryUUID == null) {
559 ruleContents = m.replaceAll("null");
561 ruleContents = m.replaceAll(aaiNamedQueryUUID);
564 p = Pattern.compile("\\$\\{aaiPatternMatch\\}");
565 m = p.matcher(ruleContents);
566 if (aaiPatternMatch == 1) {
567 ruleContents = m.replaceAll("1");
569 ruleContents = m.replaceAll("0");
572 p = Pattern.compile("\\$\\{notificationTopic\\}");
573 m = p.matcher(ruleContents);
574 if (notificationTopic == null) {
575 ruleContents = m.replaceAll("null");
577 ruleContents = m.replaceAll(notificationTopic);
580 p = Pattern.compile("\\$\\{appcTopic\\}");
581 m = p.matcher(ruleContents);
582 if (appcTopic == null) {
583 ruleContents = m.replaceAll("null");
585 ruleContents = m.replaceAll(appcTopic);
588 System.out.println(ruleContents);