d6824e1ab594f7d755d72dc395a38108a9e32ca0
[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.any;
20 import static org.mockito.Mockito.when;
21
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;
44
45 public class InterfaceStatisticsCustomizerTest extends ReaderCustomizerTest<Statistics, StatisticsBuilder> {
46
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);
64     @Mock
65     private FutureJVppStatsFacade jvppStats;
66
67     private InterfaceStatisticsManager statisticsManager;
68
69     public InterfaceStatisticsCustomizerTest() {
70         super(Statistics.class, InterfaceBuilder.class);
71     }
72
73     @Override
74     public void setUp() {
75         interfaceContext = new NamingContext("generatedIfaceName", IFC_CTX_NAME);
76         interfaceContext.addName(1, IF_NAME, ctx.getMappingContext());
77     }
78
79     @Override
80     protected ReaderCustomizer<Statistics, StatisticsBuilder> initCustomizer() {
81         statisticsManager = new InterfaceStatisticsManagerImpl();
82         return new InterfaceStatisticsCustomizer(interfaceContext, jvppStats, statisticsManager);
83     }
84
85     @Test
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()};
102
103         Assert.assertArrayEquals(expected, actual);
104     }
105
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);
114     }
115
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);
122         return dumpReply;
123     }
124 }