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.v3po.translate.v3po.notification;
18 import static io.fd.honeycomb.v3po.translate.v3po.test.ContextTestUtils.getMapping;
19 import static io.fd.honeycomb.v3po.translate.v3po.test.ContextTestUtils.getMappingIid;
20 import static org.junit.Assert.assertEquals;
21 import static org.mockito.Matchers.any;
22 import static org.mockito.Mockito.doReturn;
23 import static org.mockito.Mockito.times;
24 import static org.mockito.Mockito.verify;
26 import com.google.common.base.Optional;
27 import com.google.common.collect.Lists;
28 import io.fd.honeycomb.v3po.notification.NotificationCollector;
29 import io.fd.honeycomb.v3po.translate.MappingContext;
30 import io.fd.honeycomb.v3po.translate.v3po.util.NamingContext;
31 import java.util.List;
32 import java.util.concurrent.CompletableFuture;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.mockito.ArgumentCaptor;
36 import org.mockito.Mock;
37 import org.mockito.MockitoAnnotations;
38 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.Mappings;
39 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.MappingsBuilder;
40 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.mappings.Mapping;
41 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.mappings.MappingKey;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.InterfaceStateChange;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.InterfaceStatus;
44 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
45 import org.openvpp.jvpp.callback.SwInterfaceSetFlagsNotificationCallback;
46 import org.openvpp.jvpp.dto.SwInterfaceSetFlagsNotification;
47 import org.openvpp.jvpp.dto.WantInterfaceEvents;
48 import org.openvpp.jvpp.dto.WantInterfaceEventsReply;
49 import org.openvpp.jvpp.future.FutureJVpp;
50 import org.openvpp.jvpp.notification.NotificationRegistry;
52 public class InterfaceChangeNotificationProducerTest {
55 private FutureJVpp jVpp;
56 private NamingContext namingContext = new NamingContext("test", "test-instance");
58 private MappingContext mappingContext;
60 private NotificationCollector collector;
62 private NotificationRegistry notificationRegistry;
64 private AutoCloseable notificationListenerReg;
66 private ArgumentCaptor<SwInterfaceSetFlagsNotificationCallback> callbackArgumentCaptor;
69 public void setUp() throws Exception {
70 MockitoAnnotations.initMocks(this);
71 doReturn(notificationRegistry).when(jVpp).getNotificationRegistry();
72 callbackArgumentCaptor = ArgumentCaptor.forClass(SwInterfaceSetFlagsNotificationCallback.class);
73 doReturn(notificationListenerReg).when(notificationRegistry).registerSwInterfaceSetFlagsNotificationCallback(
74 callbackArgumentCaptor.capture());
76 final KeyedInstanceIdentifier<Mapping, MappingKey> eth0Id = getMappingIid("eth0", "test-instance");
77 final Optional<Mapping> eth0 = getMapping("eth0", 0);
79 final List<Mapping> allMappings = Lists.newArrayList(getMapping("eth0", 0).get());
80 final Mappings allMappingsBaObject = new MappingsBuilder().setMapping(allMappings).build();
81 doReturn(Optional.of(allMappingsBaObject)).when(mappingContext).read(eth0Id.firstIdentifierOf(Mappings.class));
83 doReturn(eth0).when(mappingContext).read(eth0Id);
87 public void testStart() throws Exception {
88 final CompletableFuture<WantInterfaceEventsReply> response = new CompletableFuture<>();
89 response.complete(new WantInterfaceEventsReply());
90 doReturn(response).when(jVpp).wantInterfaceEvents(any(WantInterfaceEvents.class));
91 final InterfaceChangeNotificationProducer interfaceChangeNotificationProducer =
92 new InterfaceChangeNotificationProducer(jVpp, namingContext, mappingContext);
94 interfaceChangeNotificationProducer.start(collector);
95 verify(jVpp).wantInterfaceEvents(any(WantInterfaceEvents.class));
96 verify(jVpp).getNotificationRegistry();
97 verify(notificationRegistry).registerSwInterfaceSetFlagsNotificationCallback(any(
98 SwInterfaceSetFlagsNotificationCallback.class));
100 interfaceChangeNotificationProducer.stop();
101 verify(jVpp, times(2)).wantInterfaceEvents(any(WantInterfaceEvents.class));
102 verify(notificationListenerReg).close();
106 public void testNotification() throws Exception {
107 final CompletableFuture<WantInterfaceEventsReply> response = new CompletableFuture<>();
108 response.complete(new WantInterfaceEventsReply());
109 doReturn(response).when(jVpp).wantInterfaceEvents(any(WantInterfaceEvents.class));
110 final InterfaceChangeNotificationProducer interfaceChangeNotificationProducer =
111 new InterfaceChangeNotificationProducer(jVpp, namingContext, mappingContext);
113 interfaceChangeNotificationProducer.start(collector);
115 final SwInterfaceSetFlagsNotification swInterfaceSetFlagsNotification = new SwInterfaceSetFlagsNotification();
116 swInterfaceSetFlagsNotification.deleted = 0;
117 swInterfaceSetFlagsNotification.swIfIndex = 0;
118 swInterfaceSetFlagsNotification.adminUpDown = 1;
119 swInterfaceSetFlagsNotification.linkUpDown = 1;
121 callbackArgumentCaptor.getValue().onSwInterfaceSetFlagsNotification(swInterfaceSetFlagsNotification);
122 final ArgumentCaptor<InterfaceStateChange> notificationCaptor =
123 ArgumentCaptor.forClass(InterfaceStateChange.class);
124 verify(collector).onNotification(notificationCaptor.capture());
126 assertEquals("eth0", notificationCaptor.getValue().getName().getString());
127 assertEquals(InterfaceStatus.Up, notificationCaptor.getValue().getAdminStatus());
128 assertEquals(InterfaceStatus.Up, notificationCaptor.getValue().getOperStatus());