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