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;
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;
29 import java.io.IOException;
30 import java.net.URLEncoder;
31 import java.time.Instant;
32 import java.util.HashMap;
33 import java.util.UUID;
35 import org.junit.AfterClass;
36 import org.junit.BeforeClass;
37 import org.junit.Test;
38 import org.kie.api.runtime.KieSession;
39 import org.kie.api.runtime.rule.FactHandle;
40 import org.onap.policy.controlloop.ControlLoopEventStatus;
41 import org.onap.policy.controlloop.ControlLoopNotificationType;
42 import org.onap.policy.controlloop.ControlLoopTargetType;
43 import org.onap.policy.controlloop.VirtualControlLoopEvent;
44 import org.onap.policy.controlloop.VirtualControlLoopNotification;
45 import org.onap.policy.controlloop.policy.ControlLoopPolicy;
46 import org.onap.policy.controlloop.policy.TargetType;
47 import org.onap.policy.drools.http.server.HttpServletServer;
48 import org.onap.policy.drools.impl.PolicyEngineJUnitImpl;
49 import org.onap.policy.guard.PolicyGuard;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
53 public class VDNSControlLoopTest {
55 private static final Logger logger = LoggerFactory.getLogger(VDNSControlLoopTest.class);
57 private KieSession kieSession;
58 private Util.Pair<ControlLoopPolicy, String> pair;
59 private PolicyEngineJUnitImpl engine;
62 public static void setUpSimulator() {
66 } catch (InterruptedException e) {
72 public static void tearDownSimulator() {
73 HttpServletServer.factory.destroy();
77 public void successTest() {
80 * Start the kie session
83 kieSession = startSession("src/main/resources/ControlLoop_Template_xacml_guard.drl",
84 "src/test/resources/yaml/policy_ControlLoop_SO-test.yaml",
88 } catch (IOException e) {
90 logger.debug("Could not create kieSession");
91 fail("Could not create kieSession");
95 * Create a thread to continuously fire rules
96 * until main thread calls halt
98 new Thread( new Runnable() {
101 kieSession.fireUntilHalt();
106 * Create a unique requestId and a unique trigger source
108 UUID requestID = UUID.randomUUID();
109 String triggerSourceName = "foobartriggersource36";
112 * This will be the object returned from the PolicyEngine
117 * Simulate an onset event the policy engine will
118 * receive from DCAE to kick off processing through
122 sendOnset(pair.a, requestID, triggerSourceName);
123 } catch (InterruptedException e) {
125 logger.debug("Unable to send onset event");
126 fail("Unable to send onset event");
130 * Pull the object that was sent out and make
131 * sure it is a ControlLoopNoticiation of type active
133 obj = engine.subscribe("UEB", "POLICY-CL-MGT");
135 assertTrue(obj instanceof VirtualControlLoopNotification);
136 assertTrue(((VirtualControlLoopNotification)obj).notification.equals(ControlLoopNotificationType.ACTIVE));
140 * Give the control loop time to acquire a lock
144 } catch (InterruptedException e) {
146 logger.debug("An interrupt Exception was thrown");
147 fail("An interrupt Exception was thrown");
151 * Give time to finish processing
155 } catch (InterruptedException e) {
157 logger.debug("An interrupt Exception was thrown");
158 fail("An interrupt Exception was thrown");
162 * One final check to make sure the lock is released
164 assertFalse(PolicyGuard.isLocked(TargetType.VNF, triggerSourceName, requestID));
167 * This will stop the thread that is firing the rules
172 * The only fact in memory should be Params
174 assertEquals(1, kieSession.getFactCount());
177 * Print what's left in memory
179 dumpFacts(kieSession);
182 * Gracefully shut down the kie session
184 kieSession.dispose();
188 * This method will start a kie session and instantiate
191 * @param droolsTemplate
194 * the yaml file containing the policies
199 * @param policyVersion
200 * version of the policy
201 * @return the kieSession to be used to insert facts
202 * @throws IOException
204 private KieSession startSession(String droolsTemplate,
208 String policyVersion) throws IOException {
211 * Load policies from yaml
213 pair = Util.loadYaml(yamlFile);
215 assertNotNull(pair.a);
216 assertNotNull(pair.a.getControlLoop());
217 assertNotNull(pair.a.getControlLoop().getControlLoopName());
218 assertTrue(pair.a.getControlLoop().getControlLoopName().length() > 0);
221 * Construct a kie session
223 final KieSession kieSession = Util.buildContainer(droolsTemplate,
224 pair.a.getControlLoop().getControlLoopName(),
228 URLEncoder.encode(pair.b, "UTF-8"));
231 * Retrieve the Policy Engine
233 engine = (PolicyEngineJUnitImpl) kieSession.getGlobal("Engine");
235 logger.debug("============");
236 logger.debug(URLEncoder.encode(pair.b, "UTF-8"));
237 logger.debug("============");
243 * This method is used to simulate event messages from DCAE
244 * that start the control loop (onset message).
246 * @param policy the controlLoopName comes from the policy
247 * @param requestID the requestId for this event
248 * @param triggerSourceName
249 * @throws InterruptedException
251 protected void sendOnset(ControlLoopPolicy policy, UUID requestID, String triggerSourceName) throws InterruptedException {
252 VirtualControlLoopEvent event = new VirtualControlLoopEvent();
253 event.closedLoopControlName = policy.getControlLoop().getControlLoopName();
254 event.requestID = requestID;
255 event.target = "VNF_NAME";
256 event.target_type = ControlLoopTargetType.VNF;
257 event.closedLoopAlarmStart = Instant.now();
258 event.AAI = new HashMap<>();
259 event.AAI.put("cloud-region.identity-url", "foo");
260 event.AAI.put("vserver.selflink", "bar");
261 event.AAI.put("vserver.is-closed-loop-disabled", "false");
262 event.AAI.put("vserver.vserver-name", "vserver-name-16102016-aai3255-data-11-1");
263 event.closedLoopEventStatus = ControlLoopEventStatus.ONSET;
264 kieSession.insert(event);
269 * This method is used to simulate event messages from DCAE
270 * that end the control loop (abatement message).
272 * @param policy the controlLoopName comes from the policy
273 * @param requestID the requestId for this event
274 * @param triggerSourceName
275 * @throws InterruptedException
277 protected void sendAbatement(ControlLoopPolicy policy, UUID requestID, String triggerSourceName) throws InterruptedException {
278 VirtualControlLoopEvent event = new VirtualControlLoopEvent();
279 event.closedLoopControlName = policy.getControlLoop().getControlLoopName();
280 event.requestID = requestID;
281 event.target = "generic-vnf.vnf-id";
282 event.closedLoopAlarmStart = Instant.now().minusSeconds(5);
283 event.closedLoopAlarmEnd = Instant.now();
284 event.AAI = new HashMap<>();
285 event.AAI.put("cloud-region.identity-url", "foo");
286 event.AAI.put("vserver.selflink", "bar");
287 event.AAI.put("vserver.is-closed-loop-disabled", "false");
288 event.AAI.put("generic-vnf.vnf-id", "testGenericVnfID");
289 event.closedLoopEventStatus = ControlLoopEventStatus.ABATED;
290 kieSession.insert(event);
294 * This method will dump all the facts in the working memory.
296 * @param kieSession the session containing the facts
298 public void dumpFacts(KieSession kieSession) {
299 logger.debug("Fact Count: {}", kieSession.getFactCount());
300 for (FactHandle handle : kieSession.getFactHandles()) {
301 logger.debug("FACT: {}", handle);