1 package org.opendaylight.groupbasedpolicy.renderer.faas;
3 import static org.mockito.Matchers.any;
4 import static org.mockito.Mockito.doNothing;
5 import static org.mockito.Mockito.mock;
6 import static org.mockito.Mockito.spy;
7 import static org.mockito.Mockito.when;
9 import java.util.concurrent.Executors;
10 import java.util.concurrent.ScheduledExecutorService;
12 import com.google.common.base.Optional;
13 import com.google.common.util.concurrent.CheckedFuture;
14 import org.junit.Before;
15 import org.junit.Test;
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
18 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
19 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
20 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
21 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
22 import org.opendaylight.groupbasedpolicy.renderer.faas.test.DataChangeListenerTester;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.faas.logical.faas.common.rev151013.Uuid;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.L2BridgeDomainId;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.SubnetId;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.Endpoint;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointL3;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointL3Builder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointL3Prefix;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointL3PrefixBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.faas.rev151009.mapped.tenants.entities.mapped.entity.MappedEndpoint;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.faas.rev151009.mapped.tenants.entities.mapped.entity.MappedEndpointKey;
36 import org.opendaylight.yangtools.yang.binding.DataObject;
37 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39 public class FaasEndpointManagerListenerCovrgTest {
41 private static final L2BridgeDomainId L_2_BRIDGE_DOMAIN_ID = new L2BridgeDomainId("L2BridgeDomainId");
42 private static final MacAddress MAC_ADDRESS = new MacAddress("00:00:00:00:35:02");
44 private InstanceIdentifier<Endpoint> epIid;
45 private FaasEndpointManagerListener listener;
46 private TenantId gbpTenantId = new TenantId("gbpTenantId");
47 private SubnetId subnetId = new SubnetId("subnetId");
48 private Uuid faasTenantId = new Uuid("b4511aac-ae43-11e5-bf7f-feff819cdc9f");
49 private Uuid faasSubnetId = new Uuid("c4511aac-ae43-11e5-bf7f-feff819cdc9f");
50 private final ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
51 private DataChangeListenerTester tester;
52 private DataBroker dataProvider;
53 private FaasPolicyManager faasPolicyManager;
55 @SuppressWarnings("unchecked")
57 public void init() throws ReadFailedException {
58 MappedEndpointKey mappedEndpointKey = new MappedEndpointKey(L_2_BRIDGE_DOMAIN_ID, MAC_ADDRESS);
60 dataProvider = mock(DataBroker.class);
62 WriteTransaction woTx = mock(WriteTransaction.class);
63 ReadWriteTransaction rwTx = mock(ReadWriteTransaction.class);
65 CheckedFuture<Void, TransactionCommitFailedException> futureVoid = mock(CheckedFuture.class);
66 when(woTx.submit()).thenReturn(futureVoid);
67 when(rwTx.submit()).thenReturn(futureVoid);
68 doNothing().when(woTx).put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class),
69 any(DataObject.class));
71 CheckedFuture<Optional<MappedEndpoint>, ReadFailedException> futureMappedEndpoint = mock(CheckedFuture.class);
72 Optional<MappedEndpoint> optMappedEndpoint = mock(Optional.class);
73 when(optMappedEndpoint.isPresent()).thenReturn(false);
74 when(futureMappedEndpoint.checkedGet()).thenReturn(optMappedEndpoint);
75 when(rwTx.read(LogicalDatastoreType.OPERATIONAL,
76 FaasIidFactory.mappedEndpointIid(gbpTenantId, mappedEndpointKey))).thenReturn(futureMappedEndpoint);
78 when(dataProvider.newWriteOnlyTransaction()).thenReturn(woTx);
79 when(dataProvider.newReadWriteTransaction()).thenReturn(rwTx);
81 epIid = mock(InstanceIdentifier.class);
82 faasPolicyManager = spy(new FaasPolicyManager(dataProvider, executor));
83 doNothing().when(faasPolicyManager).removeTenantLogicalNetwork(gbpTenantId, faasTenantId);
84 listener = new FaasEndpointManagerListener(faasPolicyManager, dataProvider, executor);
85 tester = new DataChangeListenerTester(listener);
86 tester.setRemovedPath(epIid);
90 public void testOnDataChanged_Endpoint() {
91 Endpoint ep = new EndpointBuilder().setTenant(gbpTenantId)
92 .setL2Context(L_2_BRIDGE_DOMAIN_ID)
93 .setMacAddress(MAC_ADDRESS)
95 tester.setDataObject(epIid, ep);
96 tester.callOnDataChanged();
97 listener.executeEvent(tester.getChangeMock());
101 public void testOnDataChanged_EndpointL3() {
102 EndpointL3 ep = new EndpointL3Builder().setTenant(gbpTenantId)
103 .setL2Context(L_2_BRIDGE_DOMAIN_ID)
104 .setMacAddress(MAC_ADDRESS)
106 tester.setDataObject(epIid, ep);
107 tester.callOnDataChanged();
108 listener.executeEvent(tester.getChangeMock());
112 public void testOnDataChanged_EndpointL3Prefix() {
113 EndpointL3Prefix ep = new EndpointL3PrefixBuilder().setTenant(gbpTenantId).build();
114 tester.setDataObject(epIid, ep);
115 tester.callOnDataChanged();
116 listener.executeEvent(tester.getChangeMock());