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