2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  * Copyright (C) 2017-2019 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.assertNotNull;
 
  25 import static org.junit.Assert.assertTrue;
 
  26 import static org.junit.Assert.fail;
 
  28 import java.time.Instant;
 
  29 import java.util.HashMap;
 
  30 import java.util.UUID;
 
  31 import org.junit.BeforeClass;
 
  32 import org.junit.Test;
 
  33 import org.onap.policy.appclcm.LcmRequest;
 
  34 import org.onap.policy.appclcm.LcmRequestWrapper;
 
  35 import org.onap.policy.appclcm.LcmResponse;
 
  36 import org.onap.policy.appclcm.LcmResponseWrapper;
 
  37 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
 
  38 import org.onap.policy.common.endpoints.event.comm.TopicListener;
 
  39 import org.onap.policy.common.endpoints.event.comm.TopicSink;
 
  40 import org.onap.policy.controlloop.ControlLoopEventStatus;
 
  41 import org.onap.policy.controlloop.ControlLoopNotificationType;
 
  42 import org.onap.policy.controlloop.VirtualControlLoopEvent;
 
  43 import org.onap.policy.controlloop.VirtualControlLoopNotification;
 
  44 import org.onap.policy.controlloop.policy.ControlLoopPolicy;
 
  46 public class ControlLoopFailureTest extends ControlLoopBase implements TopicListener {
 
  48     private UUID requestId2;
 
  49     private UUID requestId3;
 
  50     private int eventCount;
 
  56     public static void setUpBeforeClass() {
 
  57         ControlLoopBase.setUpBeforeClass(
 
  58             "../archetype-cl-amsterdam/src/main/resources/archetype-resources"
 
  59                             + "/src/main/resources/__closedLoopControlName__.drl",
 
  60             "src/test/resources/yaml/policy_ControlLoop_vCPE.yaml",
 
  61             "service=ServiceDemo;resource=Res1Demo;type=operational",
 
  63             "org.onap.closed_loop.ServiceDemo:VNFS:1.0.0");
 
  67      * This test case tests the scenario where 3 events occur and 2 of the requests refer to the
 
  68      * same target entity while the 3rd is for another entity. The expected result is that the event
 
  69      * with the duplicate target entity will have a final success result for one of the events, and
 
  70      * a rejected message for the one that was unable to obtain the lock. The event that is
 
  71      * referring to a different target entity should be able to obtain a lock since it is a
 
  72      * different target. After processing of all events there should only be the params object left
 
  76     public void targetLockedTest() {
 
  79          * Allows the PolicyEngine to callback to this object to notify that there is an event ready
 
  80          * to be pulled from the queue
 
  82         for (TopicSink sink : noopTopics) {
 
  83             assertTrue(sink.start());
 
  88          * Create a unique requestId
 
  90         requestId = UUID.randomUUID();
 
  93          * This will be a unique request for another target entity
 
  95         requestId2 = UUID.randomUUID();
 
  98          * This will be a request duplicating the target entity of the first request
 
 100         requestId3 = UUID.randomUUID();
 
 103          * Simulate an onset event the policy engine will receive from DCAE to kick off processing
 
 106         sendEvent(pair.first, requestId, ControlLoopEventStatus.ONSET, "vnf01");
 
 109          * Send a second event requesting an action for a different target entity
 
 111         sendEvent(pair.first, requestId2, ControlLoopEventStatus.ONSET, "vnf02");
 
 114          * Send a second event for a different target to ensure there are no problems with obtaining
 
 115          * a lock for a different
 
 117         kieSession.fireUntilHalt();
 
 119         // allow object clean-up
 
 120         kieSession.fireAllRules();
 
 123          * The only fact in memory should be Params
 
 125         assertEquals(1, kieSession.getFactCount());
 
 128          * Print what's left in memory
 
 130         dumpFacts(kieSession);
 
 136      * @see org.onap.policy.drools.PolicyEngineListener#newEventNotification(java.lang.String)
 
 139     public void onTopicEvent(CommInfrastructure commType, String topic, String event) {
 
 141          * Pull the object that was sent out to DMAAP and make sure it is a ControlLoopNoticiation
 
 145         if ("POLICY-CL-MGT".equals(topic)) {
 
 146             obj = org.onap.policy.controlloop.util.Serialization.gsonJunit.fromJson(event,
 
 147                     org.onap.policy.controlloop.VirtualControlLoopNotification.class);
 
 148         } else if ("APPC-LCM-READ".equals(topic)) {
 
 149             obj = org.onap.policy.appclcm.util.Serialization.gsonJunit.fromJson(event,
 
 150                     org.onap.policy.appclcm.LcmRequestWrapper.class);
 
 153         if (obj instanceof VirtualControlLoopNotification) {
 
 154             VirtualControlLoopNotification notification = (VirtualControlLoopNotification) obj;
 
 155             String policyName = notification.getPolicyName();
 
 156             if (policyName.endsWith("EVENT")) {
 
 157                 logger.debug("Rule Fired: " + notification.getPolicyName());
 
 158                 assertTrue(ControlLoopNotificationType.ACTIVE.equals(notification.getNotification()));
 
 159             } else if (policyName.endsWith("GUARD_NOT_YET_QUERIED")) {
 
 160                 logger.debug("Rule Fired: " + notification.getPolicyName());
 
 161                 assertTrue(ControlLoopNotificationType.OPERATION.equals(notification.getNotification()));
 
 162                 assertNotNull(notification.getMessage());
 
 163                 assertTrue(notification.getMessage().startsWith("Sending guard query"));
 
 164             } else if (policyName.endsWith("GUARD.RESPONSE")) {
 
 165                 logger.debug("Rule Fired: " + notification.getPolicyName());
 
 166                 assertTrue(ControlLoopNotificationType.OPERATION.equals(notification.getNotification()));
 
 167                 assertNotNull(notification.getMessage());
 
 168                 assertTrue(notification.getMessage().toLowerCase().endsWith("permit"));
 
 169             } else if (policyName.endsWith("GUARD_PERMITTED")) {
 
 170                 logger.debug("Rule Fired: " + notification.getPolicyName());
 
 171                 assertTrue(ControlLoopNotificationType.OPERATION.equals(notification.getNotification()));
 
 172                 assertNotNull(notification.getMessage());
 
 173                 assertTrue(notification.getMessage().startsWith("actor=APPC"));
 
 174             } else if (policyName.endsWith("OPERATION.TIMEOUT")) {
 
 175                 logger.debug("Rule Fired: " + notification.getPolicyName());
 
 177                 logger.debug("The operation timed out");
 
 178                 fail("Operation Timed Out");
 
 179             } else if (policyName.endsWith("APPC.LCM.RESPONSE")) {
 
 180                 logger.debug("Rule Fired: " + notification.getPolicyName());
 
 181                 assertTrue(ControlLoopNotificationType.OPERATION_SUCCESS.equals(notification.getNotification()));
 
 182                 assertNotNull(notification.getMessage());
 
 183                 assertTrue(notification.getMessage().startsWith("actor=APPC"));
 
 184                 if (requestId.equals(notification.getRequestId())) {
 
 185                     sendEvent(pair.first, requestId, ControlLoopEventStatus.ABATED, "vnf01");
 
 186                 } else if (requestId2.equals(notification.getRequestId())) {
 
 187                     sendEvent(pair.first, requestId2, ControlLoopEventStatus.ABATED, "vnf02");
 
 189             } else if (policyName.endsWith("EVENT.MANAGER")) {
 
 190                 logger.debug("Rule Fired: " + notification.getPolicyName());
 
 191                 if (requestId3.equals(notification.getRequestId())) {
 
 193                      * The event with the duplicate target should be rejected
 
 195                     assertTrue(ControlLoopNotificationType.REJECTED.equals(notification.getNotification()));
 
 197                     assertTrue(ControlLoopNotificationType.FINAL_SUCCESS.equals(notification.getNotification()));
 
 199                 if (++eventCount == 3) {
 
 202             } else if (policyName.endsWith("EVENT.MANAGER.TIMEOUT")) {
 
 203                 logger.debug("Rule Fired: " + notification.getPolicyName());
 
 205                 logger.debug("The control loop timed out");
 
 206                 fail("Control Loop Timed Out");
 
 208         } else if (obj instanceof LcmRequestWrapper) {
 
 210              * The request should be of type LCMRequestWrapper and the subrequestid should be 1
 
 212             LcmRequestWrapper dmaapRequest = (LcmRequestWrapper) obj;
 
 213             LcmRequest appcRequest = dmaapRequest.getBody();
 
 214             assertTrue(appcRequest.getCommonHeader().getSubRequestId().equals("1"));
 
 216             logger.debug("\n============ APPC received the request!!! ===========\n");
 
 219              * Simulate a success response from APPC and insert the response into the working memory
 
 221             LcmResponseWrapper dmaapResponse = new LcmResponseWrapper();
 
 222             LcmResponse appcResponse = new LcmResponse(appcRequest);
 
 223             appcResponse.getStatus().setCode(400);
 
 224             appcResponse.getStatus().setMessage("AppC success");
 
 225             dmaapResponse.setBody(appcResponse);
 
 228              * Interrupting with a different request for the same target entity to check if lock
 
 231             if (requestId.equals(appcResponse.getCommonHeader().getRequestId())) {
 
 232                 sendEvent(pair.first, requestId3, ControlLoopEventStatus.ONSET, "vnf01");
 
 234             kieSession.insert(dmaapResponse);
 
 239      * This method is used to simulate event messages from DCAE that start the control loop (onset
 
 240      * message) or end the control loop (abatement message).
 
 242      * @param policy the controlLoopName comes from the policy
 
 243      * @param requestId the requestId for this event
 
 244      * @param status could be onset or abated
 
 245      * @param target the target entity to take an action on
 
 247     protected void sendEvent(ControlLoopPolicy policy, UUID requestId, ControlLoopEventStatus status, String target) {
 
 248         VirtualControlLoopEvent event = new VirtualControlLoopEvent();
 
 249         event.setClosedLoopControlName(policy.getControlLoop().getControlLoopName());
 
 250         event.setRequestId(requestId);
 
 251         event.setTarget("generic-vnf.vnf-id");
 
 252         event.setClosedLoopAlarmStart(Instant.now());
 
 253         event.setAai(new HashMap<>());
 
 254         event.getAai().put("generic-vnf.vnf-id", target);
 
 255         event.setClosedLoopEventStatus(status);
 
 256         kieSession.insert(event);