2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  * Copyright (C) 2017 Intel Corp. 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.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.drools.PolicyEngineListener;
 
  46 import org.onap.policy.drools.http.server.HttpServletServer;
 
  47 import org.onap.policy.drools.impl.PolicyEngineJUnitImpl;
 
  48 import org.onap.policy.vfc.VFCRequest;
 
  49 import org.slf4j.Logger;
 
  50 import org.slf4j.LoggerFactory;
 
  53 public class VFCControlLoopTest implements PolicyEngineListener {
 
  55         private static final Logger logger = LoggerFactory.getLogger(VFCControlLoopTest.class);
 
  56         private KieSession kieSession;
 
  57         private Util.Pair<ControlLoopPolicy, String> pair;
 
  58         private PolicyEngineJUnitImpl engine;
 
  59         private UUID requestID;
 
  62             /* Set environment properties */
 
  70         public static void setUpSimulator() {
 
  75                 } catch (Exception e) {
 
  81         public static void tearDownSimulator() {
 
  82                 HttpServletServer.factory.destroy();
 
  86         public void successTest() throws IOException {
 
  89          * Start the kie session
 
  92             kieSession = startSession("src/main/resources/ControlLoop_Template_xacml_guard.drl", 
 
  93                         "src/test/resources/yaml/policy_ControlLoop_VFC.yaml",
 
  97         } catch (IOException e) {
 
  99             logger.debug("Could not create kieSession");
 
 100             fail("Could not create kieSession");
 
 104          * Allows the PolicyEngine to callback to this object to
 
 105          * notify that there is an event ready to be pulled 
 
 108         engine.addListener(this);
 
 111          * Create a unique requestId
 
 113         requestID = UUID.randomUUID();
 
 116          * Simulate an onset event the policy engine will 
 
 117          * receive from DCAE to kick off processing through
 
 120         sendEvent(pair.a, requestID, ControlLoopEventStatus.ONSET);
 
 122         kieSession.fireUntilHalt();
 
 125          * The only fact in memory should be Params
 
 127         assertEquals(1, kieSession.getFactCount());
 
 130          * Print what's left in memory
 
 132         dumpFacts(kieSession);
 
 135          * Gracefully shut down the kie session
 
 137         kieSession.dispose();
 
 141          * This method will start a kie session and instantiate
 
 144          * @param droolsTemplate
 
 147          *          the yaml file containing the policies
 
 152          * @param policyVersion
 
 153          *          version of the policy
 
 154          * @return the kieSession to be used to insert facts
 
 155          * @throws IOException
 
 157         private KieSession startSession(String droolsTemplate,
 
 161                                         String policyVersion) throws IOException {
 
 164          * Load policies from yaml
 
 166                 pair = Util.loadYaml(yamlFile);
 
 168                 assertNotNull(pair.a);
 
 169                 assertNotNull(pair.a.getControlLoop());
 
 170                 assertNotNull(pair.a.getControlLoop().getControlLoopName());
 
 171                 assertTrue(pair.a.getControlLoop().getControlLoopName().length() > 0);
 
 174          * Construct a kie session
 
 176                 final KieSession kieSession = Util.buildContainer(droolsTemplate,
 
 177                                 pair.a.getControlLoop().getControlLoopName(),
 
 181                                 URLEncoder.encode(pair.b, "UTF-8"));
 
 184          * Retrieve the Policy Engine
 
 186                 engine = (PolicyEngineJUnitImpl) kieSession.getGlobal("Engine");
 
 188                 logger.debug("============");
 
 189                 logger.debug(URLEncoder.encode(pair.b, "UTF-8"));
 
 190                 logger.debug("============");
 
 197      * @see org.onap.policy.drools.PolicyEngineListener#newEventNotification(java.lang.String)
 
 199         public void newEventNotification(String topic) {
 
 201          * Pull the object that was sent out to DMAAP and make
 
 202          * sure it is a ControlLoopNoticiation of type active
 
 204         Object obj = engine.subscribe("UEB", topic);
 
 206         if (obj instanceof VirtualControlLoopNotification) {
 
 207             VirtualControlLoopNotification notification = (VirtualControlLoopNotification) obj;
 
 208             String policyName = notification.policyName;
 
 209             if (policyName.endsWith("EVENT")) {
 
 210                 logger.debug("Rule Fired: " + notification.policyName);
 
 211                 assertTrue(ControlLoopNotificationType.ACTIVE.equals(notification.notification));
 
 213             else if (policyName.endsWith("GUARD_NOT_YET_QUERIED")) {
 
 214                 logger.debug("Rule Fired: " + notification.policyName);
 
 215                 assertTrue(ControlLoopNotificationType.OPERATION.equals(notification.notification));
 
 216                 assertNotNull(notification.message);
 
 217                 assertTrue(notification.message.startsWith("Sending guard query"));
 
 219             else if (policyName.endsWith("GUARD.RESPONSE")) {
 
 220                 logger.debug("Rule Fired: " + notification.policyName);
 
 221                 assertTrue(ControlLoopNotificationType.OPERATION.equals(notification.notification));
 
 222                 assertNotNull(notification.message);
 
 223                 assertTrue(notification.message.endsWith("PERMIT"));
 
 225             else if (policyName.endsWith("GUARD_PERMITTED")) {
 
 226                 logger.debug("Rule Fired: " + notification.policyName);
 
 227                 assertTrue(ControlLoopNotificationType.OPERATION.equals(notification.notification));
 
 228                 assertNotNull(notification.message);
 
 229                 assertTrue(notification.message.startsWith("actor=VFC"));
 
 231             else if (policyName.endsWith("OPERATION.TIMEOUT")) {
 
 232                 logger.debug("Rule Fired: " + notification.policyName);
 
 234                 logger.debug("The operation timed out");
 
 235                 fail("Operation Timed Out");
 
 237             else if (policyName.endsWith("VFC.RESPONSE")) {
 
 238                 logger.debug("Rule Fired: " + notification.policyName);
 
 239                 assertTrue(ControlLoopNotificationType.OPERATION_SUCCESS.equals(notification.notification));
 
 240                 assertNotNull(notification.message);
 
 241                 assertTrue(notification.message.startsWith("actor=VFC"));
 
 243             else if (policyName.endsWith("EVENT.MANAGER")) {
 
 244                 logger.debug("Rule Fired: " + notification.policyName);
 
 245                 assertTrue(ControlLoopNotificationType.FINAL_SUCCESS.equals(notification.notification));
 
 248             else if (policyName.endsWith("EVENT.MANAGER.TIMEOUT")) {
 
 249                 logger.debug("Rule Fired: " + notification.policyName);
 
 251                 logger.debug("The control loop timed out");
 
 252                 fail("Control Loop Timed Out");
 
 255         else if (obj instanceof VFCRequest) {
 
 256             logger.debug("\n============ VFC received the request!!! ===========\n");
 
 261     * This method is used to simulate event messages from DCAE
 
 262     * that start the control loop (onset message) or end the
 
 263     * control loop (abatement message).
 
 265     * @param policy the controlLoopName comes from the policy 
 
 266     * @param requestID the requestId for this event
 
 267     * @param status could be onset or abated
 
 269    protected void sendEvent(ControlLoopPolicy policy, UUID requestID, ControlLoopEventStatus status) {
 
 270        VirtualControlLoopEvent event = new VirtualControlLoopEvent();
 
 271        event.closedLoopControlName = policy.getControlLoop().getControlLoopName();
 
 272        event.requestID = UUID.randomUUID();
 
 273        event.closedLoopEventClient = "tca.instance00009";
 
 274        event.target_type = ControlLoopTargetType.VM;
 
 275        event.target = "VM_NAME";
 
 277        event.closedLoopAlarmStart = Instant.now();
 
 278        event.AAI = new HashMap<String, String>();
 
 279        event.AAI.put("vserver.vserver-name", "vserver-name-16102016-aai3255-data-11-1");
 
 280        event.AAI.put("vserver.vserver-id", "vserver-id-16102016-aai3255-data-11-1");
 
 281        event.AAI.put("generic-vnf.vnf-id", "vnf-id-16102016-aai3255-data-11-1");
 
 282        event.AAI.put("service-instance.service-instance-id", "service-instance-id-16102016-aai3255-data-11-1");
 
 283        event.AAI.put("vserver.is-closed-loop-disabled", "false");
 
 284        event.closedLoopEventStatus = ControlLoopEventStatus.ONSET;
 
 285        kieSession.insert(event);
 
 288    public static void dumpFacts(KieSession kieSession) {
 
 289        logger.debug("Fact Count: " + kieSession.getFactCount());
 
 290        for (FactHandle handle : kieSession.getFactHandles()) {
 
 291            logger.debug("FACT: " + handle);