1b8f94caefb5ad2d85b81bbe8670ad65f8fbfbcb
[policy/drools-applications.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2020 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.controlloop.common.rules.test;
22
23 import static org.awaitility.Awaitility.await;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertTrue;
27
28 import java.io.File;
29 import java.io.FileNotFoundException;
30 import java.io.IOException;
31 import java.util.Collections;
32 import java.util.List;
33 import java.util.Properties;
34 import java.util.concurrent.CountDownLatch;
35 import java.util.concurrent.TimeUnit;
36 import lombok.Getter;
37 import org.apache.commons.lang3.StringUtils;
38 import org.kie.api.event.rule.AfterMatchFiredEvent;
39 import org.kie.api.event.rule.BeforeMatchFiredEvent;
40 import org.kie.api.event.rule.DefaultAgendaEventListener;
41 import org.kie.api.event.rule.DefaultRuleRuntimeEventListener;
42 import org.kie.api.event.rule.MatchCancelledEvent;
43 import org.kie.api.event.rule.MatchCreatedEvent;
44 import org.kie.api.event.rule.ObjectDeletedEvent;
45 import org.kie.api.event.rule.ObjectInsertedEvent;
46 import org.kie.api.event.rule.ObjectUpdatedEvent;
47 import org.kie.api.event.rule.RuleRuntimeEventListener;
48 import org.kie.api.runtime.KieSession;
49 import org.onap.policy.common.utils.coder.CoderException;
50 import org.onap.policy.common.utils.coder.StandardCoder;
51 import org.onap.policy.common.utils.resources.ResourceUtils;
52 import org.onap.policy.controlloop.ControlLoopEvent;
53 import org.onap.policy.controlloop.drl.legacy.ControlLoopParams;
54 import org.onap.policy.controlloop.eventmanager.ControlLoopEventManager;
55 import org.onap.policy.drools.controller.DroolsController;
56 import org.onap.policy.drools.persistence.SystemPersistence;
57 import org.onap.policy.drools.persistence.SystemPersistenceConstants;
58 import org.onap.policy.drools.system.PolicyController;
59 import org.onap.policy.drools.system.PolicyControllerConstants;
60 import org.onap.policy.drools.system.PolicyControllerFactory;
61 import org.onap.policy.drools.system.PolicyEngine;
62 import org.onap.policy.drools.system.PolicyEngineConstants;
63 import org.onap.policy.drools.util.KieUtils;
64 import org.onap.policy.drools.utils.logging.LoggerUtil;
65 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
66 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
67 import org.slf4j.Logger;
68 import org.slf4j.LoggerFactory;
69
70 /**
71  * Mechanism by which junit tests can manage the rule engine.
72  */
73 public class Rules {
74     private static final Logger logger = LoggerFactory.getLogger(Rules.class);
75     private static final StandardCoder coder = new StandardCoder();
76     private static final String POLICY_MSG = "policy ";
77
78     /**
79      * PDP-D Engine.
80      */
81     @Getter
82     private final PolicyEngine pdpd = makeEngine();
83
84     /**
85      * PDP-D Configuration Repository.
86      */
87     @Getter
88     private final SystemPersistence pdpdRepo = makePdpdRepo();
89
90
91     @Getter
92     private final String controllerName;
93
94     @Getter
95     private PolicyController controller;
96
97
98     /**
99      * Constructs the object.
100      *
101      * @param controllerName name of the controller
102      */
103     public Rules(String controllerName) {
104         this.controllerName = controllerName;
105     }
106
107     /**
108      * Configures various items, including the PDP-D Engine.
109      *
110      * @param resourceDir path to resource directory
111      */
112     public void configure(String resourceDir) {
113         pdpdRepo.setConfigurationDir("src/test/resources/config");
114
115         try {
116             File kmoduleFile = new File(resourceDir + "/META-INF/kmodule.xml");
117             File pomFile = new File("src/test/resources/" + controllerName + ".pom");
118             String resourceDir2 = resourceDir + "/org/onap/policy/controlloop/";
119             File ruleFile = new File(resourceDir + File.separator + controllerName + ".drl");
120             List<File> ruleFiles = Collections.singletonList(ruleFile);
121
122             installArtifact(kmoduleFile, pomFile, resourceDir2, ruleFiles);
123         } catch (IOException e) {
124             throw new IllegalArgumentException("cannot configure KIE session for " + controllerName, e);
125         }
126
127         setupLogging();
128
129         pdpd.configure(new Properties());
130     }
131
132     /**
133      * Starts various items, including the PDP-D Engine.
134      */
135     public void start() {
136         controller = pdpd.createPolicyController(controllerName, pdpdRepo.getControllerProperties(controllerName));
137         pdpd.start();
138
139         setupDroolsLogging();
140     }
141
142     /**
143      * Stop PDP-D.
144      */
145     public void destroy() {
146         getControllerFactory().shutdown(controllerName);
147         pdpd.stop();
148     }
149
150     /**
151      * Removes various facts from working memory, including the Policy and Params, as well
152      * as any event managers and events.
153      */
154     public void resetFacts() {
155         List<Class<?>> classes = List.of(ToscaPolicy.class, ControlLoopParams.class, ControlLoopEventManager.class,
156                         ControlLoopEvent.class);
157
158         // delete all objects of the listed classes
159         DroolsController drools = controller.getDrools();
160         classes.forEach(drools::delete);
161
162         // wait for them to be deleted
163         for (Class<?> clazz : classes) {
164             await(clazz.getSimpleName()).atMost(5, TimeUnit.SECONDS)
165                             .until(() -> drools.facts(controllerName, clazz).isEmpty());
166         }
167     }
168
169     /**
170      * Installs a policy from policy/models (examples) repo.
171      */
172     public ToscaPolicy setupPolicyFromTemplate(String templatePath, String policyName) {
173         try {
174             return setupPolicy(getPolicyFromTemplate(templatePath, policyName));
175
176         } catch (CoderException e) {
177             throw new IllegalArgumentException(POLICY_MSG + policyName, e);
178
179         } catch (InterruptedException e) {
180             Thread.currentThread().interrupt();
181             throw new IllegalArgumentException(POLICY_MSG + policyName, e);
182         }
183     }
184
185     private ToscaPolicy getPolicyFromTemplate(String resourcePath, String policyName) throws CoderException {
186         String policyJson = ResourceUtils.getResourceAsString(resourcePath);
187         if (policyJson == null) {
188             throw new CoderException(new FileNotFoundException(resourcePath));
189         }
190
191         ToscaServiceTemplate serviceTemplate = coder.decode(policyJson, ToscaServiceTemplate.class);
192         ToscaPolicy policy = serviceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyName);
193         assertNotNull(policy);
194
195         /*
196          * name and version are used within a drl. api component and drools core will
197          * ensure that these are populated.
198          */
199         if (StringUtils.isBlank(policy.getName())) {
200             policy.setName(policyName);
201         }
202
203         if (StringUtils.isBlank(policy.getVersion())) {
204             policy.setVersion(policy.getTypeVersion());
205         }
206
207         return serviceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyName);
208     }
209
210     /**
211      * Installs a given policy.
212      */
213     public ToscaPolicy setupPolicyFromFile(String policyPath) {
214         try {
215             return setupPolicy(getPolicyFromFile(policyPath));
216
217         } catch (CoderException e) {
218             throw new IllegalArgumentException(POLICY_MSG + policyPath, e);
219
220         } catch (InterruptedException e) {
221             Thread.currentThread().interrupt();
222             throw new IllegalArgumentException(POLICY_MSG + policyPath, e);
223         }
224     }
225
226     /**
227      * Get policy from file.
228      */
229     public static ToscaPolicy getPolicyFromFile(String policyPath) throws CoderException {
230         String policyJson = ResourceUtils.getResourceAsString(policyPath);
231         if (policyJson == null) {
232             throw new CoderException(new FileNotFoundException(policyPath));
233         }
234
235         return coder.decode(policyJson, ToscaPolicy.class);
236     }
237
238     protected ToscaPolicy setupPolicy(ToscaPolicy policy) throws InterruptedException {
239         final KieObjectExpectedCallback<?> policyTracker = new KieObjectInsertedExpectedCallback<>(policy);
240         final KieObjectExpectedCallback<?> paramsTracker =
241                         new KieClassInsertedExpectedCallback<>(ControlLoopParams.class);
242
243         controller.getDrools().offer(policy);
244
245         assertTrue(policyTracker.isNotified());
246         assertTrue(paramsTracker.isNotified());
247
248         assertEquals(1, controller.getDrools().facts(controllerName, ToscaPolicy.class).stream()
249                         .filter(anotherPolicy -> anotherPolicy == policy).count());
250
251         assertEquals(1, controller.getDrools().facts(controllerName, ControlLoopParams.class).stream()
252                         .filter(params -> params.getToscaPolicy() == policy).count());
253         return policy;
254     }
255
256     /**
257      * Sets up overall logging.
258      */
259     private void setupLogging() {
260         LoggerUtil.setLevel(LoggerUtil.ROOT_LOGGER, "WARN");
261         LoggerUtil.setLevel("org.eclipse.jetty", "WARN");
262         LoggerUtil.setLevel("org.onap.policy.controlloop", "INFO");
263         LoggerUtil.setLevel("network", "INFO");
264     }
265
266     /**
267      * Sets up Drools Logging for events of interest.
268      */
269     private void setupDroolsLogging() {
270         KieSession session = getKieSession();
271
272         session.addEventListener(new RuleListenerLogger());
273         session.addEventListener(new AgendaListenerLogger());
274     }
275
276     /**
277      * Logs Modifications to Working Memory.
278      */
279     private static class RuleListenerLogger implements RuleRuntimeEventListener {
280         @Override
281         public void objectInserted(ObjectInsertedEvent event) {
282             String ruleName = (event.getRule() != null) ? event.getRule().getName() : "null";
283             logger.info("RULE {}: inserted {}", ruleName, event.getObject());
284         }
285
286         @Override
287         public void objectUpdated(ObjectUpdatedEvent event) {
288             String ruleName = (event.getRule() != null) ? event.getRule().getName() : "null";
289             logger.info("RULE {}: updated {}", ruleName, event.getObject());
290
291         }
292
293         @Override
294         public void objectDeleted(ObjectDeletedEvent event) {
295             String ruleName = (event.getRule() != null) ? event.getRule().getName() : "null";
296             logger.info("RULE {}: deleted {}", ruleName, event.getOldObject());
297         }
298     }
299
300     /**
301      * Logs Rule Matches.
302      */
303     private static class AgendaListenerLogger extends DefaultAgendaEventListener {
304         @Override
305         public void matchCreated(MatchCreatedEvent event) {
306             logger.info("RULE {}: match created", event.getMatch().getRule().getName());
307         }
308
309         @Override
310         public void matchCancelled(MatchCancelledEvent event) {
311             logger.info("RULE {}: match cancelled", event.getMatch().getRule().getName());
312         }
313
314         @Override
315         public void beforeMatchFired(BeforeMatchFiredEvent event) {
316             logger.info("RULE {}: before match fired", event.getMatch().getRule().getName());
317         }
318
319         @Override
320         public void afterMatchFired(AfterMatchFiredEvent event) {
321             logger.info("RULE {}: after match fired", event.getMatch().getRule().getName());
322         }
323     }
324
325     /**
326      * Base Class to track Working Memory updates for objects of type T.
327      */
328     private abstract class KieObjectExpectedCallback<T> extends DefaultRuleRuntimeEventListener {
329         protected T subject;
330
331         protected CountDownLatch countDownLatch = new CountDownLatch(1);
332
333         public KieObjectExpectedCallback(T affected) {
334             subject = affected;
335             register();
336         }
337
338         public boolean isNotified() throws InterruptedException {
339             return countDownLatch.await(9L, TimeUnit.SECONDS);
340         }
341
342         protected void callbacked() {
343             unregister();
344             countDownLatch.countDown();
345         }
346
347         public KieObjectExpectedCallback<T> register() {
348             getKieSession().addEventListener(this);
349             return this;
350         }
351
352         public KieObjectExpectedCallback<T> unregister() {
353             getKieSession().removeEventListener(this);
354             return this;
355         }
356     }
357
358     /**
359      * Tracks inserts in Working Memory for an object of type T.
360      */
361     private class KieObjectInsertedExpectedCallback<T> extends KieObjectExpectedCallback<T> {
362         public KieObjectInsertedExpectedCallback(T affected) {
363             super(affected);
364         }
365
366         @Override
367         public void objectInserted(ObjectInsertedEvent event) {
368             if (subject == event.getObject()) {
369                 callbacked();
370             }
371         }
372     }
373
374     /**
375      * Tracks inserts in Working Memory for any object of class T.
376      */
377     private class KieClassInsertedExpectedCallback<T> extends KieObjectInsertedExpectedCallback<T> {
378
379         public KieClassInsertedExpectedCallback(T affected) {
380             super(affected);
381         }
382
383         @Override
384         public void objectInserted(ObjectInsertedEvent event) {
385             if (subject == event.getObject().getClass()) {
386                 callbacked();
387             }
388         }
389     }
390
391     // these may be overridden by junit tests
392
393
394     protected PolicyEngine makeEngine() {
395         return PolicyEngineConstants.getManager();
396     }
397
398
399     protected SystemPersistence makePdpdRepo() {
400         return SystemPersistenceConstants.getManager();
401     }
402
403     protected KieSession getKieSession() {
404         return getControllerFactory().get(controllerName).getDrools().getContainer().getPolicySession(controllerName)
405                         .getKieSession();
406     }
407
408     protected PolicyControllerFactory getControllerFactory() {
409         return PolicyControllerConstants.getFactory();
410     }
411
412     protected void installArtifact(File kmoduleFile, File pomFile, String resourceDir, List<File> ruleFiles)
413                     throws IOException {
414
415         KieUtils.installArtifact(kmoduleFile, pomFile, resourceDir, ruleFiles);
416     }
417 }