0793b882586aaa34064e3db9e16e3735318fc09b
[policy/drools-applications.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * demo
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
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.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;
45
46 public class ControlLoopFailureTest extends ControlLoopBase implements TopicListener {
47
48     private UUID requestId2;
49     private UUID requestId3;
50     private int eventCount;
51
52     /**
53      * Setup simulator.
54      */
55     @BeforeClass
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",
62             "CL_vCPE",
63             "org.onap.closed_loop.ServiceDemo:VNFS:1.0.0");
64     }
65
66     /**
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
73      * in memory.
74      */
75     @Test
76     public void targetLockedTest() {
77
78         /*
79          * Allows the PolicyEngine to callback to this object to notify that there is an event ready
80          * to be pulled from the queue
81          */
82         for (TopicSink sink : noopTopics) {
83             assertTrue(sink.start());
84             sink.register(this);
85         }
86
87         /*
88          * Create a unique requestId
89          */
90         requestId = UUID.randomUUID();
91
92         /*
93          * This will be a unique request for another target entity
94          */
95         requestId2 = UUID.randomUUID();
96
97         /*
98          * This will be a request duplicating the target entity of the first request
99          */
100         requestId3 = UUID.randomUUID();
101
102         /*
103          * Simulate an onset event the policy engine will receive from DCAE to kick off processing
104          * through the rules
105          */
106         sendEvent(pair.first, requestId, ControlLoopEventStatus.ONSET, "vnf01");
107
108         /*
109          * Send a second event requesting an action for a different target entity
110          */
111         sendEvent(pair.first, requestId2, ControlLoopEventStatus.ONSET, "vnf02");
112
113         /*
114          * Send a second event for a different target to ensure there are no problems with obtaining
115          * a lock for a different
116          */
117         kieSession.fireUntilHalt();
118
119         // allow object clean-up
120         kieSession.fireAllRules();
121
122         /*
123          * The only fact in memory should be Params
124          */
125         assertEquals(1, kieSession.getFactCount());
126
127         /*
128          * Print what's left in memory
129          */
130         dumpFacts(kieSession);
131     }
132
133     /*
134      * (non-Javadoc)
135      *
136      * @see org.onap.policy.drools.PolicyEngineListener#newEventNotification(java.lang.String)
137      */
138     @Override
139     public void onTopicEvent(CommInfrastructure commType, String topic, String event) {
140         /*
141          * Pull the object that was sent out to DMAAP and make sure it is a ControlLoopNoticiation
142          * of type active
143          */
144         Object obj = null;
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);
151         }
152         assertNotNull(obj);
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());
176                 kieSession.halt();
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");
188                 }
189             } else if (policyName.endsWith("EVENT.MANAGER")) {
190                 logger.debug("Rule Fired: " + notification.getPolicyName());
191                 if (requestId3.equals(notification.getRequestId())) {
192                     /*
193                      * The event with the duplicate target should be rejected
194                      */
195                     assertTrue(ControlLoopNotificationType.REJECTED.equals(notification.getNotification()));
196                 } else {
197                     assertTrue(ControlLoopNotificationType.FINAL_SUCCESS.equals(notification.getNotification()));
198                 }
199                 if (++eventCount == 3) {
200                     kieSession.halt();
201                 }
202             } else if (policyName.endsWith("EVENT.MANAGER.TIMEOUT")) {
203                 logger.debug("Rule Fired: " + notification.getPolicyName());
204                 kieSession.halt();
205                 logger.debug("The control loop timed out");
206                 fail("Control Loop Timed Out");
207             }
208         } else if (obj instanceof LcmRequestWrapper) {
209             /*
210              * The request should be of type LCMRequestWrapper and the subrequestid should be 1
211              */
212             LcmRequestWrapper dmaapRequest = (LcmRequestWrapper) obj;
213             LcmRequest appcRequest = dmaapRequest.getBody();
214             assertTrue(appcRequest.getCommonHeader().getSubRequestId().equals("1"));
215
216             logger.debug("\n============ APPC received the request!!! ===========\n");
217
218             /*
219              * Simulate a success response from APPC and insert the response into the working memory
220              */
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);
226
227             /*
228              * Interrupting with a different request for the same target entity to check if lock
229              * will be denied
230              */
231             if (requestId.equals(appcResponse.getCommonHeader().getRequestId())) {
232                 sendEvent(pair.first, requestId3, ControlLoopEventStatus.ONSET, "vnf01");
233             }
234             kieSession.insert(dmaapResponse);
235         }
236     }
237
238     /**
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).
241      *
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
246      */
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.getAai().put("vserver.vserver-name", "OzVServer");
256         event.setClosedLoopEventStatus(status);
257         kieSession.insert(event);
258     }
259 }