b83bcc5417f6b05d7fc8729ad64a82d18eeedaf4
[groupbasedpolicy.git] / groupbasedpolicy / src / test / java / org / opendaylight / groupbasedpolicy / base_endpoint / BaseEndpointRpcRegistryTest.java
1 /*
2  * Copyright (c) 2016 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.base_endpoint;
10
11 import static org.mockito.Mockito.mock;
12
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;
66
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;
72
73 import javax.annotation.Nonnull;
74
75 public class BaseEndpointRpcRegistryTest extends CustomDataBrokerTest {
76
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;
83
84     private enum AddressEndpointRegistration {
85         CHILD, PARENT, BOTH, NONE
86     }
87     private enum AddressEndpointType {
88         L2, L3, BOTH, NONE
89     }
90
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;
99
100     @Nonnull
101     @Override
102     public Collection<Class<?>> getClassesFromModules() {
103         return ImmutableList.of(Endpoints.class, AddressEndpoints.class, ContainmentEndpoints.class,
104                 MacAddressType.class, IpPrefixType.class);
105     }
106
107     @Before
108     public void init() {
109         dataProvider = getDataBroker();
110         baseEndpointRendererAugmentation = mock(BaseEndpointRendererAugmentation.class);
111         RpcProviderRegistry rpcRegistry = mock(RpcProviderRegistry.class);
112
113         baseEndpointRpcRegistry = new BaseEndpointRpcRegistry(dataProvider, rpcRegistry);
114
115         NetworkDomainContainment
116             networkDomainContainment =
117             new NetworkDomainContainmentBuilder().setNetworkDomainId(new NetworkDomainId(DOMAIN)).setNetworkDomainType(
118                 Subnet.class).build();
119
120         basel3Parent = new ParentEndpointBuilder().setAddress(IP_ADDRESS)
121             .setAddressType(IpPrefixType.class)
122             .setContextId(new ContextId(CONTEXT_ID))
123             .setContextType(L3Context.class)
124             .build();
125
126         basel2Child = new ChildEndpointBuilder().setAddress(MAC_ADDRESS)
127             .setAddressType(MacAddressType.class)
128             .setContextId(new ContextId(CONTEXT_ID))
129             .setContextType(L2FloodDomain.class)
130             .build();
131
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())
142             .build();
143
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))
153             .build();
154
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))
161             .build();
162     }
163
164     @Test
165     public void testConstructor() throws Exception {
166         RpcProviderRegistry rpcRegistry = mock(RpcProviderRegistry.class);
167         BaseEndpointRpcRegistry registry = new BaseEndpointRpcRegistry(dataProvider, rpcRegistry);
168         registry.close();
169     }
170
171     @Test
172     public void testRegister() throws Exception {
173         baseEndpointRpcRegistry.register(baseEndpointRendererAugmentation);
174         Assert.assertEquals(1, BaseEndpointRpcRegistry.registeredRenderers.size());
175
176         baseEndpointRpcRegistry.unregister(baseEndpointRendererAugmentation);
177         Assert.assertEquals(0, BaseEndpointRpcRegistry.registeredRenderers.size());
178     }
179
180     @Test
181     public void testRegisterEndpoint() throws Exception {
182         RegisterEndpointInput input =
183                 createRegisterEndpointInputVariablesForTest(AddressEndpointRegistration.BOTH, AddressEndpointType.BOTH, true);
184
185         baseEndpointRpcRegistry.registerEndpoint(input);
186
187         ReadOnlyTransaction transaction = dataProvider.newReadOnlyTransaction();
188
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);
193
194         Assert.assertTrue(addressEndpointL2.isPresent());
195
196         if (addressEndpointL2.isPresent()) {
197             Assert.assertEquals(basel2Ep, addressEndpointL2.get());
198         }
199
200         key = new AddressEndpointKey(IP_ADDRESS, IpPrefixType.class, new ContextId(CONTEXT_ID), L3Context.class);
201
202         Optional<AddressEndpoint> addressEndpointL3 = DataStoreHelper.readFromDs(LogicalDatastoreType.OPERATIONAL,
203                 IidFactory.addressEndpointIid(key), transaction);
204
205         Assert.assertTrue(addressEndpointL3.isPresent());
206
207         if (addressEndpointL3.isPresent()) {
208             Assert.assertEquals(basel3Ep, addressEndpointL3.get());
209         }
210
211         ContainmentEndpointKey containmentEndpointKey =
212                 new ContainmentEndpointKey(new ContextId(CONTEXT_ID), L2FloodDomain.class);
213
214         Optional<ContainmentEndpoint> ContainmentEndpoint = DataStoreHelper.readFromDs(LogicalDatastoreType.OPERATIONAL,
215                 IidFactory.containmentEndpointIid(containmentEndpointKey), transaction);
216
217         Assert.assertTrue(ContainmentEndpoint.isPresent());
218
219         if (ContainmentEndpoint.isPresent()) {
220             Assert.assertEquals(baseContainmentEp, ContainmentEndpoint.get());
221         }
222     }
223
224     @Test
225     public void testRegisterEndpointWithParentUpdate() throws Exception {
226         setupBasicDataStore();
227         RegisterEndpointInput input =
228                 createRegisterEndpointInputVariablesForTest(AddressEndpointRegistration.BOTH, AddressEndpointType.L2, true);
229
230         baseEndpointRpcRegistry.registerEndpoint(input);
231
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());
236
237         Assert.assertTrue(addressEndpointL2.isPresent());
238
239         if (addressEndpointL2.isPresent()) {
240             ParentEndpointCase parentEndpointCase =
241                     (ParentEndpointCase) addressEndpointL2.get().getParentEndpointChoice();
242             List<ParentEndpoint> parentEndpoints = parentEndpointCase.getParentEndpoint();
243             Assert.assertEquals(parentEndpoints.size(), 1);
244         }
245
246         key = new AddressEndpointKey(IP_ADDRESS, IpPrefixType.class, new ContextId(CONTEXT_ID), L3Context.class);
247
248         Optional<AddressEndpoint> addressEndpointL3 = DataStoreHelper.readFromDs(LogicalDatastoreType.OPERATIONAL,
249                 IidFactory.addressEndpointIid(key), dataProvider.newReadOnlyTransaction());
250
251         Assert.assertTrue(addressEndpointL3.isPresent());
252
253         if (addressEndpointL3.isPresent()) {
254             Assert.assertEquals(addressEndpointL3.get().getChildEndpoint().size(), 1);
255         }
256     }
257
258     @Test
259     public void testRegisterEndpointWithParentUpdateFail() throws Exception {
260         RegisterEndpointInput input = createRegisterEndpointInputVariablesForTest(AddressEndpointRegistration.BOTH,
261                 AddressEndpointType.L2, true);
262
263         Future<RpcResult<Void>> rpcResultFuture = baseEndpointRpcRegistry.registerEndpoint(input);
264
265         Assert.assertFalse(rpcResultFuture.get().isSuccessful());
266         Assert.assertNotNull(rpcResultFuture.get().getErrors());
267         Assert.assertEquals(rpcResultFuture.get().getErrors().size(), 1);
268     }
269
270     @Test
271     public void testRegisterEndpointWithChildUpdate() throws Exception {
272         setupBasicDataStore();
273         RegisterEndpointInput input =
274                 createRegisterEndpointInputVariablesForTest(AddressEndpointRegistration.BOTH, AddressEndpointType.L3, true);
275
276         baseEndpointRpcRegistry.registerEndpoint(input);
277
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());
282
283         Assert.assertTrue(addressEndpointL2.isPresent());
284
285         if (addressEndpointL2.isPresent()) {
286             ParentEndpointCase parentEndpointCase =
287                     (ParentEndpointCase) addressEndpointL2.get().getParentEndpointChoice();
288             List<ParentEndpoint> parentEndpoints = parentEndpointCase.getParentEndpoint();
289             Assert.assertEquals(parentEndpoints.size(), 1);
290         }
291
292         key = new AddressEndpointKey(IP_ADDRESS, IpPrefixType.class, new ContextId(CONTEXT_ID), L3Context.class);
293
294         Optional<AddressEndpoint> addressEndpointL3 = DataStoreHelper.readFromDs(LogicalDatastoreType.OPERATIONAL,
295                 IidFactory.addressEndpointIid(key), dataProvider.newReadOnlyTransaction());
296
297         Assert.assertTrue(addressEndpointL3.isPresent());
298
299         if (addressEndpointL3.isPresent()) {
300             Assert.assertEquals(addressEndpointL3.get().getChildEndpoint().size(), 1);
301         }
302     }
303
304     @Test
305     public void testRegisterEndpointWithChildUpdateFail() throws Exception {
306         RegisterEndpointInput input = createRegisterEndpointInputVariablesForTest(AddressEndpointRegistration.BOTH,
307                 AddressEndpointType.L3, true);
308
309         Future<RpcResult<Void>> rpcResultFuture = baseEndpointRpcRegistry.registerEndpoint(input);
310
311         Assert.assertFalse(rpcResultFuture.get().isSuccessful());
312         Assert.assertNotNull(rpcResultFuture.get().getErrors());
313         Assert.assertEquals(rpcResultFuture.get().getErrors().size(), 1);
314     }
315
316     private void setupBasicDataStore() throws Exception {
317         InstanceIdentifier<Endpoints> id = InstanceIdentifier.builder(Endpoints.class).build();
318         dataProvider.newWriteOnlyTransaction().delete(LogicalDatastoreType.OPERATIONAL, id);
319
320         RegisterEndpointInput input =
321                 createRegisterEndpointInputVariablesForTest(AddressEndpointRegistration.NONE, AddressEndpointType.BOTH, true);
322
323         baseEndpointRpcRegistry.registerEndpoint(input);
324
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());
329
330         Assert.assertTrue(addressEndpointL2.isPresent());
331
332         if (addressEndpointL2.isPresent()) {
333             ParentEndpointCase parentEndpointCase =
334                     (ParentEndpointCase) addressEndpointL2.get().getParentEndpointChoice();
335             Assert.assertEquals(parentEndpointCase.getParentEndpoint().size(), 0);
336         }
337
338         key = new AddressEndpointKey(IP_ADDRESS, IpPrefixType.class, new ContextId(CONTEXT_ID), L3Context.class);
339
340         Optional<AddressEndpoint> addressEndpointL3 = DataStoreHelper.readFromDs(LogicalDatastoreType.OPERATIONAL,
341                 IidFactory.addressEndpointIid(key), dataProvider.newReadOnlyTransaction());
342
343         Assert.assertTrue(addressEndpointL3.isPresent());
344
345         if (addressEndpointL3.isPresent()) {
346             Assert.assertEquals(addressEndpointL3.get().getChildEndpoint().size(), 0);
347         }
348     }
349
350     @Test
351     public void testRegisterEndpointParentFail() throws Exception {
352         Future<RpcResult<Void>> rpcResultFuture =
353                 baseEndpointRpcRegistry.registerEndpoint(createRegisterEndpointInputVariablesForTest(
354                         AddressEndpointRegistration.PARENT, AddressEndpointType.BOTH, true));
355
356         RpcResult<Void> rpcResult = rpcResultFuture.get();
357
358         Assert.assertFalse(rpcResult.isSuccessful());
359         Assert.assertNull(rpcResult.getResult());
360         Assert.assertEquals(rpcResult.getErrors().size(), 1);
361
362     }
363
364     @Test
365     public void testRegisterEndpointChildFail() throws Exception {
366         Future<RpcResult<Void>> rpcResultFuture =
367                 baseEndpointRpcRegistry.registerEndpoint(createRegisterEndpointInputVariablesForTest(
368                         AddressEndpointRegistration.CHILD, AddressEndpointType.BOTH, true));
369
370         RpcResult<Void> rpcResult = rpcResultFuture.get();
371
372         Assert.assertFalse(rpcResult.isSuccessful());
373         Assert.assertNull(rpcResult.getResult());
374         Assert.assertEquals(rpcResult.getErrors().size(), 1);
375     }
376
377     @Test
378     public void testUnregisterEndpointWithParent() throws Exception {
379         RegisterEndpointInput input =
380                 createRegisterEndpointInputVariablesForTest(AddressEndpointRegistration.BOTH, AddressEndpointType.BOTH, true);
381
382         baseEndpointRpcRegistry.registerEndpoint(input);
383
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);
387
388         Optional<AddressEndpoint> addressEndpointL2 = DataStoreHelper.readFromDs(LogicalDatastoreType.OPERATIONAL,
389                 IidFactory.addressEndpointIid(key), dataProvider.newReadOnlyTransaction());
390
391         Assert.assertTrue(addressEndpointL2.isPresent());
392
393         if (addressEndpointL2.isPresent()) {
394             Assert.assertEquals(basel2Ep, addressEndpointL2.get());
395         }
396
397         Optional<ContainmentEndpoint> ContainmentEndpoint = DataStoreHelper.readFromDs(LogicalDatastoreType.OPERATIONAL,
398                 IidFactory.containmentEndpointIid(cKey), dataProvider.newReadOnlyTransaction());
399
400         Assert.assertTrue(ContainmentEndpoint.isPresent());
401
402         if (ContainmentEndpoint.isPresent()) {
403             Assert.assertEquals(baseContainmentEp, ContainmentEndpoint.get());
404         }
405
406         baseEndpointRpcRegistry.unregisterEndpoint(unregisterEndpointInputParent());
407         Optional<AddressEndpoint> endpointOptional = DataStoreHelper.readFromDs(LogicalDatastoreType.OPERATIONAL,
408                 IidFactory.addressEndpointIid(key), dataProvider.newReadOnlyTransaction());
409
410         Assert.assertFalse(endpointOptional.isPresent());
411
412         Optional<ContainmentEndpoint> containmentEndpointOptional =
413                 DataStoreHelper.readFromDs(LogicalDatastoreType.OPERATIONAL, IidFactory.containmentEndpointIid(cKey),
414                         dataProvider.newReadOnlyTransaction());
415
416         Assert.assertFalse(containmentEndpointOptional.isPresent());
417     }
418
419     @Test
420     public void testUnregisterEndpointWithChild() throws Exception {
421         RegisterEndpointInput input =
422                 createRegisterEndpointInputVariablesForTest(AddressEndpointRegistration.BOTH, AddressEndpointType.BOTH, true);
423
424         baseEndpointRpcRegistry.registerEndpoint(input);
425
426         AddressEndpointKey key =
427                 new AddressEndpointKey(IP_ADDRESS, IpPrefixType.class, new ContextId(CONTEXT_ID), L3Context.class);
428
429         Optional<AddressEndpoint> addressEndpointL3 = DataStoreHelper.readFromDs(LogicalDatastoreType.OPERATIONAL,
430                 IidFactory.addressEndpointIid(key), dataProvider.newReadOnlyTransaction());
431
432         Assert.assertTrue(addressEndpointL3.isPresent());
433
434         if (addressEndpointL3.isPresent()) {
435             Assert.assertEquals(basel3Ep, addressEndpointL3.get());
436         }
437
438         baseEndpointRpcRegistry.unregisterEndpoint(unregisterEndpointInputChild());
439         Optional<AddressEndpoint> endpointOptional = DataStoreHelper.readFromDs(LogicalDatastoreType.OPERATIONAL,
440                 IidFactory.addressEndpointIid(key), dataProvider.newReadOnlyTransaction());
441
442         Assert.assertFalse(endpointOptional.isPresent());
443
444     }
445
446     @Test
447     public void testRegisterContainmentEndpointWithChildFail() throws Exception {
448         Future<RpcResult<Void>> rpcResultFuture =
449                 baseEndpointRpcRegistry.registerEndpoint(createRegisterEndpointInputVariablesForTest(
450                         AddressEndpointRegistration.NONE, AddressEndpointType.NONE, true));
451
452         RpcResult<Void> rpcResult = rpcResultFuture.get();
453
454         Assert.assertFalse(rpcResult.isSuccessful());
455         Assert.assertNull(rpcResult.getResult());
456         Assert.assertEquals(rpcResult.getErrors().size(), 1);
457     }
458
459     @Test
460     public void testUnregisterContainmentEndpointWithChild() throws Exception {
461         InstanceIdentifier<Endpoints> id = InstanceIdentifier.builder(Endpoints.class).build();
462         dataProvider.newWriteOnlyTransaction().delete(LogicalDatastoreType.OPERATIONAL, id);
463
464         RegisterEndpointInput input =
465             createRegisterEndpointInputVariablesForTest(AddressEndpointRegistration.NONE, AddressEndpointType.L2, false);
466
467         baseEndpointRpcRegistry.registerEndpoint(input);
468
469         AddressEndpointKey key = new AddressEndpointKey(MAC_ADDRESS, MacAddressType.class, new ContextId(CONTEXT_ID),
470             L2FloodDomain.class);
471
472         Optional<AddressEndpoint> addressEndpointL2 = DataStoreHelper.readFromDs(LogicalDatastoreType.OPERATIONAL,
473             IidFactory.addressEndpointIid(key), dataProvider.newReadOnlyTransaction());
474
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());
482             }
483         }
484
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))
491             .build();
492
493         baseEndpointRpcRegistry.registerEndpoint(new RegisterEndpointInputBuilder().setContainmentEndpointReg(Collections.singletonList(containmentEndpointReg)).build());
494
495         addressEndpointL2 = DataStoreHelper.readFromDs(LogicalDatastoreType.OPERATIONAL,
496             IidFactory.addressEndpointIid(key), dataProvider.newReadOnlyTransaction());
497
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);
506             }
507         }
508
509         ContainmentEndpointUnreg containmentEndpointUnreg = new ContainmentEndpointUnregBuilder()
510             .setContextId(baseContainmentEp.getContextId())
511             .setContextType(baseContainmentEp.getContextType())
512             .build();
513
514         baseEndpointRpcRegistry.unregisterEndpoint(new UnregisterEndpointInputBuilder().setContainmentEndpointUnreg(Collections.singletonList(containmentEndpointUnreg)).build());
515
516         addressEndpointL2 = DataStoreHelper.readFromDs(LogicalDatastoreType.OPERATIONAL,
517             IidFactory.addressEndpointIid(key), dataProvider.newReadOnlyTransaction());
518
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);
527             }
528         }
529
530     }
531
532     private UnregisterEndpointInput unregisterEndpointInputParent() {
533         UnregisterEndpointInputBuilder builder = new UnregisterEndpointInputBuilder();
534
535         builder.setAddressEndpointUnreg(new ArrayList<>());
536         builder.setContainmentEndpointUnreg(new ArrayList<>());
537
538         builder.getAddressEndpointUnreg().add(new AddressEndpointUnregBuilder().setContextId(new ContextId(CONTEXT_ID))
539             .setContextType(L2FloodDomain.class)
540             .setAddress(MAC_ADDRESS)
541             .setAddressType(MacAddressType.class)
542             .build());
543
544         builder.getContainmentEndpointUnreg().add(new ContainmentEndpointUnregBuilder()
545             .setContextId(new ContextId(CONTEXT_ID)).setContextType(L2FloodDomain.class).build());
546
547         return builder.build();
548     }
549
550     private UnregisterEndpointInput unregisterEndpointInputChild() {
551         UnregisterEndpointInputBuilder builder = new UnregisterEndpointInputBuilder();
552
553         builder.setAddressEndpointUnreg(new ArrayList<>());
554         builder.setContainmentEndpointUnreg(new ArrayList<>());
555
556         builder.getAddressEndpointUnreg().add(new AddressEndpointUnregBuilder().setContextId(new ContextId(CONTEXT_ID))
557             .setContextType(L3Context.class)
558             .setAddress(IP_ADDRESS)
559             .setAddressType(IpPrefixType.class)
560             .build());
561
562         return builder.build();
563     }
564
565     private RegisterEndpointInput createRegisterEndpointInputVariablesForTest(AddressEndpointRegistration registration,
566             AddressEndpointType type, boolean containmentEpPresent) throws Exception {
567         RegisterEndpointInputBuilder registerEndpointInputBuilder = new RegisterEndpointInputBuilder();
568         long timestamp = System.currentTimeMillis();
569
570         List<ParentEndpoint> parentEndpoints = new ArrayList<>();
571         if (registration == AddressEndpointRegistration.BOTH || registration == AddressEndpointRegistration.PARENT) {
572             parentEndpoints.add(basel3Parent);
573         }
574
575         List<ChildEndpoint> childEndpoints = new ArrayList<>();
576         if (registration == AddressEndpointRegistration.BOTH || registration == AddressEndpointRegistration.CHILD) {
577             childEndpoints.add(basel2Child);
578         }
579
580         registerEndpointInputBuilder.setAddressEndpointReg(new ArrayList<>());
581         registerEndpointInputBuilder.setContainmentEndpointReg(new ArrayList<>());
582
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())
594                     .build());
595         }
596
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)
607                     .build());
608         }
609
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))
618                     .build());
619         }
620
621         return registerEndpointInputBuilder.build();
622     }
623 }