4ca89e1fba86b8334b58e1c62b338b4af23addcb
[policy/drools-applications.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * demo
4  * ================================================================================
5  * Copyright (C) 2018 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.assertNotNull;
25 import static org.junit.Assert.assertTrue;
26 import static org.junit.Assert.fail;
27
28 import java.io.IOException;
29 import java.io.UnsupportedEncodingException;
30 import java.net.URLEncoder;
31 import java.time.Instant;
32 import java.util.HashMap;
33 import java.util.LinkedList;
34 import java.util.List;
35 import java.util.Properties;
36 import java.util.UUID;
37 import org.junit.AfterClass;
38 import org.junit.BeforeClass;
39 import org.junit.Test;
40 import org.kie.api.runtime.KieSession;
41 import org.onap.policy.common.endpoints.event.comm.TopicEndpoint;
42 import org.onap.policy.common.endpoints.event.comm.TopicSink;
43 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
44 import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
45 import org.onap.policy.controlloop.ControlLoopEventStatus;
46 import org.onap.policy.controlloop.VirtualControlLoopEvent;
47 import org.onap.policy.controlloop.eventmanager.ControlLoopEventManager;
48 import org.onap.policy.controlloop.policy.ControlLoopPolicy;
49 import org.onap.policy.drools.protocol.coders.EventProtocolCoder;
50 import org.onap.policy.drools.protocol.coders.JsonProtocolFilter;
51 import org.onap.policy.drools.system.PolicyController;
52 import org.onap.policy.drools.system.PolicyEngine;
53 import org.onap.policy.drools.utils.logging.LoggerUtil;
54 import org.onap.policy.template.demo.Util.Pair;
55 import org.slf4j.Logger;
56 import org.slf4j.LoggerFactory;
57
58 /**
59  * Verifies that event objects are cleaned up when rules are updated. This loads
60  * <b>two</b> copies of the rule set into a single policy to ensure that the two copies
61  * interact appropriately with each other's event objects.
62  */
63 public class ControlLoopEventCleanupTest {
64     private static final Logger logger = LoggerFactory.getLogger(ControlLoopEventCleanupTest.class);
65
66     /**
67      * Number of objects per control loop, including the Params object.
68      */
69     private static int CL_OBJECTS = 7;
70
71     private static final String YAML = "src/test/resources/yaml/policy_ControlLoop_EventCleanup-test.yaml";
72
73     /**
74      * YAML to be used when the first rule set is updated.
75      */
76     private static final String YAML2 = "src/test/resources/yaml/policy_ControlLoop_EventCleanup-test2.yaml";
77
78     private static final String POLICY_VERSION = "v2.0";
79
80     private static final String POLICY_NAME = "CL_CleanupTest";
81
82     private static final String POLICY_SCOPE = "type=operational";
83
84     private static final String CONTROL_LOOP_NAME = "ControlLoop-Event-Cleanup-Test";
85
86     private static final String DROOLS_TEMPLATE = "../archetype-cl-amsterdam/src/main/resources/archetype-resources/"
87                     + "src/main/resources/__closedLoopControlName__.drl";
88
89     // values specific to the second copy of the rules
90
91     private static final String YAML_B = "src/test/resources/yaml/policy_ControlLoop_EventCleanup-test-B.yaml";
92     private static final String POLICY_NAME_B = "CL_CleanupTest_B";
93     private static final String CONTROL_LOOP_NAME_B = "ControlLoop-Event-Cleanup-Test-B";
94
95     private static final String GUARD_DISABLED = "guard.disabled";
96
97     private static String saveGuardFlag;
98
99     private static KieSession kieSession;
100     private static Util.RuleSpec[] specifications;
101
102     /**
103      * Setup the simulator.
104      */
105     @BeforeClass
106     public static void setUpSimulator() {
107         LoggerUtil.setLevel(LoggerUtil.ROOT_LOGGER, "INFO");
108
109         saveGuardFlag = PolicyEngine.manager.getEnvironmentProperty(GUARD_DISABLED);
110         PolicyEngine.manager.getEnvironment().setProperty(GUARD_DISABLED, "true");
111
112         Util.setAaiProps();
113
114         PolicyEngine.manager.configure(new Properties());
115         assertTrue(PolicyEngine.manager.start());
116         Properties noopSinkProperties = new Properties();
117         noopSinkProperties.put(PolicyEndPointProperties.PROPERTY_NOOP_SINK_TOPICS, "APPC-CL,POLICY-CL-MGT");
118         noopSinkProperties.put("noop.sink.topics.APPC-CL.events", "org.onap.policy.appc.Response");
119         noopSinkProperties.put("noop.sink.topics.APPC-CL.events.custom.gson",
120                         "org.onap.policy.appc.util.Serialization,gsonPretty");
121         noopSinkProperties.put("noop.sink.topics.POLICY-CL-MGT.events",
122                         "org.onap.policy.controlloop.VirtualControlLoopNotification");
123         noopSinkProperties.put("noop.sink.topics.POLICY-CL-MGT.events.custom.gson",
124                         "org.onap.policy.controlloop.util.Serialization,gsonPretty");
125         final List<TopicSink> noopTopics = TopicEndpoint.manager.addTopicSinks(noopSinkProperties);
126
127         EventProtocolCoder.manager.addEncoder("junit.groupId", "junit.artifactId", "POLICY-CL-MGT",
128                         "org.onap.policy.controlloop.VirtualControlLoopNotification", new JsonProtocolFilter(), null,
129                         null, 1111);
130         EventProtocolCoder.manager.addEncoder("junit.groupId", "junit.artifactId", "APPC-CL",
131                         "org.onap.policy.appc.Request", new JsonProtocolFilter(), null, null, 1111);
132
133         try {
134             Util.buildAaiSim();
135
136         } catch (Exception e) {
137             logger.error("Could not create simulator", e);
138             fail("Could not create simulator");
139         }
140
141         for (TopicSink sink : noopTopics) {
142             assertTrue(sink.start());
143         }
144
145         try {
146             specifications = new Util.RuleSpec[2];
147
148             specifications[0] = new Util.RuleSpec(DROOLS_TEMPLATE, CONTROL_LOOP_NAME, POLICY_SCOPE, POLICY_NAME,
149                             POLICY_VERSION, loadYaml(YAML));
150
151             specifications[1] = new Util.RuleSpec(DROOLS_TEMPLATE, CONTROL_LOOP_NAME_B, POLICY_SCOPE, POLICY_NAME_B,
152                             POLICY_VERSION, loadYaml(YAML_B));
153
154             kieSession = Util.buildContainer(POLICY_VERSION, specifications);
155
156         } catch (IOException e) {
157             logger.error("Could not create kieSession", e);
158             fail("Could not create kieSession");
159         }
160     }
161
162     /**
163      * Tear down.
164      */
165     @AfterClass
166     public static void tearDown() {
167         kieSession.dispose();
168
169         PolicyEngine.manager.stop();
170         HttpServletServer.factory.destroy();
171         PolicyController.factory.shutdown();
172         TopicEndpoint.manager.shutdown();
173
174         if (saveGuardFlag == null) {
175             PolicyEngine.manager.getEnvironment().remove(GUARD_DISABLED);
176
177         } else {
178             PolicyEngine.manager.getEnvironment().setProperty(GUARD_DISABLED, saveGuardFlag);
179         }
180     }
181
182     @Test
183     public void test() throws IOException {
184
185         /*
186          * Let rules create Params objects.
187          */
188         kieSession.fireAllRules();
189
190         injectEvent(CONTROL_LOOP_NAME);
191         injectEvent(CONTROL_LOOP_NAME_B);
192
193         kieSession.fireAllRules();
194         List<Object> facts = getSessionObjects();
195         
196         // should have events for both control loops
197         assertEquals(2 * CL_OBJECTS, facts.size());
198         assertTrue(hasEvent(facts, CONTROL_LOOP_NAME));
199         assertTrue(hasEvent(facts, CONTROL_LOOP_NAME_B));
200
201         logger.info("UPDATING VERSION TO v3.0");
202         updatePolicy(YAML2, "v3.0");
203
204         /*
205          * Let rules update Params objects. The Params for the first set of rules should
206          * now be deleted and replaced with a new one, while the Params for the second set
207          * should be unchanged.
208          */
209         kieSession.fireAllRules();
210         facts = getSessionObjects();
211
212         // should only have event for second control loop + 1 Params for first control loop
213         assertEquals(CL_OBJECTS + 1, facts.size());
214         assertTrue(hasEvent(facts, CONTROL_LOOP_NAME_B));
215
216         // add event for first control loop again
217         injectEvent(CONTROL_LOOP_NAME);
218         kieSession.fireAllRules();
219
220         logger.info("UPDATING VERSION TO v4.0");
221         updatePolicy(YAML, "v4.0");
222
223         /*
224          * Let rules update Params objects. The Params for the first set of rules should
225          * now be deleted and replaced with a new one, while the Params for the second set
226          * should be unchanged.
227          */
228         kieSession.fireAllRules();
229         facts = getSessionObjects();
230
231         // should only have event for second control loop + 1 Params for first control loop
232         assertEquals(CL_OBJECTS + 1, facts.size());
233         assertTrue(hasEvent(facts, CONTROL_LOOP_NAME_B));
234
235         // add event for first control loop again
236         injectEvent(CONTROL_LOOP_NAME);
237         kieSession.fireAllRules();
238
239         logger.info("UPDATING VERSION TO v4.0 (i.e., unchanged)");
240         updatePolicy(YAML, "v4.0");
241
242         /*
243          * Let rules update Params objects. As the version (and YAML) are unchanged for
244          * either rule set, both Params objects should be unchanged.
245          */
246         kieSession.fireAllRules();
247         facts = getSessionObjects();
248
249         // should have events for both control loops
250         assertEquals(2 * CL_OBJECTS, facts.size());
251         assertTrue(hasEvent(facts, CONTROL_LOOP_NAME));
252         assertTrue(hasEvent(facts, CONTROL_LOOP_NAME_B));
253
254         /*
255          * Now we'll delete the first rule set. That won't actually have any immediate
256          * effect, so then we'll update the second rule set, which should trigger a
257          * clean-up of both.
258          */
259         Util.RuleSpec[] specs = new Util.RuleSpec[1];
260         specs[0] = specifications[1];
261
262         logger.info("UPDATING VERSION TO v5.0 - DELETED RULE SET");
263         Util.updateContainer("v5.0", specs);
264
265         specs[0] = new Util.RuleSpec(DROOLS_TEMPLATE, CONTROL_LOOP_NAME_B, POLICY_SCOPE, POLICY_NAME_B, POLICY_VERSION,
266                         loadYaml(YAML));
267
268         logger.info("UPDATING VERSION TO v6.0 - UPDATED SECOND RULE SET");
269         Util.updateContainer("v6.0", specs);
270
271         kieSession.fireAllRules();
272         facts = getSessionObjects();
273
274         // only 1 Params should remain, for second rule set, but events should be gone
275         assertEquals(1, facts.size());
276         assertTrue(facts.stream().anyMatch(obj -> obj.toString().startsWith("Params( ")));
277     }
278
279     /**
280      * Updates the policy, changing the YAML associated with the first rule set.
281      *
282      * @param yamlFile name of the YAML file
283      * @param policyVersion policy version
284      * @throws IOException if an error occurs
285      */
286     private static void updatePolicy(String yamlFile, String policyVersion) throws IOException {
287
288         specifications[0] = new Util.RuleSpec(DROOLS_TEMPLATE, CONTROL_LOOP_NAME, POLICY_SCOPE, POLICY_NAME,
289                         policyVersion, loadYaml(yamlFile));
290
291         /*
292          * Update the policy within the container.
293          */
294         Util.updateContainer(policyVersion, specifications);
295     }
296
297     /**
298      * Loads a YAML file and URL-encodes it.
299      *
300      * @param yamlFile name of the YAML file
301      * @return the contents of the specified file, URL-encoded
302      * @throws UnsupportedEncodingException if an error occurs
303      */
304     private static String loadYaml(String yamlFile) throws UnsupportedEncodingException {
305         Pair<ControlLoopPolicy, String> pair = Util.loadYaml(yamlFile);
306         assertNotNull(pair);
307         assertNotNull(pair.first);
308         assertNotNull(pair.first.getControlLoop());
309         assertNotNull(pair.first.getControlLoop().getControlLoopName());
310         assertTrue(pair.first.getControlLoop().getControlLoopName().length() > 0);
311
312         return URLEncoder.encode(pair.second, "UTF-8");
313     }
314
315     /**
316      * Gets the session objects.
317      *
318      * @return the session objects
319      */
320     private static List<Object> getSessionObjects() {
321         // sort the objects so we know the order
322         LinkedList<Object> lst = new LinkedList<>(kieSession.getObjects());
323         lst.sort((left, right) -> left.toString().compareTo(right.toString()));
324
325         lst.forEach(obj -> logger.info("obj={}", obj));
326
327         return lst;
328     }
329
330     /**
331      * Injects an ONSET event into the rule engine.
332      *
333      * @param controlLoopName the control loop name
334      */
335     private void injectEvent(String controlLoopName) {
336         VirtualControlLoopEvent event = new VirtualControlLoopEvent();
337
338         event.setClosedLoopControlName(controlLoopName);
339
340         UUID reqid = UUID.randomUUID();
341         event.setRequestId(reqid);
342
343         event.setTarget("generic-vnf.vnf-id");
344         event.setClosedLoopAlarmStart(Instant.now());
345         event.setAai(new HashMap<>());
346         event.getAai().put("generic-vnf.vnf-id", "vnf-" + reqid.toString());
347         event.getAai().put(ControlLoopEventManager.GENERIC_VNF_IS_CLOSED_LOOP_DISABLED, "false");
348         event.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
349
350         kieSession.insert(event);
351     }
352
353     /**
354      * Determines if the facts contain an event for the given control loop.
355      * 
356      * @param facts session facts to be checked
357      * @param controlLoopName name of the control loop of interest
358      * @return {@code true} if the facts contain an event for the given control loop,
359      *         {@code false} otherwise
360      */
361     private boolean hasEvent(List<Object> facts, String controlLoopName) {
362         return (facts.stream().anyMatch(obj -> obj instanceof VirtualControlLoopEvent
363                         && controlLoopName.equals(((VirtualControlLoopEvent) obj).getClosedLoopControlName())));
364     }
365 }