5a450cfeadbf543671cc23882101c071d3f141ab
[hc2vpp.git] /
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
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.notification;
18
19 import static org.junit.Assert.assertEquals;
20 import static org.mockito.Matchers.any;
21 import static org.mockito.Mockito.doReturn;
22 import static org.mockito.Mockito.times;
23 import static org.mockito.Mockito.verify;
24
25 import io.fd.hc2vpp.common.test.util.FutureProducer;
26 import io.fd.hc2vpp.common.test.util.NamingContextHelper;
27 import io.fd.hc2vpp.common.translate.util.NamingContext;
28 import io.fd.honeycomb.notification.NotificationCollector;
29 import io.fd.honeycomb.translate.MappingContext;
30 import io.fd.vpp.jvpp.core.callback.SwInterfaceEventCallback;
31 import io.fd.vpp.jvpp.core.dto.SwInterfaceEvent;
32 import io.fd.vpp.jvpp.core.dto.WantInterfaceEvents;
33 import io.fd.vpp.jvpp.core.dto.WantInterfaceEventsReply;
34 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
35 import io.fd.vpp.jvpp.core.notification.CoreEventRegistry;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.mockito.ArgumentCaptor;
39 import org.mockito.Mock;
40 import org.mockito.MockitoAnnotations;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev181128.InterfaceStateChange;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev181128.InterfaceStatus;
43
44 public class InterfaceChangeNotificationProducerTest implements FutureProducer, NamingContextHelper {
45
46     private static final String IFC_CTX_NAME = "ifc-test-instance";
47     private static final String IFACE_NAME = "eth0";
48     private static final int IFACE_ID = 0;
49
50     @Mock
51     private FutureJVppCore jVpp;
52     private NamingContext namingContext = new NamingContext("test", IFC_CTX_NAME);
53     @Mock
54     private MappingContext mappingContext;
55     @Mock
56     private NotificationCollector collector;
57     @Mock
58     private CoreEventRegistry notificationRegistry;
59     @Mock
60     private AutoCloseable notificationListenerReg;
61
62     private ArgumentCaptor<SwInterfaceEventCallback> callbackArgumentCaptor;
63
64     @Before
65     public void setUp() throws Exception {
66         MockitoAnnotations.initMocks(this);
67         doReturn(notificationRegistry).when(jVpp).getEventRegistry();
68         callbackArgumentCaptor = ArgumentCaptor.forClass(SwInterfaceEventCallback.class);
69         doReturn(notificationListenerReg).when(notificationRegistry).registerSwInterfaceEventCallback(
70             callbackArgumentCaptor.capture());
71         defineMapping(mappingContext, IFACE_NAME, IFACE_ID, IFC_CTX_NAME);
72         doReturn(future(new WantInterfaceEventsReply())).when(jVpp).wantInterfaceEvents(any(WantInterfaceEvents.class));
73     }
74
75     @Test
76     public void testStart() throws Exception {
77         final InterfaceChangeNotificationProducer interfaceChangeNotificationProducer =
78             new InterfaceChangeNotificationProducer(jVpp, namingContext, mappingContext);
79
80         interfaceChangeNotificationProducer.start(collector);
81         verify(jVpp).wantInterfaceEvents(any(WantInterfaceEvents.class));
82         verify(jVpp).getEventRegistry();
83         verify(notificationRegistry).registerSwInterfaceEventCallback(any(
84                 SwInterfaceEventCallback.class));
85
86         interfaceChangeNotificationProducer.stop();
87         verify(jVpp, times(2)).wantInterfaceEvents(any(WantInterfaceEvents.class));
88         verify(notificationListenerReg).close();
89     }
90
91     @Test
92     public void testNotification() throws Exception {
93         final InterfaceChangeNotificationProducer interfaceChangeNotificationProducer =
94             new InterfaceChangeNotificationProducer(jVpp, namingContext, mappingContext);
95
96         interfaceChangeNotificationProducer.start(collector);
97
98         final SwInterfaceEvent swInterfaceSetFlagsNotification = new SwInterfaceEvent();
99         swInterfaceSetFlagsNotification.deleted = 0;
100         swInterfaceSetFlagsNotification.swIfIndex = IFACE_ID;
101         swInterfaceSetFlagsNotification.adminUpDown = 1;
102         swInterfaceSetFlagsNotification.linkUpDown = 1;
103         callbackArgumentCaptor.getValue().onSwInterfaceEvent(swInterfaceSetFlagsNotification);
104         final ArgumentCaptor<InterfaceStateChange> notificationCaptor =
105             ArgumentCaptor.forClass(InterfaceStateChange.class);
106         verify(collector).onNotification(notificationCaptor.capture());
107
108         assertEquals(IFACE_NAME, notificationCaptor.getValue().getName().getString());
109         assertEquals(InterfaceStatus.Up, notificationCaptor.getValue().getAdminStatus());
110         assertEquals(InterfaceStatus.Up, notificationCaptor.getValue().getOperStatus());
111     }
112 }