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