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