2 * Copyright (c) 2016 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.base_endpoint;
11 import static org.mockito.Mockito.mock;
13 import com.google.common.base.Optional;
14 import com.google.common.collect.ImmutableList;
15 import org.junit.Assert;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
19 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
21 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
22 import org.opendaylight.groupbasedpolicy.api.BaseEndpointRendererAugmentation;
23 import org.opendaylight.groupbasedpolicy.test.CustomDataBrokerTest;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.common.endpoint.fields.NetworkContainmentBuilder;
25 import org.opendaylight.groupbasedpolicy.util.DataStoreHelper;
26 import org.opendaylight.groupbasedpolicy.util.IidFactory;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.Endpoints;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.RegisterEndpointInput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.RegisterEndpointInputBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.UnregisterEndpointInput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.UnregisterEndpointInputBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.common.endpoint.fields.network.containment.containment.NetworkDomainContainment;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.common.endpoint.fields.network.containment.containment.NetworkDomainContainmentBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.endpoints.AddressEndpoints;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.endpoints.ContainmentEndpoints;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.endpoints.address.endpoints.AddressEndpoint;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.endpoints.address.endpoints.AddressEndpointBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.endpoints.address.endpoints.AddressEndpointKey;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.endpoints.containment.endpoints.ContainmentEndpoint;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.endpoints.containment.endpoints.ContainmentEndpointBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.endpoints.containment.endpoints.ContainmentEndpointKey;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.child.endpoints.ChildEndpoint;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.child.endpoints.ChildEndpointBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.parent.child.endpoints.ParentEndpointChoice;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.parent.child.endpoints.parent.endpoint.choice.ParentContainmentEndpointCase;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.parent.child.endpoints.parent.endpoint.choice.ParentEndpointCase;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.parent.child.endpoints.parent.endpoint.choice.ParentEndpointCaseBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.parent.child.endpoints.parent.endpoint.choice.parent.endpoint._case.ParentEndpoint;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.parent.child.endpoints.parent.endpoint.choice.parent.endpoint._case.ParentEndpointBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.register.endpoint.input.AddressEndpointRegBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.register.endpoint.input.ContainmentEndpointReg;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.register.endpoint.input.ContainmentEndpointRegBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.unregister.endpoint.input.AddressEndpointUnregBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.unregister.endpoint.input.ContainmentEndpointUnreg;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.unregister.endpoint.input.ContainmentEndpointUnregBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ContextId;
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.forwarding.l2_l3.rev160427.IpPrefixType;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.forwarding.l2_l3.rev160427.L2FloodDomain;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.forwarding.l2_l3.rev160427.L3Context;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.forwarding.l2_l3.rev160427.MacAddressType;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.forwarding.l2_l3.rev160427.Subnet;
64 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
65 import org.opendaylight.yangtools.yang.common.RpcResult;
67 import java.util.ArrayList;
68 import java.util.Collection;
69 import java.util.Collections;
70 import java.util.List;
71 import java.util.concurrent.Future;
73 import javax.annotation.Nonnull;
75 public class BaseEndpointRpcRegistryTest extends CustomDataBrokerTest {
77 private static final String MAC_ADDRESS = "01:23:45:67:89:AB";
78 private static final String IP_ADDRESS = "192.168.100.1/24";
79 private static final String TENANT = "admin";
80 private static final String DOMAIN = "test.domain";
81 private static final String CONTEXT_ID = "testContext";
82 private static final long timestamp = 1234567890L;
84 private enum AddressEndpointRegistration {
85 CHILD, PARENT, BOTH, NONE
87 private enum AddressEndpointType {
91 private DataBroker dataProvider;
92 private BaseEndpointRendererAugmentation baseEndpointRendererAugmentation;
93 private BaseEndpointRpcRegistry baseEndpointRpcRegistry;
94 private AddressEndpoint basel2Ep;
95 private AddressEndpoint basel3Ep;
96 private ContainmentEndpoint baseContainmentEp;
97 private ParentEndpoint basel3Parent;
98 private ChildEndpoint basel2Child;
102 public Collection<Class<?>> getClassesFromModules() {
103 return ImmutableList.of(Endpoints.class, AddressEndpoints.class, ContainmentEndpoints.class,
104 MacAddressType.class, IpPrefixType.class);
109 dataProvider = getDataBroker();
110 baseEndpointRendererAugmentation = mock(BaseEndpointRendererAugmentation.class);
111 RpcProviderRegistry rpcRegistry = mock(RpcProviderRegistry.class);
113 baseEndpointRpcRegistry = new BaseEndpointRpcRegistry(dataProvider, rpcRegistry);
115 NetworkDomainContainment
116 networkDomainContainment =
117 new NetworkDomainContainmentBuilder().setNetworkDomainId(new NetworkDomainId(DOMAIN)).setNetworkDomainType(
118 Subnet.class).build();
120 basel3Parent = new ParentEndpointBuilder().setAddress(IP_ADDRESS)
121 .setAddressType(IpPrefixType.class)
122 .setContextId(new ContextId(CONTEXT_ID))
123 .setContextType(L3Context.class)
126 basel2Child = new ChildEndpointBuilder().setAddress(MAC_ADDRESS)
127 .setAddressType(MacAddressType.class)
128 .setContextId(new ContextId(CONTEXT_ID))
129 .setContextType(L2FloodDomain.class)
132 basel2Ep = new AddressEndpointBuilder().setTimestamp(timestamp)
133 .setContextId(new ContextId(CONTEXT_ID))
134 .setContextType(L2FloodDomain.class)
135 .setTenant(new TenantId(TENANT))
136 .setAddress(MAC_ADDRESS)
137 .setAddressType(MacAddressType.class)
138 .setNetworkContainment(new NetworkContainmentBuilder().setContainment(networkDomainContainment).build())
139 .setTimestamp(timestamp)
140 .setParentEndpointChoice(
141 new ParentEndpointCaseBuilder().setParentEndpoint(Collections.singletonList(basel3Parent)).build())
144 basel3Ep = new AddressEndpointBuilder().setTimestamp(timestamp)
145 .setContextId(new ContextId(CONTEXT_ID))
146 .setContextType(L3Context.class)
147 .setTenant(new TenantId(TENANT))
148 .setAddress(IP_ADDRESS)
149 .setAddressType(IpPrefixType.class)
150 .setNetworkContainment(new NetworkContainmentBuilder().setContainment(networkDomainContainment).build())
151 .setTimestamp(timestamp)
152 .setChildEndpoint(Collections.singletonList(basel2Child))
155 baseContainmentEp = new ContainmentEndpointBuilder().setTimestamp(timestamp)
156 .setContextId(new ContextId(CONTEXT_ID))
157 .setContextType(L2FloodDomain.class)
158 .setTenant(new TenantId(TENANT))
159 .setNetworkContainment(new NetworkContainmentBuilder().setContainment(networkDomainContainment).build())
160 .setChildEndpoint(Collections.singletonList(basel2Child))
165 public void testConstructor() throws Exception {
166 RpcProviderRegistry rpcRegistry = mock(RpcProviderRegistry.class);
167 BaseEndpointRpcRegistry registry = new BaseEndpointRpcRegistry(dataProvider, rpcRegistry);
172 public void testRegister() throws Exception {
173 baseEndpointRpcRegistry.register(baseEndpointRendererAugmentation);
174 Assert.assertEquals(1, BaseEndpointRpcRegistry.registeredRenderers.size());
176 baseEndpointRpcRegistry.unregister(baseEndpointRendererAugmentation);
177 Assert.assertEquals(0, BaseEndpointRpcRegistry.registeredRenderers.size());
181 public void testRegisterEndpoint() throws Exception {
182 RegisterEndpointInput input =
183 createRegisterEndpointInputVariablesForTest(AddressEndpointRegistration.BOTH, AddressEndpointType.BOTH, true);
185 baseEndpointRpcRegistry.registerEndpoint(input);
187 ReadOnlyTransaction transaction = dataProvider.newReadOnlyTransaction();
189 AddressEndpointKey key = new AddressEndpointKey(MAC_ADDRESS, MacAddressType.class, new ContextId(CONTEXT_ID),
190 L2FloodDomain.class);
191 Optional<AddressEndpoint> addressEndpointL2 = DataStoreHelper.readFromDs(LogicalDatastoreType.OPERATIONAL,
192 IidFactory.addressEndpointIid(key), transaction);
194 Assert.assertTrue(addressEndpointL2.isPresent());
196 if (addressEndpointL2.isPresent()) {
197 Assert.assertEquals(basel2Ep, addressEndpointL2.get());
200 key = new AddressEndpointKey(IP_ADDRESS, IpPrefixType.class, new ContextId(CONTEXT_ID), L3Context.class);
202 Optional<AddressEndpoint> addressEndpointL3 = DataStoreHelper.readFromDs(LogicalDatastoreType.OPERATIONAL,
203 IidFactory.addressEndpointIid(key), transaction);
205 Assert.assertTrue(addressEndpointL3.isPresent());
207 if (addressEndpointL3.isPresent()) {
208 Assert.assertEquals(basel3Ep, addressEndpointL3.get());
211 ContainmentEndpointKey containmentEndpointKey =
212 new ContainmentEndpointKey(new ContextId(CONTEXT_ID), L2FloodDomain.class);
214 Optional<ContainmentEndpoint> ContainmentEndpoint = DataStoreHelper.readFromDs(LogicalDatastoreType.OPERATIONAL,
215 IidFactory.containmentEndpointIid(containmentEndpointKey), transaction);
217 Assert.assertTrue(ContainmentEndpoint.isPresent());
219 if (ContainmentEndpoint.isPresent()) {
220 Assert.assertEquals(baseContainmentEp, ContainmentEndpoint.get());
225 public void testRegisterEndpointWithParentUpdate() throws Exception {
226 setupBasicDataStore();
227 RegisterEndpointInput input =
228 createRegisterEndpointInputVariablesForTest(AddressEndpointRegistration.BOTH, AddressEndpointType.L2, true);
230 baseEndpointRpcRegistry.registerEndpoint(input);
232 AddressEndpointKey key = new AddressEndpointKey(MAC_ADDRESS, MacAddressType.class, new ContextId(CONTEXT_ID),
233 L2FloodDomain.class);
234 Optional<AddressEndpoint> addressEndpointL2 = DataStoreHelper.readFromDs(LogicalDatastoreType.OPERATIONAL,
235 IidFactory.addressEndpointIid(key), dataProvider.newReadOnlyTransaction());
237 Assert.assertTrue(addressEndpointL2.isPresent());
239 if (addressEndpointL2.isPresent()) {
240 ParentEndpointCase parentEndpointCase =
241 (ParentEndpointCase) addressEndpointL2.get().getParentEndpointChoice();
242 List<ParentEndpoint> parentEndpoints = parentEndpointCase.getParentEndpoint();
243 Assert.assertEquals(parentEndpoints.size(), 1);
246 key = new AddressEndpointKey(IP_ADDRESS, IpPrefixType.class, new ContextId(CONTEXT_ID), L3Context.class);
248 Optional<AddressEndpoint> addressEndpointL3 = DataStoreHelper.readFromDs(LogicalDatastoreType.OPERATIONAL,
249 IidFactory.addressEndpointIid(key), dataProvider.newReadOnlyTransaction());
251 Assert.assertTrue(addressEndpointL3.isPresent());
253 if (addressEndpointL3.isPresent()) {
254 Assert.assertEquals(addressEndpointL3.get().getChildEndpoint().size(), 1);
259 public void testRegisterEndpointWithParentUpdateFail() throws Exception {
260 RegisterEndpointInput input = createRegisterEndpointInputVariablesForTest(AddressEndpointRegistration.BOTH,
261 AddressEndpointType.L2, true);
263 Future<RpcResult<Void>> rpcResultFuture = baseEndpointRpcRegistry.registerEndpoint(input);
265 Assert.assertFalse(rpcResultFuture.get().isSuccessful());
266 Assert.assertNotNull(rpcResultFuture.get().getErrors());
267 Assert.assertEquals(rpcResultFuture.get().getErrors().size(), 1);
271 public void testRegisterEndpointWithChildUpdate() throws Exception {
272 setupBasicDataStore();
273 RegisterEndpointInput input =
274 createRegisterEndpointInputVariablesForTest(AddressEndpointRegistration.BOTH, AddressEndpointType.L3, true);
276 baseEndpointRpcRegistry.registerEndpoint(input);
278 AddressEndpointKey key = new AddressEndpointKey(MAC_ADDRESS, MacAddressType.class, new ContextId(CONTEXT_ID),
279 L2FloodDomain.class);
280 Optional<AddressEndpoint> addressEndpointL2 = DataStoreHelper.readFromDs(LogicalDatastoreType.OPERATIONAL,
281 IidFactory.addressEndpointIid(key), dataProvider.newReadOnlyTransaction());
283 Assert.assertTrue(addressEndpointL2.isPresent());
285 if (addressEndpointL2.isPresent()) {
286 ParentEndpointCase parentEndpointCase =
287 (ParentEndpointCase) addressEndpointL2.get().getParentEndpointChoice();
288 List<ParentEndpoint> parentEndpoints = parentEndpointCase.getParentEndpoint();
289 Assert.assertEquals(parentEndpoints.size(), 1);
292 key = new AddressEndpointKey(IP_ADDRESS, IpPrefixType.class, new ContextId(CONTEXT_ID), L3Context.class);
294 Optional<AddressEndpoint> addressEndpointL3 = DataStoreHelper.readFromDs(LogicalDatastoreType.OPERATIONAL,
295 IidFactory.addressEndpointIid(key), dataProvider.newReadOnlyTransaction());
297 Assert.assertTrue(addressEndpointL3.isPresent());
299 if (addressEndpointL3.isPresent()) {
300 Assert.assertEquals(addressEndpointL3.get().getChildEndpoint().size(), 1);
305 public void testRegisterEndpointWithChildUpdateFail() throws Exception {
306 RegisterEndpointInput input = createRegisterEndpointInputVariablesForTest(AddressEndpointRegistration.BOTH,
307 AddressEndpointType.L3, true);
309 Future<RpcResult<Void>> rpcResultFuture = baseEndpointRpcRegistry.registerEndpoint(input);
311 Assert.assertFalse(rpcResultFuture.get().isSuccessful());
312 Assert.assertNotNull(rpcResultFuture.get().getErrors());
313 Assert.assertEquals(rpcResultFuture.get().getErrors().size(), 1);
316 private void setupBasicDataStore() throws Exception {
317 InstanceIdentifier<Endpoints> id = InstanceIdentifier.builder(Endpoints.class).build();
318 dataProvider.newWriteOnlyTransaction().delete(LogicalDatastoreType.OPERATIONAL, id);
320 RegisterEndpointInput input =
321 createRegisterEndpointInputVariablesForTest(AddressEndpointRegistration.NONE, AddressEndpointType.BOTH, true);
323 baseEndpointRpcRegistry.registerEndpoint(input);
325 AddressEndpointKey key = new AddressEndpointKey(MAC_ADDRESS, MacAddressType.class, new ContextId(CONTEXT_ID),
326 L2FloodDomain.class);
327 Optional<AddressEndpoint> addressEndpointL2 = DataStoreHelper.readFromDs(LogicalDatastoreType.OPERATIONAL,
328 IidFactory.addressEndpointIid(key), dataProvider.newReadOnlyTransaction());
330 Assert.assertTrue(addressEndpointL2.isPresent());
332 if (addressEndpointL2.isPresent()) {
333 ParentEndpointCase parentEndpointCase =
334 (ParentEndpointCase) addressEndpointL2.get().getParentEndpointChoice();
335 Assert.assertEquals(parentEndpointCase.getParentEndpoint().size(), 0);
338 key = new AddressEndpointKey(IP_ADDRESS, IpPrefixType.class, new ContextId(CONTEXT_ID), L3Context.class);
340 Optional<AddressEndpoint> addressEndpointL3 = DataStoreHelper.readFromDs(LogicalDatastoreType.OPERATIONAL,
341 IidFactory.addressEndpointIid(key), dataProvider.newReadOnlyTransaction());
343 Assert.assertTrue(addressEndpointL3.isPresent());
345 if (addressEndpointL3.isPresent()) {
346 Assert.assertEquals(addressEndpointL3.get().getChildEndpoint().size(), 0);
351 public void testRegisterEndpointParentFail() throws Exception {
352 Future<RpcResult<Void>> rpcResultFuture =
353 baseEndpointRpcRegistry.registerEndpoint(createRegisterEndpointInputVariablesForTest(
354 AddressEndpointRegistration.PARENT, AddressEndpointType.BOTH, true));
356 RpcResult<Void> rpcResult = rpcResultFuture.get();
358 Assert.assertFalse(rpcResult.isSuccessful());
359 Assert.assertNull(rpcResult.getResult());
360 Assert.assertEquals(rpcResult.getErrors().size(), 1);
365 public void testRegisterEndpointChildFail() throws Exception {
366 Future<RpcResult<Void>> rpcResultFuture =
367 baseEndpointRpcRegistry.registerEndpoint(createRegisterEndpointInputVariablesForTest(
368 AddressEndpointRegistration.CHILD, AddressEndpointType.BOTH, true));
370 RpcResult<Void> rpcResult = rpcResultFuture.get();
372 Assert.assertFalse(rpcResult.isSuccessful());
373 Assert.assertNull(rpcResult.getResult());
374 Assert.assertEquals(rpcResult.getErrors().size(), 1);
378 public void testUnregisterEndpointWithParent() throws Exception {
379 RegisterEndpointInput input =
380 createRegisterEndpointInputVariablesForTest(AddressEndpointRegistration.BOTH, AddressEndpointType.BOTH, true);
382 baseEndpointRpcRegistry.registerEndpoint(input);
384 AddressEndpointKey key = new AddressEndpointKey(MAC_ADDRESS, MacAddressType.class, new ContextId(CONTEXT_ID),
385 L2FloodDomain.class);
386 ContainmentEndpointKey cKey = new ContainmentEndpointKey(new ContextId(CONTEXT_ID), L2FloodDomain.class);
388 Optional<AddressEndpoint> addressEndpointL2 = DataStoreHelper.readFromDs(LogicalDatastoreType.OPERATIONAL,
389 IidFactory.addressEndpointIid(key), dataProvider.newReadOnlyTransaction());
391 Assert.assertTrue(addressEndpointL2.isPresent());
393 if (addressEndpointL2.isPresent()) {
394 Assert.assertEquals(basel2Ep, addressEndpointL2.get());
397 Optional<ContainmentEndpoint> ContainmentEndpoint = DataStoreHelper.readFromDs(LogicalDatastoreType.OPERATIONAL,
398 IidFactory.containmentEndpointIid(cKey), dataProvider.newReadOnlyTransaction());
400 Assert.assertTrue(ContainmentEndpoint.isPresent());
402 if (ContainmentEndpoint.isPresent()) {
403 Assert.assertEquals(baseContainmentEp, ContainmentEndpoint.get());
406 baseEndpointRpcRegistry.unregisterEndpoint(unregisterEndpointInputParent());
407 Optional<AddressEndpoint> endpointOptional = DataStoreHelper.readFromDs(LogicalDatastoreType.OPERATIONAL,
408 IidFactory.addressEndpointIid(key), dataProvider.newReadOnlyTransaction());
410 Assert.assertFalse(endpointOptional.isPresent());
412 Optional<ContainmentEndpoint> containmentEndpointOptional =
413 DataStoreHelper.readFromDs(LogicalDatastoreType.OPERATIONAL, IidFactory.containmentEndpointIid(cKey),
414 dataProvider.newReadOnlyTransaction());
416 Assert.assertFalse(containmentEndpointOptional.isPresent());
420 public void testUnregisterEndpointWithChild() throws Exception {
421 RegisterEndpointInput input =
422 createRegisterEndpointInputVariablesForTest(AddressEndpointRegistration.BOTH, AddressEndpointType.BOTH, true);
424 baseEndpointRpcRegistry.registerEndpoint(input);
426 AddressEndpointKey key =
427 new AddressEndpointKey(IP_ADDRESS, IpPrefixType.class, new ContextId(CONTEXT_ID), L3Context.class);
429 Optional<AddressEndpoint> addressEndpointL3 = DataStoreHelper.readFromDs(LogicalDatastoreType.OPERATIONAL,
430 IidFactory.addressEndpointIid(key), dataProvider.newReadOnlyTransaction());
432 Assert.assertTrue(addressEndpointL3.isPresent());
434 if (addressEndpointL3.isPresent()) {
435 Assert.assertEquals(basel3Ep, addressEndpointL3.get());
438 baseEndpointRpcRegistry.unregisterEndpoint(unregisterEndpointInputChild());
439 Optional<AddressEndpoint> endpointOptional = DataStoreHelper.readFromDs(LogicalDatastoreType.OPERATIONAL,
440 IidFactory.addressEndpointIid(key), dataProvider.newReadOnlyTransaction());
442 Assert.assertFalse(endpointOptional.isPresent());
447 public void testRegisterContainmentEndpointWithChildFail() throws Exception {
448 Future<RpcResult<Void>> rpcResultFuture =
449 baseEndpointRpcRegistry.registerEndpoint(createRegisterEndpointInputVariablesForTest(
450 AddressEndpointRegistration.NONE, AddressEndpointType.NONE, true));
452 RpcResult<Void> rpcResult = rpcResultFuture.get();
454 Assert.assertFalse(rpcResult.isSuccessful());
455 Assert.assertNull(rpcResult.getResult());
456 Assert.assertEquals(rpcResult.getErrors().size(), 1);
460 public void testUnregisterContainmentEndpointWithChild() throws Exception {
461 InstanceIdentifier<Endpoints> id = InstanceIdentifier.builder(Endpoints.class).build();
462 dataProvider.newWriteOnlyTransaction().delete(LogicalDatastoreType.OPERATIONAL, id);
464 RegisterEndpointInput input =
465 createRegisterEndpointInputVariablesForTest(AddressEndpointRegistration.NONE, AddressEndpointType.L2, false);
467 baseEndpointRpcRegistry.registerEndpoint(input);
469 AddressEndpointKey key = new AddressEndpointKey(MAC_ADDRESS, MacAddressType.class, new ContextId(CONTEXT_ID),
470 L2FloodDomain.class);
472 Optional<AddressEndpoint> addressEndpointL2 = DataStoreHelper.readFromDs(LogicalDatastoreType.OPERATIONAL,
473 IidFactory.addressEndpointIid(key), dataProvider.newReadOnlyTransaction());
475 Assert.assertTrue(addressEndpointL2.isPresent());
476 if (addressEndpointL2.isPresent()){
477 ParentEndpointChoice parentEndpointChoice = addressEndpointL2.get().getParentEndpointChoice();
478 if(parentEndpointChoice instanceof ParentContainmentEndpointCase){
479 ParentContainmentEndpointCase
480 parentEndpointCase = (ParentContainmentEndpointCase) parentEndpointChoice;
481 Assert.assertNull(parentEndpointCase.getParentContainmentEndpoint());
485 ContainmentEndpointReg containmentEndpointReg = new ContainmentEndpointRegBuilder().setTimestamp(baseContainmentEp.getTimestamp())
486 .setContextId(baseContainmentEp.getContextId())
487 .setContextType(baseContainmentEp.getContextType())
488 .setTenant(baseContainmentEp.getTenant())
489 .setNetworkContainment(baseContainmentEp.getNetworkContainment())
490 .setChildEndpoint(Collections.singletonList(basel2Child))
493 baseEndpointRpcRegistry.registerEndpoint(new RegisterEndpointInputBuilder().setContainmentEndpointReg(Collections.singletonList(containmentEndpointReg)).build());
495 addressEndpointL2 = DataStoreHelper.readFromDs(LogicalDatastoreType.OPERATIONAL,
496 IidFactory.addressEndpointIid(key), dataProvider.newReadOnlyTransaction());
498 Assert.assertTrue(addressEndpointL2.isPresent());
499 if (addressEndpointL2.isPresent()) {
500 ParentEndpointChoice parentEndpointChoice = addressEndpointL2.get().getParentEndpointChoice();
501 if(parentEndpointChoice instanceof ParentContainmentEndpointCase){
502 ParentContainmentEndpointCase
503 parentEndpointCase = (ParentContainmentEndpointCase) parentEndpointChoice;
504 Assert.assertNotNull(parentEndpointCase.getParentContainmentEndpoint());
505 Assert.assertEquals(parentEndpointCase.getParentContainmentEndpoint().size(),1);
509 ContainmentEndpointUnreg containmentEndpointUnreg = new ContainmentEndpointUnregBuilder()
510 .setContextId(baseContainmentEp.getContextId())
511 .setContextType(baseContainmentEp.getContextType())
514 baseEndpointRpcRegistry.unregisterEndpoint(new UnregisterEndpointInputBuilder().setContainmentEndpointUnreg(Collections.singletonList(containmentEndpointUnreg)).build());
516 addressEndpointL2 = DataStoreHelper.readFromDs(LogicalDatastoreType.OPERATIONAL,
517 IidFactory.addressEndpointIid(key), dataProvider.newReadOnlyTransaction());
519 Assert.assertTrue(addressEndpointL2.isPresent());
520 if (addressEndpointL2.isPresent()) {
521 ParentEndpointChoice parentEndpointChoice = addressEndpointL2.get().getParentEndpointChoice();
522 if(parentEndpointChoice instanceof ParentContainmentEndpointCase){
523 ParentContainmentEndpointCase
524 parentEndpointCase = (ParentContainmentEndpointCase) parentEndpointChoice;
525 Assert.assertNotNull(parentEndpointCase.getParentContainmentEndpoint());
526 Assert.assertEquals(parentEndpointCase.getParentContainmentEndpoint().size(),0);
532 private UnregisterEndpointInput unregisterEndpointInputParent() {
533 UnregisterEndpointInputBuilder builder = new UnregisterEndpointInputBuilder();
535 builder.setAddressEndpointUnreg(new ArrayList<>());
536 builder.setContainmentEndpointUnreg(new ArrayList<>());
538 builder.getAddressEndpointUnreg().add(new AddressEndpointUnregBuilder().setContextId(new ContextId(CONTEXT_ID))
539 .setContextType(L2FloodDomain.class)
540 .setAddress(MAC_ADDRESS)
541 .setAddressType(MacAddressType.class)
544 builder.getContainmentEndpointUnreg().add(new ContainmentEndpointUnregBuilder()
545 .setContextId(new ContextId(CONTEXT_ID)).setContextType(L2FloodDomain.class).build());
547 return builder.build();
550 private UnregisterEndpointInput unregisterEndpointInputChild() {
551 UnregisterEndpointInputBuilder builder = new UnregisterEndpointInputBuilder();
553 builder.setAddressEndpointUnreg(new ArrayList<>());
554 builder.setContainmentEndpointUnreg(new ArrayList<>());
556 builder.getAddressEndpointUnreg().add(new AddressEndpointUnregBuilder().setContextId(new ContextId(CONTEXT_ID))
557 .setContextType(L3Context.class)
558 .setAddress(IP_ADDRESS)
559 .setAddressType(IpPrefixType.class)
562 return builder.build();
565 private RegisterEndpointInput createRegisterEndpointInputVariablesForTest(AddressEndpointRegistration registration,
566 AddressEndpointType type, boolean containmentEpPresent) throws Exception {
567 RegisterEndpointInputBuilder registerEndpointInputBuilder = new RegisterEndpointInputBuilder();
568 long timestamp = System.currentTimeMillis();
570 List<ParentEndpoint> parentEndpoints = new ArrayList<>();
571 if (registration == AddressEndpointRegistration.BOTH || registration == AddressEndpointRegistration.PARENT) {
572 parentEndpoints.add(basel3Parent);
575 List<ChildEndpoint> childEndpoints = new ArrayList<>();
576 if (registration == AddressEndpointRegistration.BOTH || registration == AddressEndpointRegistration.CHILD) {
577 childEndpoints.add(basel2Child);
580 registerEndpointInputBuilder.setAddressEndpointReg(new ArrayList<>());
581 registerEndpointInputBuilder.setContainmentEndpointReg(new ArrayList<>());
583 if (type == AddressEndpointType.BOTH || type == AddressEndpointType.L2) {
584 registerEndpointInputBuilder.getAddressEndpointReg()
585 .add(new AddressEndpointRegBuilder().setTimestamp(timestamp)
586 .setContextId(basel2Ep.getContextId())
587 .setContextType(basel2Ep.getContextType())
588 .setTenant(basel2Ep.getTenant())
589 .setAddress(basel2Ep.getAddress())
590 .setAddressType(basel2Ep.getAddressType())
591 .setNetworkContainment(basel2Ep.getNetworkContainment())
592 .setTimestamp(basel2Ep.getTimestamp())
593 .setParentEndpointChoice(new ParentEndpointCaseBuilder().setParentEndpoint(parentEndpoints).build())
597 if (type == AddressEndpointType.BOTH || type == AddressEndpointType.L3) {
598 registerEndpointInputBuilder.getAddressEndpointReg()
599 .add(new AddressEndpointRegBuilder().setContextId(basel3Ep.getContextId())
600 .setContextType(basel3Ep.getContextType())
601 .setTenant(basel3Ep.getTenant())
602 .setAddress(basel3Ep.getAddress())
603 .setAddressType(basel3Ep.getAddressType())
604 .setNetworkContainment(basel3Ep.getNetworkContainment())
605 .setTimestamp(basel3Ep.getTimestamp())
606 .setChildEndpoint(childEndpoints)
610 if(containmentEpPresent) {
611 registerEndpointInputBuilder.getContainmentEndpointReg()
612 .add(new ContainmentEndpointRegBuilder().setTimestamp(baseContainmentEp.getTimestamp())
613 .setContextId(baseContainmentEp.getContextId())
614 .setContextType(baseContainmentEp.getContextType())
615 .setTenant(baseContainmentEp.getTenant())
616 .setNetworkContainment(baseContainmentEp.getNetworkContainment())
617 .setChildEndpoint(Collections.singletonList(basel2Child))
621 return registerEndpointInputBuilder.build();