2 * Copyright (c) 2016 Huawei Technologies 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.assertTrue;
11 import static org.junit.Assert.fail;
12 import static org.mockito.Mockito.mock;
13 import static org.mockito.Mockito.when;
15 import java.util.ArrayList;
16 import java.util.HashMap;
17 import java.util.HashSet;
18 import java.util.List;
21 import java.util.concurrent.Executors;
22 import java.util.concurrent.ScheduledExecutorService;
23 import java.util.concurrent.TimeUnit;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
29 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
30 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
31 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
32 import org.opendaylight.faas.uln.datastore.api.UlnDatastoreApi;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.faas.logical.faas.common.rev151013.Uuid;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ContractId;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.EndpointGroupId;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.L2BridgeDomainId;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.L2FloodDomainId;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.L3ContextId;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.SubnetId;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.faas.rev151009.ServiceCommunicationLayer;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.L2BridgeDomain;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.L2BridgeDomainBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.L2FloodDomain;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.L2FloodDomainBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.L3Context;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.L3ContextBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.Subnet;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.SubnetBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.EndpointGroup;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.EndpointGroupBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.resolved.policy.rev150828.resolved.policies.ResolvedPolicy;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.resolved.policy.rev150828.resolved.policies.ResolvedPolicy.ExternalImplicitGroup;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.resolved.policy.rev150828.resolved.policies.ResolvedPolicyBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.resolved.policy.rev150828.resolved.policies.resolved.policy.PolicyRuleGroupWithEndpointConstraints;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.resolved.policy.rev150828.resolved.policies.resolved.policy.PolicyRuleGroupWithEndpointConstraintsBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.resolved.policy.rev150828.resolved.policies.resolved.policy.policy.rule.group.with.endpoint.constraints.PolicyRuleGroup;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.resolved.policy.rev150828.resolved.policies.resolved.policy.policy.rule.group.with.endpoint.constraints.PolicyRuleGroupBuilder;
59 import org.opendaylight.yangtools.yang.binding.DataObject;
60 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
61 import org.powermock.api.mockito.PowerMockito;
62 import org.powermock.core.classloader.annotations.PrepareForTest;
63 import org.powermock.modules.junit4.PowerMockRunner;
65 import com.google.common.util.concurrent.CheckedFuture;
67 public class FaasPolicyManagerTest {
69 private InstanceIdentifier<DataObject> policyId;
70 private AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> change;
71 DataBroker dataProvider;
72 private final ScheduledExecutorService executor = Executors.newScheduledThreadPool(Runtime.getRuntime()
73 .availableProcessors());
74 EndpointGroupId consumerEpgId = new EndpointGroupId("consumerEpgId");
75 SubnetId consumerSubnet = new SubnetId("consumerSubnet");
76 SubnetId providerSubnet = new SubnetId("providerSubnet");
77 EndpointGroupId providerEpgId = new EndpointGroupId("providerEpgId");
78 ContractId contractId = new ContractId("contractId");
79 TenantId tenantId = new TenantId("tenantId");
80 Uuid faasTenantId = new Uuid("0eb98cf5-086c-4a81-8a4e-0c3b4566108b");
81 Uuid faasSecRulesId = new Uuid("1eb98cf5-086c-4a81-8a4e-0c3b4566108b");
82 L3ContextId l3Context = new L3ContextId("l3ContextId");
84 @SuppressWarnings("unchecked")
87 policyId = mock(InstanceIdentifier.class);
88 change = mock(AsyncDataChangeEvent.class);
89 policyId = mock(InstanceIdentifier.class);
90 dataProvider = mock(DataBroker.class);
91 WriteTransaction writeTransaction = mock(WriteTransaction.class);
92 when(dataProvider.newWriteOnlyTransaction()).thenReturn(writeTransaction);
93 CheckedFuture<Void, TransactionCommitFailedException> checkedFuture = mock(CheckedFuture.class);
94 when(writeTransaction.submit()).thenReturn(checkedFuture);
96 Set<InstanceIdentifier<?>> removedPaths = new HashSet<>();
97 removedPaths.add(policyId);
98 when(change.getRemovedPaths()).thenReturn(removedPaths);
101 @SuppressWarnings("resource")
103 public void testLayer2ResolvedPolicyWithImpExternalEpg() {
104 // prepare input test data
105 MockFaasPolicyManager policyManager = new MockFaasPolicyManager(dataProvider, executor);
107 // mock input test policy
108 policyManager.storeTestEpg(makeTestEndpointGroup(consumerEpgId));
109 policyManager.storeTestEpg(makeTestEndpointGroup(providerEpgId));
110 policyManager.storeTestFaasTenantId(tenantId, faasTenantId);
111 L2BridgeDomain brdg = makeTestBridgeDomain("bridge");
112 policyManager.storeTestL2BridgeDomain(brdg);
113 L2FloodDomain fld1 = makeTestL2FloodDomain("fld1", brdg.getId());
114 policyManager.storeTestL2FloodDomain(fld1);
115 policyManager.storeTestSubnet(makeTestSubnet(consumerSubnet, fld1.getId()));
116 L2FloodDomain fld2 = makeTestL2FloodDomain("fld2", brdg.getId());
117 policyManager.storeTestL2FloodDomain(fld2);
118 policyManager.storeTestSubnet(makeTestSubnet(providerSubnet, fld2.getId()));
119 policyManager.storeTestSecIdPerContract(contractId, faasSecRulesId);
121 // mock endpoint attached to consumer side
122 policyManager.registerTenant(tenantId, consumerEpgId);
123 policyManager.registerSubnetWithEpg(consumerEpgId, tenantId, consumerSubnet);
124 // mock endpoint attached to provider side
125 policyManager.registerTenant(tenantId, providerEpgId);
126 policyManager.registerSubnetWithEpg(providerEpgId, tenantId, providerSubnet);
128 // input test resolved policy
129 DataObject testPolicy = makeTestResolvedPolicyWithImpExternalEpg();
130 Map<InstanceIdentifier<?>, DataObject> testData = new HashMap<>();
131 testData.put(policyId, testPolicy);
132 when(change.getCreatedData()).thenReturn(testData);
133 when(change.getOriginalData()).thenReturn(testData);
134 when(change.getUpdatedData()).thenReturn(testData);
135 // invoke event -- expected data is verified in mocked classes
136 policyManager.onDataChanged(change);
138 // make sure internal threads have completed
141 executor.awaitTermination(10, TimeUnit.SECONDS);
142 } catch (InterruptedException e) {
143 fail("FaasPolicyManagerTest: Exception = " + e.toString());
147 assertTrue("FaasPolicyManagerTest", policyManager.getComLayer().equals(ServiceCommunicationLayer.Layer2));
148 assertTrue("FaasPolicyManagerTest", policyManager.getExternalImplicitGroup() != null);
151 @SuppressWarnings("resource")
153 public void testLayer3ResolvedPolicy() {
154 // prepare input test data
155 MockFaasPolicyManager policyManager = new MockFaasPolicyManager(dataProvider, executor);
157 // mock input test policy
158 policyManager.storeTestL3Contextes(makeTestL3Context());
159 policyManager.storeTestEpg(makeTestEndpointGroup(consumerEpgId));
160 policyManager.storeTestEpg(makeTestEndpointGroup(providerEpgId));
161 policyManager.storeTestFaasTenantId(tenantId, faasTenantId);
162 L2BridgeDomain brdg1 = makeTestBridgeDomain("bridge1");
163 policyManager.storeTestL2BridgeDomain(brdg1);
164 L2FloodDomain fld1 = makeTestL2FloodDomain("fld1", brdg1.getId());
165 policyManager.storeTestL2FloodDomain(fld1);
166 policyManager.storeTestSubnet(makeTestSubnet(consumerSubnet, fld1.getId()));
167 L2BridgeDomain brdg2 = makeTestBridgeDomain("bridge2");
168 policyManager.storeTestL2BridgeDomain(brdg2);
169 L2FloodDomain fld2 = makeTestL2FloodDomain("fld2", brdg2.getId());
170 policyManager.storeTestL2FloodDomain(fld2);
171 policyManager.storeTestSubnet(makeTestSubnet(providerSubnet, fld2.getId()));
172 policyManager.storeTestSecIdPerContract(contractId, faasSecRulesId);
174 // mock endpoint attached to consumer side
175 policyManager.registerTenant(tenantId, consumerEpgId);
176 policyManager.registerSubnetWithEpg(consumerEpgId, tenantId, consumerSubnet);
177 // mock endpoint attached to provider side
178 policyManager.registerTenant(tenantId, providerEpgId);
179 policyManager.registerSubnetWithEpg(providerEpgId, tenantId, providerSubnet);
181 // input test resolved policy
182 DataObject testPolicy = makeTestResolvedPolicy();
183 Map<InstanceIdentifier<?>, DataObject> testData = new HashMap<>();
184 testData.put(policyId, testPolicy);
185 when(change.getCreatedData()).thenReturn(testData);
186 when(change.getOriginalData()).thenReturn(testData);
187 when(change.getUpdatedData()).thenReturn(testData);
188 // invoke event -- expected data is verified in mocked classes
189 policyManager.onDataChanged(change);
191 // make sure internal threads have completed
194 executor.awaitTermination(10, TimeUnit.SECONDS);
195 } catch (InterruptedException e) {
196 fail("FaasPolicyManagerTest: Exception = " + e.toString());
200 assertTrue("FaasPolicyManagerTest", policyManager.getComLayer().equals(ServiceCommunicationLayer.Layer3));
201 assertTrue("FaasPolicyManagerTest", policyManager.getExternalImplicitGroup() == null);
204 private L3Context makeTestL3Context() {
205 L3ContextBuilder builder = new L3ContextBuilder();
206 builder.setId(l3Context);
207 return builder.build();
210 private L2FloodDomain makeTestL2FloodDomain(String id, L2BridgeDomainId brdgId) {
211 L2FloodDomainBuilder builder = new L2FloodDomainBuilder();
212 builder.setId(new L2FloodDomainId(id));
213 builder.setParent(brdgId);
214 return builder.build();
217 private L2BridgeDomain makeTestBridgeDomain(String id) {
218 L2BridgeDomainBuilder builder = new L2BridgeDomainBuilder();
219 builder.setId(new L2BridgeDomainId(id));
220 builder.setParent(l3Context);
221 return builder.build();
224 private EndpointGroup makeTestEndpointGroup(EndpointGroupId epgId) {
225 EndpointGroupBuilder builder = new EndpointGroupBuilder();
226 builder.setId(epgId);
227 return builder.build();
230 private Subnet makeTestSubnet(SubnetId subnetId, L2FloodDomainId l2FloodDomainId) {
231 SubnetBuilder builder = new SubnetBuilder();
232 builder.setId(subnetId);
233 builder.setParent(l2FloodDomainId);
234 return builder.build();
238 private DataObject makeTestResolvedPolicy() {
239 ResolvedPolicyBuilder builder = new ResolvedPolicyBuilder();
240 builder.setConsumerEpgId(consumerEpgId);
241 builder.setConsumerTenantId(tenantId);
242 builder.setProviderEpgId(providerEpgId);
243 builder.setProviderTenantId(tenantId);
244 List<PolicyRuleGroupWithEndpointConstraints> pRulesGrpsWEp = new ArrayList<>();
245 PolicyRuleGroupWithEndpointConstraintsBuilder pRulesGrpWEp = new PolicyRuleGroupWithEndpointConstraintsBuilder();
246 List<PolicyRuleGroup> pRulesGrps = new ArrayList<>();
247 PolicyRuleGroupBuilder pRulesGrp = new PolicyRuleGroupBuilder();
248 pRulesGrp.setContractId(contractId);
249 pRulesGrps.add(pRulesGrp.build());
250 pRulesGrpWEp.setPolicyRuleGroup(pRulesGrps);
251 pRulesGrpsWEp.add(pRulesGrpWEp.build());
252 builder.setPolicyRuleGroupWithEndpointConstraints(pRulesGrpsWEp);
253 return builder.build();
256 private DataObject makeTestResolvedPolicyWithImpExternalEpg() {
257 ResolvedPolicyBuilder builder = new ResolvedPolicyBuilder((ResolvedPolicy) makeTestResolvedPolicy());
258 builder.setExternalImplicitGroup(ExternalImplicitGroup.ConsumerEpg);
259 return builder.build();