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