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