HONEYCOMB-154: update revison of models that changed since 16.09
[honeycomb.git] / v3po / v3po2vpp / src / test / java / io / fd / honeycomb / translate / v3po / notification / InterfaceChangeNotificationProducerTest.java
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 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.rev161214.InterfaceStateChange;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.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;
42
43 public class InterfaceChangeNotificationProducerTest implements FutureProducer, NamingContextHelper {
44
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;
48
49     @Mock
50     private FutureJVppCore jVpp;
51     private NamingContext namingContext = new NamingContext("test", IFC_CTX_NAME);
52     @Mock
53     private MappingContext mappingContext;
54     @Mock
55     private NotificationCollector collector;
56     @Mock
57     private CoreNotificationRegistry notificationRegistry;
58     @Mock
59     private AutoCloseable notificationListenerReg;
60
61     private ArgumentCaptor<SwInterfaceSetFlagsNotificationCallback> callbackArgumentCaptor;
62
63     @Before
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));
72     }
73
74     @Test
75     public void testStart() throws Exception {
76         final InterfaceChangeNotificationProducer interfaceChangeNotificationProducer =
77             new InterfaceChangeNotificationProducer(jVpp, namingContext, mappingContext);
78
79         interfaceChangeNotificationProducer.start(collector);
80         verify(jVpp).wantInterfaceEvents(any(WantInterfaceEvents.class));
81         verify(jVpp).getNotificationRegistry();
82         verify(notificationRegistry).registerSwInterfaceSetFlagsNotificationCallback(any(
83             SwInterfaceSetFlagsNotificationCallback.class));
84
85         interfaceChangeNotificationProducer.stop();
86         verify(jVpp, times(2)).wantInterfaceEvents(any(WantInterfaceEvents.class));
87         verify(notificationListenerReg).close();
88     }
89
90     @Test
91     public void testNotification() throws Exception {
92         final InterfaceChangeNotificationProducer interfaceChangeNotificationProducer =
93             new InterfaceChangeNotificationProducer(jVpp, namingContext, mappingContext);
94
95         interfaceChangeNotificationProducer.start(collector);
96
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());
106
107         assertEquals(IFACE_NAME, notificationCaptor.getValue().getName().getString());
108         assertEquals(InterfaceStatus.Up, notificationCaptor.getValue().getAdminStatus());
109         assertEquals(InterfaceStatus.Up, notificationCaptor.getValue().getOperStatus());
110     }
111 }