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