4254dfcc93fb8ca5e0d1efe2e2fc0fd9c62a3e9e
[groupbasedpolicy.git] / renderers / faas / src / test / java / org / opendaylight / groupbasedpolicy / renderer / faas / FaasPolicyManagerCovrgTest.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.junit.Assert.assertFalse;
11 import static org.junit.Assert.assertTrue;
12 import static org.mockito.Matchers.any;
13 import static org.mockito.Matchers.eq;
14 import static org.mockito.Mockito.doNothing;
15 import static org.mockito.Mockito.mock;
16 import static org.mockito.Mockito.spy;
17 import static org.mockito.Mockito.verify;
18 import static org.mockito.Mockito.when;
19
20 import java.util.ArrayList;
21 import java.util.HashSet;
22 import java.util.List;
23 import java.util.Set;
24 import java.util.concurrent.Executors;
25 import java.util.concurrent.ScheduledExecutorService;
26
27 import com.google.common.base.Optional;
28 import com.google.common.util.concurrent.CheckedFuture;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
33 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
34 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
35 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
36 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
37 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
38 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
39 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
40 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
41 import org.opendaylight.faas.uln.datastore.api.UlnDatastoreApi;
42 import org.opendaylight.groupbasedpolicy.util.IidFactory;
43 import org.opendaylight.groupbasedpolicy.util.TenantUtils;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.faas.logical.faas.common.rev151013.Uuid;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ContractId;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.EndpointGroupId;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.L2BridgeDomainId;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.L2FloodDomainId;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.L3ContextId;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.SubnetId;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.faas.rev151009.LogicalNetworks;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.faas.rev151009.LogicalNetworksBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.faas.rev151009.ScopeType;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.faas.rev151009.ServiceCommunicationLayer;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.faas.rev151009.logical.networks.LogicalNetwork;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.faas.rev151009.logical.networks.LogicalNetworkBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.faas.rev151009.logical.networks.logical.network.ConsumerNetworkBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.faas.rev151009.logical.networks.logical.network.ProviderNetworkBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.faas.rev151009.mapped.tenants.entities.MappedEntity;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.faas.rev151009.mapped.tenants.entities.MappedTenant;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.faas.rev151009.mapped.tenants.entities.mapped.entity.MappedSubnet;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.faas.rev151009.mapped.tenants.entities.mapped.entity.MappedSubnetBuilder;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.Tenant;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.L2BridgeDomain;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.L2FloodDomain;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.L3Context;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.Subnet;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.EndpointGroup;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.EndpointGroupBuilder;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.resolved.policy.rev150828.ResolvedPolicies;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.resolved.policy.rev150828.resolved.policies.ResolvedPolicy;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.resolved.policy.rev150828.resolved.policies.ResolvedPolicy.ExternalImplicitGroup;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.resolved.policy.rev150828.resolved.policies.ResolvedPolicyBuilder;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.resolved.policy.rev150828.resolved.policies.resolved.policy.PolicyRuleGroupWithEndpointConstraints;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.resolved.policy.rev150828.resolved.policies.resolved.policy.PolicyRuleGroupWithEndpointConstraintsBuilder;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.resolved.policy.rev150828.resolved.policies.resolved.policy.policy.rule.group.with.endpoint.constraints.PolicyRuleGroup;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.resolved.policy.rev150828.resolved.policies.resolved.policy.policy.rule.group.with.endpoint.constraints.PolicyRuleGroupBuilder;
79 import org.opendaylight.yangtools.yang.binding.DataObject;
80 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
81 import org.powermock.api.mockito.PowerMockito;
82 import org.powermock.core.classloader.annotations.PrepareForTest;
83 import org.powermock.modules.junit4.PowerMockRunner;
84
85 @RunWith(PowerMockRunner.class)
86 @PrepareForTest(UlnDatastoreApi.class)
87 public class FaasPolicyManagerCovrgTest {
88
89     private InstanceIdentifier<DataObject> policyId;
90     private AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> change;
91     DataBroker dataProvider;
92     private final ScheduledExecutorService executor = Executors.newScheduledThreadPool(Runtime.getRuntime()
93             .availableProcessors());
94     private EndpointGroupId consumerEpgId = new EndpointGroupId("consumerEpgId");
95     private SubnetId consumerSubnet = new SubnetId("consumerSubnet");
96     private SubnetId providerSubnet = new SubnetId("providerSubnet");
97     private EndpointGroupId providerEpgId = new EndpointGroupId("providerEpgId");
98     private ContractId contractId = new ContractId("contractId");
99     private TenantId tenantId = new TenantId("tenantId");
100     private Uuid faasTenantId = new Uuid("0eb98cf5-086c-4a81-8a4e-0c3b4566108b");
101     private Uuid faasSecRulesId = new Uuid("1eb98cf5-086c-4a81-8a4e-0c3b4566108b");
102     private L3ContextId l3Context = new L3ContextId("l3ContextId");
103     private EndpointGroupId epgId = new EndpointGroupId("epgId");
104     private SubnetId subnetId = new SubnetId("subnetId");
105     private Uuid dummyUuid1 = new Uuid("2eb98cf5-086c-4a81-8a4e-0c3b4566108b");
106     private Uuid dummyUuid2 = new Uuid("3eb98cf5-086c-4a81-8a4e-0c3b4566108b");
107
108     @SuppressWarnings("unchecked")
109     @Before
110     public void init() throws Exception {
111         policyId = mock(InstanceIdentifier.class);
112         change = mock(AsyncDataChangeEvent.class);
113         policyId = mock(InstanceIdentifier.class);
114         dataProvider = mock(DataBroker.class);
115
116         WriteTransaction writeTransaction = mock(WriteTransaction.class);
117         when(dataProvider.newWriteOnlyTransaction()).thenReturn(writeTransaction);
118         CheckedFuture<Void, TransactionCommitFailedException> futureVoid =
119                 mock(CheckedFuture.class);
120         when(writeTransaction.submit()).thenReturn(futureVoid);
121
122         Set<InstanceIdentifier<?>> removedPaths = new HashSet<>();
123         removedPaths.add(policyId);
124         when(change.getRemovedPaths()).thenReturn(removedPaths);
125     }
126
127     @Test
128     public void testConstructor() throws Exception {
129         FaasPolicyManager other = new MockFaasPolicyManager(dataProvider, executor);
130
131         verify(dataProvider).registerDataChangeListener(LogicalDatastoreType.OPERATIONAL,
132                 InstanceIdentifier.builder(ResolvedPolicies.class)
133                         .child(ResolvedPolicy.class)
134                         .build(), other, AsyncDataBroker.DataChangeScope.SUBTREE);
135         other.close();
136     }
137
138     @SuppressWarnings("unchecked")
139     @Test
140     public void testRegisterTenant() throws Exception {
141         ReadOnlyTransaction roTx = mock(ReadOnlyTransaction.class);
142         when(dataProvider.newReadOnlyTransaction()).thenReturn(roTx);
143
144         ReadWriteTransaction rwTx = mock(ReadWriteTransaction.class);
145         when(dataProvider.newReadWriteTransaction()).thenReturn(rwTx);
146
147         CheckedFuture<Optional<MappedTenant>, ReadFailedException> futureMappedTenant =
148                 mock(CheckedFuture.class);
149         when(roTx.read(LogicalDatastoreType.OPERATIONAL,
150                 FaasIidFactory.mappedTenantIid(tenantId))).thenReturn(futureMappedTenant);
151         Optional<MappedTenant> optMappedTenant = mock(Optional.class);
152         when(optMappedTenant.isPresent()).thenReturn(false);
153         when(futureMappedTenant.checkedGet()).thenReturn(optMappedTenant);
154
155         CheckedFuture<Optional<Tenant>, ReadFailedException> futureTenant =
156                 mock(CheckedFuture.class);
157         when(roTx.read(LogicalDatastoreType.CONFIGURATION,
158                 TenantUtils.tenantIid(tenantId))).thenReturn(futureTenant);
159         Optional<Tenant> optTenant = mock(Optional.class);
160         when(futureTenant.checkedGet()).thenReturn(optTenant);
161
162         CheckedFuture<Optional<MappedEntity>, ReadFailedException> futureMappedEntity =
163                 mock(CheckedFuture.class);
164         when(roTx.read(LogicalDatastoreType.OPERATIONAL,
165                 FaasIidFactory.mappedEntityIid(tenantId))).thenReturn(futureMappedEntity);
166         Optional<MappedEntity> optMappedEntity = mock(Optional.class);
167         when(futureMappedEntity.checkedGet()).thenReturn(optMappedEntity);
168
169         CheckedFuture<Optional<ResolvedPolicies>, ReadFailedException> futureResolvedPolicies =
170                 mock(CheckedFuture.class);
171         when(roTx.read(LogicalDatastoreType.OPERATIONAL,
172                 InstanceIdentifier.builder(ResolvedPolicies.class).build())).thenReturn(futureResolvedPolicies);
173         Optional<ResolvedPolicies> optResolvedPolicies = mock(Optional.class);
174         when(optResolvedPolicies.isPresent()).thenReturn(false);
175         when(futureResolvedPolicies.checkedGet()).thenReturn(optResolvedPolicies);
176
177         CheckedFuture<Optional<LogicalNetworks>, ReadFailedException> futureLogicalNetworks =
178                 mock(CheckedFuture.class);
179         when(roTx.read(LogicalDatastoreType.OPERATIONAL,
180                 FaasIidFactory.logicalNetworksIid())).thenReturn(futureLogicalNetworks);
181         LogicalNetworks logicalNetworks = new LogicalNetworksBuilder()
182                 .setLogicalNetwork(new ArrayList<LogicalNetwork>())
183                 .build();
184         Optional<LogicalNetworks> optLogicalNetworks = mock(Optional.class);
185         when(optLogicalNetworks.isPresent()).thenReturn(true);
186         when(optLogicalNetworks.get()).thenReturn(logicalNetworks);
187         when(futureLogicalNetworks.checkedGet()).thenReturn(optLogicalNetworks);
188
189         EndpointGroupId epgId = new EndpointGroupId("epgId");
190         FaasPolicyManager policyManager = spy(new FaasPolicyManager(dataProvider, executor));
191         doNothing().when(policyManager).removeTenantLogicalNetwork(tenantId, faasTenantId, false);
192
193         policyManager.registerTenant(tenantId, epgId);
194     }
195
196     @Test
197     public void testRegisterTenant_null() {
198         FaasPolicyManager policyManager = spy(new FaasPolicyManager(dataProvider, executor));
199         doNothing().when(policyManager).registerTenant(tenantId, null);
200
201         policyManager.registerTenant(tenantId);
202     }
203
204     @SuppressWarnings("unchecked")
205     @Test
206     public void testRemoveTenantLogicalNetwork() throws ReadFailedException {
207         ReadOnlyTransaction roTx = mock(ReadOnlyTransaction.class);
208         when(dataProvider.newReadOnlyTransaction()).thenReturn(roTx);
209
210         ReadWriteTransaction rwTx = mock(ReadWriteTransaction.class);
211         when(dataProvider.newReadWriteTransaction()).thenReturn(rwTx);
212
213         CheckedFuture<Void, TransactionCommitFailedException> futureVoid = mock(CheckedFuture.class);
214         when(rwTx.submit()).thenReturn(futureVoid);
215
216         CheckedFuture<Optional<LogicalNetworks>, ReadFailedException> futureLogicalNetworks =
217                 mock(CheckedFuture.class);
218         when(roTx.read(LogicalDatastoreType.OPERATIONAL,
219                 FaasIidFactory.logicalNetworksIid())).thenReturn(futureLogicalNetworks);
220         List<LogicalNetwork> lns = new ArrayList<>();
221         LogicalNetwork ln = new LogicalNetworkBuilder()
222                 .setConsumerEpgId(consumerEpgId)
223                 .setConsumerTenantId(tenantId)
224                 .setContractId(contractId)
225                 .setProviderEpgId(providerEpgId)
226                 .setProviderTenantId(tenantId)
227                 .build();
228         lns.add(ln);
229         LogicalNetworks logicalNetworks = new LogicalNetworksBuilder()
230                 .setLogicalNetwork(lns)
231                 .build();
232         Optional<LogicalNetworks> optLogicalNetworks = mock(Optional.class);
233         when(optLogicalNetworks.isPresent()).thenReturn(true);
234         when(optLogicalNetworks.get()).thenReturn(logicalNetworks);
235         when(futureLogicalNetworks.checkedGet()).thenReturn(optLogicalNetworks);
236
237         CheckedFuture<Optional<LogicalNetwork>, ReadFailedException> futureLogicalNetwork =
238                 mock(CheckedFuture.class);
239         when(rwTx.read(eq(LogicalDatastoreType.OPERATIONAL),
240                 eq(FaasIidFactory.logicalNetworkIid(consumerEpgId, tenantId, contractId,
241                         providerEpgId, tenantId)))).thenReturn(futureLogicalNetwork);
242         Optional<LogicalNetwork> optionalLogicalNetwork = mock(Optional.class);
243         when(futureLogicalNetwork.checkedGet()).thenReturn(optionalLogicalNetwork);
244
245         CheckedFuture<Optional<MappedEntity>, ReadFailedException> futureMappedEntity =
246                 mock(CheckedFuture.class);
247         when(rwTx.read(LogicalDatastoreType.OPERATIONAL,
248                 FaasIidFactory.mappedEntityIid(tenantId))).thenReturn(futureMappedEntity);
249         Optional<MappedEntity> optMappedEntity = mock(Optional.class);
250         when(optMappedEntity.isPresent()).thenReturn(true);
251         when(futureMappedEntity.checkedGet()).thenReturn(optMappedEntity);
252
253         CheckedFuture<Optional<MappedTenant>, ReadFailedException> futureMappedTenant =
254                 mock(CheckedFuture.class);
255         when(rwTx.read(LogicalDatastoreType.OPERATIONAL,
256                 FaasIidFactory.mappedTenantIid(tenantId))).thenReturn(futureMappedTenant);
257         Optional<MappedTenant> optMappedTenant = mock(Optional.class);
258         when(optMappedTenant.isPresent()).thenReturn(true);
259         when(futureMappedTenant.checkedGet()).thenReturn(optMappedTenant);
260
261         PowerMockito.mockStatic(UlnDatastoreApi.class);
262         PowerMockito.doNothing().when(UlnDatastoreApi.class);
263         UlnDatastoreApi.removeTenantFromDsIfExists(any(Uuid.class));
264
265         FaasPolicyManager policyManager = spy(new FaasPolicyManager(dataProvider, executor));
266
267         policyManager.removeTenantLogicalNetwork(tenantId, faasTenantId);
268     }
269
270     @Test
271     public void testUnregisterTenant() throws Exception {
272         FaasPolicyManager policyManager = new FaasPolicyManager(dataProvider, executor);
273
274         policyManager.unregisterTenant(tenantId);
275     }
276
277     @Test
278     public void testIsUuid() {
279         assertFalse(FaasPolicyManager.isUUid(null));
280         assertFalse(FaasPolicyManager.isUUid("non-matching string"));
281         assertTrue(FaasPolicyManager.isUUid("12345678-1234-5123-b123-0123456789ab"));
282     }
283
284     @Test
285     public void testHandledPolicy_notEquals() {
286         FaasPolicyManager policyManager = new FaasPolicyManager(dataProvider, executor);
287         ResolvedPolicy policy = new ResolvedPolicyBuilder()
288                 .setConsumerTenantId(new TenantId("t1"))
289                 .setProviderTenantId(new TenantId("t2"))
290                 .build();
291
292         assertFalse(policyManager.handledPolicy(policy));
293     }
294
295     @Test
296     public void testRegisterFollowedEndpointgroup() {
297         EndpointGroupId epgId = new EndpointGroupId("epgId");
298         FaasPolicyManager policyManager = new FaasPolicyManager(dataProvider, executor);
299
300         policyManager.registerFollowedEndpointgroup(tenantId, null);
301         policyManager.registerFollowedEndpointgroup(tenantId, epgId);
302     }
303
304     @SuppressWarnings("unchecked")
305     @Test
306     public void testRegisterSubnetWithEpg() throws ReadFailedException {
307         FaasPolicyManager policyManager = new FaasPolicyManager(dataProvider, executor);
308
309         ReadOnlyTransaction roTx = mock(ReadOnlyTransaction.class);
310         when(dataProvider.newReadOnlyTransaction()).thenReturn(roTx);
311
312         CheckedFuture<Optional<ResolvedPolicies>, ReadFailedException> futureResolvedPolicies =
313                 mock(CheckedFuture.class);
314         when(roTx.read(LogicalDatastoreType.OPERATIONAL,
315                 InstanceIdentifier.builder(ResolvedPolicies.class).build())).thenReturn(futureResolvedPolicies);
316         Optional<ResolvedPolicies> optResolvedPolicies = mock(Optional.class);
317         when(optResolvedPolicies.isPresent()).thenReturn(false);
318         when(futureResolvedPolicies.checkedGet()).thenReturn(optResolvedPolicies);
319
320
321         policyManager.registerSubnetWithEpg(epgId, tenantId, subnetId);
322     }
323
324     @SuppressWarnings("unchecked")
325     @Test
326     public void testReadEndpointGroup() throws ReadFailedException {
327         FaasPolicyManager policyManager = new FaasPolicyManager(dataProvider, executor);
328         ReadOnlyTransaction roTx = mock(ReadOnlyTransaction.class);
329         CheckedFuture<Optional<EndpointGroup>, ReadFailedException> futureEndpointGroup =
330                 mock(CheckedFuture.class);
331         Optional<EndpointGroup> optEndpointGroup = mock(Optional.class);
332         when(optEndpointGroup.isPresent()).thenReturn(true);
333         when(futureEndpointGroup.checkedGet()).thenReturn(optEndpointGroup);
334         when(roTx.read(LogicalDatastoreType.CONFIGURATION,
335                 IidFactory.endpointGroupIid(tenantId, epgId))).thenReturn(futureEndpointGroup);
336         doNothing().when(roTx).close();
337         when(dataProvider.newReadOnlyTransaction()).thenReturn(roTx);
338
339         policyManager.readEndpointGroup(epgId, tenantId);
340
341         when(optEndpointGroup.isPresent()).thenReturn(false);
342         policyManager.readEndpointGroup(epgId, tenantId);
343     }
344
345     @SuppressWarnings("unchecked")
346     @Test
347     public void testReadSubnet() throws ReadFailedException {
348         FaasPolicyManager policyManager = new FaasPolicyManager(dataProvider, executor);
349         ReadOnlyTransaction roTx = mock(ReadOnlyTransaction.class);
350         CheckedFuture<Optional<Subnet>, ReadFailedException> futureSubnet =
351                 mock(CheckedFuture.class);
352         Optional<Subnet> optSubnet = mock(Optional.class);
353         when(optSubnet.isPresent()).thenReturn(true);
354         when(futureSubnet.checkedGet()).thenReturn(optSubnet);
355         when(roTx.read(LogicalDatastoreType.CONFIGURATION, IidFactory.subnetIid(tenantId, subnetId))).thenReturn(futureSubnet);
356         doNothing().when(roTx).close();
357         when(dataProvider.newReadOnlyTransaction()).thenReturn(roTx);
358
359         policyManager.readSubnet(subnetId, tenantId);
360
361         when(optSubnet.isPresent()).thenReturn(false);
362         policyManager.readSubnet(subnetId, tenantId);
363     }
364
365     @SuppressWarnings("unchecked")
366     @Test
367     public void testReadL3ContextInstance() throws ReadFailedException {
368         FaasPolicyManager policyManager = new FaasPolicyManager(dataProvider, executor);
369         ReadOnlyTransaction roTx = mock(ReadOnlyTransaction.class);
370         CheckedFuture<Optional<L3Context>, ReadFailedException> futureL3Context =
371                 mock(CheckedFuture.class);
372         Optional<L3Context> optL3Context = mock(Optional.class);
373         when(optL3Context.isPresent()).thenReturn(true);
374         when(futureL3Context.checkedGet()).thenReturn(optL3Context);
375
376         L3ContextId l3cId = new L3ContextId("l3cId");
377         when(roTx.read(LogicalDatastoreType.CONFIGURATION, IidFactory.l3ContextIid(tenantId, l3cId))).thenReturn(futureL3Context);
378         doNothing().when(roTx).close();
379         when(dataProvider.newReadOnlyTransaction()).thenReturn(roTx);
380
381         policyManager.readL3ContextInstance(tenantId, l3cId);
382
383         when(optL3Context.isPresent()).thenReturn(false);
384         policyManager.readL3ContextInstance(tenantId, l3cId);
385     }
386
387     @SuppressWarnings("unchecked")
388     @Test
389     public void testReadL2BridgeDomainInstance() throws ReadFailedException {
390         FaasPolicyManager policyManager = new FaasPolicyManager(dataProvider, executor);
391         ReadOnlyTransaction roTx = mock(ReadOnlyTransaction.class);
392         CheckedFuture<Optional<L2BridgeDomain>, ReadFailedException> futureL2BridgeDomain =
393                 mock(CheckedFuture.class);
394         Optional<L2BridgeDomain> optL2BridgeDomain = mock(Optional.class);
395         when(optL2BridgeDomain.isPresent()).thenReturn(true);
396         when(futureL2BridgeDomain.checkedGet()).thenReturn(optL2BridgeDomain);
397
398         L2BridgeDomainId l2bId = new L2BridgeDomainId("l2bId");
399         when(roTx.read(LogicalDatastoreType.CONFIGURATION, IidFactory.l2BridgeDomainIid(tenantId,
400                 l2bId))).thenReturn(futureL2BridgeDomain);
401         doNothing().when(roTx).close();
402         when(dataProvider.newReadOnlyTransaction()).thenReturn(roTx);
403
404         policyManager.readL2BridgeDomainInstance(tenantId, l2bId);
405
406         when(optL2BridgeDomain.isPresent()).thenReturn(false);
407         policyManager.readL2BridgeDomainInstance(tenantId, l2bId);
408     }
409
410     @SuppressWarnings("unchecked")
411     @Test
412     public void testReadL2FloodDomain() throws ReadFailedException {
413         FaasPolicyManager policyManager = new FaasPolicyManager(dataProvider, executor);
414         ReadOnlyTransaction roTx = mock(ReadOnlyTransaction.class);
415         CheckedFuture<Optional<L2FloodDomain>, ReadFailedException> futureL2FloodDomain =
416                 mock(CheckedFuture.class);
417         Optional<L2FloodDomain> optL2FloodDomain = mock(Optional.class);
418         when(optL2FloodDomain.isPresent()).thenReturn(true);
419         when(futureL2FloodDomain.checkedGet()).thenReturn(optL2FloodDomain);
420
421         L2FloodDomainId l2fId = new L2FloodDomainId("l2fId");
422         when(roTx.read(LogicalDatastoreType.CONFIGURATION, IidFactory.l2FloodDomainIid(tenantId,
423                 l2fId))).thenReturn(futureL2FloodDomain);
424         doNothing().when(roTx).close();
425         when(dataProvider.newReadOnlyTransaction()).thenReturn(roTx);
426
427         policyManager.readL2FloodDomain(l2fId, tenantId);
428
429         when(optL2FloodDomain.isPresent()).thenReturn(false);
430         policyManager.readL2FloodDomain(l2fId, tenantId);
431     }
432
433     @SuppressWarnings("unchecked")
434     @Test
435     public void testNeedToCreateLogicalNetwork() throws ReadFailedException {
436
437         ServiceCommunicationLayer comLayer = ServiceCommunicationLayer.Layer2;
438         List<SubnetId> consSubnetIds = new ArrayList<>();
439         SubnetId consSubnetId = new SubnetId("consSubnetId");
440         consSubnetIds.add(consSubnetId);
441         List<SubnetId> provSubnetIds = new ArrayList<>();
442         SubnetId provSubnetId = new SubnetId("provSubnetId");
443         provSubnetIds.add(provSubnetId);
444         ContractId contractId = new ContractId("contractId");
445         EndpointGroup providerEpg = new EndpointGroupBuilder()
446                 .setId(providerEpgId)
447                 .build();
448         EndpointGroup consumerEpg = new EndpointGroupBuilder()
449                 .setId(consumerEpgId)
450                 .build();
451         ExternalImplicitGroup externalImplicitGroup = ExternalImplicitGroup.ProviderEpg;
452
453         FaasPolicyManager policyManager = new FaasPolicyManager(dataProvider, executor);
454         ReadOnlyTransaction roTx = mock(ReadOnlyTransaction.class);
455         CheckedFuture<Optional<LogicalNetwork>, ReadFailedException> futureLogicalNetwork =
456                 mock(CheckedFuture.class);
457         Optional<LogicalNetwork> optLogicalNetwork = mock(Optional.class);
458         when(optLogicalNetwork.isPresent()).thenReturn(true);
459         LogicalNetwork logicalNet =
460                 new LogicalNetworkBuilder()
461                         .setCommunicationLayer(ServiceCommunicationLayer.Layer2)
462                         .setConsumerNetwork(
463                                 new ConsumerNetworkBuilder().setNetworkScopeType(ScopeType.Private)
464                                         .setGbpSubnetId(consSubnetIds)
465                                         .build())
466                         .setProviderNetwork(
467                                 new ProviderNetworkBuilder().setNetworkScopeType(ScopeType.Public)
468                                         .setGbpSubnetId(provSubnetIds)
469                                         .build())
470                         .build();
471         when(optLogicalNetwork.get()).thenReturn(logicalNet);
472         when(futureLogicalNetwork.checkedGet()).thenReturn(optLogicalNetwork);
473
474         when(roTx.read(LogicalDatastoreType.OPERATIONAL,
475                 FaasIidFactory.logicalNetworkIid(consumerEpg.getId(), tenantId, contractId,
476                         providerEpg.getId(), tenantId))).thenReturn(futureLogicalNetwork);
477         doNothing().when(roTx).close();
478         when(dataProvider.newReadOnlyTransaction()).thenReturn(roTx);
479
480         policyManager.needToCreateLogicalNetwork(comLayer, consSubnetIds, provSubnetIds, tenantId,
481                 contractId, providerEpg, consumerEpg, externalImplicitGroup);
482     }
483
484     @SuppressWarnings("unchecked")
485     @Test
486     public void testRemoveLogicalNetwork()
487             throws ReadFailedException, TransactionCommitFailedException {
488         ServiceCommunicationLayer comLayer = ServiceCommunicationLayer.Layer2;
489         List<SubnetId> consSubnetIds = new ArrayList<>();
490         SubnetId consSubnetId = new SubnetId("consSubnetId");
491         consSubnetIds.add(consSubnetId);
492         List<SubnetId> provSubnetIds = new ArrayList<>();
493         SubnetId provSubnetId = new SubnetId("provSubnetId");
494         provSubnetIds.add(provSubnetId);
495         ContractId contractId = new ContractId("contractId");
496         EndpointGroup providerEpg = new EndpointGroupBuilder()
497                 .setId(providerEpgId)
498                 .build();
499         EndpointGroup consumerEpg = new EndpointGroupBuilder()
500                 .setId(consumerEpgId)
501                 .build();
502         ExternalImplicitGroup externalImplicitGroup = ExternalImplicitGroup.ProviderEpg;
503
504         PowerMockito.mockStatic(UlnDatastoreApi.class);
505         PowerMockito.doNothing().when(UlnDatastoreApi.class);
506         UlnDatastoreApi.removeLogicalSwitchFromDsIfExists(any(Uuid.class), any(Uuid.class));
507         PowerMockito.doNothing().when(UlnDatastoreApi.class);
508         UlnDatastoreApi.removeLogicalRouterFromDsIfExists(any(Uuid.class), any(Uuid.class));
509
510         FaasPolicyManager policyManager = new FaasPolicyManager(dataProvider, executor);
511         ReadWriteTransaction rwTx = mock(ReadWriteTransaction.class);
512         CheckedFuture<Optional<LogicalNetwork>, ReadFailedException> futureLogicalNetwork =
513                 mock(CheckedFuture.class);
514         CheckedFuture<Void, TransactionCommitFailedException> futureVoid =
515                 mock(CheckedFuture.class);
516         Optional<LogicalNetwork> optLogicalNetwork = mock(Optional.class);
517         Optional<Void> optVoid = mock(Optional.class);
518         when(optLogicalNetwork.isPresent()).thenReturn(true);
519         LogicalNetwork logicalNet =
520                 new LogicalNetworkBuilder().setCommunicationLayer(ServiceCommunicationLayer.Layer2)
521                         .setConsumerNetwork(
522                                 new ConsumerNetworkBuilder().setNetworkScopeType(ScopeType.Private)
523                                         .setGbpSubnetId(consSubnetIds)
524                                         .setLogicalRouterId(dummyUuid1)
525                                         .build())
526                         .setProviderNetwork(
527                                 new ProviderNetworkBuilder().setNetworkScopeType(ScopeType.Public)
528                                         .setGbpSubnetId(provSubnetIds)
529                                         .setLogicalRouterId(dummyUuid2).build())
530                         .setConsumerTenantId(tenantId)
531                         .setProviderTenantId(tenantId)
532                         .build();
533         when(optLogicalNetwork.get()).thenReturn(logicalNet);
534         when(futureLogicalNetwork.checkedGet()).thenReturn(optLogicalNetwork);
535         when(futureVoid.checkedGet()).thenReturn(null);
536
537         when(rwTx.read(LogicalDatastoreType.OPERATIONAL,
538                 FaasIidFactory.logicalNetworkIid(consumerEpg.getId(), tenantId, contractId,
539                         providerEpg.getId(), tenantId))).thenReturn(futureLogicalNetwork);
540         when(rwTx.submit()).thenReturn(futureVoid);
541         when(dataProvider.newReadWriteTransaction()).thenReturn(rwTx);
542
543         List<PolicyRuleGroup> prg = new ArrayList<>();
544         PolicyRuleGroup prgElement = new PolicyRuleGroupBuilder()
545                 .setContractId(new ContractId("contractId"))
546                 .build();
547         prg.add(prgElement);
548         List<PolicyRuleGroupWithEndpointConstraints> prgwec = new ArrayList<>();
549         PolicyRuleGroupWithEndpointConstraints prgwecElement = new PolicyRuleGroupWithEndpointConstraintsBuilder()
550                 .setPolicyRuleGroup(prg)
551                 .build();
552         prgwec.add(prgwecElement);
553         ResolvedPolicy oldPolicy = new ResolvedPolicyBuilder()
554                 .setConsumerEpgId(consumerEpgId)
555                 .setConsumerTenantId(tenantId)
556                 .setProviderEpgId(providerEpgId)
557                 .setProviderTenantId(tenantId)
558                 .setPolicyRuleGroupWithEndpointConstraints(prgwec)
559                 .build();
560
561         policyManager.removeLogicalNetwork(oldPolicy);
562
563     }
564
565     @Test
566     public void testRemoveLogicalNetwork_null() {
567         FaasPolicyManager policyManager = new FaasPolicyManager(dataProvider, executor);
568
569         policyManager.removeLogicalNetwork(null);
570     }
571
572     @SuppressWarnings("unchecked")
573     @Test
574     public void testCreateLayer3LogicalNetwork() throws ReadFailedException {
575         ReadOnlyTransaction roTx1 = mock(ReadOnlyTransaction.class);
576         ReadOnlyTransaction roTx2 = mock(ReadOnlyTransaction.class);
577
578         CheckedFuture<Optional<ResolvedPolicies>, ReadFailedException> futureResolvedPolicies =
579                 mock(CheckedFuture.class);
580         Optional<ResolvedPolicies> optResolvedPolicies = mock(Optional.class);
581         when(futureResolvedPolicies.checkedGet()).thenReturn(optResolvedPolicies);
582         when(roTx1.read(LogicalDatastoreType.OPERATIONAL,
583                 InstanceIdentifier.builder(ResolvedPolicies.class).build())).thenReturn(
584                 futureResolvedPolicies);
585
586         MappedSubnet mappedSubnet = new MappedSubnetBuilder()
587                 .setFaasSubnetId(dummyUuid1)
588                 .build();
589         CheckedFuture<Optional<MappedSubnet>, ReadFailedException> futureMappedSubnet =
590                 mock(CheckedFuture.class);
591         Optional<MappedSubnet> optMappedSubnet = mock(Optional.class);
592         when(optMappedSubnet.isPresent()).thenReturn(false);
593         when(optMappedSubnet.get()).thenReturn(mappedSubnet);
594         when(futureMappedSubnet.checkedGet()).thenReturn(optMappedSubnet);
595         when(roTx2.read(LogicalDatastoreType.OPERATIONAL,
596                 FaasIidFactory.mappedSubnetIid(tenantId, subnetId))).thenReturn(
597                 futureMappedSubnet);
598
599         when(dataProvider.newReadOnlyTransaction()).thenReturn(roTx1);
600         when(dataProvider.newReadOnlyTransaction()).thenReturn(roTx2);
601
602         EndpointGroup consumerEpg = new EndpointGroupBuilder().setId(consumerEpgId).build();
603         EndpointGroup providerEpg = new EndpointGroupBuilder().setId(providerEpgId).build();
604         FaasPolicyManager policyManager = new FaasPolicyManager(dataProvider, executor);
605
606         policyManager.createLayer3LogicalNetwork(consumerEpg, contractId, providerEpg, tenantId,
607                 ServiceCommunicationLayer.Layer3, ExternalImplicitGroup.ProviderEpg);
608     }
609
610 }