1 package org.opendaylight.groupbasedpolicy.renderer.faas;
3 import static org.junit.Assert.assertFalse;
4 import static org.junit.Assert.assertTrue;
5 import static org.mockito.Matchers.any;
6 import static org.mockito.Matchers.eq;
7 import static org.mockito.Mockito.doNothing;
8 import static org.mockito.Mockito.mock;
9 import static org.mockito.Mockito.spy;
10 import static org.mockito.Mockito.verify;
11 import static org.mockito.Mockito.when;
13 import java.util.ArrayList;
14 import java.util.HashSet;
15 import java.util.List;
17 import java.util.concurrent.Executors;
18 import java.util.concurrent.ScheduledExecutorService;
20 import com.google.common.base.Optional;
21 import com.google.common.util.concurrent.CheckedFuture;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.junit.runner.RunWith;
25 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
26 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
27 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
28 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
29 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
30 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
31 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
32 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
33 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
34 import org.opendaylight.faas.uln.datastore.api.UlnDatastoreApi;
35 import org.opendaylight.groupbasedpolicy.util.IidFactory;
36 import org.opendaylight.groupbasedpolicy.util.TenantUtils;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.faas.logical.faas.common.rev151013.Uuid;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ContractId;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.EndpointGroupId;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.L2BridgeDomainId;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.L2FloodDomainId;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.L3ContextId;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.SubnetId;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.faas.rev151009.LogicalNetworks;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.faas.rev151009.LogicalNetworksBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.faas.rev151009.ScopeType;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.faas.rev151009.ServiceCommunicationLayer;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.faas.rev151009.logical.networks.LogicalNetwork;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.faas.rev151009.logical.networks.LogicalNetworkBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.faas.rev151009.logical.networks.logical.network.ConsumerNetworkBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.faas.rev151009.logical.networks.logical.network.ProviderNetworkBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.faas.rev151009.mapped.tenants.entities.MappedEntity;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.faas.rev151009.mapped.tenants.entities.MappedTenant;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.Tenant;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.L2BridgeDomain;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.L2BridgeDomainBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.L2FloodDomain;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.L2FloodDomainBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.L3Context;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.L3ContextBuilder;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.Subnet;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.SubnetBuilder;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.EndpointGroup;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.EndpointGroupBuilder;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.resolved.policy.rev150828.ResolvedPolicies;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.resolved.policy.rev150828.resolved.policies.ResolvedPolicy;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.resolved.policy.rev150828.resolved.policies.ResolvedPolicy.ExternalImplicitGroup;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.resolved.policy.rev150828.resolved.policies.ResolvedPolicyBuilder;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.resolved.policy.rev150828.resolved.policies.resolved.policy.PolicyRuleGroupWithEndpointConstraints;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.resolved.policy.rev150828.resolved.policies.resolved.policy.PolicyRuleGroupWithEndpointConstraintsBuilder;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.resolved.policy.rev150828.resolved.policies.resolved.policy.policy.rule.group.with.endpoint.constraints.PolicyRuleGroup;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.resolved.policy.rev150828.resolved.policies.resolved.policy.policy.rule.group.with.endpoint.constraints.PolicyRuleGroupBuilder;
74 import org.opendaylight.yangtools.yang.binding.DataObject;
75 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
76 import org.powermock.api.mockito.PowerMockito;
77 import org.powermock.core.classloader.annotations.PrepareForTest;
78 import org.powermock.modules.junit4.PowerMockRunner;
80 @RunWith(PowerMockRunner.class)
81 @PrepareForTest({UlnDatastoreApi.class})
82 public class FaasPolicyManagerCovrgTest {
84 private InstanceIdentifier<DataObject> policyId;
85 private AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> change;
86 DataBroker dataProvider;
87 private final ScheduledExecutorService executor = Executors.newScheduledThreadPool(Runtime.getRuntime()
88 .availableProcessors());
89 EndpointGroupId consumerEpgId = new EndpointGroupId("consumerEpgId");
90 SubnetId consumerSubnet = new SubnetId("consumerSubnet");
91 SubnetId providerSubnet = new SubnetId("providerSubnet");
92 EndpointGroupId providerEpgId = new EndpointGroupId("providerEpgId");
93 ContractId contractId = new ContractId("contractId");
94 TenantId tenantId = new TenantId("tenantId");
95 Uuid faasTenantId = new Uuid("0eb98cf5-086c-4a81-8a4e-0c3b4566108b");
96 Uuid faasSecRulesId = new Uuid("1eb98cf5-086c-4a81-8a4e-0c3b4566108b");
97 L3ContextId l3Context = new L3ContextId("l3ContextId");
98 EndpointGroupId epgId = new EndpointGroupId("epgId");
99 SubnetId subnetId = new SubnetId("subnetId");
100 Uuid dummyUuid1 = new Uuid("2eb98cf5-086c-4a81-8a4e-0c3b4566108b");
101 Uuid dummyUuid2 = new Uuid("3eb98cf5-086c-4a81-8a4e-0c3b4566108b");
103 @SuppressWarnings("unchecked")
105 public void init() throws Exception {
106 policyId = mock(InstanceIdentifier.class);
107 change = mock(AsyncDataChangeEvent.class);
108 policyId = mock(InstanceIdentifier.class);
109 dataProvider = mock(DataBroker.class);
111 WriteTransaction writeTransaction = mock(WriteTransaction.class);
112 when(dataProvider.newWriteOnlyTransaction()).thenReturn(writeTransaction);
113 CheckedFuture<Void, TransactionCommitFailedException> futureVoid =
114 mock(CheckedFuture.class);
115 when(writeTransaction.submit()).thenReturn(futureVoid);
117 Set<InstanceIdentifier<?>> removedPaths = new HashSet<>();
118 removedPaths.add(policyId);
119 when(change.getRemovedPaths()).thenReturn(removedPaths);
123 public void testConstructor() throws Exception {
124 FaasPolicyManager other = new MockFaasPolicyManager(dataProvider, executor);
126 verify(dataProvider).registerDataChangeListener(LogicalDatastoreType.OPERATIONAL,
127 InstanceIdentifier.builder(ResolvedPolicies.class)
128 .child(ResolvedPolicy.class)
129 .build(), other, AsyncDataBroker.DataChangeScope.SUBTREE);
133 @SuppressWarnings("unchecked")
135 public void testRegisterTenant() throws Exception {
136 ReadOnlyTransaction roTx = mock(ReadOnlyTransaction.class);
137 when(dataProvider.newReadOnlyTransaction()).thenReturn(roTx);
139 ReadWriteTransaction rwTx = mock(ReadWriteTransaction.class);
140 when(dataProvider.newReadWriteTransaction()).thenReturn(rwTx);
142 CheckedFuture<Optional<MappedTenant>, ReadFailedException> futureMappedTenant =
143 mock(CheckedFuture.class);
144 when(roTx.read(LogicalDatastoreType.OPERATIONAL,
145 FaasIidFactory.mappedTenantIid(tenantId))).thenReturn(futureMappedTenant);
146 Optional<MappedTenant> optMappedTenant = mock(Optional.class);
147 when(optMappedTenant.isPresent()).thenReturn(false);
148 when(futureMappedTenant.checkedGet()).thenReturn(optMappedTenant);
150 CheckedFuture<Optional<Tenant>, ReadFailedException> futureTenant =
151 mock(CheckedFuture.class);
152 when(roTx.read(LogicalDatastoreType.CONFIGURATION,
153 TenantUtils.tenantIid(tenantId))).thenReturn(futureTenant);
154 Optional<Tenant> optTenant = mock(Optional.class);
155 when(futureTenant.checkedGet()).thenReturn(optTenant);
157 CheckedFuture<Optional<MappedEntity>, ReadFailedException> futureMappedEntity =
158 mock(CheckedFuture.class);
159 when(roTx.read(LogicalDatastoreType.OPERATIONAL,
160 FaasIidFactory.mappedEntityIid(tenantId))).thenReturn(futureMappedEntity);
161 Optional<MappedEntity> optMappedEntity = mock(Optional.class);
162 when(futureMappedEntity.checkedGet()).thenReturn(optMappedEntity);
164 CheckedFuture<Optional<ResolvedPolicies>, ReadFailedException> futureResolvedPolicies =
165 mock(CheckedFuture.class);
166 when(roTx.read(LogicalDatastoreType.OPERATIONAL,
167 InstanceIdentifier.builder(ResolvedPolicies.class).build())).thenReturn(futureResolvedPolicies);
168 Optional<ResolvedPolicies> optResolvedPolicies = mock(Optional.class);
169 when(optResolvedPolicies.isPresent()).thenReturn(false);
170 when(futureResolvedPolicies.checkedGet()).thenReturn(optResolvedPolicies);
172 CheckedFuture<Optional<LogicalNetworks>, ReadFailedException> futureLogicalNetworks =
173 mock(CheckedFuture.class);
174 when(roTx.read(LogicalDatastoreType.OPERATIONAL,
175 FaasIidFactory.logicalNetworksIid())).thenReturn(futureLogicalNetworks);
176 LogicalNetworks logicalNetworks = new LogicalNetworksBuilder()
177 .setLogicalNetwork( new ArrayList<LogicalNetwork>())
179 Optional<LogicalNetworks> optLogicalNetworks = mock(Optional.class);
180 when(optLogicalNetworks.isPresent()).thenReturn(true);
181 when(optLogicalNetworks.get()).thenReturn(logicalNetworks);
182 when(futureLogicalNetworks.checkedGet()).thenReturn(optLogicalNetworks);
184 EndpointGroupId epgId = new EndpointGroupId("epgId");
185 FaasPolicyManager policyManager = spy(new FaasPolicyManager(dataProvider, executor));
186 doNothing().when(policyManager).removeTenantLogicalNetwork(tenantId, faasTenantId, false);
188 policyManager.registerTenant(tenantId, epgId);
192 public void testRegisterTenant_null(){
193 FaasPolicyManager policyManager = spy(new FaasPolicyManager(dataProvider, executor));
194 doNothing().when(policyManager).registerTenant(tenantId, null);
196 policyManager.registerTenant(tenantId);
199 @SuppressWarnings("unchecked")
201 public void testRemoveTenantLogicalNetwork() throws ReadFailedException {
202 ReadOnlyTransaction roTx = mock(ReadOnlyTransaction.class);
203 when(dataProvider.newReadOnlyTransaction()).thenReturn(roTx);
205 ReadWriteTransaction rwTx = mock(ReadWriteTransaction.class);
206 when(dataProvider.newReadWriteTransaction()).thenReturn(rwTx);
208 CheckedFuture<Void, TransactionCommitFailedException> futureVoid = mock(CheckedFuture.class);
209 when(rwTx.submit()).thenReturn(futureVoid);
211 CheckedFuture<Optional<LogicalNetworks>, ReadFailedException> futureLogicalNetworks =
212 mock(CheckedFuture.class);
213 when(roTx.read(LogicalDatastoreType.OPERATIONAL,
214 FaasIidFactory.logicalNetworksIid())).thenReturn(futureLogicalNetworks);
215 List<LogicalNetwork> lns = new ArrayList<>();
216 LogicalNetwork ln = new LogicalNetworkBuilder()
217 .setConsumerEpgId(consumerEpgId)
218 .setConsumerTenantId(tenantId)
219 .setContractId(contractId)
220 .setProviderEpgId(providerEpgId)
221 .setProviderTenantId(tenantId)
224 LogicalNetworks logicalNetworks = new LogicalNetworksBuilder()
225 .setLogicalNetwork(lns)
227 Optional<LogicalNetworks> optLogicalNetworks = mock(Optional.class);
228 when(optLogicalNetworks.isPresent()).thenReturn(true);
229 when(optLogicalNetworks.get()).thenReturn(logicalNetworks);
230 when(futureLogicalNetworks.checkedGet()).thenReturn(optLogicalNetworks);
232 CheckedFuture<Optional<LogicalNetwork>, ReadFailedException> futureLogicalNetwork =
233 mock(CheckedFuture.class);
234 when(rwTx.read(eq(LogicalDatastoreType.OPERATIONAL),
235 eq(FaasIidFactory.logicalNetworkIid(consumerEpgId, tenantId, contractId,
236 providerEpgId, tenantId)))).thenReturn(futureLogicalNetwork);
237 Optional<LogicalNetwork> optionalLogicalNetwork = mock(Optional.class);
238 when(futureLogicalNetwork.checkedGet()).thenReturn(optionalLogicalNetwork);
240 CheckedFuture<Optional<MappedEntity>, ReadFailedException> futureMappedEntity =
241 mock(CheckedFuture.class);
242 when(rwTx.read(LogicalDatastoreType.OPERATIONAL,
243 FaasIidFactory.mappedEntityIid(tenantId))).thenReturn(futureMappedEntity);
244 Optional<MappedEntity> optMappedEntity = mock(Optional.class);
245 when(optMappedEntity.isPresent()).thenReturn(true);
246 when(futureMappedEntity.checkedGet()).thenReturn(optMappedEntity);
248 CheckedFuture<Optional<MappedTenant>, ReadFailedException> futureMappedTenant =
249 mock(CheckedFuture.class);
250 when(rwTx.read(LogicalDatastoreType.OPERATIONAL,
251 FaasIidFactory.mappedTenantIid(tenantId))).thenReturn(futureMappedTenant);
252 Optional<MappedTenant> optMappedTenant = mock(Optional.class);
253 when(optMappedTenant.isPresent()).thenReturn(true);
254 when(futureMappedTenant.checkedGet()).thenReturn(optMappedTenant);
256 PowerMockito.mockStatic(UlnDatastoreApi.class);
257 PowerMockito.doNothing().when(UlnDatastoreApi.class);
258 UlnDatastoreApi.removeTenantFromDsIfExists(any(Uuid.class));
260 FaasPolicyManager policyManager = spy(new FaasPolicyManager(dataProvider, executor));
262 policyManager.removeTenantLogicalNetwork(tenantId, faasTenantId);
266 public void testUnregisterTenant() throws Exception {
267 FaasPolicyManager policyManager = new FaasPolicyManager(dataProvider, executor);
269 policyManager.unregisterTenant(tenantId);
273 public void testIsUuid(){
274 assertFalse(FaasPolicyManager.isUUid(null));
275 assertFalse(FaasPolicyManager.isUUid("non-matching string"));
276 assertTrue(FaasPolicyManager.isUUid("12345678-1234-5123-b123-0123456789ab"));
280 public void testHandledPolicy_notEquals(){
281 FaasPolicyManager policyManager = new FaasPolicyManager(dataProvider, executor);
282 ResolvedPolicy policy = new ResolvedPolicyBuilder()
283 .setConsumerTenantId(new TenantId("t1"))
284 .setProviderTenantId(new TenantId("t2"))
287 assertFalse(policyManager.handledPolicy(policy));
291 public void testRegisterFollowedEndpointgroup(){
292 EndpointGroupId epgId = new EndpointGroupId("epgId");
293 FaasPolicyManager policyManager = new FaasPolicyManager(dataProvider, executor);
295 policyManager.registerFollowedEndpointgroup(tenantId, null);
296 policyManager.registerFollowedEndpointgroup(tenantId, epgId);
299 @SuppressWarnings("unchecked")
301 public void testRegisterSubnetWithEpg() throws ReadFailedException {
302 FaasPolicyManager policyManager = new FaasPolicyManager(dataProvider, executor);
304 ReadOnlyTransaction roTx = mock(ReadOnlyTransaction.class);
305 when(dataProvider.newReadOnlyTransaction()).thenReturn(roTx);
307 CheckedFuture<Optional<ResolvedPolicies>, ReadFailedException> futureResolvedPolicies =
308 mock(CheckedFuture.class);
309 when(roTx.read(LogicalDatastoreType.OPERATIONAL,
310 InstanceIdentifier.builder(ResolvedPolicies.class).build())).thenReturn(futureResolvedPolicies);
311 Optional<ResolvedPolicies> optResolvedPolicies = mock(Optional.class);
312 when(optResolvedPolicies.isPresent()).thenReturn(false);
313 when(futureResolvedPolicies.checkedGet()).thenReturn(optResolvedPolicies);
316 policyManager.registerSubnetWithEpg(epgId, tenantId, subnetId);
319 @SuppressWarnings("unchecked")
321 public void testReadEndpointGroup() throws ReadFailedException {
322 FaasPolicyManager policyManager = new FaasPolicyManager(dataProvider, executor);
323 ReadOnlyTransaction roTx = mock(ReadOnlyTransaction.class);
324 CheckedFuture<Optional<EndpointGroup>, ReadFailedException> futureEndpointGroup =
325 mock(CheckedFuture.class);
326 Optional<EndpointGroup> optEndpointGroup = mock(Optional.class);
327 when(optEndpointGroup.isPresent()).thenReturn(true);
328 when(futureEndpointGroup.checkedGet()).thenReturn(optEndpointGroup);
329 when(roTx.read(LogicalDatastoreType.CONFIGURATION,
330 IidFactory.endpointGroupIid(tenantId, epgId))).thenReturn(futureEndpointGroup);
331 doNothing().when(roTx).close();
332 when(dataProvider.newReadOnlyTransaction()).thenReturn(roTx);
334 policyManager.readEndpointGroup(epgId, tenantId);
336 when(optEndpointGroup.isPresent()).thenReturn(false);
337 policyManager.readEndpointGroup(epgId, tenantId);
340 @SuppressWarnings("unchecked")
342 public void testReadSubnet() throws ReadFailedException {
343 FaasPolicyManager policyManager = new FaasPolicyManager(dataProvider, executor);
344 ReadOnlyTransaction roTx = mock(ReadOnlyTransaction.class);
345 CheckedFuture<Optional<Subnet>, ReadFailedException> futureSubnet =
346 mock(CheckedFuture.class);
347 Optional<Subnet> optSubnet = mock(Optional.class);
348 when(optSubnet.isPresent()).thenReturn(true);
349 when(futureSubnet.checkedGet()).thenReturn(optSubnet);
350 when(roTx.read(LogicalDatastoreType.CONFIGURATION, IidFactory.subnetIid(tenantId, subnetId))).thenReturn(futureSubnet);
351 doNothing().when(roTx).close();
352 when(dataProvider.newReadOnlyTransaction()).thenReturn(roTx);
354 policyManager.readSubnet(subnetId, tenantId);
356 when(optSubnet.isPresent()).thenReturn(false);
357 policyManager.readSubnet(subnetId, tenantId);
360 @SuppressWarnings("unchecked")
362 public void testReadL3ContextInstance() throws ReadFailedException {
363 FaasPolicyManager policyManager = new FaasPolicyManager(dataProvider, executor);
364 ReadOnlyTransaction roTx = mock(ReadOnlyTransaction.class);
365 CheckedFuture<Optional<L3Context>, ReadFailedException> futureL3Context =
366 mock(CheckedFuture.class);
367 Optional<L3Context> optL3Context = mock(Optional.class);
368 when(optL3Context.isPresent()).thenReturn(true);
369 when(futureL3Context.checkedGet()).thenReturn(optL3Context);
371 L3ContextId l3cId = new L3ContextId("l3cId");
372 when(roTx.read(LogicalDatastoreType.CONFIGURATION, IidFactory.l3ContextIid(tenantId, l3cId))).thenReturn(futureL3Context);
373 doNothing().when(roTx).close();
374 when(dataProvider.newReadOnlyTransaction()).thenReturn(roTx);
376 policyManager.readL3ContextInstance(tenantId, l3cId);
378 when(optL3Context.isPresent()).thenReturn(false);
379 policyManager.readL3ContextInstance(tenantId, l3cId);
382 @SuppressWarnings("unchecked")
384 public void testReadL2BridgeDomainInstance() throws ReadFailedException {
385 FaasPolicyManager policyManager = new FaasPolicyManager(dataProvider, executor);
386 ReadOnlyTransaction roTx = mock(ReadOnlyTransaction.class);
387 CheckedFuture<Optional<L2BridgeDomain>, ReadFailedException> futureL2BridgeDomain =
388 mock(CheckedFuture.class);
389 Optional<L2BridgeDomain> optL2BridgeDomain = mock(Optional.class);
390 when(optL2BridgeDomain.isPresent()).thenReturn(true);
391 when(futureL2BridgeDomain.checkedGet()).thenReturn(optL2BridgeDomain);
393 L2BridgeDomainId l2bId = new L2BridgeDomainId("l2bId");
394 when(roTx.read(LogicalDatastoreType.CONFIGURATION, IidFactory.l2BridgeDomainIid(tenantId,
395 l2bId))).thenReturn(futureL2BridgeDomain);
396 doNothing().when(roTx).close();
397 when(dataProvider.newReadOnlyTransaction()).thenReturn(roTx);
399 policyManager.readL2BridgeDomainInstance(tenantId, l2bId);
401 when(optL2BridgeDomain.isPresent()).thenReturn(false);
402 policyManager.readL2BridgeDomainInstance(tenantId, l2bId);
405 @SuppressWarnings("unchecked")
407 public void testReadL2FloodDomain() throws ReadFailedException {
408 FaasPolicyManager policyManager = new FaasPolicyManager(dataProvider, executor);
409 ReadOnlyTransaction roTx = mock(ReadOnlyTransaction.class);
410 CheckedFuture<Optional<L2FloodDomain>, ReadFailedException> futureL2FloodDomain =
411 mock(CheckedFuture.class);
412 Optional<L2FloodDomain> optL2FloodDomain = mock(Optional.class);
413 when(optL2FloodDomain.isPresent()).thenReturn(true);
414 when(futureL2FloodDomain.checkedGet()).thenReturn(optL2FloodDomain);
416 L2FloodDomainId l2fId = new L2FloodDomainId("l2fId");
417 when(roTx.read(LogicalDatastoreType.CONFIGURATION, IidFactory.l2FloodDomainIid(tenantId,
418 l2fId))).thenReturn(futureL2FloodDomain);
419 doNothing().when(roTx).close();
420 when(dataProvider.newReadOnlyTransaction()).thenReturn(roTx);
422 policyManager.readL2FloodDomain(l2fId, tenantId);
424 when(optL2FloodDomain.isPresent()).thenReturn(false);
425 policyManager.readL2FloodDomain(l2fId, tenantId);
428 @SuppressWarnings("unchecked")
430 public void testNeedToCreateLogicalNetwork() throws ReadFailedException {
432 ServiceCommunicationLayer comLayer = ServiceCommunicationLayer.Layer2;
433 List<SubnetId> consSubnetIds = new ArrayList<>();
434 SubnetId consSubnetId = new SubnetId("consSubnetId");
435 consSubnetIds.add(consSubnetId);
436 List<SubnetId> provSubnetIds = new ArrayList<>();
437 SubnetId provSubnetId = new SubnetId("provSubnetId");
438 provSubnetIds.add(provSubnetId);
439 ContractId contractId = new ContractId("contractId");
440 EndpointGroup providerEpg = new EndpointGroupBuilder()
441 .setId(providerEpgId)
443 EndpointGroup consumerEpg = new EndpointGroupBuilder()
444 .setId(consumerEpgId)
446 ExternalImplicitGroup externalImplicitGroup = ExternalImplicitGroup.ProviderEpg;
448 FaasPolicyManager policyManager = new FaasPolicyManager(dataProvider, executor);
449 ReadOnlyTransaction roTx = mock(ReadOnlyTransaction.class);
450 CheckedFuture<Optional<LogicalNetwork>, ReadFailedException> futureLogicalNetwork =
451 mock(CheckedFuture.class);
452 Optional<LogicalNetwork> optLogicalNetwork = mock(Optional.class);
453 when(optLogicalNetwork.isPresent()).thenReturn(true);
454 LogicalNetwork logicalNet =
455 new LogicalNetworkBuilder()
456 .setCommunicationLayer(ServiceCommunicationLayer.Layer2)
458 new ConsumerNetworkBuilder().setNetworkScopeType(ScopeType.Private)
459 .setGbpSubnetId(consSubnetIds)
462 new ProviderNetworkBuilder().setNetworkScopeType(ScopeType.Public)
463 .setGbpSubnetId(provSubnetIds)
466 when(optLogicalNetwork.get()).thenReturn(logicalNet);
467 when(futureLogicalNetwork.checkedGet()).thenReturn(optLogicalNetwork);
469 when(roTx.read(LogicalDatastoreType.OPERATIONAL,
470 FaasIidFactory.logicalNetworkIid(consumerEpg.getId(), tenantId, contractId,
471 providerEpg.getId(), tenantId))).thenReturn(futureLogicalNetwork);
472 doNothing().when(roTx).close();
473 when(dataProvider.newReadOnlyTransaction()).thenReturn(roTx);
475 policyManager.needToCreateLogicalNetwork(comLayer, consSubnetIds, provSubnetIds, tenantId,
476 contractId, providerEpg, consumerEpg, externalImplicitGroup);
479 @SuppressWarnings("unchecked")
481 public void testRemoveLogicalNetwork()
482 throws ReadFailedException, TransactionCommitFailedException {
483 ServiceCommunicationLayer comLayer = ServiceCommunicationLayer.Layer2;
484 List<SubnetId> consSubnetIds = new ArrayList<>();
485 SubnetId consSubnetId = new SubnetId("consSubnetId");
486 consSubnetIds.add(consSubnetId);
487 List<SubnetId> provSubnetIds = new ArrayList<>();
488 SubnetId provSubnetId = new SubnetId("provSubnetId");
489 provSubnetIds.add(provSubnetId);
490 ContractId contractId = new ContractId("contractId");
491 EndpointGroup providerEpg = new EndpointGroupBuilder()
492 .setId(providerEpgId)
494 EndpointGroup consumerEpg = new EndpointGroupBuilder()
495 .setId(consumerEpgId)
497 ExternalImplicitGroup externalImplicitGroup = ExternalImplicitGroup.ProviderEpg;
499 PowerMockito.mockStatic(UlnDatastoreApi.class);
500 PowerMockito.doNothing().when(UlnDatastoreApi.class);
501 UlnDatastoreApi.removeLogicalSwitchFromDsIfExists(any(Uuid.class), any(Uuid.class));
502 PowerMockito.doNothing().when(UlnDatastoreApi.class);
503 UlnDatastoreApi.removeLogicalRouterFromDsIfExists(any(Uuid.class), any(Uuid.class));
505 FaasPolicyManager policyManager = new FaasPolicyManager(dataProvider, executor);
506 ReadWriteTransaction rwTx = mock(ReadWriteTransaction.class);
507 CheckedFuture<Optional<LogicalNetwork>, ReadFailedException> futureLogicalNetwork =
508 mock(CheckedFuture.class);
509 CheckedFuture<Void, TransactionCommitFailedException> futureVoid =
510 mock(CheckedFuture.class);
511 Optional<LogicalNetwork> optLogicalNetwork = mock(Optional.class);
512 Optional<Void> optVoid = mock(Optional.class);
513 when(optLogicalNetwork.isPresent()).thenReturn(true);
514 LogicalNetwork logicalNet =
515 new LogicalNetworkBuilder().setCommunicationLayer(ServiceCommunicationLayer.Layer2)
517 new ConsumerNetworkBuilder().setNetworkScopeType(ScopeType.Private)
518 .setGbpSubnetId(consSubnetIds)
519 .setLogicalRouterId(dummyUuid1)
522 new ProviderNetworkBuilder().setNetworkScopeType(ScopeType.Public)
523 .setGbpSubnetId(provSubnetIds)
524 .setLogicalRouterId(dummyUuid2).build())
525 .setConsumerTenantId(tenantId)
526 .setProviderTenantId(tenantId)
528 when(optLogicalNetwork.get()).thenReturn(logicalNet);
529 when(futureLogicalNetwork.checkedGet()).thenReturn(optLogicalNetwork);
530 when(futureVoid.checkedGet()).thenReturn(null);
532 when(rwTx.read(LogicalDatastoreType.OPERATIONAL,
533 FaasIidFactory.logicalNetworkIid(consumerEpg.getId(), tenantId, contractId,
534 providerEpg.getId(), tenantId))).thenReturn(futureLogicalNetwork);
535 when(rwTx.submit()).thenReturn(futureVoid);
536 when(dataProvider.newReadWriteTransaction()).thenReturn(rwTx);
538 List<PolicyRuleGroup> prg = new ArrayList<>();
539 PolicyRuleGroup prgElement = new PolicyRuleGroupBuilder()
540 .setContractId(new ContractId("contractId"))
543 List<PolicyRuleGroupWithEndpointConstraints> prgwec = new ArrayList<>();
544 PolicyRuleGroupWithEndpointConstraints prgwecElement = new PolicyRuleGroupWithEndpointConstraintsBuilder()
545 .setPolicyRuleGroup(prg)
547 prgwec.add(prgwecElement);
548 ResolvedPolicy oldPolicy = new ResolvedPolicyBuilder()
549 .setConsumerEpgId(consumerEpgId)
550 .setConsumerTenantId(tenantId)
551 .setProviderEpgId(providerEpgId)
552 .setProviderTenantId(tenantId)
553 .setPolicyRuleGroupWithEndpointConstraints(prgwec)
556 policyManager.removeLogicalNetwork(oldPolicy);
561 public void testRemoveLogicalNetwork_null(){
562 FaasPolicyManager policyManager = new FaasPolicyManager(dataProvider, executor);
564 policyManager.removeLogicalNetwork(null);
567 /*@SuppressWarnings("unchecked")
569 public void testCreateLayer3LogicalNetwork() throws ReadFailedException {
570 ReadOnlyTransaction roTx = mock(ReadOnlyTransaction.class);
572 CheckedFuture<Optional<ResolvedPolicies>, ReadFailedException> futureResolvedPolicies =
573 mock(CheckedFuture.class);
574 Optional<ResolvedPolicies> optResolvedPolicies = mock(Optional.class);
575 when(futureResolvedPolicies.checkedGet()).thenReturn(optResolvedPolicies);
576 when(roTx.read(LogicalDatastoreType.OPERATIONAL,
577 InstanceIdentifier.builder(ResolvedPolicies.class).build())).thenReturn(
578 futureResolvedPolicies);
580 MappedSubnet mappedSubnet = new MappedSubnetBuilder()
581 .setFaasSubnetId(dummyUuid1)
583 CheckedFuture<Optional<MappedSubnet>, ReadFailedException> futureMappedSubnet =
584 mock(CheckedFuture.class);
585 Optional<MappedSubnet> optMappedSubnet = mock(Optional.class);
586 when(optMappedSubnet.isPresent()).thenReturn(true);
587 when(optMappedSubnet.get()).thenReturn(mappedSubnet);
588 when(futureMappedSubnet.checkedGet()).thenReturn(optMappedSubnet);
589 when(roTx.read(LogicalDatastoreType.OPERATIONAL,
590 FaasIidFactory.mappedSubnetIid(tenantId, subnetId))).thenReturn(
593 when(dataProvider.newReadOnlyTransaction()).thenReturn(roTx);
595 EndpointGroup consumerEpg = new EndpointGroupBuilder().setId(consumerEpgId).build();
596 EndpointGroup providerEpg = new EndpointGroupBuilder().setId(providerEpgId).build();
597 FaasPolicyManager policyManager = new FaasPolicyManager(dataProvider, executor);
599 policyManager.registerSubnetWithEpg(consumerEpgId, tenantId, consumerSubnet);
600 policyManager.registerSubnetWithEpg(providerEpgId, tenantId, providerSubnet);
601 policyManager.createLayer3LogicalNetwork(consumerEpg, contractId, providerEpg, tenantId,
602 ServiceCommunicationLayer.Layer3, ExternalImplicitGroup.ProviderEpg);
609 private L3Context makeTestL3Context() {
610 L3ContextBuilder builder = new L3ContextBuilder();
611 builder.setId(l3Context);
612 return builder.build();
615 private L2FloodDomain makeTestL2FloodDomain(String id, L2BridgeDomainId brdgId) {
616 L2FloodDomainBuilder builder = new L2FloodDomainBuilder();
617 builder.setId(new L2FloodDomainId(id));
618 builder.setParent(brdgId);
619 return builder.build();
622 private L2BridgeDomain makeTestBridgeDomain(String id) {
623 L2BridgeDomainBuilder builder = new L2BridgeDomainBuilder();
624 builder.setId(new L2BridgeDomainId(id));
625 builder.setParent(l3Context);
626 return builder.build();
629 private EndpointGroup makeTestEndpointGroup(EndpointGroupId epgId) {
630 EndpointGroupBuilder builder = new EndpointGroupBuilder();
631 builder.setId(epgId);
632 return builder.build();
635 private Subnet makeTestSubnet(SubnetId subnetId, L2FloodDomainId l2FloodDomainId) {
636 SubnetBuilder builder = new SubnetBuilder();
637 builder.setId(subnetId);
638 builder.setParent(l2FloodDomainId);
639 return builder.build();
643 private DataObject makeTestResolvedPolicy() {
644 ResolvedPolicyBuilder builder = new ResolvedPolicyBuilder();
645 builder.setConsumerEpgId(consumerEpgId);
646 builder.setConsumerTenantId(tenantId);
647 builder.setProviderEpgId(providerEpgId);
648 builder.setProviderTenantId(tenantId);
649 List<PolicyRuleGroupWithEndpointConstraints> pRulesGrpsWEp = new ArrayList<>();
650 PolicyRuleGroupWithEndpointConstraintsBuilder pRulesGrpWEp = new PolicyRuleGroupWithEndpointConstraintsBuilder();
651 List<PolicyRuleGroup> pRulesGrps = new ArrayList<>();
652 PolicyRuleGroupBuilder pRulesGrp = new PolicyRuleGroupBuilder();
653 pRulesGrp.setContractId(contractId);
654 pRulesGrps.add(pRulesGrp.build());
655 pRulesGrpWEp.setPolicyRuleGroup(pRulesGrps);
656 pRulesGrpsWEp.add(pRulesGrpWEp.build());
657 builder.setPolicyRuleGroupWithEndpointConstraints(pRulesGrpsWEp);
658 return builder.build();
661 private DataObject makeTestResolvedPolicyWithImpExternalEpg() {
662 ResolvedPolicyBuilder builder = new ResolvedPolicyBuilder((ResolvedPolicy) makeTestResolvedPolicy());
663 builder.setExternalImplicitGroup(ExternalImplicitGroup.ConsumerEpg);
664 return builder.build();