2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.groupbasedpolicy.renderer.faas;
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;
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;
81 @RunWith(PowerMockRunner.class)
82 @PrepareForTest(UlnDatastoreApi.class)
83 public class FaasPolicyManagerCovrgTest {
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");
102 @SuppressWarnings("unchecked")
104 public void init() throws Exception {
105 policyId = mock(InstanceIdentifier.class);
106 policyId = mock(InstanceIdentifier.class);
107 dataProvider = mock(DataBroker.class);
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);
117 public void testConstructor() throws Exception {
118 FaasPolicyManager other = new MockFaasPolicyManager(dataProvider, executor);
120 verify(dataProvider).registerDataTreeChangeListener(new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL,
121 InstanceIdentifier.builder(ResolvedPolicies.class).child(ResolvedPolicy.class).build()), other);
125 @SuppressWarnings("unchecked")
127 public void testRegisterTenant() throws Exception {
128 ReadOnlyTransaction roTx = mock(ReadOnlyTransaction.class);
129 when(dataProvider.newReadOnlyTransaction()).thenReturn(roTx);
131 ReadWriteTransaction rwTx = mock(ReadWriteTransaction.class);
132 when(dataProvider.newReadWriteTransaction()).thenReturn(rwTx);
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);
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);
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);
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);
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>())
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);
176 EndpointGroupId epgId = new EndpointGroupId("epgId");
177 FaasPolicyManager policyManager = spy(new FaasPolicyManager(dataProvider, executor));
178 doNothing().when(policyManager).removeTenantLogicalNetwork(tenantId, faasTenantId, false);
180 policyManager.registerTenant(tenantId, epgId);
184 public void testRegisterTenant_null() {
185 FaasPolicyManager policyManager = spy(new FaasPolicyManager(dataProvider, executor));
186 doNothing().when(policyManager).registerTenant(tenantId, null);
188 policyManager.registerTenant(tenantId);
191 @SuppressWarnings("unchecked")
193 public void testRemoveTenantLogicalNetwork() throws ReadFailedException {
194 ReadOnlyTransaction roTx = mock(ReadOnlyTransaction.class);
195 when(dataProvider.newReadOnlyTransaction()).thenReturn(roTx);
197 ReadWriteTransaction rwTx = mock(ReadWriteTransaction.class);
198 when(dataProvider.newReadWriteTransaction()).thenReturn(rwTx);
200 CheckedFuture<Void, TransactionCommitFailedException> futureVoid = mock(CheckedFuture.class);
201 when(rwTx.submit()).thenReturn(futureVoid);
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)
216 LogicalNetworks logicalNetworks = new LogicalNetworksBuilder()
217 .setLogicalNetwork(lns)
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);
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);
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);
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);
248 PowerMockito.mockStatic(UlnDatastoreApi.class);
249 PowerMockito.doNothing().when(UlnDatastoreApi.class);
250 UlnDatastoreApi.removeTenantFromDsIfExists(any(Uuid.class));
252 FaasPolicyManager policyManager = spy(new FaasPolicyManager(dataProvider, executor));
254 policyManager.removeTenantLogicalNetwork(tenantId, faasTenantId);
258 public void testUnregisterTenant() throws Exception {
259 FaasPolicyManager policyManager = new FaasPolicyManager(dataProvider, executor);
261 policyManager.unregisterTenant(tenantId);
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"));
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"))
279 assertFalse(policyManager.handledPolicy(policy));
283 public void testRegisterFollowedEndpointgroup() {
284 EndpointGroupId epgId = new EndpointGroupId("epgId");
285 FaasPolicyManager policyManager = new FaasPolicyManager(dataProvider, executor);
287 policyManager.registerFollowedEndpointgroup(tenantId, null);
288 policyManager.registerFollowedEndpointgroup(tenantId, epgId);
291 @SuppressWarnings("unchecked")
293 public void testRegisterSubnetWithEpg() throws ReadFailedException {
294 FaasPolicyManager policyManager = new FaasPolicyManager(dataProvider, executor);
296 ReadOnlyTransaction roTx = mock(ReadOnlyTransaction.class);
297 when(dataProvider.newReadOnlyTransaction()).thenReturn(roTx);
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);
308 policyManager.registerSubnetWithEpg(epgId, tenantId, subnetId);
311 @SuppressWarnings("unchecked")
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);
326 policyManager.readEndpointGroup(epgId, tenantId);
328 when(optEndpointGroup.isPresent()).thenReturn(false);
329 policyManager.readEndpointGroup(epgId, tenantId);
332 @SuppressWarnings("unchecked")
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);
346 policyManager.readSubnet(subnetId, tenantId);
348 when(optSubnet.isPresent()).thenReturn(false);
349 policyManager.readSubnet(subnetId, tenantId);
352 @SuppressWarnings("unchecked")
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);
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);
368 policyManager.readL3ContextInstance(tenantId, l3cId);
370 when(optL3Context.isPresent()).thenReturn(false);
371 policyManager.readL3ContextInstance(tenantId, l3cId);
374 @SuppressWarnings("unchecked")
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);
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);
391 policyManager.readL2BridgeDomainInstance(tenantId, l2bId);
393 when(optL2BridgeDomain.isPresent()).thenReturn(false);
394 policyManager.readL2BridgeDomainInstance(tenantId, l2bId);
397 @SuppressWarnings("unchecked")
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);
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);
414 policyManager.readL2FloodDomain(l2fId, tenantId);
416 when(optL2FloodDomain.isPresent()).thenReturn(false);
417 policyManager.readL2FloodDomain(l2fId, tenantId);
420 @SuppressWarnings("unchecked")
422 public void testNeedToCreateLogicalNetwork() throws ReadFailedException {
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)
435 EndpointGroup consumerEpg = new EndpointGroupBuilder()
436 .setId(consumerEpgId)
438 ExternalImplicitGroup externalImplicitGroup = ExternalImplicitGroup.ProviderEpg;
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)
450 new ConsumerNetworkBuilder().setNetworkScopeType(ScopeType.Private)
451 .setGbpSubnetId(consSubnetIds)
454 new ProviderNetworkBuilder().setNetworkScopeType(ScopeType.Public)
455 .setGbpSubnetId(provSubnetIds)
458 when(optLogicalNetwork.get()).thenReturn(logicalNet);
459 when(futureLogicalNetwork.checkedGet()).thenReturn(optLogicalNetwork);
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);
467 policyManager.needToCreateLogicalNetwork(comLayer, consSubnetIds, provSubnetIds, tenantId,
468 contractId, providerEpg, consumerEpg, externalImplicitGroup);
471 @SuppressWarnings("unchecked")
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)
486 EndpointGroup consumerEpg = new EndpointGroupBuilder()
487 .setId(consumerEpgId)
489 ExternalImplicitGroup externalImplicitGroup = ExternalImplicitGroup.ProviderEpg;
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));
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)
509 new ConsumerNetworkBuilder().setNetworkScopeType(ScopeType.Private)
510 .setGbpSubnetId(consSubnetIds)
511 .setLogicalRouterId(dummyUuid1)
514 new ProviderNetworkBuilder().setNetworkScopeType(ScopeType.Public)
515 .setGbpSubnetId(provSubnetIds)
516 .setLogicalRouterId(dummyUuid2).build())
517 .setConsumerTenantId(tenantId)
518 .setProviderTenantId(tenantId)
520 when(optLogicalNetwork.get()).thenReturn(logicalNet);
521 when(futureLogicalNetwork.checkedGet()).thenReturn(optLogicalNetwork);
522 when(futureVoid.checkedGet()).thenReturn(null);
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);
530 List<PolicyRuleGroup> prg = new ArrayList<>();
531 PolicyRuleGroup prgElement = new PolicyRuleGroupBuilder()
532 .setContractId(new ContractId("contractId"))
535 List<PolicyRuleGroupWithEndpointConstraints> prgwec = new ArrayList<>();
536 PolicyRuleGroupWithEndpointConstraints prgwecElement = new PolicyRuleGroupWithEndpointConstraintsBuilder()
537 .setPolicyRuleGroup(prg)
539 prgwec.add(prgwecElement);
540 ResolvedPolicy oldPolicy = new ResolvedPolicyBuilder()
541 .setConsumerEpgId(consumerEpgId)
542 .setConsumerTenantId(tenantId)
543 .setProviderEpgId(providerEpgId)
544 .setProviderTenantId(tenantId)
545 .setPolicyRuleGroupWithEndpointConstraints(prgwec)
548 policyManager.removeLogicalNetwork(oldPolicy);
553 public void testRemoveLogicalNetwork_null() {
554 FaasPolicyManager policyManager = new FaasPolicyManager(dataProvider, executor);
556 policyManager.removeLogicalNetwork(null);
559 @SuppressWarnings("unchecked")
561 public void testCreateLayer3LogicalNetwork() throws ReadFailedException {
562 ReadOnlyTransaction roTx1 = mock(ReadOnlyTransaction.class);
563 ReadOnlyTransaction roTx2 = mock(ReadOnlyTransaction.class);
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);
573 MappedSubnet mappedSubnet = new MappedSubnetBuilder()
574 .setFaasSubnetId(dummyUuid1)
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(
586 when(dataProvider.newReadOnlyTransaction()).thenReturn(roTx1);
587 when(dataProvider.newReadOnlyTransaction()).thenReturn(roTx2);
589 EndpointGroup consumerEpg = new EndpointGroupBuilder().setId(consumerEpgId).build();
590 EndpointGroup providerEpg = new EndpointGroupBuilder().setId(providerEpgId).build();
591 FaasPolicyManager policyManager = new FaasPolicyManager(dataProvider, executor);
593 policyManager.createLayer3LogicalNetwork(consumerEpg, contractId, providerEpg, tenantId,
594 ServiceCommunicationLayer.Layer3, ExternalImplicitGroup.ProviderEpg);