010bd53f01cb3b292c769b48d23bd35a1a47aefb
[netvirt.git] / vpnservice / aclservice / impl / src / test / java / org / opendaylight / netvirt / aclservice / stats / AclLiveStatisticsRpcServiceTest.java
1 /*
2  * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.netvirt.aclservice.stats;
10
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertNull;
14 import static org.junit.Assert.assertTrue;
15 import static org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType.CONFIGURATION;
16 import static org.opendaylight.netvirt.aclservice.tests.StateInterfaceBuilderHelper.putNewStateInterface;
17
18 import java.util.Arrays;
19 import java.util.List;
20 import java.util.concurrent.Future;
21 import javax.inject.Inject;
22
23 import org.eclipse.xtext.xbase.lib.Pair;
24 import org.junit.Assert;
25 import org.junit.Before;
26 import org.junit.Rule;
27 import org.junit.Test;
28 import org.junit.rules.MethodRule;
29 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
30 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
31 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
32 import org.opendaylight.genius.datastoreutils.SingleTransactionDataBroker;
33 import org.opendaylight.genius.datastoreutils.testutils.AsyncEventsWaiter;
34 import org.opendaylight.genius.datastoreutils.testutils.JobCoordinatorTestModule;
35 import org.opendaylight.genius.datastoreutils.testutils.TestableDataTreeChangeListenerModule;
36 import org.opendaylight.genius.testutils.TestInterfaceManager;
37 import org.opendaylight.infrautils.inject.guice.testutils.GuiceRule;
38 import org.opendaylight.netvirt.aclservice.tests.AclServiceModule;
39 import org.opendaylight.netvirt.aclservice.tests.AclServiceTestModule;
40 import org.opendaylight.netvirt.aclservice.tests.IdentifiedInterfaceWithAclBuilder;
41 import org.opendaylight.netvirt.aclservice.tests.infra.DataBrokerPairsUtil;
42 import org.opendaylight.netvirt.aclservice.utils.AclServiceUtils;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.direct.statistics.rev160511.OpendaylightDirectStatisticsService;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.acl.live.statistics.rev161129.AclLiveStatisticsService;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.acl.live.statistics.rev161129.Direction;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.acl.live.statistics.rev161129.GetAclPortStatisticsInput;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.acl.live.statistics.rev161129.GetAclPortStatisticsInputBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.acl.live.statistics.rev161129.GetAclPortStatisticsOutput;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.acl.live.statistics.rev161129.acl.stats.output.AclPortStats;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.acl.live.statistics.rev161129.acl.stats.output.acl.port.stats.AclDropStats;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.config.rev160806.AclserviceConfig;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.config.rev160806.AclserviceConfig.SecurityGroupMode;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstanceBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.ElanInterface;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.ElanInterfaceBuilder;
58 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
59 import org.opendaylight.yangtools.yang.common.RpcResult;
60 import org.slf4j.Logger;
61 import org.slf4j.LoggerFactory;
62
63 public class AclLiveStatisticsRpcServiceTest {
64
65     private static final Logger LOG = LoggerFactory.getLogger(AclLiveStatisticsRpcServiceTest.class);
66
67     public @Rule MethodRule guice = new GuiceRule(new AclServiceModule(),
68             new AclServiceTestModule(SecurityGroupMode.Stateful), new TestableDataTreeChangeListenerModule(),
69             new JobCoordinatorTestModule());
70
71     @Inject
72     AclserviceConfig config;
73     @Inject
74     DataBroker dataBroker;
75     @Inject
76     DataBrokerPairsUtil dataBrokerUtil;
77     @Inject
78     AsyncEventsWaiter asyncEventsWaiter;
79     @Inject
80     OpendaylightDirectStatisticsService odlDirectStatsService;
81     SingleTransactionDataBroker singleTransactionDataBroker;
82     @Inject TestInterfaceManager testInterfaceManager;
83
84     private AclLiveStatisticsService aclStatsService;
85
86     private static final String PORT_MAC_1 = "0D:AA:D8:42:30:F3";
87     private static final String PORT_1 = "port1";
88     private static final String PORT_2 = "port2";
89
90     static String ELAN = "elan1";
91     static long ELAN_TAG = 5000L;
92
93     @Before
94     public void setUp() throws Exception {
95         aclStatsService = new AclLiveStatisticsRpcServiceImpl(config, dataBroker, odlDirectStatsService);
96         singleTransactionDataBroker = new SingleTransactionDataBroker(dataBroker);
97
98         LOG.info("Acl mode: {}", config.getSecurityGroupMode());
99
100         newElan(ELAN, ELAN_TAG);
101         newElanInterface(ELAN, PORT_1, true);
102
103         Pair<DataTreeIdentifier<Interface>, Interface> port1 = new IdentifiedInterfaceWithAclBuilder()
104                 .interfaceName(PORT_1)
105                 .portSecurity(true).build();
106         dataBrokerUtil.put(port1);
107         testInterfaceManager.addInterface(port1.getValue());
108         putNewStateInterface(dataBroker, "port1", PORT_MAC_1);
109         asyncEventsWaiter.awaitEventsConsumption();
110     }
111
112     // FIXME copied from AclServiceTestBase
113     protected void newElan(String elanName, long elanId) throws TransactionCommitFailedException {
114         ElanInstance elan = new ElanInstanceBuilder().setElanInstanceName(elanName).setElanTag(5000L).build();
115         singleTransactionDataBroker.syncWrite(CONFIGURATION,
116                 AclServiceUtils.getElanInstanceConfigurationDataPath(elanName),
117                 elan);
118     }
119
120     protected void newElanInterface(String elanName, String portName, boolean isWrite)
121             throws TransactionCommitFailedException {
122         ElanInterface elanInterface = new ElanInterfaceBuilder().setName(portName)
123                 .setElanInstanceName(elanName).build();
124         InstanceIdentifier<ElanInterface> id = AclServiceUtils.getElanInterfaceConfigurationDataPathId(portName);
125         if (isWrite) {
126             singleTransactionDataBroker.syncWrite(CONFIGURATION, id, elanInterface);
127         } else {
128             singleTransactionDataBroker.syncDelete(CONFIGURATION, id);
129         }
130     }
131
132
133     /**
134      * Test stats for one port and both direction. <br>
135      * port1 is valid <br>
136      * Expectation: This is a success case.
137      *
138      * @throws Exception the exception
139      */
140     @Test
141     public void getStatsOnePortBothDirection() throws Exception {
142         List<String> lstInterfaces = Arrays.asList(PORT_1);
143         Direction direction = Direction.Both;
144
145         GetAclPortStatisticsInput input =
146                 new GetAclPortStatisticsInputBuilder().setDirection(direction).setInterfaceNames(lstInterfaces).build();
147         Future<RpcResult<GetAclPortStatisticsOutput>> rpcResultFuture = aclStatsService.getAclPortStatistics(input);
148         RpcResult<GetAclPortStatisticsOutput> output = rpcResultFuture.get();
149         LOG.info("getStatsOnePortBothDirection output = {}", output);
150
151         assertStatsOutput(output, direction);
152     }
153
154     /**
155      * Test stats for two ports and both direction. <br>
156      * port1 is valid <br>
157      * port2 is invalid <br>
158      * Expectation: Error expected for port2
159      *
160      * @throws Exception the exception
161      */
162     @Test
163     public void getStatsTwoPortBothDirection() throws Exception {
164         List<String> lstInterfaces = Arrays.asList(PORT_1, PORT_2);
165         Direction direction = Direction.Both;
166
167         GetAclPortStatisticsInput input =
168                 new GetAclPortStatisticsInputBuilder().setDirection(direction).setInterfaceNames(lstInterfaces).build();
169         Future<RpcResult<GetAclPortStatisticsOutput>> rpcResultFuture = aclStatsService.getAclPortStatistics(input);
170         RpcResult<GetAclPortStatisticsOutput> output = rpcResultFuture.get();
171         LOG.info("getStatsTwoPortBothDirection output = {}", output);
172
173         assertStatsOutput(output, direction);
174     }
175
176     /**
177      * Test stats for two ports and egress direction only. <br>
178      * port1 is valid <br>
179      * port2 is invalid <br>
180      * Expectation: Error expected for port2. Drop stats should be available for
181      * egress direction only.
182      *
183      * @throws Exception the exception
184      */
185     @Test
186     public void getStatsTwoPortEgressOnly() throws Exception {
187         List<String> lstInterfaces = Arrays.asList(PORT_1, PORT_2);
188         Direction direction = Direction.Egress;
189
190         GetAclPortStatisticsInput input =
191                 new GetAclPortStatisticsInputBuilder().setDirection(direction).setInterfaceNames(lstInterfaces).build();
192         Future<RpcResult<GetAclPortStatisticsOutput>> rpcResultFuture = aclStatsService.getAclPortStatistics(input);
193         RpcResult<GetAclPortStatisticsOutput> output = rpcResultFuture.get();
194         LOG.info("getStatsTwoPortEgressOnly output = {}", output);
195
196         assertStatsOutput(output, direction);
197     }
198
199     private void assertStatsOutput(RpcResult<GetAclPortStatisticsOutput> output, Direction inputDirection) {
200         assertNotNull(output);
201
202         GetAclPortStatisticsOutput aclPortStats = output.getResult();
203         assertNotNull(aclPortStats);
204
205         List<AclPortStats> lstAclPortStats = aclPortStats.getAclPortStats();
206         assertNotNull(lstAclPortStats);
207         assertFalse(lstAclPortStats.isEmpty());
208         for (AclPortStats stats : lstAclPortStats) {
209             List<AclDropStats> aclDropStats = stats.getAclDropStats();
210             if (stats.getInterfaceName().equals(PORT_1)) {
211                 assertNotNull(aclDropStats);
212                 assertTrue(!aclDropStats.isEmpty());
213
214                 if (inputDirection == Direction.Both) {
215                     assertTrue(aclDropStats.size() == 2);
216                 } else {
217                     assertTrue(aclDropStats.size() == 1);
218                 }
219                 for (AclDropStats dropStats : aclDropStats) {
220                     if (inputDirection != Direction.Both) {
221                         Assert.assertEquals(dropStats.getDirection(), inputDirection);
222                     }
223                     assertTrue(dropStats.getBytes().getDropCount().intValue() > 0);
224                     assertTrue(dropStats.getBytes().getInvalidDropCount().intValue() > 0);
225
226                     assertTrue(dropStats.getPackets().getDropCount().intValue() > 0);
227                     assertTrue(dropStats.getPackets().getInvalidDropCount().intValue() > 0);
228                 }
229                 assertNull(stats.getError());
230             } else {
231                 // Other than port1, error is returned in the output
232                 assertNull(aclDropStats);
233                 assertNotNull(stats.getError());
234             }
235         }
236     }
237 }