1608c9fa00c39770d9ce1209ccd139ec7a1d9089
[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.interfaces;
18
19 import static org.mockito.ArgumentMatchers.any;
20 import static org.mockito.ArgumentMatchers.anyInt;
21 import static org.mockito.Mockito.doReturn;
22 import static org.mockito.Mockito.verify;
23
24 import io.fd.hc2vpp.common.test.write.WriterCustomizerTest;
25 import io.fd.hc2vpp.common.translate.util.NamingContext;
26 import io.fd.hc2vpp.v3po.interfacesstate.cache.InterfaceCacheStatisticsDumpManager;
27 import io.fd.vpp.jvpp.core.dto.WantPerInterfaceCombinedStats;
28 import io.fd.vpp.jvpp.core.dto.WantPerInterfaceCombinedStatsReply;
29 import org.junit.Assert;
30 import org.junit.Test;
31 import org.mockito.ArgumentCaptor;
32 import org.mockito.Mock;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev181128.VppInterfaceStatsCollectionAugmentation;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev181128.interfaces._interface.StatisticsCollection;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev181128.interfaces._interface.StatisticsCollectionBuilder;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40
41 public class InterfaceStatisticsEnableCustomizerTest extends WriterCustomizerTest {
42     private InterfaceStatisticsEnableCustomizer statCustomizer;
43     private static final String IFC_TEST_INSTANCE = "ifc-test-instance";
44
45     private NamingContext namingContext;
46
47     @Mock
48     private InterfaceCacheStatisticsDumpManager statManager;
49
50     @Override
51     public void setUpTest() throws Exception {
52         namingContext = new NamingContext("ifcintest", IFC_TEST_INSTANCE);
53         defineMapping(writeContext.getMappingContext(), "tap", 1, IFC_TEST_INSTANCE);
54         doReturn(future(new WantPerInterfaceCombinedStatsReply())).when(api).wantPerInterfaceCombinedStats(any());
55         statCustomizer = new InterfaceStatisticsEnableCustomizer(this.api, namingContext, statManager);
56     }
57
58     @Test
59     public void testEnableStatisticsCollection() throws Exception {
60         StatisticsCollectionBuilder statCollectionBuilder = new StatisticsCollectionBuilder();
61         statCollectionBuilder.setStatisticsEnabled(true);
62         statCustomizer.writeCurrentAttributes(getTapId("tap"), statCollectionBuilder.build(), writeContext);
63         verify(statManager).enableInterface(anyInt());
64         ArgumentCaptor<WantPerInterfaceCombinedStats> argument = ArgumentCaptor.forClass(WantPerInterfaceCombinedStats.class);
65         verify(api).wantPerInterfaceCombinedStats(argument.capture());
66         Assert.assertEquals(1, argument.getValue().enableDisable);
67     }
68
69     @Test
70     public void testDisableStatisticsCollection() throws Exception {
71         StatisticsCollectionBuilder statCollectionBuilder = new StatisticsCollectionBuilder();
72         statCollectionBuilder.setStatisticsEnabled(false);
73         statCustomizer.writeCurrentAttributes(getTapId("tap"), statCollectionBuilder.build(), writeContext);
74         verify(statManager).disableInterface(anyInt());
75         ArgumentCaptor<WantPerInterfaceCombinedStats> argument = ArgumentCaptor.forClass(WantPerInterfaceCombinedStats.class);
76         verify(api).wantPerInterfaceCombinedStats(argument.capture());
77         Assert.assertEquals(0, argument.getValue().enableDisable);
78     }
79
80     @Test
81     public void testDeleteStatisticsCollection() throws Exception {
82         StatisticsCollectionBuilder statCollectionBuilder = new StatisticsCollectionBuilder();
83         statCollectionBuilder.setStatisticsEnabled(true);
84         statCustomizer.deleteCurrentAttributes(getTapId("tap"), statCollectionBuilder.build(), writeContext);
85         verify(statManager).disableInterface(anyInt());
86         ArgumentCaptor<WantPerInterfaceCombinedStats> argument = ArgumentCaptor.forClass(WantPerInterfaceCombinedStats.class);
87         verify(api).wantPerInterfaceCombinedStats(argument.capture());
88         Assert.assertEquals(0, argument.getValue().enableDisable);
89     }
90
91     private InstanceIdentifier<StatisticsCollection> getTapId(final String tap) {
92         return InstanceIdentifier.create(Interfaces.class).child(Interface.class, new InterfaceKey(tap)).augmentation(
93                 VppInterfaceStatsCollectionAugmentation.class).child(StatisticsCollection.class);
94     }
95 }