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.translate.v3po.test.ContextTestUtils;
27 import io.fd.honeycomb.translate.v3po.util.NamingContext;
28 import java.util.concurrent.CompletableFuture;
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 org.openvpp.jvpp.core.callback.SwInterfaceSetFlagsNotificationCallback;
37 import org.openvpp.jvpp.core.dto.SwInterfaceSetFlagsNotification;
38 import org.openvpp.jvpp.core.dto.WantInterfaceEvents;
39 import org.openvpp.jvpp.core.dto.WantInterfaceEventsReply;
40 import org.openvpp.jvpp.core.future.FutureJVppCore;
41 import org.openvpp.jvpp.core.notification.CoreNotificationRegistry;
43 public class InterfaceChangeNotificationProducerTest {
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 ContextTestUtils.mockMapping(mappingContext, IFACE_NAME, IFACE_ID, IFC_CTX_NAME);
74 public void testStart() throws Exception {
75 final CompletableFuture<WantInterfaceEventsReply> response = new CompletableFuture<>();
76 response.complete(new WantInterfaceEventsReply());
77 doReturn(response).when(jVpp).wantInterfaceEvents(any(WantInterfaceEvents.class));
78 final InterfaceChangeNotificationProducer interfaceChangeNotificationProducer =
79 new InterfaceChangeNotificationProducer(jVpp, namingContext, mappingContext);
81 interfaceChangeNotificationProducer.start(collector);
82 verify(jVpp).wantInterfaceEvents(any(WantInterfaceEvents.class));
83 verify(jVpp).getNotificationRegistry();
84 verify(notificationRegistry).registerSwInterfaceSetFlagsNotificationCallback(any(
85 SwInterfaceSetFlagsNotificationCallback.class));
87 interfaceChangeNotificationProducer.stop();
88 verify(jVpp, times(2)).wantInterfaceEvents(any(WantInterfaceEvents.class));
89 verify(notificationListenerReg).close();
93 public void testNotification() throws Exception {
94 final CompletableFuture<WantInterfaceEventsReply> response = new CompletableFuture<>();
95 response.complete(new WantInterfaceEventsReply());
96 doReturn(response).when(jVpp).wantInterfaceEvents(any(WantInterfaceEvents.class));
97 final InterfaceChangeNotificationProducer interfaceChangeNotificationProducer =
98 new InterfaceChangeNotificationProducer(jVpp, namingContext, mappingContext);
100 interfaceChangeNotificationProducer.start(collector);
102 final SwInterfaceSetFlagsNotification swInterfaceSetFlagsNotification = new SwInterfaceSetFlagsNotification();
103 swInterfaceSetFlagsNotification.deleted = 0;
104 swInterfaceSetFlagsNotification.swIfIndex = IFACE_ID;
105 swInterfaceSetFlagsNotification.adminUpDown = 1;
106 swInterfaceSetFlagsNotification.linkUpDown = 1;
107 callbackArgumentCaptor.getValue().onSwInterfaceSetFlagsNotification(swInterfaceSetFlagsNotification);
108 final ArgumentCaptor<InterfaceStateChange> notificationCaptor =
109 ArgumentCaptor.forClass(InterfaceStateChange.class);
110 verify(collector).onNotification(notificationCaptor.capture());
112 assertEquals(IFACE_NAME, notificationCaptor.getValue().getName().getString());
113 assertEquals(InterfaceStatus.Up, notificationCaptor.getValue().getAdminStatus());
114 assertEquals(InterfaceStatus.Up, notificationCaptor.getValue().getOperStatus());