4f472a07bbd7c96dda7276650d6224eb54101031
[policy/drools-applications.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * demo
4  * ================================================================================
5  * Copyright (C) 2017-2018 Intel Corp. All rights reserved.
6  * Modifications Copyright (C) 2018-2020 AT&T Intellectual Property. All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.template.demo;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertTrue;
27 import static org.junit.Assert.fail;
28
29 import java.io.IOException;
30 import java.time.Instant;
31 import java.util.HashMap;
32 import java.util.UUID;
33 import org.junit.BeforeClass;
34 import org.junit.Test;
35 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
36 import org.onap.policy.common.endpoints.event.comm.TopicListener;
37 import org.onap.policy.common.endpoints.event.comm.TopicSink;
38 import org.onap.policy.controlloop.ControlLoopEventStatus;
39 import org.onap.policy.controlloop.ControlLoopNotificationType;
40 import org.onap.policy.controlloop.ControlLoopTargetType;
41 import org.onap.policy.controlloop.VirtualControlLoopEvent;
42 import org.onap.policy.controlloop.VirtualControlLoopNotification;
43 import org.onap.policy.controlloop.policy.ControlLoopPolicy;
44 import org.onap.policy.vfc.VfcRequest;
45
46
47 public class VfcControlLoopTest extends ControlLoopBase implements TopicListener {
48
49     /**
50      * Setup the simulator.
51      */
52     @BeforeClass
53     public static void setUpBeforeClass() {
54         ControlLoopBase.setUpBeforeClass(
55             "../archetype-cl-amsterdam/src/main/resources/archetype-resources/"
56                             + "src/main/resources/__closedLoopControlName__.drl",
57             "src/test/resources/yaml/policy_ControlLoop_VFC.yaml",
58             "type=operational",
59             "CL_VoLTE",
60             "v2.0");
61     }
62
63     @Test
64     public void testSuccess() throws IOException {
65
66         /*
67          * Allows the PolicyEngine to callback to this object to notify that there is an event ready
68          * to be pulled from the queue
69          */
70         for (TopicSink sink : noopTopics) {
71             assertTrue(sink.start());
72             sink.register(this);
73         }
74
75         /*
76          * Create a unique requestId
77          */
78         requestId = UUID.randomUUID();
79
80         /*
81          * Simulate an onset event the policy engine will receive from DCAE to kick off processing
82          * through the rules
83          */
84         sendEvent(pair.first, requestId, ControlLoopEventStatus.ONSET);
85
86         kieSession.fireUntilHalt();
87
88         // allow object clean-up
89         kieSession.fireAllRules();
90
91         /*
92          * The only fact in memory should be Params
93          */
94         assertEquals(1, kieSession.getFactCount());
95
96         /*
97          * Print what's left in memory
98          */
99         dumpFacts(kieSession);
100     }
101
102     @Test
103     public void testNullRequest() throws IOException {
104
105         /*
106          * Allows the PolicyEngine to callback to this object to notify that there is an event ready
107          * to be pulled from the queue
108          */
109         for (TopicSink sink : noopTopics) {
110             assertTrue(sink.start());
111             sink.register(this);
112         }
113
114         /*
115          * Create a unique requestId
116          */
117         requestId = UUID.randomUUID();
118
119         /*
120          * Simulate an onset event the policy engine will receive from DCAE to kick off processing
121          * through the rules
122          */
123
124         VirtualControlLoopEvent event = new VirtualControlLoopEvent();
125         event.setClosedLoopControlName(pair.first.getControlLoop().getControlLoopName());
126         event.setRequestId(UUID.randomUUID());
127         event.setClosedLoopEventClient("tca.instance00009");
128         event.setTargetType(ControlLoopTargetType.VM);
129         event.setTarget("vserver.vserver-name");
130         event.setFrom("DCAE");
131         event.setClosedLoopAlarmStart(Instant.now());
132         event.setAai(new HashMap<String, String>());
133         event.getAai().put("vserver.vserver-name", "nullRequest");
134         event.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
135         kieSession.insert(event);
136
137         kieSession.fireUntilHalt();
138
139         // allow object clean-up
140         kieSession.fireAllRules();
141
142         /*
143          * The only fact in memory should be Params
144          */
145         assertEquals(1, kieSession.getFactCount());
146
147         /*
148          * Print what's left in memory
149          */
150         dumpFacts(kieSession);
151     }
152
153     /*
154      * (non-Javadoc)
155      *
156      * @see org.onap.policy.drools.PolicyEngineListener#newEventNotification(java.lang.String)
157      */
158     @Override
159     public void onTopicEvent(CommInfrastructure commType, String topic, String event) {
160         /*
161          * Pull the object that was sent out to DMAAP and make sure it is a ControlLoopNoticiation
162          * of type active
163          */
164         Object obj = null;
165         if ("POLICY-CL-MGT".equals(topic)) {
166             obj = org.onap.policy.controlloop.util.Serialization.gsonJunit.fromJson(event,
167                     org.onap.policy.controlloop.VirtualControlLoopNotification.class);
168         }
169         assertNotNull(obj);
170         if (obj instanceof VirtualControlLoopNotification) {
171             VirtualControlLoopNotification notification = (VirtualControlLoopNotification) obj;
172             String policyName = notification.getPolicyName();
173             if (policyName.endsWith("EVENT")) {
174                 logger.debug("Rule Fired: " + notification.getPolicyName());
175                 assertTrue(ControlLoopNotificationType.ACTIVE.equals(notification.getNotification()));
176             } else if (policyName.endsWith("GUARD_NOT_YET_QUERIED")) {
177                 logger.debug("Rule Fired: " + notification.getPolicyName());
178                 assertTrue(ControlLoopNotificationType.OPERATION.equals(notification.getNotification()));
179                 assertNotNull(notification.getMessage());
180                 assertTrue(notification.getMessage().startsWith("Sending guard query"));
181             } else if (policyName.endsWith("GUARD.RESPONSE")) {
182                 logger.debug("Rule Fired: " + notification.getPolicyName());
183                 assertTrue(ControlLoopNotificationType.OPERATION.equals(notification.getNotification()));
184                 assertNotNull(notification.getMessage());
185                 assertTrue(notification.getMessage().toLowerCase().endsWith("permit"));
186             } else if (policyName.endsWith("GUARD_PERMITTED")) {
187                 logger.debug("Rule Fired: " + notification.getPolicyName());
188                 assertTrue(ControlLoopNotificationType.OPERATION.equals(notification.getNotification()));
189                 assertNotNull(notification.getMessage());
190                 assertTrue(notification.getMessage().startsWith("actor=VFC"));
191             } else if (policyName.endsWith("OPERATION.TIMEOUT")) {
192                 logger.debug("Rule Fired: " + notification.getPolicyName());
193                 kieSession.halt();
194                 logger.debug("The operation timed out");
195                 fail("Operation Timed Out");
196             } else if (policyName.endsWith("VFC.RESPONSE")) {
197                 logger.debug("Rule Fired: " + notification.getPolicyName());
198                 assertTrue(ControlLoopNotificationType.OPERATION_SUCCESS.equals(notification.getNotification()));
199                 assertNotNull(notification.getMessage());
200                 assertTrue(notification.getMessage().startsWith("actor=VFC"));
201             } else if (policyName.endsWith("EVENT.MANAGER")) {
202                 logger.debug("Rule Fired: " + notification.getPolicyName());
203                 if ("nullRequest".equals(notification.getAai().get("vserver.vserver-name"))) {
204                     assertEquals(ControlLoopNotificationType.FINAL_FAILURE, notification.getNotification());
205                 } else {
206                     assertEquals(ControlLoopNotificationType.FINAL_SUCCESS, notification.getNotification());
207                 }
208                 kieSession.halt();
209             } else if (policyName.endsWith("EVENT.MANAGER.TIMEOUT")) {
210                 logger.debug("Rule Fired: " + notification.getPolicyName());
211                 kieSession.halt();
212                 logger.debug("The control loop timed out");
213                 fail("Control Loop Timed Out");
214             }
215         } else if (obj instanceof VfcRequest) {
216             logger.debug("\n============ VFC received the request!!! ===========\n");
217         }
218     }
219
220     /**
221      * This method is used to simulate event messages from DCAE that start the control loop (onset
222      * message) or end the control loop (abatement message).
223      *
224      * @param policy the controlLoopName comes from the policy
225      * @param requestId the requestId for this event
226      * @param status could be onset or abated
227      */
228     protected void sendEvent(ControlLoopPolicy policy, UUID requestId, ControlLoopEventStatus status) {
229         VirtualControlLoopEvent event = new VirtualControlLoopEvent();
230         event.setClosedLoopControlName(policy.getControlLoop().getControlLoopName());
231         event.setRequestId(UUID.randomUUID());
232         event.setClosedLoopEventClient("tca.instance00009");
233         event.setTargetType(ControlLoopTargetType.VM);
234         event.setTarget("vserver.vserver-name");
235         event.setFrom("DCAE");
236         event.setClosedLoopAlarmStart(Instant.now());
237         event.setAai(new HashMap<String, String>());
238         event.getAai().put("vserver.vserver-name", "vserver-name-16102016-aai3255-data-11-1");
239         event.getAai().put("vserver.vserver-id", "vserver-id-16102016-aai3255-data-11-1");
240         event.getAai().put("generic-vnf.vnf-id", "vnf-id-16102016-aai3255-data-11-1");
241         event.getAai().put("service-instance.service-instance-id", "service-instance-id-16102016-aai3255-data-11-1");
242         event.getAai().put("vserver.is-closed-loop-disabled", "false");
243         event.getAai().put("vserver.prov-status", "ACTIVE");
244         event.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
245         kieSession.insert(event);
246     }
247 }