2 * Copyright (c) 2019 PANTHEON.tech.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package io.fd.hc2vpp.v3po.interfacesstate;
19 import static org.mockito.ArgumentMatchers.any;
20 import static org.mockito.Mockito.when;
22 import io.fd.hc2vpp.common.test.read.ReaderCustomizerTest;
23 import io.fd.hc2vpp.common.translate.util.NamingContext;
24 import io.fd.hc2vpp.v3po.interfacesstate.cache.InterfaceStatisticsManager;
25 import io.fd.hc2vpp.v3po.interfacesstate.cache.InterfaceStatisticsManagerImpl;
26 import io.fd.honeycomb.translate.read.ReadFailedException;
27 import io.fd.honeycomb.translate.spi.read.ReaderCustomizer;
28 import io.fd.jvpp.stats.dto.InterfaceStatistics;
29 import io.fd.jvpp.stats.dto.InterfaceStatisticsDetails;
30 import io.fd.jvpp.stats.dto.InterfaceStatisticsDetailsReplyDump;
31 import io.fd.jvpp.stats.future.FutureJVppStatsFacade;
32 import java.util.Optional;
33 import org.junit.Assert;
34 import org.junit.Test;
35 import org.mockito.Mock;
36 import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.naming.context.rev160513.contexts.naming.context.mappings.MappingBuilder;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceBuilder;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceKey;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state._interface.Statistics;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state._interface.StatisticsBuilder;
43 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
45 public class InterfaceStatisticsCustomizerTest extends ReaderCustomizerTest<Statistics, StatisticsBuilder> {
47 private static final String IFC_CTX_NAME = "ifc-test-stats-instance";
48 private static final String IF_NAME = "testIfc";
49 private static final int SW_IF_INDEX = 1;
50 private static final int OUT_ERRORS = 2;
51 private static final int OUT_MULTI = 3;
52 private static final int OUT_UNI = 4;
53 private static final int OUT_BROAD = 5;
54 private static final int OUT_BYTES = 6;
55 private static final int IN_ERRORS = 22;
56 private static final int IN_MULTI = 33;
57 private static final int IN_UNI = 44;
58 private static final int IN_BROAD = 55;
59 private static final int IN_BYTES = 66;
60 private NamingContext interfaceContext;
61 private static final InstanceIdentifier<Statistics> IID =
62 InstanceIdentifier.create(InterfacesState.class).child(Interface.class, new InterfaceKey(IF_NAME))
63 .child(Statistics.class);
65 private FutureJVppStatsFacade jvppStats;
67 private InterfaceStatisticsManager statisticsManager;
69 public InterfaceStatisticsCustomizerTest() {
70 super(Statistics.class, InterfaceBuilder.class);
75 interfaceContext = new NamingContext("generatedIfaceName", IFC_CTX_NAME);
76 interfaceContext.addName(1, IF_NAME, ctx.getMappingContext());
80 protected ReaderCustomizer<Statistics, StatisticsBuilder> initCustomizer() {
81 statisticsManager = new InterfaceStatisticsManagerImpl();
82 return new InterfaceStatisticsCustomizer(interfaceContext, jvppStats, statisticsManager);
86 public void testReadStatistics() throws Exception {
87 statisticsManager.enableStatistics();
88 when(ctx.getMappingContext().read(any()))
89 .thenReturn(Optional.of(new MappingBuilder().setName(IF_NAME).setIndex(SW_IF_INDEX).build()));
90 when(jvppStats.interfaceStatisticsDump(any())).thenReturn(future(getStatistics()));
91 StatisticsBuilder statBuilder = new StatisticsBuilder();
92 getCustomizer().readCurrentAttributes(IID, statBuilder, ctx);
93 Statistics stat = statBuilder.build();
94 int[] expected = new int[]{SW_IF_INDEX, OUT_ERRORS, OUT_MULTI, OUT_UNI, OUT_BROAD,
95 OUT_BYTES, IN_ERRORS, IN_MULTI, IN_UNI, IN_BROAD, IN_BYTES};
96 int[] actual = new int[]{interfaceContext.getIndex(IF_NAME, ctx.getMappingContext()),
97 stat.getOutErrors().getValue().intValue(), stat.getOutMulticastPkts().getValue().intValue(),
98 stat.getOutUnicastPkts().getValue().intValue(), stat.getOutBroadcastPkts().getValue().intValue(),
99 stat.getOutOctets().getValue().intValue(), stat.getInErrors().getValue().intValue(),
100 stat.getInMulticastPkts().getValue().intValue(), stat.getInUnicastPkts().getValue().intValue(),
101 stat.getInBroadcastPkts().getValue().intValue(), stat.getInOctets().getValue().intValue()};
103 Assert.assertArrayEquals(expected, actual);
106 @Test(expected = ReadFailedException.class)
107 public void testReadStatisticsFailed() throws Exception {
108 statisticsManager.enableStatistics();
109 when(ctx.getMappingContext().read(any()))
110 .thenReturn(Optional.of(new MappingBuilder().setName(IF_NAME).setIndex(SW_IF_INDEX).build()));
111 when(jvppStats.interfaceStatisticsDump(any())).thenReturn(future(null));
112 StatisticsBuilder statBuilder = new StatisticsBuilder();
113 getCustomizer().readCurrentAttributes(IID, statBuilder, ctx);
116 private InterfaceStatisticsDetailsReplyDump getStatistics() {
117 InterfaceStatisticsDetailsReplyDump dumpReply = new InterfaceStatisticsDetailsReplyDump();
118 dumpReply.interfaceStatisticsDetails = new InterfaceStatisticsDetails(1, 1);
119 dumpReply.interfaceStatisticsDetails.interfaceStatistics[0] =
120 new InterfaceStatistics(SW_IF_INDEX, OUT_ERRORS, OUT_MULTI, OUT_UNI, OUT_BROAD,
121 OUT_BYTES, IN_ERRORS, IN_MULTI, IN_UNI, IN_BROAD, IN_BYTES);