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.mdsal.common.api.LogicalDatastoreType.CONFIGURATION;
16 import static org.opendaylight.netvirt.aclservice.tests.StateInterfaceBuilderHelper.putNewStateInterface;
18 import java.util.Arrays;
19 import java.util.List;
21 import java.util.concurrent.Future;
22 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.Ignore;
27 import org.junit.Rule;
28 import org.junit.Test;
29 import org.junit.rules.MethodRule;
30 import org.opendaylight.genius.datastoreutils.SingleTransactionDataBroker;
31 import org.opendaylight.genius.datastoreutils.testutils.AsyncEventsWaiter;
32 import org.opendaylight.genius.datastoreutils.testutils.JobCoordinatorTestModule;
33 import org.opendaylight.genius.datastoreutils.testutils.TestableDataTreeChangeListenerModule;
34 import org.opendaylight.genius.testutils.TestInterfaceManager;
35 import org.opendaylight.infrautils.inject.guice.testutils.GuiceRule;
36 import org.opendaylight.mdsal.binding.api.DataBroker;
37 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
38 import org.opendaylight.mdsal.common.api.TransactionCommitFailedException;
39 import org.opendaylight.netvirt.aclservice.tests.AclServiceModule;
40 import org.opendaylight.netvirt.aclservice.tests.AclServiceTestModule;
41 import org.opendaylight.netvirt.aclservice.tests.IdentifiedInterfaceWithAclBuilder;
42 import org.opendaylight.netvirt.aclservice.tests.infra.DataBrokerPairsUtil;
43 import org.opendaylight.netvirt.aclservice.utils.AclServiceUtils;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.direct.statistics.rev160511.OpendaylightDirectStatisticsService;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.acl.live.statistics.rev161129.AclLiveStatisticsService;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.acl.live.statistics.rev161129.Direction;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.acl.live.statistics.rev161129.GetAclPortStatisticsInput;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.acl.live.statistics.rev161129.GetAclPortStatisticsInputBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.acl.live.statistics.rev161129.GetAclPortStatisticsOutput;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.acl.live.statistics.rev161129.acl.stats.output.AclPortStats;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.acl.live.statistics.rev161129.acl.stats.output.AclPortStatsKey;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.acl.live.statistics.rev161129.acl.stats.output.acl.port.stats.AclDropStats;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.config.rev160806.AclserviceConfig;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.config.rev160806.AclserviceConfig.SecurityGroupMode;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstanceBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.ElanInterface;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.ElanInterfaceBuilder;
60 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
61 import org.opendaylight.yangtools.yang.common.RpcResult;
62 import org.slf4j.Logger;
63 import org.slf4j.LoggerFactory;
66 public class AclLiveStatisticsRpcServiceTest {
68 private static final Logger LOG = LoggerFactory.getLogger(AclLiveStatisticsRpcServiceTest.class);
70 public @Rule MethodRule guice = new GuiceRule(new AclServiceModule(),
71 new AclServiceTestModule(SecurityGroupMode.Stateful), new TestableDataTreeChangeListenerModule(),
72 new JobCoordinatorTestModule());
75 AclserviceConfig config;
77 DataBroker dataBroker;
79 DataBrokerPairsUtil dataBrokerUtil;
81 AsyncEventsWaiter asyncEventsWaiter;
83 OpendaylightDirectStatisticsService odlDirectStatsService;
84 SingleTransactionDataBroker singleTransactionDataBroker;
85 @Inject TestInterfaceManager testInterfaceManager;
87 private AclLiveStatisticsService aclStatsService;
89 private static final String PORT_MAC_1 = "0D:AA:D8:42:30:F3";
90 private static final String PORT_1 = "port1";
91 private static final String PORT_2 = "port2";
93 static String ELAN = "elan1";
94 static long ELAN_TAG = 5000L;
97 public void setUp() throws Exception {
98 aclStatsService = new AclLiveStatisticsRpcServiceImpl(config, dataBroker, odlDirectStatsService);
99 singleTransactionDataBroker = new SingleTransactionDataBroker(dataBroker);
101 LOG.info("Acl mode: {}", config.getSecurityGroupMode());
103 newElan(ELAN, ELAN_TAG);
104 newElanInterface(ELAN, PORT_1, true);
106 Pair<DataTreeIdentifier<Interface>, Interface> port1 = new IdentifiedInterfaceWithAclBuilder()
107 .interfaceName(PORT_1)
108 .portSecurity(true).build();
109 dataBrokerUtil.put(port1);
110 testInterfaceManager.addInterface(port1.getValue());
111 putNewStateInterface(dataBroker, "port1", PORT_MAC_1);
112 asyncEventsWaiter.awaitEventsConsumption();
115 // FIXME copied from AclServiceTestBase
116 protected void newElan(String elanName, long elanId) throws TransactionCommitFailedException {
117 ElanInstance elan = new ElanInstanceBuilder().setElanInstanceName(elanName).setElanTag(5000L).build();
118 singleTransactionDataBroker.syncWrite(CONFIGURATION,
119 AclServiceUtils.getElanInstanceConfigurationDataPath(elanName),
123 protected void newElanInterface(String elanName, String portName, boolean isWrite)
124 throws TransactionCommitFailedException {
125 ElanInterface elanInterface = new ElanInterfaceBuilder().setName(portName)
126 .setElanInstanceName(elanName).build();
127 InstanceIdentifier<ElanInterface> id = AclServiceUtils.getElanInterfaceConfigurationDataPathId(portName);
129 singleTransactionDataBroker.syncWrite(CONFIGURATION, id, elanInterface);
131 singleTransactionDataBroker.syncDelete(CONFIGURATION, id);
137 * Test stats for one port and both direction. <br>
138 * port1 is valid <br>
139 * Expectation: This is a success case.
141 * @throws Exception the exception
144 public void getStatsOnePortBothDirection() throws Exception {
145 List<String> lstInterfaces = Arrays.asList(PORT_1);
146 Direction direction = Direction.Both;
148 GetAclPortStatisticsInput input =
149 new GetAclPortStatisticsInputBuilder().setDirection(direction).setInterfaceNames(lstInterfaces).build();
150 Future<RpcResult<GetAclPortStatisticsOutput>> rpcResultFuture = aclStatsService.getAclPortStatistics(input);
151 RpcResult<GetAclPortStatisticsOutput> output = rpcResultFuture.get();
152 LOG.info("getStatsOnePortBothDirection output = {}", output);
154 assertStatsOutput(output, direction);
158 * Test stats for two ports and both direction. <br>
159 * port1 is valid <br>
160 * port2 is invalid <br>
161 * Expectation: Error expected for port2
163 * @throws Exception the exception
166 public void getStatsTwoPortBothDirection() throws Exception {
167 List<String> lstInterfaces = Arrays.asList(PORT_1, PORT_2);
168 Direction direction = Direction.Both;
170 GetAclPortStatisticsInput input =
171 new GetAclPortStatisticsInputBuilder().setDirection(direction).setInterfaceNames(lstInterfaces).build();
172 Future<RpcResult<GetAclPortStatisticsOutput>> rpcResultFuture = aclStatsService.getAclPortStatistics(input);
173 RpcResult<GetAclPortStatisticsOutput> output = rpcResultFuture.get();
174 LOG.info("getStatsTwoPortBothDirection output = {}", output);
176 assertStatsOutput(output, direction);
180 * Test stats for two ports and egress direction only. <br>
181 * port1 is valid <br>
182 * port2 is invalid <br>
183 * Expectation: Error expected for port2. Drop stats should be available for
184 * egress direction only.
186 * @throws Exception the exception
189 public void getStatsTwoPortEgressOnly() throws Exception {
190 List<String> lstInterfaces = Arrays.asList(PORT_1, PORT_2);
191 Direction direction = Direction.Egress;
193 GetAclPortStatisticsInput input =
194 new GetAclPortStatisticsInputBuilder().setDirection(direction).setInterfaceNames(lstInterfaces).build();
195 Future<RpcResult<GetAclPortStatisticsOutput>> rpcResultFuture = aclStatsService.getAclPortStatistics(input);
196 RpcResult<GetAclPortStatisticsOutput> output = rpcResultFuture.get();
197 LOG.info("getStatsTwoPortEgressOnly output = {}", output);
199 assertStatsOutput(output, direction);
202 private void assertStatsOutput(RpcResult<GetAclPortStatisticsOutput> output, Direction inputDirection) {
203 assertNotNull(output);
205 GetAclPortStatisticsOutput aclPortStats = output.getResult();
206 assertNotNull(aclPortStats);
208 Map<AclPortStatsKey, AclPortStats> aclPortStatsKeyAclPortStatsMap = aclPortStats.getAclPortStats();
209 assertNotNull(aclPortStatsKeyAclPortStatsMap);
210 assertFalse(aclPortStatsKeyAclPortStatsMap.isEmpty());
211 for (AclPortStats stats : aclPortStatsKeyAclPortStatsMap.values()) {
212 List<AclDropStats> aclDropStats = stats.getAclDropStats();
213 if (stats.getInterfaceName().equals(PORT_1)) {
214 assertNotNull(aclDropStats);
215 assertTrue(!aclDropStats.isEmpty());
217 if (inputDirection == Direction.Both) {
218 assertTrue(aclDropStats.size() == 2);
220 assertTrue(aclDropStats.size() == 1);
222 for (AclDropStats dropStats : aclDropStats) {
223 if (inputDirection != Direction.Both) {
224 Assert.assertEquals(dropStats.getDirection(), inputDirection);
226 assertTrue(dropStats.getBytes().getDropCount().intValue() > 0);
227 assertTrue(dropStats.getBytes().getInvalidDropCount().intValue() > 0);
229 assertTrue(dropStats.getPackets().getDropCount().intValue() > 0);
230 assertTrue(dropStats.getPackets().getInvalidDropCount().intValue() > 0);
232 assertNull(stats.getError());
234 // Other than port1, error is returned in the output
235 assertNull(aclDropStats);
236 assertNotNull(stats.getError());