2 * Copyright (c) 2016 Cisco and/or its affiliates.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package io.fd.honeycomb.translate.v3po.notification;
18 import static org.junit.Assert.assertEquals;
19 import static org.mockito.Matchers.any;
20 import static org.mockito.Mockito.doReturn;
21 import static org.mockito.Mockito.times;
22 import static org.mockito.Mockito.verify;
24 import io.fd.honeycomb.notification.NotificationCollector;
25 import io.fd.honeycomb.translate.MappingContext;
26 import io.fd.honeycomb.vpp.test.util.NamingContextHelper;
27 import io.fd.honeycomb.translate.vpp.util.NamingContext;
28 import io.fd.honeycomb.vpp.test.util.FutureProducer;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.mockito.ArgumentCaptor;
32 import org.mockito.Mock;
33 import org.mockito.MockitoAnnotations;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.InterfaceStateChange;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.InterfaceStatus;
36 import io.fd.vpp.jvpp.core.callback.SwInterfaceSetFlagsNotificationCallback;
37 import io.fd.vpp.jvpp.core.dto.SwInterfaceSetFlagsNotification;
38 import io.fd.vpp.jvpp.core.dto.WantInterfaceEvents;
39 import io.fd.vpp.jvpp.core.dto.WantInterfaceEventsReply;
40 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
41 import io.fd.vpp.jvpp.core.notification.CoreNotificationRegistry;
43 public class InterfaceChangeNotificationProducerTest implements FutureProducer, NamingContextHelper {
45 private static final String IFC_CTX_NAME = "ifc-test-instance";
46 private static final String IFACE_NAME = "eth0";
47 private static final int IFACE_ID = 0;
50 private FutureJVppCore jVpp;
51 private NamingContext namingContext = new NamingContext("test", IFC_CTX_NAME);
53 private MappingContext mappingContext;
55 private NotificationCollector collector;
57 private CoreNotificationRegistry notificationRegistry;
59 private AutoCloseable notificationListenerReg;
61 private ArgumentCaptor<SwInterfaceSetFlagsNotificationCallback> callbackArgumentCaptor;
64 public void setUp() throws Exception {
65 MockitoAnnotations.initMocks(this);
66 doReturn(notificationRegistry).when(jVpp).getNotificationRegistry();
67 callbackArgumentCaptor = ArgumentCaptor.forClass(SwInterfaceSetFlagsNotificationCallback.class);
68 doReturn(notificationListenerReg).when(notificationRegistry).registerSwInterfaceSetFlagsNotificationCallback(
69 callbackArgumentCaptor.capture());
70 defineMapping(mappingContext, IFACE_NAME, IFACE_ID, IFC_CTX_NAME);
71 doReturn(future(new WantInterfaceEventsReply())).when(jVpp).wantInterfaceEvents(any(WantInterfaceEvents.class));
75 public void testStart() throws Exception {
76 final InterfaceChangeNotificationProducer interfaceChangeNotificationProducer =
77 new InterfaceChangeNotificationProducer(jVpp, namingContext, mappingContext);
79 interfaceChangeNotificationProducer.start(collector);
80 verify(jVpp).wantInterfaceEvents(any(WantInterfaceEvents.class));
81 verify(jVpp).getNotificationRegistry();
82 verify(notificationRegistry).registerSwInterfaceSetFlagsNotificationCallback(any(
83 SwInterfaceSetFlagsNotificationCallback.class));
85 interfaceChangeNotificationProducer.stop();
86 verify(jVpp, times(2)).wantInterfaceEvents(any(WantInterfaceEvents.class));
87 verify(notificationListenerReg).close();
91 public void testNotification() throws Exception {
92 final InterfaceChangeNotificationProducer interfaceChangeNotificationProducer =
93 new InterfaceChangeNotificationProducer(jVpp, namingContext, mappingContext);
95 interfaceChangeNotificationProducer.start(collector);
97 final SwInterfaceSetFlagsNotification swInterfaceSetFlagsNotification = new SwInterfaceSetFlagsNotification();
98 swInterfaceSetFlagsNotification.deleted = 0;
99 swInterfaceSetFlagsNotification.swIfIndex = IFACE_ID;
100 swInterfaceSetFlagsNotification.adminUpDown = 1;
101 swInterfaceSetFlagsNotification.linkUpDown = 1;
102 callbackArgumentCaptor.getValue().onSwInterfaceSetFlagsNotification(swInterfaceSetFlagsNotification);
103 final ArgumentCaptor<InterfaceStateChange> notificationCaptor =
104 ArgumentCaptor.forClass(InterfaceStateChange.class);
105 verify(collector).onNotification(notificationCaptor.capture());
107 assertEquals(IFACE_NAME, notificationCaptor.getValue().getName().getString());
108 assertEquals(InterfaceStatus.Up, notificationCaptor.getValue().getAdminStatus());
109 assertEquals(InterfaceStatus.Up, notificationCaptor.getValue().getOperStatus());