Migrate to MD-SAL APIs
[lispflowmapping.git] / mappingservice / implementation / src / test / java / org / opendaylight / lispflowmapping / implementation / mdsal / MappingDataListenerTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.lispflowmapping.implementation.mdsal;
9
10 import static org.junit.Assert.assertEquals;
11
12 import com.google.common.collect.Lists;
13 import java.util.List;
14 import org.junit.Before;
15 import org.junit.Ignore;
16 import org.junit.Test;
17 import org.mockito.ArgumentCaptor;
18 import org.mockito.Mockito;
19 import org.opendaylight.lispflowmapping.implementation.util.MSNotificationInputUtil;
20 import org.opendaylight.lispflowmapping.interfaces.mapcache.IMappingSystem;
21 import org.opendaylight.lispflowmapping.lisp.type.MappingData;
22 import org.opendaylight.lispflowmapping.lisp.util.LispAddressUtil;
23 import org.opendaylight.mdsal.binding.api.DataBroker;
24 import org.opendaylight.mdsal.binding.api.DataObjectModification;
25 import org.opendaylight.mdsal.binding.api.DataObjectModification.ModificationType;
26 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
27 import org.opendaylight.mdsal.binding.api.DataTreeModification;
28 import org.opendaylight.mdsal.binding.api.NotificationPublishService;
29 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container.MappingRecord;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container.MappingRecordBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.MappingChange;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.MappingChanged;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.MappingOrigin;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.db.instance.Mapping;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.db.instance.MappingBuilder;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39
40 public class MappingDataListenerTest {
41
42     private static IMappingSystem iMappingSystemMock;
43     private static NotificationPublishService notificationPublishServiceMock;
44
45     private static DataTreeModification<Mapping> change_del;
46     private static DataTreeModification<Mapping> change_subtreeModified;
47     private static DataTreeModification<Mapping> change_write;
48     private static DataObjectModification<Mapping> mod_del;
49     private static DataObjectModification<Mapping> mod_subtreeModified;
50     private static DataObjectModification<Mapping> mod_write;
51
52     private static MappingDataListener mappingDataListener;
53
54     private static final String IPV4_STRING_1 = "192.168.0.1";
55     private static final String IPV4_STRING_2 = "192.168.0.2";
56     private static final String IPV4_STRING_3 = "192.168.0.3";
57     private static final Eid IPV4_EID_1 = LispAddressUtil.asIpv4Eid(IPV4_STRING_1);
58     private static final Eid IPV4_EID_2 = LispAddressUtil.asIpv4Eid(IPV4_STRING_2);
59     private static final Eid IPV4_EID_3 = LispAddressUtil.asIpv4Eid(IPV4_STRING_3);
60
61     private static final Mapping MAPPING_EID_1_NB = getDefaultMapping(IPV4_EID_1, MappingOrigin.Northbound);
62     private static final Mapping MAPPING_EID_1_SB = getDefaultMapping(IPV4_EID_1, MappingOrigin.Southbound);
63     private static final Mapping MAPPING_EID_2_NB = getDefaultMapping(IPV4_EID_2, MappingOrigin.Northbound);
64     private static final Mapping MAPPING_EID_2_SB = getDefaultMapping(IPV4_EID_2, MappingOrigin.Southbound);
65     private static final Mapping MAPPING_EID_3_NB = getDefaultMapping(IPV4_EID_3, MappingOrigin.Northbound);
66     private static final Mapping MAPPING_EID_3_SB = getDefaultMapping(IPV4_EID_3, MappingOrigin.Southbound);
67
68     @Before
69     @SuppressWarnings("unchecked")
70     public void init() {
71         final DataBroker dataBrokerMock = Mockito.mock(DataBroker.class);
72         iMappingSystemMock = Mockito.mock(IMappingSystem.class);
73         notificationPublishServiceMock = Mockito.mock(NotificationPublishService .class);
74         mappingDataListener =
75                 new MappingDataListener(dataBrokerMock, iMappingSystemMock, notificationPublishServiceMock);
76
77         final InstanceIdentifier<Mapping> instanceIdentifierMock = Mockito.mock(InstanceIdentifier.class);
78         final DataTreeIdentifier<Mapping> dataTreeIdentifier =
79                 DataTreeIdentifier.create(LogicalDatastoreType.CONFIGURATION, instanceIdentifierMock);
80
81         change_del = Mockito.mock(DataTreeModification.class);
82         change_subtreeModified = Mockito.mock(DataTreeModification.class);
83         change_write = Mockito.mock(DataTreeModification.class);
84         mod_del = Mockito.mock(DataObjectModification.class);
85         mod_subtreeModified = Mockito.mock(DataObjectModification.class);
86         mod_write = Mockito.mock(DataObjectModification.class);
87
88         Mockito.when(change_del.getRootPath()).thenReturn(dataTreeIdentifier);
89         Mockito.when(change_del.getRootNode()).thenReturn(mod_del);
90         Mockito.when(change_subtreeModified.getRootPath()).thenReturn(dataTreeIdentifier);
91         Mockito.when(change_subtreeModified.getRootNode()).thenReturn(mod_subtreeModified);
92         Mockito.when(change_write.getRootPath()).thenReturn(dataTreeIdentifier);
93         Mockito.when(change_write.getRootNode()).thenReturn(mod_write);
94         Mockito.when(mod_del.getModificationType()).thenReturn(ModificationType.DELETE);
95         Mockito.when(mod_subtreeModified.getModificationType()).thenReturn(ModificationType.SUBTREE_MODIFIED);
96         Mockito.when(mod_write.getModificationType()).thenReturn(ModificationType.WRITE);
97         Mockito.when(iMappingSystemMock.isMaster()).thenReturn(true);
98     }
99
100     /**
101      * Tests {@link MappingDataListener#onDataTreeChanged} method with DELETE modification type from northbound.
102      */
103     @Test
104     public void onDataTreeChangedTest_delete_NB() throws InterruptedException {
105         final List<DataTreeModification<Mapping>> changes = Lists.newArrayList(change_del);
106         Mockito.when(mod_del.getDataBefore()).thenReturn(MAPPING_EID_1_NB);
107
108         mappingDataListener.onDataTreeChanged(changes);
109         Mockito.verify(iMappingSystemMock).removeMapping(MappingOrigin.Northbound, IPV4_EID_1);
110     }
111
112     /**
113      * Tests {@link MappingDataListener#onDataTreeChanged} method with DELETE modification type from southbound.
114      */
115     @Test
116     public void onDataTreeChangedTest_delete_SB() {
117         final List<DataTreeModification<Mapping>> changes = Lists.newArrayList(change_del);
118         Mockito.when(mod_del.getDataBefore()).thenReturn(MAPPING_EID_1_SB);
119
120         mappingDataListener.onDataTreeChanged(changes);
121         //Mockito.verifyZeroInteractions(iMappingSystemMock);
122         Mockito.verifyZeroInteractions(notificationPublishServiceMock);
123     }
124
125     /**
126      * Tests {@link MappingDataListener#onDataTreeChanged} method with SUBTREE_MODIFIED modification type from
127      * northbound.
128      */
129     @Test
130     @Ignore
131     public void onDataTreeChangedTest_subtreeModified_NB() throws InterruptedException {
132         final List<DataTreeModification<Mapping>> changes = Lists.newArrayList(change_subtreeModified);
133         final MappingChanged mapChanged = MSNotificationInputUtil.toMappingChanged(
134                 MAPPING_EID_2_NB.getMappingRecord(), null, null, null, MappingChange.Updated);
135         Mockito.when(mod_subtreeModified.getDataAfter()).thenReturn(MAPPING_EID_2_NB);
136
137         mappingDataListener.onDataTreeChanged(changes);
138         final ArgumentCaptor<MappingData> captor = ArgumentCaptor.forClass(MappingData.class);
139         Mockito.verify(iMappingSystemMock)
140                 .addMapping(Mockito.eq(MappingOrigin.Northbound), Mockito.eq(IPV4_EID_2), captor.capture());
141         assertEquals(captor.getValue().getRecord(), MAPPING_EID_2_NB.getMappingRecord());
142         Mockito.verify(notificationPublishServiceMock).putNotification(mapChanged);
143     }
144
145     /**
146      * Tests {@link MappingDataListener#onDataTreeChanged} method with SUBTREE_MODIFIED modification type from
147      * southbound.
148      */
149     @Test
150     public void onDataTreeChangedTest_subtreeModified_SB() {
151         final List<DataTreeModification<Mapping>> changes = Lists.newArrayList(change_subtreeModified);
152         Mockito.when(mod_subtreeModified.getDataAfter()).thenReturn(MAPPING_EID_2_SB);
153
154         mappingDataListener.onDataTreeChanged(changes);
155         //Mockito.verifyZeroInteractions(iMappingSystemMock);
156         Mockito.verifyZeroInteractions(notificationPublishServiceMock);
157     }
158
159     /**
160      * Tests {@link MappingDataListener#onDataTreeChanged} method with WRITE modification type from northbound.
161      */
162     @Test
163     @Ignore
164     public void onDataTreeChangedTest_write_NB() throws InterruptedException {
165         final List<DataTreeModification<Mapping>> changes = Lists.newArrayList(change_write);
166         final MappingChanged mapChanged = MSNotificationInputUtil.toMappingChanged(
167                 MAPPING_EID_3_NB.getMappingRecord(), null, null, null, MappingChange.Created);
168         Mockito.when(mod_write.getDataAfter()).thenReturn(MAPPING_EID_3_NB);
169
170         mappingDataListener.onDataTreeChanged(changes);
171         final ArgumentCaptor<MappingData> captor = ArgumentCaptor.forClass(MappingData.class);
172         Mockito.verify(iMappingSystemMock)
173                 .addMapping(Mockito.eq(MappingOrigin.Northbound), Mockito.eq(IPV4_EID_3), captor.capture());
174         assertEquals(captor.getValue().getRecord(), MAPPING_EID_3_NB.getMappingRecord());
175         Mockito.verify(notificationPublishServiceMock).putNotification(mapChanged);
176     }
177
178     /**
179      * Tests {@link MappingDataListener#onDataTreeChanged} method with WRITE modification type from southbound.
180      */
181     @Test
182     public void onDataTreeChangedTest_write_SB() {
183         final List<DataTreeModification<Mapping>> changes = Lists.newArrayList(change_write);
184         Mockito.when(mod_write.getDataAfter()).thenReturn(MAPPING_EID_3_SB);
185
186         mappingDataListener.onDataTreeChanged(changes);
187         //Mockito.verifyZeroInteractions(iMappingSystemMock);
188         Mockito.verifyZeroInteractions(notificationPublishServiceMock);
189     }
190
191     /**
192      * Tests {@link MappingDataListener#onDataTreeChanged} method with multiple changes.
193      */
194     @Test
195     @Ignore
196     public void onDataTreeChangedTest_multipleChanges() throws InterruptedException {
197         final List<DataTreeModification<Mapping>> changes =
198                 Lists.newArrayList(change_del, change_subtreeModified, change_write);
199         final MappingChanged mapChangedSubtreeMod = MSNotificationInputUtil.toMappingChanged(
200                 MAPPING_EID_2_NB.getMappingRecord(), null, null, null, MappingChange.Updated);
201
202         Mockito.when(mod_del.getDataBefore()).thenReturn(MAPPING_EID_1_NB);
203         Mockito.when(mod_subtreeModified.getDataAfter()).thenReturn(MAPPING_EID_2_NB);
204         Mockito.when(mod_write.getDataAfter()).thenReturn(MAPPING_EID_3_SB);
205
206         mappingDataListener.onDataTreeChanged(changes);
207         final ArgumentCaptor<MappingData> captor = ArgumentCaptor.forClass(MappingData.class);
208         Mockito.verify(iMappingSystemMock).removeMapping(MappingOrigin.Northbound, IPV4_EID_1);
209         Mockito.verify(iMappingSystemMock)
210                 .addMapping(Mockito.eq(MappingOrigin.Northbound), Mockito.eq(IPV4_EID_2), captor.capture());
211         assertEquals(captor.getValue().getRecord(), MAPPING_EID_2_NB.getMappingRecord());
212         Mockito.verify(notificationPublishServiceMock).putNotification(mapChangedSubtreeMod);
213         //Mockito.verifyNoMoreInteractions(iMappingSystemMock);
214         Mockito.verifyNoMoreInteractions(notificationPublishServiceMock);
215     }
216
217     /**
218      * Tests {@link MappingDataListener#onDataTreeChanged} method with no modification type.
219      */
220     @Test
221     @SuppressWarnings("unchecked")
222     public void onDataTreeChangedTest_noModType() {
223         final DataTreeModification<Mapping> changeNoModType = Mockito.mock(DataTreeModification.class);
224         final DataObjectModification<Mapping> modNoType = Mockito.mock(DataObjectModification.class);
225         final List<DataTreeModification<Mapping>> changes = Lists.newArrayList(changeNoModType);
226
227         Mockito.when(changeNoModType.getRootNode()).thenReturn(modNoType);
228         Mockito.when(modNoType.getModificationType()).thenReturn(null);
229
230         mappingDataListener.onDataTreeChanged(changes);
231
232         Mockito.verifyZeroInteractions(iMappingSystemMock);
233         Mockito.verifyZeroInteractions(notificationPublishServiceMock);
234     }
235
236
237     private static Mapping getDefaultMapping(Eid eid, MappingOrigin origin) {
238         final MappingRecord record = new MappingRecordBuilder().setEid(eid).build();
239         return new MappingBuilder().setOrigin(origin).setMappingRecord(record).build();
240     }
241 }