6566dfe0b753fff1080afa8bfe815d52e8ec58af
[honeycomb.git] /
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 package io.fd.honeycomb.translate.v3po.notification;
17
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;
23
24 import com.google.common.base.Optional;
25 import com.google.common.collect.Lists;
26 import io.fd.honeycomb.notification.NotificationCollector;
27 import io.fd.honeycomb.translate.MappingContext;
28 import io.fd.honeycomb.translate.v3po.test.ContextTestUtils;
29 import io.fd.honeycomb.translate.v3po.util.NamingContext;
30 import java.util.List;
31 import java.util.concurrent.CompletableFuture;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.mockito.ArgumentCaptor;
35 import org.mockito.Mock;
36 import org.mockito.MockitoAnnotations;
37 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.Mappings;
38 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.MappingsBuilder;
39 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.mappings.Mapping;
40 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.mappings.MappingKey;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.InterfaceStateChange;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.InterfaceStatus;
43 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
44 import org.openvpp.jvpp.core.callback.SwInterfaceSetFlagsNotificationCallback;
45 import org.openvpp.jvpp.core.dto.SwInterfaceSetFlagsNotification;
46 import org.openvpp.jvpp.core.dto.WantInterfaceEvents;
47 import org.openvpp.jvpp.core.dto.WantInterfaceEventsReply;
48 import org.openvpp.jvpp.core.future.FutureJVppCore;
49 import org.openvpp.jvpp.core.notification.CoreNotificationRegistry;
50
51 public class InterfaceChangeNotificationProducerTest {
52
53     @Mock
54     private FutureJVppCore jVpp;
55     private NamingContext namingContext = new NamingContext("test", "test-instance");
56     @Mock
57     private MappingContext mappingContext;
58     @Mock
59     private NotificationCollector collector;
60     @Mock
61     private CoreNotificationRegistry notificationRegistry;
62     @Mock
63     private AutoCloseable notificationListenerReg;
64
65     private ArgumentCaptor<SwInterfaceSetFlagsNotificationCallback> callbackArgumentCaptor;
66
67     @Before
68     public void setUp() throws Exception {
69         MockitoAnnotations.initMocks(this);
70         doReturn(notificationRegistry).when(jVpp).getNotificationRegistry();
71         callbackArgumentCaptor = ArgumentCaptor.forClass(SwInterfaceSetFlagsNotificationCallback.class);
72         doReturn(notificationListenerReg).when(notificationRegistry).registerSwInterfaceSetFlagsNotificationCallback(
73             callbackArgumentCaptor.capture());
74
75         final KeyedInstanceIdentifier<Mapping, MappingKey> eth0Id = ContextTestUtils
76                 .getMappingIid("eth0", "test-instance");
77         final Optional<Mapping> eth0 = ContextTestUtils.getMapping("eth0", 0);
78
79         final List<Mapping> allMappings = Lists.newArrayList(ContextTestUtils.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));
82
83         doReturn(eth0).when(mappingContext).read(eth0Id);
84     }
85
86     @Test
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);
93
94         interfaceChangeNotificationProducer.start(collector);
95         verify(jVpp).wantInterfaceEvents(any(WantInterfaceEvents.class));
96         verify(jVpp).getNotificationRegistry();
97         verify(notificationRegistry).registerSwInterfaceSetFlagsNotificationCallback(any(
98             SwInterfaceSetFlagsNotificationCallback.class));
99
100         interfaceChangeNotificationProducer.stop();
101         verify(jVpp, times(2)).wantInterfaceEvents(any(WantInterfaceEvents.class));
102         verify(notificationListenerReg).close();
103     }
104
105     @Test
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);
112
113         interfaceChangeNotificationProducer.start(collector);
114
115         final SwInterfaceSetFlagsNotification swInterfaceSetFlagsNotification = new SwInterfaceSetFlagsNotification();
116         swInterfaceSetFlagsNotification.deleted = 0;
117         swInterfaceSetFlagsNotification.swIfIndex = 0;
118         swInterfaceSetFlagsNotification.adminUpDown = 1;
119         swInterfaceSetFlagsNotification.linkUpDown = 1;
120
121         callbackArgumentCaptor.getValue().onSwInterfaceSetFlagsNotification(swInterfaceSetFlagsNotification);
122         final ArgumentCaptor<InterfaceStateChange> notificationCaptor =
123             ArgumentCaptor.forClass(InterfaceStateChange.class);
124         verify(collector).onNotification(notificationCaptor.capture());
125
126         assertEquals("eth0", notificationCaptor.getValue().getName().getString());
127         assertEquals(InterfaceStatus.Up, notificationCaptor.getValue().getAdminStatus());
128         assertEquals(InterfaceStatus.Up, notificationCaptor.getValue().getOperStatus());
129     }
130 }