2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.groupbasedpolicy.renderer.ofoverlay.endpoint;
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;
24 import java.util.Collection;
25 import java.util.Collections;
26 import java.util.HashSet;
27 import java.util.List;
30 import java.util.concurrent.ScheduledExecutorService;
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;
79 public class EndpointManagerTest {
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;
102 // TODO get rid of unnecessary mocks (endpoint1, endpoint2, their parameters)
104 @SuppressWarnings("unchecked")
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);
124 when(rpcRegistry.addRpcImplementation(any(Class.class), any(RpcService.class))).thenReturn(rpcRegistration);
126 manager = spy(new EndpointManager(dataProvider, rpcRegistry, notificationService, executor, switchManager));
127 endpointListener = mock(EndpointListener.class);
128 manager.registerListener(endpointListener);
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");
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);
163 oldL3Ep = mock(EndpointL3.class);
164 newL3Ep = mock(EndpointL3.class);
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);
183 public void getGroupsForNodeTest() {
184 assertTrue(manager.getGroupsForNode(nodeId1).isEmpty());
185 manager.processEndpoint(null, endpoint1);
186 assertFalse(manager.getGroupsForNode(nodeId1).isEmpty());
190 public void getNodesForGroupTest() {
191 EgKey egKey = mock(EgKey.class);
192 Set<NodeId> nodesForGroup = manager.getNodesForGroup(egKey);
193 assertNotNull(nodesForGroup);
194 assertTrue(nodesForGroup.isEmpty());
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());
206 public void getEndpointsForNodeTestNodeId() {
207 assertTrue(manager.getEndpointsForNode(nodeId1).isEmpty());
208 manager.processEndpoint(null, endpoint1);
209 assertFalse(manager.getEndpointsForNode(nodeId1).isEmpty());
213 public void getEndpoint() {
214 EpKey epKey = new EpKey(endpoint1.getL2Context(), endpoint1.getMacAddress());
215 manager.processEndpoint(null, endpoint1);
216 assertEquals(endpoint1, manager.getEndpoint(epKey));
219 @SuppressWarnings("unchecked")
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);
236 Collection<EndpointL3Prefix> result = manager.getEndpointsL3PrefixForTenant(tenantId);
237 assertTrue(result.contains(endpointL3Prefix));
240 @SuppressWarnings("unchecked")
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());
253 when(optional.isPresent()).thenReturn(true);
254 Endpoints endpoints = mock(Endpoints.class);
255 when(optional.get()).thenReturn(endpoints);
256 assertEquals(endpoints, manager.getEndpointsFromDataStore());
258 manager = new EndpointManager(null, rpcRegistry, notificationService, executor, switchManager);
259 assertNull(manager.getEndpointsFromDataStore());
263 public void getL3EndpointsTestEndpointsNull() throws Exception {
264 assertNull(manager.getL3Endpoints());
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);
274 assertNull(manager.getL3Endpoints());
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);
285 assertEquals(endpointL3List, manager.getL3Endpoints());
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);
297 NatAddress overlayL3Nat = mock(NatAddress.class);
298 when(endpointL3.getAugmentation(NatAddress.class)).thenReturn(overlayL3Nat);
299 when(overlayL3Nat.getNatAddress()).thenReturn(mock(IpAddress.class));
301 when(endpointL3.getL2Context()).thenReturn(mock(L2BridgeDomainId.class));
302 when(endpointL3.getMacAddress()).thenReturn(mock(MacAddress.class));
304 Map<EndpointKey, EndpointL3> result = manager.getL3EpWithNatByL2Key();
305 assertTrue(result.containsValue(endpointL3));
309 public void getL3EpWithNatByL2KeyTestL3EpsNull() {
310 Map<EndpointKey, EndpointL3> result = manager.getL3EpWithNatByL2Key();
311 assertTrue(result.isEmpty());
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);
323 NatAddress overlayL3Nat = mock(NatAddress.class);
324 when(endpointL3.getAugmentation(NatAddress.class)).thenReturn(overlayL3Nat);
326 when(endpointL3.getL2Context()).thenReturn(mock(L2BridgeDomainId.class));
327 when(endpointL3.getMacAddress()).thenReturn(null);
329 Map<EndpointKey, EndpointL3> result = manager.getL3EpWithNatByL2Key();
330 assertTrue(result.isEmpty());
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);
342 NatAddress overlayL3Nat = mock(NatAddress.class);
343 when(endpointL3.getAugmentation(NatAddress.class)).thenReturn(overlayL3Nat);
345 when(endpointL3.getL2Context()).thenReturn(null);
346 when(endpointL3.getMacAddress()).thenReturn(mock(MacAddress.class));
348 Map<EndpointKey, EndpointL3> result = manager.getL3EpWithNatByL2Key();
349 assertTrue(result.isEmpty());
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());
361 public void getConditionsForEndpoint() {
362 Endpoint endpoint = mock(Endpoint.class);
363 when(endpoint.getCondition()).thenReturn(null);
364 assertTrue(manager.getConditionsForEndpoint(endpoint).isEmpty());
366 List<ConditionName> conditionNameList = Collections.singletonList(mock(ConditionName.class));
367 when(endpoint.getCondition()).thenReturn(conditionNameList);
368 assertEquals(conditionNameList, manager.getConditionsForEndpoint(endpoint));
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));
378 OfOverlayL3Context ofOverlayL3Context = mock(OfOverlayL3Context.class);
379 when(newL3Ep.getAugmentation(OfOverlayL3Context.class)).thenReturn(ofOverlayL3Context);
381 NetworkDomainId networkDomainId = mock(NetworkDomainId.class);
382 when(newL3Ep.getNetworkContainment()).thenReturn(networkDomainId);
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));
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));
400 OfOverlayL3Context ofOverlayL3Context = mock(OfOverlayL3Context.class);
401 when(newL3Ep.getAugmentation(OfOverlayL3Context.class)).thenReturn(ofOverlayL3Context);
403 when(newL3Ep.getNetworkContainment()).thenReturn(null);
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));
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));
421 OfOverlayL3Context ofOverlayL3Context = mock(OfOverlayL3Context.class);
422 when(newL3Ep.getAugmentation(OfOverlayL3Context.class)).thenReturn(ofOverlayL3Context);
424 when(newL3Ep.getNetworkContainment()).thenReturn(null);
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));
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));
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));
452 public void updateEndpointL3TestUpdate() throws Exception {
453 when(oldL3Ep.getIpAddress()).thenReturn(new IpAddress(new Ipv4Address("1.1.1.1")));
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));
460 OfOverlayL3Context ofOverlayL3Context = mock(OfOverlayL3Context.class);
461 when(newL3Ep.getAugmentation(OfOverlayL3Context.class)).thenReturn(ofOverlayL3Context);
463 when(newL3Ep.getNetworkContainment()).thenReturn(null);
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")));
469 manager.processL3Endpoint(null, oldL3Ep);
470 manager.processL3Endpoint(oldL3Ep, newL3Ep);
472 verify(endpointListener).endpointUpdated(any(EpKey.class));
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());
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());
492 public void updateEndpointTestNewLocNullOldLocNullOfOverlayContextAugmentationNull() {
493 when(endpoint1.getAugmentation(OfOverlayContext.class)).thenReturn(null);
494 when(endpoint2.getAugmentation(OfOverlayContext.class)).thenReturn(null);
496 manager.processEndpoint(endpoint1, endpoint2);
497 verify(endpointListener, never()).endpointUpdated(any(EpKey.class));
498 verify(endpointListener, never()).nodeEndpointUpdated(any(NodeId.class), any(EpKey.class));
502 public void updateEndpointTestNewLocNullOldLocNull() {
503 when(context1.getNodeId()).thenReturn(null);
504 when(context2.getNodeId()).thenReturn(null);
506 manager.processEndpoint(endpoint1, endpoint2);
507 verify(endpointListener, never()).endpointUpdated(any(EpKey.class));
508 verify(endpointListener, never()).nodeEndpointUpdated(any(NodeId.class), any(EpKey.class));
512 public void updateEndpointTestNewLocNullOldLocNullExternalPut() {
513 when(endpoint1.getAugmentation(OfOverlayContext.class)).thenReturn(null);
514 when(endpoint2.getAugmentation(OfOverlayContext.class)).thenReturn(null);
516 manager.processEndpoint(endpoint1, endpoint2);
517 verify(endpointListener, never()).endpointUpdated(any(EpKey.class));
518 verify(endpointListener, never()).nodeEndpointUpdated(any(NodeId.class), any(EpKey.class));
522 public void updateEndpointTestNewLocNullOldLocNullExternalRemove() {
523 when(context1.getNodeId()).thenReturn(null);
524 manager.processEndpoint(null, endpoint1);
526 manager.processEndpoint(endpoint1, null);
527 verify(endpointListener, never()).endpointUpdated(any(EpKey.class));
528 verify(endpointListener, never()).nodeEndpointUpdated(any(NodeId.class), any(EpKey.class));
532 * Endpoint changes it's location
535 public void updateEndpointLocationTestUpdate() {
536 Collection<Endpoint> collection;
537 manager.processEndpoint(null, endpoint1);
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());
548 * Endpoint changes it's EPG
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);
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());
568 * Endpoint changes it's location and EPGs
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);
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());
588 * Endpoint becomes external when removing it's location augmentation.
589 * This might happen when an endpoint is removed from a device.
592 public void updateEndpointLocationRemovedTestUpdate() {
593 Collection<Endpoint> collection;
594 when(endpoint2.getAugmentation(OfOverlayContext.class)).thenReturn(null);
595 manager.processEndpoint(null, endpoint1);
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());
605 * Endpoint is created when adding location augmentation.
606 * Endpoint is not external anymore.
609 public void updateEndpointLocationAddedTestUpdate() {
610 Collection<Endpoint> collection;
611 when(endpoint1.getAugmentation(OfOverlayContext.class)).thenReturn(null);
612 manager.processEndpoint(null, endpoint1);
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());
622 public void closeTest() throws Exception {
624 verify(listenerReg, times(3)).close();
632 public void getEgKeyTest() {
633 assertNotNull(manager.getEgKey(endpoint1));
634 assertNull(manager.getEgKey(null));
636 when(endpoint1.getTenant()).thenReturn(null);
637 assertNull(manager.getEgKey(endpoint1));
639 when(endpoint1.getTenant()).thenReturn(tenantId);
640 when(endpoint1.getEndpointGroup()).thenReturn(null);
641 assertNull(manager.getEgKey(endpoint1));
643 when(endpoint1.getEndpointGroup()).thenReturn(endpointGroupId);
644 when(endpoint1.getEndpointGroups()).thenReturn(null);
645 assertNotNull(manager.getEgKey(endpoint1));
647 when(endpoint1.getEndpointGroup()).thenReturn(null);
648 assertNull(manager.getEgKey(endpoint1));
650 when(endpoint1.getEndpointGroup()).thenReturn(endpointGroupId);
651 when(endpoint1.getL2Context()).thenReturn(null);
652 assertNull(manager.getEgKey(endpoint1));
654 when(endpoint1.getL2Context()).thenReturn(l2BridgeDomainId);
655 when(endpoint1.getMacAddress()).thenReturn(null);
656 assertNull(manager.getEgKey(endpoint1));
660 public void getEgKeysForEndpointTest() {
661 Endpoint endpoint = mock(Endpoint.class);
664 when(endpoint.getEndpointGroups()).thenReturn(null);
665 egKeys = manager.getEgKeysForEndpoint(endpoint);
666 assertTrue(egKeys.isEmpty());
668 EndpointGroupId endpointGroupId = mock(EndpointGroupId.class);
669 when(endpoint.getEndpointGroup()).thenReturn(endpointGroupId);
670 egKeys = manager.getEgKeysForEndpoint(endpoint);
671 assertEquals(1, egKeys.size());
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());
681 public void isExternalIsInternalTest() {
682 Endpoint endpoint = mock(Endpoint.class);
683 when(endpoint.getAugmentation(OfOverlayContext.class)).thenReturn(null);
685 // assertFalse(manager.isExternal(endpoint));
686 // assertTrue(manager.isInternal(endpoint));
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));
694 // when(ofc.getLocationType()).thenReturn(LocationType.Internal);
695 // assertFalse(manager.isExternal(endpoint));
696 // assertTrue(manager.isInternal(endpoint));
698 // when(ofc.getLocationType()).thenReturn(LocationType.External);
699 // assertTrue(manager.isExternal(endpoint));
700 // assertFalse(manager.isInternal(endpoint));
704 public void testGetL2EndpointFromL3() {
705 when(newL3Ep.getL2Context()).thenReturn(mock(L2BridgeDomainId.class));
706 when(newL3Ep.getMacAddress()).thenReturn(mock(MacAddress.class));
708 Endpoint ep = manager.getL2EndpointFromL3(newL3Ep);
714 public void testGetL2EndpointFromL3_noL2Context_noMacAddr() {
715 when(newL3Ep.getL2Context()).thenReturn(null);
716 when(newL3Ep.getMacAddress()).thenReturn(null);
718 Endpoint ep = manager.getL2EndpointFromL3(newL3Ep);