Changes for checkstyle 8.32
[policy/apex-pdp.git] / examples / examples-aadm / src / test / java / org / onap / policy / apex / examples / aadm / AadmUseCaseTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2019-2020 Nordix Foundation.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.examples.aadm;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertTrue;
27
28 import java.io.IOException;
29 import org.junit.After;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.onap.policy.apex.context.ContextAlbum;
33 import org.onap.policy.apex.context.impl.schema.java.JavaSchemaHelperParameters;
34 import org.onap.policy.apex.context.parameters.ContextParameterConstants;
35 import org.onap.policy.apex.context.parameters.ContextParameters;
36 import org.onap.policy.apex.context.parameters.SchemaParameters;
37 import org.onap.policy.apex.core.engine.EngineParameters;
38 import org.onap.policy.apex.core.engine.engine.impl.ApexEngineFactory;
39 import org.onap.policy.apex.core.engine.engine.impl.ApexEngineImpl;
40 import org.onap.policy.apex.core.engine.event.EnEvent;
41 import org.onap.policy.apex.examples.aadm.concepts.ENodeBStatus;
42 import org.onap.policy.apex.examples.aadm.model.AadmDomainModelFactory;
43 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
44 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
45 import org.onap.policy.apex.model.eventmodel.concepts.AxEvent;
46 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
47 import org.onap.policy.apex.plugins.executor.mvel.MvelExecutorParameters;
48 import org.onap.policy.common.parameters.ParameterService;
49 import org.slf4j.ext.XLogger;
50 import org.slf4j.ext.XLoggerFactory;
51
52 // TODO: Auto-generated Javadoc
53 /**
54  * This class tests AADM use case.
55  *
56  * @author Sergey Sachkov (sergey.sachkov@ericsson.com)
57  *
58  */
59 public class AadmUseCaseTest {
60     private static final XLogger logger = XLoggerFactory.getXLogger(AadmUseCaseTest.class);
61
62     private SchemaParameters schemaParameters;
63     private ContextParameters contextParameters;
64     private EngineParameters engineParameters;
65
66     /**
67      * Test AADM use case setup.
68      */
69     @Before
70     public void beforeTest() {
71         schemaParameters = new SchemaParameters();
72
73         schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME);
74         schemaParameters.getSchemaHelperParameterMap().put("JAVA", new JavaSchemaHelperParameters());
75
76         ParameterService.register(schemaParameters);
77
78         contextParameters = new ContextParameters();
79
80         contextParameters.setName(ContextParameterConstants.MAIN_GROUP_NAME);
81         contextParameters.getDistributorParameters().setName(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME);
82         contextParameters.getLockManagerParameters().setName(ContextParameterConstants.LOCKING_GROUP_NAME);
83         contextParameters.getPersistorParameters().setName(ContextParameterConstants.PERSISTENCE_GROUP_NAME);
84
85         ParameterService.register(contextParameters);
86         ParameterService.register(contextParameters.getDistributorParameters());
87         ParameterService.register(contextParameters.getLockManagerParameters());
88         ParameterService.register(contextParameters.getPersistorParameters());
89
90         engineParameters = new EngineParameters();
91         engineParameters.getExecutorParameterMap().put("MVEL", new MvelExecutorParameters());
92         ParameterService.register(engineParameters);
93     }
94
95     /**
96      * After test.
97      */
98     @After
99     public void afterTest() {
100         ParameterService.deregister(engineParameters);
101
102         ParameterService.deregister(contextParameters.getDistributorParameters());
103         ParameterService.deregister(contextParameters.getLockManagerParameters());
104         ParameterService.deregister(contextParameters.getPersistorParameters());
105         ParameterService.deregister(contextParameters);
106
107         ParameterService.deregister(schemaParameters);
108     }
109
110     /**
111      * Test aadm case.
112      *
113      * @throws ApexException the apex exception
114      * @throws InterruptedException the interrupted exception
115      * @throws IOException Signals that an I/O exception has occurred.
116      */
117     @Test
118     public void testAadmCase() throws ApexException, InterruptedException, IOException {
119         final AxPolicyModel apexPolicyModel = new AadmDomainModelFactory().getAadmPolicyModel();
120         assertNotNull(apexPolicyModel);
121         final AxArtifactKey key = new AxArtifactKey("AADMApexEngine", "0.0.1");
122
123         final ApexEngineImpl apexEngine = (ApexEngineImpl) new ApexEngineFactory().createApexEngine(key);
124         final TestApexActionListener listener = new TestApexActionListener("Test");
125         apexEngine.addEventListener("listener", listener);
126         apexEngine.updateModel(apexPolicyModel, false);
127         apexEngine.start();
128
129         final AxEvent axEvent = getTriggerEvent(apexPolicyModel);
130         assertNotNull(axEvent);
131
132         // getting number of connections send it to policy, expecting probe action
133         logger.info("Sending too many connections trigger ");
134         EnEvent event = apexEngine.createEvent(axEvent.getKey());
135         event.put("IMSI", Long.valueOf(123456));
136         event.put("IMSI_IP", "101.111.121.131");
137         event.put("ENODEB_ID", Long.valueOf(123));
138         event.put("SERVICE_REQUEST_COUNT", 99);
139         event.put("AVG_SUBSCRIBER_SERVICE_REQUEST", 101.0);
140         event.put("UE_IP_ADDRESS", "101.111.121.131");
141         event.put("NUM_SUBSCRIBERS", 101);
142         event.put("ACTTASK", "");
143         event.put("APPLICATION", "");
144         event.put("ATTACH_COUNT", 0);
145         event.put("AVG_SUBSCRIBER_ATTACH", 0D);
146         event.put("DoS", false);
147         event.put("NW_IP", "");
148         event.put("PROBE_ON", false);
149         event.put("SGW_IP_ADDRESS", "");
150         event.put("TCP_ON", false);
151         event.put("TCP_UE_SIDE_AVG_THROUGHPUT", 0D);
152         event.put("TCP_UE_SIDE_MEDIAN_RTT_TX_TO_RX", 0L);
153         event.put("http_host_class", "");
154         event.put("protocol_group", "");
155         apexEngine.handleEvent(event);
156         EnEvent result = listener.getResult();
157         assertTrue(result.getName().startsWith("XSTREAM_AADM_ACT_EVENT"));
158         assertEquals("ExecutionIDs are different", event.getExecutionId(), result.getExecutionId());
159         // no DOS_IN_eNodeB set so return probe action
160         assertTrue(result.get("ACTTASK").equals("probe"));
161         assertTrue((boolean) result.get("TCP_ON"));
162         assertTrue((boolean) result.get("PROBE_ON"));
163         logger.info("Receiving action event with {} action", result.get("ACTTASK"));
164
165         final ContextAlbum eNodeBStatusAlbum = apexEngine.getInternalContext().get("ENodeBStatusAlbum");
166         final ENodeBStatus eNodeBStatus = (ENodeBStatus) eNodeBStatusAlbum.get("123");
167         eNodeBStatus.setDosCount(101);
168         eNodeBStatusAlbum.put(eNodeBStatus.getENodeB(), eNodeBStatus);
169
170         logger.info("Sending too many connections trigger ");
171         event = apexEngine.createEvent(axEvent.getKey());
172         event.put("IMSI", Long.valueOf(123456));
173         event.put("IMSI_IP", "101.111.121.131");
174         event.put("ENODEB_ID", Long.valueOf(123));
175         event.put("SERVICE_REQUEST_COUNT", 101);
176         event.put("AVG_SUBSCRIBER_SERVICE_REQUEST", 99.0);
177         event.put("UE_IP_ADDRESS", "101.111.121.131");
178         event.put("NUM_SUBSCRIBERS", 101);
179         event.put("TCP_UE_SIDE_AVG_THROUGHPUT", 101.0);
180         event.put("ACTTASK", "");
181         event.put("APPLICATION", "");
182         event.put("ATTACH_COUNT", 0);
183         event.put("AVG_SUBSCRIBER_ATTACH", 0D);
184         event.put("DoS", false);
185         event.put("NW_IP", "");
186         event.put("PROBE_ON", false);
187         event.put("SGW_IP_ADDRESS", "");
188         event.put("TCP_ON", false);
189         event.put("TCP_UE_SIDE_MEDIAN_RTT_TX_TO_RX", 0L);
190         event.put("http_host_class", "");
191         event.put("protocol_group", "");
192
193         apexEngine.handleEvent(event);
194         result = listener.getResult();
195         assertTrue(result.getName().startsWith("XSTREAM_AADM_ACT_EVENT"));
196         assertEquals("ExecutionIDs are different", event.getExecutionId(), result.getExecutionId());
197         // DOS_IN_eNodeB set to be more than throughput so return act action
198         assertTrue(result.get("ACTTASK").equals("act"));
199         // only one imsi was sent to process, so stop probe and tcp
200         assertTrue(!(boolean) result.get("TCP_ON"));
201         assertTrue(!(boolean) result.get("PROBE_ON"));
202         assertEquals(100, ((ENodeBStatus) eNodeBStatusAlbum.get("123")).getDosCount());
203         logger.info("Receiving action event with {} action", result.get("ACTTASK"));
204
205         ((ENodeBStatus) eNodeBStatusAlbum.get("123")).setDosCount(99);
206
207         // getting number of connections send it to policy, expecting probe action
208         logger.info("Sending too many connections trigger ");
209         event = apexEngine.createEvent(axEvent.getKey());
210         event.put("IMSI", Long.valueOf(123456));
211         event.put("IMSI_IP", "101.111.121.131");
212         event.put("ENODEB_ID", Long.valueOf(123));
213         event.put("SERVICE_REQUEST_COUNT", 99);
214         event.put("AVG_SUBSCRIBER_SERVICE_REQUEST", 101.0);
215         event.put("UE_IP_ADDRESS", "101.111.121.131");
216         event.put("NUM_SUBSCRIBERS", 99);
217         event.put("ACTTASK", "");
218         event.put("APPLICATION", "");
219         event.put("ATTACH_COUNT", 0);
220         event.put("AVG_SUBSCRIBER_ATTACH", 0D);
221         event.put("DoS", false);
222         event.put("NW_IP", "");
223         event.put("PROBE_ON", false);
224         event.put("SGW_IP_ADDRESS", "");
225         event.put("TCP_ON", false);
226         event.put("TCP_UE_SIDE_AVG_THROUGHPUT", 0D);
227         event.put("TCP_UE_SIDE_MEDIAN_RTT_TX_TO_RX", 0L);
228         event.put("http_host_class", "");
229         event.put("protocol_group", "");
230
231         apexEngine.handleEvent(event);
232         result = listener.getResult();
233         assertTrue(result.getName().startsWith("XSTREAM_AADM_ACT_EVENT"));
234         assertEquals("ExecutionIDs are different", event.getExecutionId(), result.getExecutionId());
235         assertTrue(result.get("ACTTASK").equals("probe"));
236         assertTrue((boolean) result.get("TCP_ON"));
237         assertTrue((boolean) result.get("PROBE_ON"));
238         assertEquals(99, ((ENodeBStatus) eNodeBStatusAlbum.get("123")).getDosCount());
239
240         ((ENodeBStatus) eNodeBStatusAlbum.get("123")).setDosCount(99);
241
242         // tcp correlation return positive dos
243         logger.info("Receiving action event with {} action", result.get("ACTTASK"));
244         event = apexEngine.createEvent(axEvent.getKey());
245         event.put("IMSI", Long.valueOf(123456));
246         event.put("IMSI_IP", "101.111.121.131");
247         event.put("ENODEB_ID", Long.valueOf(123));
248         event.put("TCP_UE_SIDE_AVG_THROUGHPUT", 101.0);
249         event.put("ACTTASK", "");
250         event.put("APPLICATION", "");
251         event.put("ATTACH_COUNT", 0);
252         event.put("AVG_SUBSCRIBER_ATTACH", 0D);
253         event.put("AVG_SUBSCRIBER_SERVICE_REQUEST", 0.0);
254         event.put("DoS", false);
255         event.put("NUM_SUBSCRIBERS", 0);
256         event.put("NW_IP", "");
257         event.put("PROBE_ON", false);
258         event.put("SERVICE_REQUEST_COUNT", 0);
259         event.put("SGW_IP_ADDRESS", "");
260         event.put("TCP_ON", false);
261         event.put("TCP_UE_SIDE_MEDIAN_RTT_TX_TO_RX", 0L);
262         event.put("UE_IP_ADDRESS", "");
263         event.put("http_host_class", "");
264         event.put("protocol_group", "");
265
266         apexEngine.handleEvent(event);
267         result = listener.getResult();
268         assertTrue(result.getName().startsWith("XSTREAM_AADM_ACT_EVENT"));
269         assertEquals("ExecutionIDs are different", event.getExecutionId(), result.getExecutionId());
270         assertTrue(result.get("ACTTASK").equals("act"));
271         assertTrue(!(boolean) result.get("TCP_ON"));
272         assertTrue(!(boolean) result.get("PROBE_ON"));
273         assertEquals(98, ((ENodeBStatus) eNodeBStatusAlbum.get("123")).getDosCount());
274         logger.info("Receiving action event with {} action", result.get("ACTTASK"));
275
276         ((ENodeBStatus) eNodeBStatusAlbum.get("123")).setDosCount(101);
277
278         // user moving enodeB
279         logger.info("Sending too many connections trigger ");
280         event = apexEngine.createEvent(axEvent.getKey());
281         event.put("IMSI", Long.valueOf(123456));
282         event.put("IMSI_IP", "101.111.121.131");
283         event.put("ENODEB_ID", Long.valueOf(123));
284         event.put("SERVICE_REQUEST_COUNT", 99);
285         event.put("AVG_SUBSCRIBER_SERVICE_REQUEST", 101.0);
286         event.put("UE_IP_ADDRESS", "101.111.121.131");
287         event.put("NUM_SUBSCRIBERS", 101);
288         event.put("ACTTASK", "");
289         event.put("APPLICATION", "");
290         event.put("ATTACH_COUNT", 0);
291         event.put("AVG_SUBSCRIBER_ATTACH", 0D);
292         event.put("AVG_SUBSCRIBER_SERVICE_REQUEST", 0.0);
293         event.put("DoS", false);
294         event.put("NW_IP", "");
295         event.put("PROBE_ON", false);
296         event.put("SGW_IP_ADDRESS", "");
297         event.put("TCP_ON", false);
298         event.put("TCP_UE_SIDE_AVG_THROUGHPUT", 0D);
299         event.put("TCP_UE_SIDE_MEDIAN_RTT_TX_TO_RX", 0L);
300         event.put("http_host_class", "");
301         event.put("protocol_group", "");
302
303         apexEngine.handleEvent(event);
304         result = listener.getResult();
305         assertTrue(result.getName().startsWith("XSTREAM_AADM_ACT_EVENT"));
306         assertEquals("ExecutionIDs are different", event.getExecutionId(), result.getExecutionId());
307         assertTrue(result.get("ACTTASK").equals("act"));
308         assertTrue(!(boolean) result.get("TCP_ON"));
309         assertTrue(!(boolean) result.get("PROBE_ON"));
310         assertEquals(100, ((ENodeBStatus) eNodeBStatusAlbum.get("123")).getDosCount());
311         logger.info("Receiving action event with {} action", result.get("ACTTASK"));
312
313         logger.info("Sending too many connections trigger ");
314         event = apexEngine.createEvent(axEvent.getKey());
315         event.put("IMSI", Long.valueOf(123456));
316         event.put("ENODEB_ID", Long.valueOf(124));
317         event.put("SERVICE_REQUEST_COUNT", 99);
318         event.put("AVG_SUBSCRIBER_SERVICE_REQUEST", 101.0);
319         event.put("UE_IP_ADDRESS", "101.111.121.131");
320         event.put("NUM_SUBSCRIBERS", 101);
321         event.put("ACTTASK", "");
322         event.put("APPLICATION", "");
323         event.put("ATTACH_COUNT", 0);
324         event.put("AVG_SUBSCRIBER_ATTACH", 0D);
325         event.put("DoS", false);
326         event.put("IMSI_IP", "");
327         event.put("NW_IP", "");
328         event.put("PROBE_ON", false);
329         event.put("SGW_IP_ADDRESS", "");
330         event.put("TCP_ON", false);
331         event.put("TCP_UE_SIDE_AVG_THROUGHPUT", 0D);
332         event.put("TCP_UE_SIDE_MEDIAN_RTT_TX_TO_RX", 0L);
333         event.put("http_host_class", "");
334         event.put("protocol_group", "");
335
336         apexEngine.handleEvent(event);
337         result = listener.getResult();
338         assertTrue(result.getName().startsWith("XSTREAM_AADM_ACT_EVENT"));
339         assertEquals("ExecutionIDs are different", event.getExecutionId(), result.getExecutionId());
340         assertTrue(result.get("ACTTASK").equals("probe"));
341         assertTrue((boolean) result.get("TCP_ON"));
342         assertTrue((boolean) result.get("PROBE_ON"));
343         assertEquals(99, ((ENodeBStatus) eNodeBStatusAlbum.get("123")).getDosCount());
344         assertEquals(1, ((ENodeBStatus) eNodeBStatusAlbum.get("124")).getDosCount());
345         logger.info("Receiving action event with {} action", result.get("ACTTASK"));
346         // End of user moving enodeB
347
348         ((ENodeBStatus) eNodeBStatusAlbum.get("123")).setDosCount(101);
349
350         // user becomes non anomalous
351         logger.info("Sending too many connections trigger ");
352         event = apexEngine.createEvent(axEvent.getKey());
353         event.put("IMSI", Long.valueOf(123456));
354         event.put("IMSI_IP", "101.111.121.131");
355         event.put("ENODEB_ID", Long.valueOf(123));
356         event.put("SERVICE_REQUEST_COUNT", 99);
357         event.put("AVG_SUBSCRIBER_SERVICE_REQUEST", 101.0);
358         event.put("UE_IP_ADDRESS", "101.111.121.131");
359         event.put("NUM_SUBSCRIBERS", 101);
360         event.put("ACTTASK", "");
361         event.put("APPLICATION", "");
362         event.put("ATTACH_COUNT", 0);
363         event.put("AVG_SUBSCRIBER_ATTACH", 0D);
364         event.put("DoS", false);
365         event.put("NW_IP", "");
366         event.put("PROBE_ON", false);
367         event.put("SGW_IP_ADDRESS", "");
368         event.put("TCP_ON", false);
369         event.put("TCP_UE_SIDE_AVG_THROUGHPUT", 0D);
370         event.put("TCP_UE_SIDE_MEDIAN_RTT_TX_TO_RX", 0L);
371         event.put("http_host_class", "");
372         event.put("protocol_group", "");
373
374         apexEngine.handleEvent(event);
375         result = listener.getResult();
376         assertTrue(result.getName().startsWith("XSTREAM_AADM_ACT_EVENT"));
377         assertEquals("ExecutionIDs are different", event.getExecutionId(), result.getExecutionId());
378         assertTrue(result.get("ACTTASK").equals("probe"));
379         assertTrue((boolean) result.get("TCP_ON"));
380         assertTrue((boolean) result.get("PROBE_ON"));
381         assertEquals(102, ((ENodeBStatus) eNodeBStatusAlbum.get("123")).getDosCount());
382         logger.info("Receiving action event with {} action", result.get("ACTTASK"));
383
384         logger.info("Sending too many connections trigger ");
385         event = apexEngine.createEvent(axEvent.getKey());
386         event.put("IMSI", Long.valueOf(123456));
387         event.put("ENODEB_ID", Long.valueOf(123));
388         event.put("SERVICE_REQUEST_COUNT", 99);
389         event.put("UE_IP_ADDRESS", "101.111.121.131");
390         event.put("ACTTASK", "");
391         event.put("APPLICATION", "");
392         event.put("ATTACH_COUNT", 0);
393         event.put("AVG_SUBSCRIBER_ATTACH", 0D);
394         event.put("AVG_SUBSCRIBER_SERVICE_REQUEST", 0.0);
395         event.put("DoS", false);
396         event.put("IMSI_IP", "");
397         event.put("NUM_SUBSCRIBERS", 0);
398         event.put("NW_IP", "");
399         event.put("PROBE_ON", false);
400         event.put("SGW_IP_ADDRESS", "");
401         event.put("TCP_ON", false);
402         event.put("TCP_UE_SIDE_AVG_THROUGHPUT", 0D);
403         event.put("TCP_UE_SIDE_MEDIAN_RTT_TX_TO_RX", 0L);
404         event.put("http_host_class", "");
405         event.put("protocol_group", "");
406
407         apexEngine.handleEvent(event);
408         result = listener.getResult();
409         assertTrue(result.getName().startsWith("XSTREAM_AADM_ACT_EVENT"));
410         assertEquals("ExecutionIDs are different", event.getExecutionId(), result.getExecutionId());
411         assertTrue(result.get("ACTTASK").equals("probe"));
412         assertTrue((boolean) result.get("TCP_ON"));
413         assertTrue((boolean) result.get("PROBE_ON"));
414         assertEquals(102, ((ENodeBStatus) eNodeBStatusAlbum.get("123")).getDosCount());
415         logger.info("Receiving action event with {} action", result.get("ACTTASK"));
416         // End of user becomes non anomalous
417         apexEngine.handleEvent(result);
418         result = listener.getResult();
419         assertTrue(result.getName().startsWith("SAPCBlacklistSubscriberEvent"));
420         assertTrue(result.get("PROFILE").equals("ServiceA"));
421         assertTrue(result.get("BLACKLIST_ON").equals(true));
422
423         event = apexEngine.createEvent(new AxArtifactKey("PeriodicEvent", "0.0.1"));
424         event.put("PERIODIC_EVENT_COUNT", (long) 100);
425         event.put("PERIODIC_DELAY", (long) 1000);
426         event.put("PERIODIC_FIRST_TIME", System.currentTimeMillis());
427         event.put("PERIODIC_CURRENT_TIME", System.currentTimeMillis());
428         event.put("PERIODIC_LAST_TIME", System.currentTimeMillis());
429         apexEngine.handleEvent(event);
430         result = listener.getResult();
431         assertTrue(result.getName().startsWith("SAPCBlacklistSubscriberEvent"));
432         assertEquals("ExecutionIDs are different", event.getExecutionId(), result.getExecutionId());
433         assertEquals(0L, result.get("IMSI"));
434         assertTrue(result.get("PROFILE").equals("ServiceA"));
435         assertTrue(result.get("BLACKLIST_ON").equals(false));
436
437         apexEngine.stop();
438     }
439
440     /**
441      * Test vpn cleardown.
442      */
443     @After
444     public void testAadmCleardown() {
445         // Not used
446     }
447
448     /**
449      * Gets the trigger event.
450      *
451      * @param apexPolicyModel the apex policy model
452      * @return the trigger event
453      */
454     private AxEvent getTriggerEvent(final AxPolicyModel apexPolicyModel) {
455         for (final AxEvent axEvent : apexPolicyModel.getEvents().getEventMap().values()) {
456             if (axEvent.getKey().getId().equals("AADMEvent:0.0.1")) {
457                 return axEvent;
458             }
459         }
460         return null;
461     }
462 }