549d821051aa3bea29307493d9a17c8315a82c2b
[hc2vpp.git] /
1 /*
2  * Copyright (c) 2019 PANTHEON.tech.
3  *
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:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package io.fd.hc2vpp.v3po.interfacesstate;
18
19 import static org.mockito.ArgumentMatchers.anyInt;
20 import static org.mockito.Mockito.verify;
21 import static org.mockito.Mockito.when;
22
23 import io.fd.hc2vpp.common.test.read.ReaderCustomizerTest;
24 import io.fd.hc2vpp.common.translate.util.NamingContext;
25 import io.fd.hc2vpp.v3po.interfacesstate.cache.InterfaceCacheStatisticsDumpManager;
26 import io.fd.hc2vpp.v3po.interfacesstate.cache.InterfaceCacheStatisticsSample;
27 import io.fd.honeycomb.translate.read.ReadFailedException;
28 import io.fd.honeycomb.translate.spi.read.ReaderCustomizer;
29 import io.fd.vpp.jvpp.core.dto.VnetPerInterfaceCombinedCounters;
30 import io.fd.vpp.jvpp.core.types.VnetCombinedCounter;
31 import java.math.BigInteger;
32 import java.time.LocalDateTime;
33 import org.junit.Assert;
34 import org.junit.Test;
35 import org.mockito.Mock;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceBuilder;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceKey;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state._interface.Statistics;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state._interface.StatisticsBuilder;
42 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
43
44 public class InterfaceStatisticsCustomizerTest extends ReaderCustomizerTest<Statistics, StatisticsBuilder> {
45
46     private static final String IFC_CTX_NAME = "ifc-test-instance";
47     private static final String IF_NAME = "tap";
48     private static final int RX_BROADCAST_P = 8;
49     private static final int RX_MULTICAST_P = 7;
50     private static final int RX_UNICAST_P = 6;
51     private static final int RX_BYTES = 1000;
52     private static final int TX_BROADCAST_P = 88;
53     private static final int TX_MULTICAST_P = 77;
54     private static final int TX_UNICAST_P = 66;
55     private static final int TX_BYTES = 1100;
56
57     private NamingContext namingContext;
58     private static final InstanceIdentifier<Statistics> IID =
59             InstanceIdentifier.create(InterfacesState.class).child(Interface.class, new InterfaceKey(IF_NAME))
60                     .child(Statistics.class);
61     @Mock
62     private InterfaceCacheStatisticsDumpManager statManager;
63
64
65     public InterfaceStatisticsCustomizerTest() {
66         super(Statistics.class, InterfaceBuilder.class);
67     }
68
69     @Override
70     protected void setUp() throws Exception {
71         namingContext = new NamingContext("generatedIfaceName", IFC_CTX_NAME);
72         defineMapping(mappingContext, IF_NAME, 1, IFC_CTX_NAME);
73     }
74
75     @Override
76     protected ReaderCustomizer<Statistics, StatisticsBuilder> initCustomizer() {
77         return new InterfaceStatisticsCustomizer(namingContext, statManager);
78     }
79
80     @Test
81     public void testRead() throws ReadFailedException {
82         when(statManager.getStatisticsData(anyInt())).thenReturn(
83                 (new InterfaceCacheStatisticsSample(getDummyPerIfcCombinedCounters(), LocalDateTime.now())));
84         StatisticsBuilder builder = new StatisticsBuilder();
85         getCustomizer().readCurrentAttributes(IID, builder, ctx);
86         verify(statManager).getStatisticsData(anyInt());
87         Assert.assertEquals(BigInteger.valueOf(TX_BROADCAST_P), builder.getOutBroadcastPkts().getValue());
88         Assert.assertEquals(BigInteger.valueOf(TX_MULTICAST_P), builder.getOutMulticastPkts().getValue());
89         Assert.assertEquals(BigInteger.valueOf(TX_UNICAST_P), builder.getOutUnicastPkts().getValue());
90         Assert.assertEquals(BigInteger.valueOf(TX_BYTES), builder.getOutOctets().getValue());
91         Assert.assertEquals(BigInteger.valueOf(RX_BROADCAST_P), builder.getInBroadcastPkts().getValue());
92         Assert.assertEquals(BigInteger.valueOf(RX_MULTICAST_P), builder.getInMulticastPkts().getValue());
93         Assert.assertEquals(BigInteger.valueOf(RX_UNICAST_P), builder.getInUnicastPkts().getValue());
94         Assert.assertEquals(BigInteger.valueOf(RX_BYTES), builder.getInOctets().getValue());
95     }
96
97     @Test
98     public void testReadFailed() throws ReadFailedException {
99         when(statManager.getStatisticsData(anyInt())).thenReturn(null);
100         StatisticsBuilder builder = new StatisticsBuilder();
101         getCustomizer().readCurrentAttributes(IID, builder, ctx);
102         Assert.assertEquals(BigInteger.ZERO, builder.getInUnicastPkts().getValue());
103     }
104
105     private VnetPerInterfaceCombinedCounters getDummyPerIfcCombinedCounters() {
106         VnetPerInterfaceCombinedCounters counters = new VnetPerInterfaceCombinedCounters();
107         counters.count = 1;
108         counters.timestamp = 1;
109         VnetCombinedCounter data = new VnetCombinedCounter();
110         data.rxBroadcastPackets = RX_BROADCAST_P;
111         data.rxMulticastPackets = RX_MULTICAST_P;
112         data.rxUnicastPackets = RX_UNICAST_P;
113         data.rxBytes = RX_BYTES;
114         data.txBroadcastPackets = TX_BROADCAST_P;
115         data.txMulticastPackets = TX_MULTICAST_P;
116         data.txUnicastPackets = TX_UNICAST_P;
117         data.txBytes = TX_BYTES;
118         counters.data = new VnetCombinedCounter[]{data};
119         return counters;
120     }
121 }