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.
17 package io.fd.honeycomb.translate.v3po.notification;
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;
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;
44 public class InterfaceChangeNotificationProducerTest implements FutureProducer, NamingContextHelper {
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;
51 private FutureJVppCore jVpp;
52 private NamingContext namingContext = new NamingContext("test", IFC_CTX_NAME);
54 private MappingContext mappingContext;
56 private NotificationCollector collector;
58 private CoreNotificationRegistry notificationRegistry;
60 private AutoCloseable notificationListenerReg;
62 private ArgumentCaptor<SwInterfaceSetFlagsNotificationCallback> callbackArgumentCaptor;
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));
76 public void testStart() throws Exception {
77 final InterfaceChangeNotificationProducer interfaceChangeNotificationProducer =
78 new InterfaceChangeNotificationProducer(jVpp, namingContext, mappingContext);
80 interfaceChangeNotificationProducer.start(collector);
81 verify(jVpp).wantInterfaceEvents(any(WantInterfaceEvents.class));
82 verify(jVpp).getNotificationRegistry();
83 verify(notificationRegistry).registerSwInterfaceSetFlagsNotificationCallback(any(
84 SwInterfaceSetFlagsNotificationCallback.class));
86 interfaceChangeNotificationProducer.stop();
87 verify(jVpp, times(2)).wantInterfaceEvents(any(WantInterfaceEvents.class));
88 verify(notificationListenerReg).close();
92 public void testNotification() throws Exception {
93 final InterfaceChangeNotificationProducer interfaceChangeNotificationProducer =
94 new InterfaceChangeNotificationProducer(jVpp, namingContext, mappingContext);
96 interfaceChangeNotificationProducer.start(collector);
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());
108 assertEquals(IFACE_NAME, notificationCaptor.getValue().getName().getString());
109 assertEquals(InterfaceStatus.Up, notificationCaptor.getValue().getAdminStatus());
110 assertEquals(InterfaceStatus.Up, notificationCaptor.getValue().getOperStatus());