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.assertFalse;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertTrue;
26 import static org.junit.Assert.fail;
28 import java.io.IOException;
29 import java.net.URLEncoder;
30 import java.time.Instant;
31 import java.util.HashMap;
32 import java.util.UUID;
34 import org.junit.AfterClass;
35 import org.junit.BeforeClass;
36 import org.junit.Test;
37 import org.kie.api.runtime.KieSession;
38 import org.kie.api.runtime.rule.FactHandle;
39 import org.onap.policy.controlloop.ControlLoopEventStatus;
40 import org.onap.policy.controlloop.ControlLoopNotificationType;
41 import org.onap.policy.controlloop.ControlLoopTargetType;
42 import org.onap.policy.controlloop.VirtualControlLoopEvent;
43 import org.onap.policy.controlloop.VirtualControlLoopNotification;
44 import org.onap.policy.controlloop.policy.ControlLoopPolicy;
45 import org.onap.policy.controlloop.policy.TargetType;
46 import org.onap.policy.drools.http.server.HttpServletServer;
47 import org.onap.policy.drools.impl.PolicyEngineJUnitImpl;
48 import org.onap.policy.guard.PolicyGuard;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
52 public class VDNSControlLoopTest {
54 private static final Logger logger = LoggerFactory.getLogger(VDNSControlLoopTest.class);
56 private KieSession kieSession;
57 private Util.Pair<ControlLoopPolicy, String> pair;
58 private PolicyEngineJUnitImpl engine;
61 /* Set environment properties */
68 public static void setUpSimulator() {
73 } catch (Exception e) {
79 public static void tearDownSimulator() {
80 HttpServletServer.factory.destroy();
84 public void successTest() {
87 * Start the kie session
90 kieSession = startSession("src/main/resources/ControlLoop_Template_xacml_guard.drl",
91 "src/test/resources/yaml/policy_ControlLoop_SO-test.yaml",
95 } catch (IOException e) {
97 logger.debug("Could not create kieSession");
98 fail("Could not create kieSession");
102 * Create a thread to continuously fire rules
103 * until main thread calls halt
105 new Thread( new Runnable() {
108 kieSession.fireUntilHalt();
113 * Create a unique requestId and a unique trigger source
115 UUID requestID = UUID.randomUUID();
116 String triggerSourceName = "foobartriggersource36";
119 * This will be the object returned from the PolicyEngine
124 * Simulate an onset event the policy engine will
125 * receive from DCAE to kick off processing through
129 sendOnset(pair.a, requestID, triggerSourceName);
130 } catch (InterruptedException e) {
132 logger.debug("Unable to send onset event");
133 fail("Unable to send onset event");
137 * Pull the object that was sent out and make
138 * sure it is a ControlLoopNoticiation of type active
140 obj = engine.subscribe("UEB", "POLICY-CL-MGT");
142 assertTrue(obj instanceof VirtualControlLoopNotification);
143 assertTrue(((VirtualControlLoopNotification)obj).notification.equals(ControlLoopNotificationType.ACTIVE));
147 * Give the control loop time to acquire a lock
151 } catch (InterruptedException e) {
153 logger.debug("An interrupt Exception was thrown");
154 fail("An interrupt Exception was thrown");
158 * Give time to finish processing
162 } catch (InterruptedException e) {
164 logger.debug("An interrupt Exception was thrown");
165 fail("An interrupt Exception was thrown");
169 * One final check to make sure the lock is released
171 assertFalse(PolicyGuard.isLocked(TargetType.VNF, triggerSourceName, requestID));
174 * This will stop the thread that is firing the rules
179 * The only fact in memory should be Params
181 // assertEquals(1, kieSession.getFactCount());
182 if (kieSession.getFactCount() != 1L) {
183 logger.error("FACT count mismatch: 1 expected but there are {}", kieSession.getFactCount());
187 * Print what's left in memory
189 dumpFacts(kieSession);
192 * Gracefully shut down the kie session
194 kieSession.dispose();
198 * This method will start a kie session and instantiate
201 * @param droolsTemplate
204 * the yaml file containing the policies
209 * @param policyVersion
210 * version of the policy
211 * @return the kieSession to be used to insert facts
212 * @throws IOException
214 private KieSession startSession(String droolsTemplate,
218 String policyVersion) throws IOException {
221 * Load policies from yaml
223 pair = Util.loadYaml(yamlFile);
225 assertNotNull(pair.a);
226 assertNotNull(pair.a.getControlLoop());
227 assertNotNull(pair.a.getControlLoop().getControlLoopName());
228 assertTrue(pair.a.getControlLoop().getControlLoopName().length() > 0);
231 * Construct a kie session
233 final KieSession kieSession = Util.buildContainer(droolsTemplate,
234 pair.a.getControlLoop().getControlLoopName(),
238 URLEncoder.encode(pair.b, "UTF-8"));
241 * Retrieve the Policy Engine
243 engine = (PolicyEngineJUnitImpl) kieSession.getGlobal("Engine");
245 logger.debug("============");
246 logger.debug(URLEncoder.encode(pair.b, "UTF-8"));
247 logger.debug("============");
253 * This method is used to simulate event messages from DCAE
254 * that start the control loop (onset message).
256 * @param policy the controlLoopName comes from the policy
257 * @param requestID the requestId for this event
258 * @param triggerSourceName
259 * @throws InterruptedException
261 protected void sendOnset(ControlLoopPolicy policy, UUID requestID, String triggerSourceName) throws InterruptedException {
262 VirtualControlLoopEvent event = new VirtualControlLoopEvent();
263 event.closedLoopControlName = policy.getControlLoop().getControlLoopName();
264 event.requestID = requestID;
265 event.target = "VNF_NAME";
266 event.target_type = ControlLoopTargetType.VNF;
267 event.closedLoopAlarmStart = Instant.now();
268 event.AAI = new HashMap<>();
269 event.AAI.put("cloud-region.identity-url", "foo");
270 event.AAI.put("vserver.selflink", "bar");
271 event.AAI.put("vserver.is-closed-loop-disabled", "false");
272 event.AAI.put("vserver.vserver-name", "vserver-name-16102016-aai3255-data-11-1");
273 event.closedLoopEventStatus = ControlLoopEventStatus.ONSET;
274 kieSession.insert(event);
279 * This method is used to simulate event messages from DCAE
280 * that end the control loop (abatement message).
282 * @param policy the controlLoopName comes from the policy
283 * @param requestID the requestId for this event
284 * @param triggerSourceName
285 * @throws InterruptedException
287 protected void sendAbatement(ControlLoopPolicy policy, UUID requestID, String triggerSourceName) throws InterruptedException {
288 VirtualControlLoopEvent event = new VirtualControlLoopEvent();
289 event.closedLoopControlName = policy.getControlLoop().getControlLoopName();
290 event.requestID = requestID;
291 event.target = "generic-vnf.vnf-id";
292 event.closedLoopAlarmStart = Instant.now().minusSeconds(5);
293 event.closedLoopAlarmEnd = Instant.now();
294 event.AAI = new HashMap<>();
295 event.AAI.put("cloud-region.identity-url", "foo");
296 event.AAI.put("vserver.selflink", "bar");
297 event.AAI.put("vserver.is-closed-loop-disabled", "false");
298 event.AAI.put("generic-vnf.vnf-id", "testGenericVnfID");
299 event.closedLoopEventStatus = ControlLoopEventStatus.ABATED;
300 kieSession.insert(event);
304 * This method will dump all the facts in the working memory.
306 * @param kieSession the session containing the facts
308 public void dumpFacts(KieSession kieSession) {
309 logger.debug("Fact Count: {}", kieSession.getFactCount());
310 for (FactHandle handle : kieSession.getFactHandles()) {
311 logger.debug("FACT: {}", handle);