40ae35700b9a02700a48e1f720fc2ded31721a00
[honeycomb.git] / v3po / v3po2vpp / src / test / java / io / fd / honeycomb / translate / v3po / notification / InterfaceChangeNotificationProducerTest.java
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.honeycomb.translate.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.honeycomb.notification.NotificationCollector;
26 import io.fd.honeycomb.translate.MappingContext;
27 import io.fd.honeycomb.vpp.test.util.NamingContextHelper;
28 import io.fd.honeycomb.translate.vpp.util.NamingContext;
29 import io.fd.honeycomb.vpp.test.util.FutureProducer;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.mockito.ArgumentCaptor;
33 import org.mockito.Mock;
34 import org.mockito.MockitoAnnotations;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.InterfaceStateChange;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.InterfaceStatus;
37 import io.fd.vpp.jvpp.core.callback.SwInterfaceSetFlagsNotificationCallback;
38 import io.fd.vpp.jvpp.core.dto.SwInterfaceSetFlagsNotification;
39 import io.fd.vpp.jvpp.core.dto.WantInterfaceEvents;
40 import io.fd.vpp.jvpp.core.dto.WantInterfaceEventsReply;
41 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
42 import io.fd.vpp.jvpp.core.notification.CoreNotificationRegistry;
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 CoreNotificationRegistry notificationRegistry;
59     @Mock
60     private AutoCloseable notificationListenerReg;
61
62     private ArgumentCaptor<SwInterfaceSetFlagsNotificationCallback> callbackArgumentCaptor;
63
64     @Before
65     public void setUp() throws Exception {
66         MockitoAnnotations.initMocks(this);
67         doReturn(notificationRegistry).when(jVpp).getNotificationRegistry();
68         callbackArgumentCaptor = ArgumentCaptor.forClass(SwInterfaceSetFlagsNotificationCallback.class);
69         doReturn(notificationListenerReg).when(notificationRegistry).registerSwInterfaceSetFlagsNotificationCallback(
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).getNotificationRegistry();
83         verify(notificationRegistry).registerSwInterfaceSetFlagsNotificationCallback(any(
84             SwInterfaceSetFlagsNotificationCallback.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 SwInterfaceSetFlagsNotification swInterfaceSetFlagsNotification = new SwInterfaceSetFlagsNotification();
99         swInterfaceSetFlagsNotification.deleted = 0;
100         swInterfaceSetFlagsNotification.swIfIndex = IFACE_ID;
101         swInterfaceSetFlagsNotification.adminUpDown = 1;
102         swInterfaceSetFlagsNotification.linkUpDown = 1;
103         callbackArgumentCaptor.getValue().onSwInterfaceSetFlagsNotification(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 }