1 package org.opendaylight.groupbasedpolicy.renderer.faas;
3 import static org.mockito.Matchers.any;
4 import static org.mockito.Matchers.eq;
5 import static org.mockito.Mockito.doNothing;
6 import static org.mockito.Mockito.mock;
7 import static org.mockito.Mockito.when;
9 import java.util.ArrayList;
10 import java.util.List;
11 import java.util.concurrent.Executors;
12 import java.util.concurrent.ScheduledExecutorService;
14 import com.google.common.base.Optional;
15 import com.google.common.util.concurrent.CheckedFuture;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.junit.runner.RunWith;
19 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
20 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
21 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
22 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
23 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
24 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
25 import org.opendaylight.faas.uln.datastore.api.UlnDatastoreApi;
26 import org.opendaylight.groupbasedpolicy.renderer.faas.test.DataChangeListenerTester;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
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.SubnetId;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.faas.rev151009.mapped.tenants.entities.mapped.entity.MappedSubnet;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.faas.rev151009.mapped.tenants.entities.mapped.entity.MappedSubnetBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.Subnet;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.SubnetBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.subnet.Gateways;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.subnet.GatewaysBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.subnet.gateways.Prefixes;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.subnet.gateways.PrefixesBuilder;
42 import org.opendaylight.yangtools.yang.binding.DataObject;
43 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
44 import org.powermock.api.mockito.PowerMockito;
45 import org.powermock.core.classloader.annotations.PrepareForTest;
46 import org.powermock.modules.junit4.PowerMockRunner;
48 @RunWith(PowerMockRunner.class)
49 @PrepareForTest(UlnDatastoreApi.class)
50 public class FaasSubnetManagerListenerCovrgTest {
52 private InstanceIdentifier<Subnet> subnetIid;
53 private FaasSubnetManagerListener listener;
54 private TenantId gbpTenantId = new TenantId("gbpTenantId");
55 private SubnetId subnetId = new SubnetId("subnetId");
56 private Uuid faasTenantId = new Uuid("b4511aac-ae43-11e5-bf7f-feff819cdc9f");
57 private Uuid faasSubnetId = new Uuid("c4511aac-ae43-11e5-bf7f-feff819cdc9f");
58 private final ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
59 private DataChangeListenerTester tester;
60 private DataBroker dataProvider;
62 @SuppressWarnings("unchecked")
65 dataProvider = mock(DataBroker.class);
66 subnetIid = mock(InstanceIdentifier.class);
67 listener = new FaasSubnetManagerListener(dataProvider, gbpTenantId, faasTenantId, executor);
68 tester = new DataChangeListenerTester(listener);
69 tester.setRemovedPath(subnetIid);
72 @SuppressWarnings("unchecked")
74 public void testOnDataChanged() throws ReadFailedException {
75 PowerMockito.mockStatic(UlnDatastoreApi.class);
76 PowerMockito.doNothing().when(UlnDatastoreApi.class);
77 UlnDatastoreApi.submitSubnetToDs(any(
78 org.opendaylight.yang.gen.v1.urn.opendaylight.faas.logical.faas.subnets.rev151013.subnets.container.subnets.Subnet.class));
79 PowerMockito.doNothing().when(UlnDatastoreApi.class);
80 UlnDatastoreApi.removeSubnetFromDsIfExists(any(Uuid.class), any(Uuid.class));
82 ReadWriteTransaction rwTx = mock(ReadWriteTransaction.class);
83 WriteTransaction woTx = mock(WriteTransaction.class);
84 CheckedFuture<Void, TransactionCommitFailedException> futureVoid = mock(CheckedFuture.class);
85 when(rwTx.submit()).thenReturn(futureVoid);
86 when(woTx.submit()).thenReturn(futureVoid);
88 CheckedFuture<Optional<MappedSubnet>, ReadFailedException> futureMappedSubnet = mock(CheckedFuture.class);
89 Optional<MappedSubnet> optMappedSubnet = mock(Optional.class);
90 when(optMappedSubnet.isPresent()).thenReturn(true);
91 when(futureMappedSubnet.checkedGet()).thenReturn(optMappedSubnet);
92 when(rwTx.read(LogicalDatastoreType.OPERATIONAL, FaasIidFactory.mappedSubnetIid(gbpTenantId, subnetId)))
93 .thenReturn(futureMappedSubnet);
94 doNothing().when(woTx).put(eq(LogicalDatastoreType.OPERATIONAL),
95 eq(FaasIidFactory.mappedSubnetIid(gbpTenantId, subnetId)), any(MappedSubnet.class));
97 when(dataProvider.newReadWriteTransaction()).thenReturn(rwTx);
98 when(dataProvider.newWriteOnlyTransaction()).thenReturn(woTx);
100 Subnet subnet = new SubnetBuilder().setId(subnetId).build();
101 tester.setDataObject(subnetIid, subnet);
102 tester.callOnDataChanged();
103 listener.executeEvent(tester.getChangeMock());
107 public void testLoadAll() {
108 PowerMockito.mockStatic(UlnDatastoreApi.class);
109 PowerMockito.doNothing().when(UlnDatastoreApi.class);
110 UlnDatastoreApi.submitSubnetToDs(any(
111 org.opendaylight.yang.gen.v1.urn.opendaylight.faas.logical.faas.subnets.rev151013.subnets.container.subnets.Subnet.class));
113 List<Subnet> subnets = new ArrayList<>();
114 List<MappedSubnet> mpSubnets = new ArrayList<>();
116 Subnet subnet = new SubnetBuilder().setId(subnetId).build();
118 MappedSubnet mappedSubnet =
119 new MappedSubnetBuilder().setGbpSubnetId(subnetId).setFaasSubnetId(faasSubnetId).build();
120 mpSubnets.add(mappedSubnet);
122 listener.loadAll(subnets, mpSubnets);
125 @SuppressWarnings("unchecked")
127 public void testInitSubnetBuilder() {
128 WriteTransaction wTx = mock(WriteTransaction.class);
129 CheckedFuture<Void, TransactionCommitFailedException> futureVoid = mock(CheckedFuture.class);
130 when(wTx.submit()).thenReturn(futureVoid);
131 doNothing().when(wTx).put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class),
132 any(DataObject.class));
133 when(dataProvider.newWriteOnlyTransaction()).thenReturn(wTx);
135 List<Gateways> gatewaysList = new ArrayList<>();
136 List<Prefixes> prefixesList = new ArrayList<>();
137 Prefixes prefixes = new PrefixesBuilder().setPrefix(new IpPrefix(new Ipv4Prefix("10.0.0.0/8"))).build();
138 prefixesList.add(prefixes);
139 Gateways gateways = new GatewaysBuilder().setGateway(new IpAddress(new Ipv4Address("10.0.0.55")))
140 .setPrefixes(prefixesList)
142 gatewaysList.add(gateways);
144 Subnet subnet = new SubnetBuilder().setId(subnetId).setGateways(gatewaysList).build();
145 listener.initSubnetBuilder(subnet);