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 java.util.ArrayList;
21 import java.util.HashSet;
22 import java.util.List;
24 import java.util.concurrent.Executors;
25 import java.util.concurrent.ScheduledExecutorService;
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;
87 @RunWith(PowerMockRunner.class)
88 @PrepareForTest({UlnDatastoreApi.class})
89 public class FaasPolicyManagerCovrgTest {
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");
110 @SuppressWarnings("unchecked")
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);
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);
124 Set<InstanceIdentifier<?>> removedPaths = new HashSet<>();
125 removedPaths.add(policyId);
126 when(change.getRemovedPaths()).thenReturn(removedPaths);
130 public void testConstructor() throws Exception {
131 FaasPolicyManager other = new MockFaasPolicyManager(dataProvider, executor);
133 verify(dataProvider).registerDataChangeListener(LogicalDatastoreType.OPERATIONAL,
134 InstanceIdentifier.builder(ResolvedPolicies.class)
135 .child(ResolvedPolicy.class)
136 .build(), other, AsyncDataBroker.DataChangeScope.SUBTREE);
140 @SuppressWarnings("unchecked")
142 public void testRegisterTenant() throws Exception {
143 ReadOnlyTransaction roTx = mock(ReadOnlyTransaction.class);
144 when(dataProvider.newReadOnlyTransaction()).thenReturn(roTx);
146 ReadWriteTransaction rwTx = mock(ReadWriteTransaction.class);
147 when(dataProvider.newReadWriteTransaction()).thenReturn(rwTx);
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);
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);
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);
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);
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>())
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);
191 EndpointGroupId epgId = new EndpointGroupId("epgId");
192 FaasPolicyManager policyManager = spy(new FaasPolicyManager(dataProvider, executor));
193 doNothing().when(policyManager).removeTenantLogicalNetwork(tenantId, faasTenantId, false);
195 policyManager.registerTenant(tenantId, epgId);
199 public void testRegisterTenant_null(){
200 FaasPolicyManager policyManager = spy(new FaasPolicyManager(dataProvider, executor));
201 doNothing().when(policyManager).registerTenant(tenantId, null);
203 policyManager.registerTenant(tenantId);
206 @SuppressWarnings("unchecked")
208 public void testRemoveTenantLogicalNetwork() throws ReadFailedException {
209 ReadOnlyTransaction roTx = mock(ReadOnlyTransaction.class);
210 when(dataProvider.newReadOnlyTransaction()).thenReturn(roTx);
212 ReadWriteTransaction rwTx = mock(ReadWriteTransaction.class);
213 when(dataProvider.newReadWriteTransaction()).thenReturn(rwTx);
215 CheckedFuture<Void, TransactionCommitFailedException> futureVoid = mock(CheckedFuture.class);
216 when(rwTx.submit()).thenReturn(futureVoid);
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)
231 LogicalNetworks logicalNetworks = new LogicalNetworksBuilder()
232 .setLogicalNetwork(lns)
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);
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);
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);
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);
263 PowerMockito.mockStatic(UlnDatastoreApi.class);
264 PowerMockito.doNothing().when(UlnDatastoreApi.class);
265 UlnDatastoreApi.removeTenantFromDsIfExists(any(Uuid.class));
267 FaasPolicyManager policyManager = spy(new FaasPolicyManager(dataProvider, executor));
269 policyManager.removeTenantLogicalNetwork(tenantId, faasTenantId);
273 public void testUnregisterTenant() throws Exception {
274 FaasPolicyManager policyManager = new FaasPolicyManager(dataProvider, executor);
276 policyManager.unregisterTenant(tenantId);
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"));
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"))
294 assertFalse(policyManager.handledPolicy(policy));
298 public void testRegisterFollowedEndpointgroup(){
299 EndpointGroupId epgId = new EndpointGroupId("epgId");
300 FaasPolicyManager policyManager = new FaasPolicyManager(dataProvider, executor);
302 policyManager.registerFollowedEndpointgroup(tenantId, null);
303 policyManager.registerFollowedEndpointgroup(tenantId, epgId);
306 @SuppressWarnings("unchecked")
308 public void testRegisterSubnetWithEpg() throws ReadFailedException {
309 FaasPolicyManager policyManager = new FaasPolicyManager(dataProvider, executor);
311 ReadOnlyTransaction roTx = mock(ReadOnlyTransaction.class);
312 when(dataProvider.newReadOnlyTransaction()).thenReturn(roTx);
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);
323 policyManager.registerSubnetWithEpg(epgId, tenantId, subnetId);
326 @SuppressWarnings("unchecked")
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);
341 policyManager.readEndpointGroup(epgId, tenantId);
343 when(optEndpointGroup.isPresent()).thenReturn(false);
344 policyManager.readEndpointGroup(epgId, tenantId);
347 @SuppressWarnings("unchecked")
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);
361 policyManager.readSubnet(subnetId, tenantId);
363 when(optSubnet.isPresent()).thenReturn(false);
364 policyManager.readSubnet(subnetId, tenantId);
367 @SuppressWarnings("unchecked")
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);
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);
383 policyManager.readL3ContextInstance(tenantId, l3cId);
385 when(optL3Context.isPresent()).thenReturn(false);
386 policyManager.readL3ContextInstance(tenantId, l3cId);
389 @SuppressWarnings("unchecked")
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);
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);
406 policyManager.readL2BridgeDomainInstance(tenantId, l2bId);
408 when(optL2BridgeDomain.isPresent()).thenReturn(false);
409 policyManager.readL2BridgeDomainInstance(tenantId, l2bId);
412 @SuppressWarnings("unchecked")
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);
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);
429 policyManager.readL2FloodDomain(l2fId, tenantId);
431 when(optL2FloodDomain.isPresent()).thenReturn(false);
432 policyManager.readL2FloodDomain(l2fId, tenantId);
435 @SuppressWarnings("unchecked")
437 public void testNeedToCreateLogicalNetwork() throws ReadFailedException {
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)
450 EndpointGroup consumerEpg = new EndpointGroupBuilder()
451 .setId(consumerEpgId)
453 ExternalImplicitGroup externalImplicitGroup = ExternalImplicitGroup.ProviderEpg;
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)
465 new ConsumerNetworkBuilder().setNetworkScopeType(ScopeType.Private)
466 .setGbpSubnetId(consSubnetIds)
469 new ProviderNetworkBuilder().setNetworkScopeType(ScopeType.Public)
470 .setGbpSubnetId(provSubnetIds)
473 when(optLogicalNetwork.get()).thenReturn(logicalNet);
474 when(futureLogicalNetwork.checkedGet()).thenReturn(optLogicalNetwork);
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);
482 policyManager.needToCreateLogicalNetwork(comLayer, consSubnetIds, provSubnetIds, tenantId,
483 contractId, providerEpg, consumerEpg, externalImplicitGroup);
486 @SuppressWarnings("unchecked")
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)
501 EndpointGroup consumerEpg = new EndpointGroupBuilder()
502 .setId(consumerEpgId)
504 ExternalImplicitGroup externalImplicitGroup = ExternalImplicitGroup.ProviderEpg;
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));
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)
524 new ConsumerNetworkBuilder().setNetworkScopeType(ScopeType.Private)
525 .setGbpSubnetId(consSubnetIds)
526 .setLogicalRouterId(dummyUuid1)
529 new ProviderNetworkBuilder().setNetworkScopeType(ScopeType.Public)
530 .setGbpSubnetId(provSubnetIds)
531 .setLogicalRouterId(dummyUuid2).build())
532 .setConsumerTenantId(tenantId)
533 .setProviderTenantId(tenantId)
535 when(optLogicalNetwork.get()).thenReturn(logicalNet);
536 when(futureLogicalNetwork.checkedGet()).thenReturn(optLogicalNetwork);
537 when(futureVoid.checkedGet()).thenReturn(null);
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);
545 List<PolicyRuleGroup> prg = new ArrayList<>();
546 PolicyRuleGroup prgElement = new PolicyRuleGroupBuilder()
547 .setContractId(new ContractId("contractId"))
550 List<PolicyRuleGroupWithEndpointConstraints> prgwec = new ArrayList<>();
551 PolicyRuleGroupWithEndpointConstraints prgwecElement = new PolicyRuleGroupWithEndpointConstraintsBuilder()
552 .setPolicyRuleGroup(prg)
554 prgwec.add(prgwecElement);
555 ResolvedPolicy oldPolicy = new ResolvedPolicyBuilder()
556 .setConsumerEpgId(consumerEpgId)
557 .setConsumerTenantId(tenantId)
558 .setProviderEpgId(providerEpgId)
559 .setProviderTenantId(tenantId)
560 .setPolicyRuleGroupWithEndpointConstraints(prgwec)
563 policyManager.removeLogicalNetwork(oldPolicy);
568 public void testRemoveLogicalNetwork_null(){
569 FaasPolicyManager policyManager = new FaasPolicyManager(dataProvider, executor);
571 policyManager.removeLogicalNetwork(null);
574 /*@SuppressWarnings("unchecked")
576 public void testCreateLayer3LogicalNetwork() throws ReadFailedException {
577 ReadOnlyTransaction roTx = mock(ReadOnlyTransaction.class);
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);
587 MappedSubnet mappedSubnet = new MappedSubnetBuilder()
588 .setFaasSubnetId(dummyUuid1)
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(
600 when(dataProvider.newReadOnlyTransaction()).thenReturn(roTx);
602 EndpointGroup consumerEpg = new EndpointGroupBuilder().setId(consumerEpgId).build();
603 EndpointGroup providerEpg = new EndpointGroupBuilder().setId(providerEpgId).build();
604 FaasPolicyManager policyManager = new FaasPolicyManager(dataProvider, executor);
606 policyManager.registerSubnetWithEpg(consumerEpgId, tenantId, consumerSubnet);
607 policyManager.registerSubnetWithEpg(providerEpgId, tenantId, providerSubnet);
608 policyManager.createLayer3LogicalNetwork(consumerEpg, contractId, providerEpg, tenantId,
609 ServiceCommunicationLayer.Layer3, ExternalImplicitGroup.ProviderEpg);
616 private L3Context makeTestL3Context() {
617 L3ContextBuilder builder = new L3ContextBuilder();
618 builder.setId(l3Context);
619 return builder.build();
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();
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();
636 private EndpointGroup makeTestEndpointGroup(EndpointGroupId epgId) {
637 EndpointGroupBuilder builder = new EndpointGroupBuilder();
638 builder.setId(epgId);
639 return builder.build();
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();
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();
668 private DataObject makeTestResolvedPolicyWithImpExternalEpg() {
669 ResolvedPolicyBuilder builder = new ResolvedPolicyBuilder((ResolvedPolicy) makeTestResolvedPolicy());
670 builder.setExternalImplicitGroup(ExternalImplicitGroup.ConsumerEpg);
671 return builder.build();