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