2 * Copyright (c) 2015 Cisco Systems. 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.iovisor;
11 import static org.mockito.Matchers.any;
12 import static org.mockito.Mockito.mock;
13 import static org.mockito.Mockito.spy;
14 import static org.mockito.Mockito.verify;
15 import static org.mockito.Mockito.when;
17 import java.util.List;
20 import com.google.common.base.Optional;
21 import com.google.common.collect.ImmutableList;
22 import com.google.common.collect.ImmutableSet;
23 import org.junit.Assert;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
27 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
28 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
29 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
30 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
31 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
32 import org.opendaylight.groupbasedpolicy.renderer.iovisor.test.GbpIovisorDataBrokerTest;
33 import org.opendaylight.groupbasedpolicy.renderer.iovisor.utils.IovisorIidFactory;
34 import org.opendaylight.groupbasedpolicy.util.DataStoreHelper;
35 import org.opendaylight.groupbasedpolicy.util.IidFactory;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.EndpointGroupId;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.iovisor.rev151030.IovisorResolvedEndpointsByTenantByEndpointgroupId;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.iovisor.rev151030.IovisorResolvedEndpointsByTenantByEndpointgroupIdBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.iovisor.rev151030.iovisor.resolved.endpoints.by.tenant.by.endpointgroup.id.IovisorResolvedEndpointByTenantByEndpointgroupId;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.iovisor.rev151030.iovisor.resolved.endpoints.by.tenant.by.endpointgroup.id.IovisorResolvedEndpointByTenantByEndpointgroupIdBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.interests.followed.tenants.followed.tenant.FollowedEndpointGroup;
43 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
45 public class IovisorResolvedEndpointListenerTest extends GbpIovisorDataBrokerTest {
47 private DataBroker dataBroker;
48 private IovisorResolvedEndpointListener iovisorResolvedEndpointListener;
49 private DataObjectModification<IovisorResolvedEndpointsByTenantByEndpointgroupId> rootNode;
50 private Set<DataTreeModification<IovisorResolvedEndpointsByTenantByEndpointgroupId>> changes;
51 private InstanceIdentifier<IovisorResolvedEndpointsByTenantByEndpointgroupId> rootIdentifier;
53 private final TenantId tenant1 = new TenantId("tenant1");
54 private final EndpointGroupId epg1 = new EndpointGroupId("client");
57 public void iovisorInit() {
58 dataBroker = getDataBroker();
59 iovisorResolvedEndpointListener = spy(new IovisorResolvedEndpointListener(dataBroker));
61 rootNode = mock(DataObjectModification.class);
62 rootIdentifier = IovisorIidFactory.iovisorResolvedEndpointsByTenantIdByEndpointGroupIdWildCardIid();
63 DataTreeIdentifier<IovisorResolvedEndpointsByTenantByEndpointgroupId> rootPath =
64 new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL,
65 IovisorIidFactory.iovisorResolvedEndpointsByTenantIdByEndpointGroupIdWildCardIid());
66 DataTreeModification<IovisorResolvedEndpointsByTenantByEndpointgroupId> change = mock(DataTreeModification.class);
68 when(change.getRootNode()).thenReturn(rootNode);
69 when(change.getRootPath()).thenReturn(rootPath);
71 changes = ImmutableSet.of(change);
73 IovisorResolvedEndpointByTenantByEndpointgroupId testElement = new IovisorResolvedEndpointByTenantByEndpointgroupIdBuilder()
75 .setEndpointgroupId(epg1)
77 List<IovisorResolvedEndpointByTenantByEndpointgroupId> list = ImmutableList.of(testElement);
79 IovisorResolvedEndpointsByTenantByEndpointgroupId testData = new IovisorResolvedEndpointsByTenantByEndpointgroupIdBuilder()
80 .setIovisorResolvedEndpointByTenantByEndpointgroupId(list)
83 when(rootNode.getDataAfter()).thenReturn(testData);
87 public void testOnWrite() {
88 when(rootNode.getModificationType()).thenReturn(DataObjectModification.ModificationType.WRITE);
90 iovisorResolvedEndpointListener.onDataTreeChanged(changes);
92 verify(iovisorResolvedEndpointListener).onSubtreeModified(rootNode, rootIdentifier);
95 @Test(expected = UnsupportedOperationException.class)
96 public void testOnDelete() {
97 when(rootNode.getModificationType()).thenReturn(DataObjectModification.ModificationType.DELETE);
99 iovisorResolvedEndpointListener.onDataTreeChanged(changes);
103 public void testOnSubtreeModified() {
104 when(rootNode.getModificationType()).thenReturn(DataObjectModification.ModificationType.SUBTREE_MODIFIED);
106 iovisorResolvedEndpointListener.onDataTreeChanged(changes);
108 verify(iovisorResolvedEndpointListener).endpointPolicyUpdated(any(TenantId.class), any(EndpointGroupId.class), any(WriteTransaction.class));
113 public void endpointPolicyUpdatedTest() {
114 iovisorResolvedEndpointListener.endpointPolicyUpdated(tenant1, epg1, dataBroker.newWriteOnlyTransaction());
115 Optional<FollowedEndpointGroup> readFromDs = DataStoreHelper.readFromDs(LogicalDatastoreType.OPERATIONAL,
116 IidFactory.followedEndpointgroupIid(IovisorRenderer.RENDERER_NAME, tenant1, epg1),
117 dataBroker.newReadOnlyTransaction());
118 Assert.assertTrue(readFromDs.isPresent());
119 Assert.assertEquals(epg1, readFromDs.get().getId());