22e434f35974c6c069571caa73e17d7ed0383d46
[groupbasedpolicy.git] / renderers / ofoverlay / src / test / java / org / opendaylight / groupbasedpolicy / renderer / ofoverlay / endpoint / EndpointManagerTest.java
1 /*
2  * Copyright (c) 2015 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
9 package org.opendaylight.groupbasedpolicy.renderer.ofoverlay.endpoint;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertFalse;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertNull;
15 import static org.junit.Assert.assertTrue;
16 import static org.mockito.Matchers.any;
17 import static org.mockito.Mockito.mock;
18 import static org.mockito.Mockito.never;
19 import static org.mockito.Mockito.spy;
20 import static org.mockito.Mockito.times;
21 import static org.mockito.Mockito.verify;
22 import static org.mockito.Mockito.when;
23
24 import java.util.Collection;
25 import java.util.Collections;
26 import java.util.HashSet;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.Set;
30 import java.util.concurrent.ScheduledExecutorService;
31
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.opendaylight.controller.md.sal.binding.api.ClusteredDataTreeChangeListener;
35 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
36 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
37 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
38 import org.opendaylight.controller.md.sal.binding.api.NotificationService;
39 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
40 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
41 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
42 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
43 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
44 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
45 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
46 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
47 import org.opendaylight.groupbasedpolicy.dto.EgKey;
48 import org.opendaylight.groupbasedpolicy.dto.EpKey;
49 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.EndpointListener;
50 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.node.SwitchManager;
51 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
52 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
53 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ConditionName;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.EndpointGroupId;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.L2BridgeDomainId;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.L3ContextId;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.NetworkDomainId;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.EndpointService;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.Endpoints;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.Endpoint;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointKey;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointL3;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointL3Key;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointL3Prefix;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.l3endpoint.rev151217.NatAddress;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.OfOverlayContext;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.OfOverlayL3Context;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketProcessingService;
73 import org.opendaylight.yangtools.concepts.ListenerRegistration;
74 import org.opendaylight.yangtools.yang.binding.DataObject;
75 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
76
77 import com.google.common.base.Optional;
78 import com.google.common.util.concurrent.CheckedFuture;
79
80 public class EndpointManagerTest {
81
82     private EndpointManager manager;
83     private DataBroker dataProvider;
84     private PacketProcessingService packetService;
85     private SalFlowService flowService;
86     private NotificationService notificationService;
87     private ScheduledExecutorService executor;
88     private SwitchManager switchManager;
89     private ListenerRegistration<DataChangeListener> listenerReg;
90     private EndpointListener endpointListener;
91     private Endpoint endpoint1;
92     private Endpoint endpoint2;
93     private TenantId tenantId;
94     private EndpointGroupId endpointGroupId;
95     private L2BridgeDomainId l2BridgeDomainId;
96     private OfOverlayContext context1;
97     private OfOverlayContext context2;
98     private NodeId nodeId1;
99     private NodeId nodeId2;
100     private EndpointL3 oldL3Ep;
101     private EndpointL3 newL3Ep;
102     private Optional<Endpoints> optionalRead;
103
104     // TODO get rid of unnecessary mocks (endpoint1, endpoint2, their parameters)
105
106     @SuppressWarnings("unchecked")
107     @Before
108     public void initialisation() throws Exception {
109         dataProvider = mock(DataBroker.class);
110         packetService = mock(PacketProcessingService.class);
111         flowService = mock(SalFlowService.class);
112         notificationService = mock(NotificationService.class);
113         executor = mock(ScheduledExecutorService.class);
114         switchManager = mock(SwitchManager.class);
115         WriteTransaction writeTransaction = mock(WriteTransaction.class);
116         when(dataProvider.newWriteOnlyTransaction()).thenReturn(writeTransaction);
117         CheckedFuture<Void, TransactionCommitFailedException> checkedFutureWrite = mock(CheckedFuture.class);
118         when(writeTransaction.submit()).thenReturn(checkedFutureWrite);
119         BindingAwareBroker.RpcRegistration<EndpointService> rpcRegistration =
120                 mock(BindingAwareBroker.RpcRegistration.class);
121         listenerReg = mock(ListenerRegistration.class);
122         when(dataProvider.registerDataChangeListener(any(LogicalDatastoreType.class), any(InstanceIdentifier.class),
123                 any(DataChangeListener.class), any(DataChangeScope.class))).thenReturn(listenerReg);
124         when(dataProvider.registerDataTreeChangeListener(any(DataTreeIdentifier.class),
125                 any(ClusteredDataTreeChangeListener.class))).thenReturn(listenerReg);
126
127         manager = spy(new EndpointManager(dataProvider, packetService, flowService, notificationService, executor, switchManager));
128         endpointListener = mock(EndpointListener.class);
129         manager.registerListener(endpointListener);
130
131         endpoint1 = mock(Endpoint.class);
132         endpoint2 = mock(Endpoint.class);
133         tenantId = mock(TenantId.class);
134         endpointGroupId = mock(EndpointGroupId.class);
135         l2BridgeDomainId = mock(L2BridgeDomainId.class);
136         MacAddress macAddress = new MacAddress("12:34:56:78:9a:bc");
137         when(endpoint1.getTenant()).thenReturn(tenantId);
138         when(endpoint1.getEndpointGroup()).thenReturn(endpointGroupId);
139         when(endpoint1.getL2Context()).thenReturn(l2BridgeDomainId);
140         when(endpoint1.getMacAddress()).thenReturn(macAddress);
141         when(endpoint2.getTenant()).thenReturn(tenantId);
142         when(endpoint2.getEndpointGroup()).thenReturn(endpointGroupId);
143         when(endpoint2.getL2Context()).thenReturn(l2BridgeDomainId);
144         when(endpoint2.getMacAddress()).thenReturn(macAddress);
145         context1 = mock(OfOverlayContext.class);
146         context2 = mock(OfOverlayContext.class);
147         when(endpoint1.getAugmentation(OfOverlayContext.class)).thenReturn(context1);
148         when(endpoint2.getAugmentation(OfOverlayContext.class)).thenReturn(context2);
149         nodeId1 = mock(NodeId.class);
150         nodeId2 = mock(NodeId.class);
151         when(context1.getNodeId()).thenReturn(nodeId1);
152         when(context2.getNodeId()).thenReturn(nodeId2);
153         when(nodeId1.getValue()).thenReturn("nodeValue1");
154         when(nodeId2.getValue()).thenReturn("nodeValue2");
155
156         // onDataChanged
157         AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> change = mock(AsyncDataChangeEvent.class);
158         InstanceIdentifier<DataObject> endpointId = mock(InstanceIdentifier.class);
159         Set<InstanceIdentifier<?>> removedPaths = new HashSet<>();
160         removedPaths.add(endpointId);
161         when(change.getRemovedPaths()).thenReturn(removedPaths);
162
163         // updateEndpointL3
164         oldL3Ep = mock(EndpointL3.class);
165         newL3Ep = mock(EndpointL3.class);
166
167         // getEndpointsFromDataStore
168         ReadOnlyTransaction readTransaction = mock(ReadOnlyTransaction.class);
169         when(dataProvider.newReadOnlyTransaction()).thenReturn(readTransaction);
170         CheckedFuture<Optional<Endpoints>, ReadFailedException> checkedFutureRead = mock(CheckedFuture.class);
171         when(readTransaction.read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class)))
172             .thenReturn(checkedFutureRead);
173         optionalRead = mock(Optional.class);
174         when(checkedFutureRead.checkedGet()).thenReturn(optionalRead);
175         when(optionalRead.isPresent()).thenReturn(false);
176
177     }
178
179     // ***************
180     // EndpointManager
181     // ***************
182
183     @Test
184     public void getGroupsForNodeTest() {
185         assertTrue(manager.getGroupsForNode(nodeId1).isEmpty());
186         manager.processEndpoint(null, endpoint1);
187         assertFalse(manager.getGroupsForNode(nodeId1).isEmpty());
188     }
189
190     @Test
191     public void getNodesForGroupTest() {
192         EgKey egKey = mock(EgKey.class);
193         Set<NodeId> nodesForGroup = manager.getNodesForGroup(egKey);
194         assertNotNull(nodesForGroup);
195         assertTrue(nodesForGroup.isEmpty());
196     }
197
198     @Test
199     public void getEndpointsForNodeTestNodeIdEgKey() {
200         EgKey egKey = new EgKey(tenantId, endpointGroupId);
201         assertTrue(manager.getEndpointsForNode(nodeId1, egKey).isEmpty());
202         manager.processEndpoint(null, endpoint1);
203         assertFalse(manager.getEndpointsForNode(nodeId1, egKey).isEmpty());
204     }
205
206     @Test
207     public void getEndpointsForNodeTestNodeId() {
208         assertTrue(manager.getEndpointsForNode(nodeId1).isEmpty());
209         manager.processEndpoint(null, endpoint1);
210         assertFalse(manager.getEndpointsForNode(nodeId1).isEmpty());
211     }
212
213     @Test
214     public void getEndpoint() {
215         EpKey epKey = new EpKey(endpoint1.getL2Context(), endpoint1.getMacAddress());
216         manager.processEndpoint(null, endpoint1);
217         assertEquals(endpoint1, manager.getEndpoint(epKey));
218     }
219
220     @SuppressWarnings("unchecked")
221     @Test
222     public void getEndpointsL3PrefixForTenantTest() throws Exception {
223         ReadOnlyTransaction readTransaction = mock(ReadOnlyTransaction.class);
224         when(dataProvider.newReadOnlyTransaction()).thenReturn(readTransaction);
225         CheckedFuture<Optional<Endpoints>, ReadFailedException> resultFuture = mock(CheckedFuture.class);
226         when(readTransaction.read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class)))
227             .thenReturn(resultFuture);
228         Optional<Endpoints> optional = mock(Optional.class);
229         when(resultFuture.checkedGet()).thenReturn(optional);
230         when(optional.isPresent()).thenReturn(true);
231         Endpoints endpoints = mock(Endpoints.class);
232         when(optional.get()).thenReturn(endpoints);
233         EndpointL3Prefix endpointL3Prefix = mock(EndpointL3Prefix.class);
234         when(endpoints.getEndpointL3Prefix()).thenReturn(Collections.singletonList(endpointL3Prefix));
235         when(endpointL3Prefix.getTenant()).thenReturn(tenantId);
236
237         Collection<EndpointL3Prefix> result = manager.getEndpointsL3PrefixForTenant(tenantId);
238         assertTrue(result.contains(endpointL3Prefix));
239     }
240
241     @SuppressWarnings("unchecked")
242     @Test
243     public void getEndpointsFromDataStoreTest() throws Exception {
244         ReadOnlyTransaction transaction = mock(ReadOnlyTransaction.class);
245         when(dataProvider.newReadOnlyTransaction()).thenReturn(transaction);
246         CheckedFuture<Optional<Endpoints>, ReadFailedException> checkedFuture = mock(CheckedFuture.class);
247         when(transaction.read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class)))
248             .thenReturn(checkedFuture);
249         Optional<Endpoints> optional = mock(Optional.class);
250         when(checkedFuture.checkedGet()).thenReturn(optional);
251         when(optional.isPresent()).thenReturn(false);
252         assertNull(manager.getEndpointsFromDataStore());
253
254         when(optional.isPresent()).thenReturn(true);
255         Endpoints endpoints = mock(Endpoints.class);
256         when(optional.get()).thenReturn(endpoints);
257         assertEquals(endpoints, manager.getEndpointsFromDataStore());
258
259         manager = new EndpointManager(null, packetService, flowService, notificationService, executor, switchManager);
260         assertNull(manager.getEndpointsFromDataStore());
261     }
262
263     @Test
264     public void getL3EndpointsTestEndpointsNull() throws Exception {
265         assertNull(manager.getL3Endpoints());
266     }
267
268     @Test
269     public void getL3EndpointsTestEndpointL3Null() throws Exception {
270         when(optionalRead.isPresent()).thenReturn(true);
271         Endpoints endpoints = mock(Endpoints.class);
272         when(optionalRead.get()).thenReturn(endpoints);
273         when(endpoints.getEndpointL3()).thenReturn(null);
274
275         assertNull(manager.getL3Endpoints());
276     }
277
278     @Test
279     public void getL3EndpointsTest() throws Exception {
280         when(optionalRead.isPresent()).thenReturn(true);
281         Endpoints endpoints = mock(Endpoints.class);
282         when(optionalRead.get()).thenReturn(endpoints);
283         List<EndpointL3> endpointL3List = Collections.singletonList(mock(EndpointL3.class));
284         when(endpoints.getEndpointL3()).thenReturn(endpointL3List);
285
286         assertEquals(endpointL3List, manager.getL3Endpoints());
287     }
288
289     @Test
290     public void getL3EpWithNatByL2KeyTest() {
291         when(optionalRead.isPresent()).thenReturn(true);
292         Endpoints endpoints = mock(Endpoints.class);
293         when(optionalRead.get()).thenReturn(endpoints);
294         EndpointL3 endpointL3 = mock(EndpointL3.class);
295         List<EndpointL3> endpointL3List = Collections.singletonList(endpointL3);
296         when(endpoints.getEndpointL3()).thenReturn(endpointL3List);
297
298         NatAddress overlayL3Nat = mock(NatAddress.class);
299         when(endpointL3.getAugmentation(NatAddress.class)).thenReturn(overlayL3Nat);
300         when(overlayL3Nat.getNatAddress()).thenReturn(mock(IpAddress.class));
301
302         when(endpointL3.getL2Context()).thenReturn(mock(L2BridgeDomainId.class));
303         when(endpointL3.getMacAddress()).thenReturn(mock(MacAddress.class));
304
305         Map<EndpointKey, EndpointL3> result = manager.getL3EpWithNatByL2Key();
306         assertTrue(result.containsValue(endpointL3));
307     }
308
309     @Test
310     public void getL3EpWithNatByL2KeyTestL3EpsNull() {
311         Map<EndpointKey, EndpointL3> result = manager.getL3EpWithNatByL2Key();
312         assertTrue(result.isEmpty());
313     }
314
315     @Test
316     public void getL3EpWithNatByL2KeyTestGetMacAddressNull() {
317         when(optionalRead.isPresent()).thenReturn(true);
318         Endpoints endpoints = mock(Endpoints.class);
319         when(optionalRead.get()).thenReturn(endpoints);
320         EndpointL3 endpointL3 = mock(EndpointL3.class);
321         List<EndpointL3> endpointL3List = Collections.singletonList(endpointL3);
322         when(endpoints.getEndpointL3()).thenReturn(endpointL3List);
323
324         NatAddress overlayL3Nat = mock(NatAddress.class);
325         when(endpointL3.getAugmentation(NatAddress.class)).thenReturn(overlayL3Nat);
326
327         when(endpointL3.getL2Context()).thenReturn(mock(L2BridgeDomainId.class));
328         when(endpointL3.getMacAddress()).thenReturn(null);
329
330         Map<EndpointKey, EndpointL3> result = manager.getL3EpWithNatByL2Key();
331         assertTrue(result.isEmpty());
332     }
333
334     @Test
335     public void getL3EpWithNatByL2KeyTestGetL2ContextNull() {
336         when(optionalRead.isPresent()).thenReturn(true);
337         Endpoints endpoints = mock(Endpoints.class);
338         when(optionalRead.get()).thenReturn(endpoints);
339         EndpointL3 endpointL3 = mock(EndpointL3.class);
340         List<EndpointL3> endpointL3List = Collections.singletonList(endpointL3);
341         when(endpoints.getEndpointL3()).thenReturn(endpointL3List);
342
343         NatAddress overlayL3Nat = mock(NatAddress.class);
344         when(endpointL3.getAugmentation(NatAddress.class)).thenReturn(overlayL3Nat);
345
346         when(endpointL3.getL2Context()).thenReturn(null);
347         when(endpointL3.getMacAddress()).thenReturn(mock(MacAddress.class));
348
349         Map<EndpointKey, EndpointL3> result = manager.getL3EpWithNatByL2Key();
350         assertTrue(result.isEmpty());
351     }
352
353     @Test
354     public void getEndpointsForGroupTest() {
355         EgKey newEgKey = new EgKey(tenantId, endpointGroupId);
356         assertTrue(manager.getEndpointsForGroup(newEgKey).isEmpty());
357         manager.processEndpoint(null, endpoint1);
358         assertFalse(manager.getEndpointsForGroup(newEgKey).isEmpty());
359     }
360
361     @Test
362     public void getConditionsForEndpoint() {
363         Endpoint endpoint = mock(Endpoint.class);
364         when(endpoint.getCondition()).thenReturn(null);
365         assertTrue(manager.getConditionsForEndpoint(endpoint).isEmpty());
366
367         List<ConditionName> conditionNameList = Collections.singletonList(mock(ConditionName.class));
368         when(endpoint.getCondition()).thenReturn(conditionNameList);
369         assertEquals(conditionNameList, manager.getConditionsForEndpoint(endpoint));
370     }
371
372     @Test
373     public void updateEndpointL3TestNewL3EpValidExternalTrue() throws Exception {
374         when(newL3Ep.getTenant()).thenReturn(mock(TenantId.class));
375         when(newL3Ep.getEndpointGroup()).thenReturn(mock(EndpointGroupId.class));
376         when(newL3Ep.getL3Context()).thenReturn(mock(L3ContextId.class));
377         when(newL3Ep.getIpAddress()).thenReturn(mock(IpAddress.class));
378
379         OfOverlayL3Context ofOverlayL3Context = mock(OfOverlayL3Context.class);
380         when(newL3Ep.getAugmentation(OfOverlayL3Context.class)).thenReturn(ofOverlayL3Context);
381
382         NetworkDomainId networkDomainId = mock(NetworkDomainId.class);
383         when(newL3Ep.getNetworkContainment()).thenReturn(networkDomainId);
384
385         EndpointL3Key endpointL3Key = mock(EndpointL3Key.class);
386         when(newL3Ep.getKey()).thenReturn(endpointL3Key);
387         IpAddress ipAddress = mock(IpAddress.class);
388         when(endpointL3Key.getIpAddress()).thenReturn(ipAddress);
389         when(newL3Ep.getIpAddress()).thenReturn(new IpAddress(new Ipv4Address("1.1.1.1")));
390         manager.processL3Endpoint(null, newL3Ep);
391         verify(endpointListener, never()).endpointUpdated(any(EpKey.class));
392     }
393
394     @Test
395     public void updateEndpointL3TestNewL3EpValidExternalTrueNetworkContainmentNull() throws Exception {
396         when(newL3Ep.getTenant()).thenReturn(mock(TenantId.class));
397         when(newL3Ep.getEndpointGroup()).thenReturn(mock(EndpointGroupId.class));
398         when(newL3Ep.getL3Context()).thenReturn(mock(L3ContextId.class));
399         when(newL3Ep.getIpAddress()).thenReturn(mock(IpAddress.class));
400
401         OfOverlayL3Context ofOverlayL3Context = mock(OfOverlayL3Context.class);
402         when(newL3Ep.getAugmentation(OfOverlayL3Context.class)).thenReturn(ofOverlayL3Context);
403
404         when(newL3Ep.getNetworkContainment()).thenReturn(null);
405
406         EndpointL3Key endpointL3Key = mock(EndpointL3Key.class);
407         when(newL3Ep.getKey()).thenReturn(endpointL3Key);
408         IpAddress ipAddress = mock(IpAddress.class);
409         when(endpointL3Key.getIpAddress()).thenReturn(ipAddress);
410         when(newL3Ep.getIpAddress()).thenReturn(new IpAddress(new Ipv4Address("1.1.1.1")));
411         manager.processL3Endpoint(null, newL3Ep);
412         verify(endpointListener, never()).endpointUpdated(any(EpKey.class));
413     }
414
415     @Test
416     public void updateEndpointL3TestNewL3EpValidInternal() throws Exception {
417         when(newL3Ep.getTenant()).thenReturn(mock(TenantId.class));
418         when(newL3Ep.getEndpointGroup()).thenReturn(mock(EndpointGroupId.class));
419         when(newL3Ep.getL3Context()).thenReturn(mock(L3ContextId.class));
420         when(newL3Ep.getIpAddress()).thenReturn(mock(IpAddress.class));
421
422         OfOverlayL3Context ofOverlayL3Context = mock(OfOverlayL3Context.class);
423         when(newL3Ep.getAugmentation(OfOverlayL3Context.class)).thenReturn(ofOverlayL3Context);
424
425         when(newL3Ep.getNetworkContainment()).thenReturn(null);
426
427         when(newL3Ep.getL2Context()).thenReturn(mock(L2BridgeDomainId.class));
428         when(newL3Ep.getMacAddress()).thenReturn(mock(MacAddress.class));
429         when(newL3Ep.getIpAddress()).thenReturn(new IpAddress(new Ipv4Address("1.1.1.1")));
430         manager.processL3Endpoint(null, newL3Ep);
431         verify(endpointListener).endpointUpdated(any(EpKey.class));
432     }
433
434     @Test
435     public void updateEndpointL3TestNewL3EpValidFalse() throws Exception {
436         when(newL3Ep.getTenant()).thenReturn(mock(TenantId.class));
437         when(newL3Ep.getEndpointGroup()).thenReturn(mock(EndpointGroupId.class));
438         when(newL3Ep.getL3Context()).thenReturn(mock(L3ContextId.class));
439         when(newL3Ep.getIpAddress()).thenReturn(null);
440         when(newL3Ep.getIpAddress()).thenReturn(new IpAddress(new Ipv4Address("1.1.1.1")));
441         manager.processL3Endpoint(null, newL3Ep);
442         verify(endpointListener, never()).endpointUpdated(any(EpKey.class));
443     }
444
445     @Test
446     public void updateEndpointL3TestDelete() throws Exception {
447         when(oldL3Ep.getIpAddress()).thenReturn(new IpAddress(new Ipv4Address("1.1.1.1")));
448         manager.processL3Endpoint(oldL3Ep, null);
449         verify(endpointListener).endpointUpdated(any(EpKey.class));
450     }
451
452     @Test
453     public void updateEndpointL3TestUpdate() throws Exception {
454         when(oldL3Ep.getIpAddress()).thenReturn(new IpAddress(new Ipv4Address("1.1.1.1")));
455
456         when(newL3Ep.getTenant()).thenReturn(mock(TenantId.class));
457         when(newL3Ep.getEndpointGroup()).thenReturn(mock(EndpointGroupId.class));
458         when(newL3Ep.getL3Context()).thenReturn(mock(L3ContextId.class));
459         when(newL3Ep.getIpAddress()).thenReturn(mock(IpAddress.class));
460
461         OfOverlayL3Context ofOverlayL3Context = mock(OfOverlayL3Context.class);
462         when(newL3Ep.getAugmentation(OfOverlayL3Context.class)).thenReturn(ofOverlayL3Context);
463
464         when(newL3Ep.getNetworkContainment()).thenReturn(null);
465
466         when(newL3Ep.getL2Context()).thenReturn(mock(L2BridgeDomainId.class));
467         when(newL3Ep.getMacAddress()).thenReturn(mock(MacAddress.class));
468         when(newL3Ep.getIpAddress()).thenReturn(new IpAddress(new Ipv4Address("1.1.1.1")));
469
470         manager.processL3Endpoint(null, oldL3Ep);
471         manager.processL3Endpoint(oldL3Ep, newL3Ep);
472
473         verify(endpointListener).endpointUpdated(any(EpKey.class));
474     }
475
476     @Test
477     public void updateEndpointTestNewEndpointRemove() {
478         Collection<Endpoint> collection;
479         manager.processEndpoint(null, endpoint2);
480         verify(endpointListener).endpointUpdated(any(EpKey.class));
481         verify(endpointListener).nodeEndpointUpdated(any(NodeId.class), any(EpKey.class));
482         collection = manager.getEndpointsForGroup(new EgKey(tenantId, endpointGroupId));
483         assertFalse(collection.isEmpty());
484
485         manager.processEndpoint(endpoint2, null);
486         verify(endpointListener, times(2)).endpointUpdated(any(EpKey.class));
487         verify(endpointListener, times(2)).nodeEndpointUpdated(any(NodeId.class), any(EpKey.class));
488         collection = manager.getEndpointsForGroup(new EgKey(tenantId, endpointGroupId));
489         assertTrue(collection.isEmpty());
490     }
491
492     @Test
493     public void updateEndpointTestNewLocNullOldLocNullOfOverlayContextAugmentationNull() {
494         when(endpoint1.getAugmentation(OfOverlayContext.class)).thenReturn(null);
495         when(endpoint2.getAugmentation(OfOverlayContext.class)).thenReturn(null);
496
497         manager.processEndpoint(endpoint1, endpoint2);
498         verify(endpointListener, never()).endpointUpdated(any(EpKey.class));
499         verify(endpointListener, never()).nodeEndpointUpdated(any(NodeId.class), any(EpKey.class));
500     }
501
502     @Test
503     public void updateEndpointTestNewLocNullOldLocNull() {
504         when(context1.getNodeId()).thenReturn(null);
505         when(context2.getNodeId()).thenReturn(null);
506
507         manager.processEndpoint(endpoint1, endpoint2);
508         verify(endpointListener, never()).endpointUpdated(any(EpKey.class));
509         verify(endpointListener, never()).nodeEndpointUpdated(any(NodeId.class), any(EpKey.class));
510     }
511
512     @Test
513     public void updateEndpointTestNewLocNullOldLocNullExternalPut() {
514         when(endpoint1.getAugmentation(OfOverlayContext.class)).thenReturn(null);
515         when(endpoint2.getAugmentation(OfOverlayContext.class)).thenReturn(null);
516
517         manager.processEndpoint(endpoint1, endpoint2);
518         verify(endpointListener, never()).endpointUpdated(any(EpKey.class));
519         verify(endpointListener, never()).nodeEndpointUpdated(any(NodeId.class), any(EpKey.class));
520     }
521
522     @Test
523     public void updateEndpointTestNewLocNullOldLocNullExternalRemove() {
524         when(context1.getNodeId()).thenReturn(null);
525         manager.processEndpoint(null, endpoint1);
526
527         manager.processEndpoint(endpoint1, null);
528         verify(endpointListener, never()).endpointUpdated(any(EpKey.class));
529         verify(endpointListener, never()).nodeEndpointUpdated(any(NodeId.class), any(EpKey.class));
530     }
531
532     /**
533      * Endpoint changes it's location
534      */
535     @Test
536     public void updateEndpointLocationTestUpdate() {
537         Collection<Endpoint> collection;
538         manager.processEndpoint(null, endpoint1);
539
540         manager.processEndpoint(endpoint1, endpoint2);
541         verify(endpointListener, times(2)).endpointUpdated(any(EpKey.class));
542         // create: node1, update: node1 -> node2
543         verify(endpointListener, times(3)).nodeEndpointUpdated(any(NodeId.class), any(EpKey.class));
544         collection = manager.getEndpointsForGroup(new EgKey(tenantId, endpointGroupId));
545         assertFalse(collection.isEmpty());
546     }
547
548     /**
549      * Endpoint changes it's EPG
550      */
551     @Test
552     public void updateEndpointGroupTestUpdate() {
553         Collection<Endpoint> collection;
554         EndpointGroupId otherEndpointGroupId = mock(EndpointGroupId.class);
555         when(endpoint2.getEndpointGroup()).thenReturn(otherEndpointGroupId);
556         when(endpoint2.getAugmentation(OfOverlayContext.class)).thenReturn(context1);
557         manager.processEndpoint(null, endpoint1);
558
559         manager.processEndpoint(endpoint1, endpoint2);
560         verify(endpointListener, times(2)).endpointUpdated(any(EpKey.class));
561         verify(endpointListener, times(1)).nodeEndpointUpdated(any(NodeId.class), any(EpKey.class));
562         collection = manager.getEndpointsForGroup(new EgKey(tenantId, endpointGroupId));
563         assertTrue(collection.isEmpty());
564         collection = manager.getEndpointsForGroup(new EgKey(tenantId, otherEndpointGroupId));
565         assertFalse(collection.isEmpty());
566     }
567
568     /**
569      * Endpoint changes it's location and EPGs
570      */
571     @Test
572     public void updateEndpointLocationAndGroupTestUpdate() {
573         Collection<Endpoint> collection;
574         EndpointGroupId otherEndpointGroupId = mock(EndpointGroupId.class);
575         when(endpoint2.getEndpointGroup()).thenReturn(otherEndpointGroupId);
576         manager.processEndpoint(null, endpoint1);
577
578         manager.processEndpoint(endpoint1, endpoint2);
579         verify(endpointListener, times(2)).endpointUpdated(any(EpKey.class));
580         // create: node1, update: node1 -> node2
581         verify(endpointListener, times(3)).nodeEndpointUpdated(any(NodeId.class), any(EpKey.class));
582         collection = manager.getEndpointsForGroup(new EgKey(tenantId, endpointGroupId));
583         assertTrue(collection.isEmpty());
584         collection = manager.getEndpointsForGroup(new EgKey(tenantId, otherEndpointGroupId));
585         assertFalse(collection.isEmpty());
586     }
587
588     /**
589      * Endpoint becomes external when removing it's location augmentation.
590      * This might happen when an endpoint is removed from a device.
591      */
592     @Test
593     public void updateEndpointLocationRemovedTestUpdate() {
594         Collection<Endpoint> collection;
595         when(endpoint2.getAugmentation(OfOverlayContext.class)).thenReturn(null);
596         manager.processEndpoint(null, endpoint1);
597
598         manager.processEndpoint(endpoint1, endpoint2);
599         verify(endpointListener, times(2)).endpointUpdated(any(EpKey.class));
600         verify(endpointListener, times(2)).nodeEndpointUpdated(any(NodeId.class), any(EpKey.class));
601         collection = manager.getEndpointsForGroup(new EgKey(tenantId, endpointGroupId));
602         assertTrue(collection.isEmpty());
603     }
604
605     /**
606      * Endpoint is created when adding location augmentation.
607      * Endpoint is not external anymore.
608      */
609     @Test
610     public void updateEndpointLocationAddedTestUpdate() {
611         Collection<Endpoint> collection;
612         when(endpoint1.getAugmentation(OfOverlayContext.class)).thenReturn(null);
613         manager.processEndpoint(null, endpoint1);
614
615         manager.processEndpoint(endpoint1, endpoint2);
616         verify(endpointListener).endpointUpdated(any(EpKey.class));
617         verify(endpointListener).nodeEndpointUpdated(any(NodeId.class), any(EpKey.class));
618         collection = manager.getEndpointsForGroup(new EgKey(tenantId, endpointGroupId));
619         assertFalse(collection.isEmpty());
620     }
621
622     @Test
623     public void closeTest() throws Exception {
624         manager.close();
625         verify(listenerReg, times(3)).close();
626     }
627
628     // **************
629     // Helper Functions
630     // **************
631
632     @Test
633     public void getEgKeyTest() {
634         assertNotNull(manager.getEgKey(endpoint1));
635         assertNull(manager.getEgKey(null));
636
637         when(endpoint1.getTenant()).thenReturn(null);
638         assertNull(manager.getEgKey(endpoint1));
639
640         when(endpoint1.getTenant()).thenReturn(tenantId);
641         when(endpoint1.getEndpointGroup()).thenReturn(null);
642         assertNull(manager.getEgKey(endpoint1));
643
644         when(endpoint1.getEndpointGroup()).thenReturn(endpointGroupId);
645         when(endpoint1.getEndpointGroups()).thenReturn(null);
646         assertNotNull(manager.getEgKey(endpoint1));
647
648         when(endpoint1.getEndpointGroup()).thenReturn(null);
649         assertNull(manager.getEgKey(endpoint1));
650
651         when(endpoint1.getEndpointGroup()).thenReturn(endpointGroupId);
652         when(endpoint1.getL2Context()).thenReturn(null);
653         assertNull(manager.getEgKey(endpoint1));
654
655         when(endpoint1.getL2Context()).thenReturn(l2BridgeDomainId);
656         when(endpoint1.getMacAddress()).thenReturn(null);
657         assertNull(manager.getEgKey(endpoint1));
658     }
659
660     @Test
661     public void getEgKeysForEndpointTest() {
662         Endpoint endpoint = mock(Endpoint.class);
663         Set<EgKey> egKeys;
664
665         when(endpoint.getEndpointGroups()).thenReturn(null);
666         egKeys = manager.getEgKeysForEndpoint(endpoint);
667         assertTrue(egKeys.isEmpty());
668
669         EndpointGroupId endpointGroupId = mock(EndpointGroupId.class);
670         when(endpoint.getEndpointGroup()).thenReturn(endpointGroupId);
671         egKeys = manager.getEgKeysForEndpoint(endpoint);
672         assertEquals(1, egKeys.size());
673
674         EndpointGroupId epgId = mock(EndpointGroupId.class);
675         List<EndpointGroupId> endpointGroups = Collections.singletonList(epgId);
676         when(endpoint.getEndpointGroups()).thenReturn(endpointGroups);
677         egKeys = manager.getEgKeysForEndpoint(endpoint);
678         assertEquals(2, egKeys.size());
679     }
680
681     @Test
682     public void isExternalIsInternalTest() {
683         Endpoint endpoint = mock(Endpoint.class);
684         when(endpoint.getAugmentation(OfOverlayContext.class)).thenReturn(null);
685         // TODO
686         // assertFalse(manager.isExternal(endpoint));
687         // assertTrue(manager.isInternal(endpoint));
688         //
689         // OfOverlayContext ofc = mock(OfOverlayContext.class);
690         // when(endpoint.getAugmentation(OfOverlayContext.class)).thenReturn(ofc);
691         // when(ofc.getLocationType()).thenReturn(null);
692         // assertFalse(manager.isExternal(endpoint));
693         // assertTrue(manager.isInternal(endpoint));
694         //
695         // when(ofc.getLocationType()).thenReturn(LocationType.Internal);
696         // assertFalse(manager.isExternal(endpoint));
697         // assertTrue(manager.isInternal(endpoint));
698         //
699         // when(ofc.getLocationType()).thenReturn(LocationType.External);
700         // assertTrue(manager.isExternal(endpoint));
701         // assertFalse(manager.isInternal(endpoint));
702     }
703
704     @Test
705     public void testGetL2EndpointFromL3() {
706         when(newL3Ep.getL2Context()).thenReturn(mock(L2BridgeDomainId.class));
707         when(newL3Ep.getMacAddress()).thenReturn(mock(MacAddress.class));
708
709         Endpoint ep = manager.getL2EndpointFromL3(newL3Ep);
710
711         assertNull(ep);
712     }
713
714     @Test
715     public void testGetL2EndpointFromL3_noL2Context_noMacAddr() {
716         when(newL3Ep.getL2Context()).thenReturn(null);
717         when(newL3Ep.getMacAddress()).thenReturn(null);
718
719         Endpoint ep = manager.getL2EndpointFromL3(newL3Ep);
720
721         assertNull(ep);
722     }
723
724 }