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