2 * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
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
9 package org.opendaylight.netvirt.aclservice.stats;
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;
18 import java.util.Arrays;
19 import java.util.List;
20 import java.util.concurrent.Future;
21 import javax.inject.Inject;
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;
63 public class AclLiveStatisticsRpcServiceTest {
65 private static final Logger LOG = LoggerFactory.getLogger(AclLiveStatisticsRpcServiceTest.class);
67 public @Rule MethodRule guice = new GuiceRule(new AclServiceModule(),
68 new AclServiceTestModule(SecurityGroupMode.Stateful), new TestableDataTreeChangeListenerModule(),
69 new JobCoordinatorTestModule());
72 AclserviceConfig config;
74 DataBroker dataBroker;
76 DataBrokerPairsUtil dataBrokerUtil;
78 AsyncEventsWaiter asyncEventsWaiter;
80 OpendaylightDirectStatisticsService odlDirectStatsService;
81 SingleTransactionDataBroker singleTransactionDataBroker;
82 @Inject TestInterfaceManager testInterfaceManager;
84 private AclLiveStatisticsService aclStatsService;
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";
90 static String ELAN = "elan1";
91 static long ELAN_TAG = 5000L;
94 public void setUp() throws Exception {
95 aclStatsService = new AclLiveStatisticsRpcServiceImpl(config, dataBroker, odlDirectStatsService);
96 singleTransactionDataBroker = new SingleTransactionDataBroker(dataBroker);
98 LOG.info("Acl mode: {}", config.getSecurityGroupMode());
100 newElan(ELAN, ELAN_TAG);
101 newElanInterface(ELAN, PORT_1, true);
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();
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),
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);
126 singleTransactionDataBroker.syncWrite(CONFIGURATION, id, elanInterface);
128 singleTransactionDataBroker.syncDelete(CONFIGURATION, id);
134 * Test stats for one port and both direction. <br>
135 * port1 is valid <br>
136 * Expectation: This is a success case.
138 * @throws Exception the exception
141 public void getStatsOnePortBothDirection() throws Exception {
142 List<String> lstInterfaces = Arrays.asList(PORT_1);
143 Direction direction = Direction.Both;
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);
151 assertStatsOutput(output, direction);
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
160 * @throws Exception the exception
163 public void getStatsTwoPortBothDirection() throws Exception {
164 List<String> lstInterfaces = Arrays.asList(PORT_1, PORT_2);
165 Direction direction = Direction.Both;
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);
173 assertStatsOutput(output, direction);
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.
183 * @throws Exception the exception
186 public void getStatsTwoPortEgressOnly() throws Exception {
187 List<String> lstInterfaces = Arrays.asList(PORT_1, PORT_2);
188 Direction direction = Direction.Egress;
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);
196 assertStatsOutput(output, direction);
199 private void assertStatsOutput(RpcResult<GetAclPortStatisticsOutput> output, Direction inputDirection) {
200 assertNotNull(output);
202 GetAclPortStatisticsOutput aclPortStats = output.getResult();
203 assertNotNull(aclPortStats);
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());
214 if (inputDirection == Direction.Both) {
215 assertTrue(aclDropStats.size() == 2);
217 assertTrue(aclDropStats.size() == 1);
219 for (AclDropStats dropStats : aclDropStats) {
220 if (inputDirection != Direction.Both) {
221 Assert.assertEquals(dropStats.getDirection(), inputDirection);
223 assertTrue(dropStats.getBytes().getDropCount().intValue() > 0);
224 assertTrue(dropStats.getBytes().getInvalidDropCount().intValue() > 0);
226 assertTrue(dropStats.getPackets().getDropCount().intValue() > 0);
227 assertTrue(dropStats.getPackets().getInvalidDropCount().intValue() > 0);
229 assertNull(stats.getError());
231 // Other than port1, error is returned in the output
232 assertNull(aclDropStats);
233 assertNotNull(stats.getError());