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
9 package org.opendaylight.groupbasedpolicy.renderer;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertFalse;
13 import static org.junit.Assert.assertTrue;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.junit.runner.RunWith;
20 import org.mockito.Mock;
21 import org.mockito.runners.MockitoJUnitRunner;
22 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.EndpointLocations;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.EndpointLocationsBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.Endpoints;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.EndpointsBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.endpoint.locations.AddressEndpointLocation;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.endpoints.AddressEndpointsBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.endpoints.address.endpoints.AddressEndpoint;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.absolute.location.absolute.location.location.type.InternalLocationCase;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.absolute.location.absolute.location.location.type.InternalLocationCaseBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ContractId;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.EndpointGroupId;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.RuleName;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.SubjectName;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.Tenants;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.EndpointPolicyParticipation;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.has.peer.endpoints.PeerEndpointKey;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.has.rule.group.with.renderer.endpoint.participation.RuleGroupWithRendererEndpointParticipation;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.renderer.policy.configuration.renderer.endpoints.RendererEndpointKey;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.resolved.policy.rev150828.ResolvedPolicies;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.resolved.policy.rev150828.ResolvedPoliciesBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.resolved.policy.rev150828.has.resolved.rules.ResolvedRule;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.resolved.policy.rev150828.resolved.policies.ResolvedPolicy;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.resolved.policy.rev150828.resolved.policies.resolved.policy.policy.rule.group.with.endpoint.constraints.PolicyRuleGroup;
46 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
48 import com.google.common.collect.ImmutableList;
49 import com.google.common.collect.ImmutableTable;
51 @RunWith(MockitoJUnitRunner.class)
52 public class RendererManagerTest {
54 private static final EndpointGroupId EPG_BLUE = new EndpointGroupId("blue_epg");
55 private static final EndpointGroupId EPG_PURPLE = new EndpointGroupId("purple_epg");
56 private static final EndpointGroupId EPG_RED = new EndpointGroupId("red_epg");
57 private static final EndpointGroupId EPG_GREY = new EndpointGroupId("grey_epg");
58 private static final ContractId CONTRACT_1 = new ContractId("contract_1");
59 private static final SubjectName SUBJECT_1 = new SubjectName("subject_1");
60 private static final RuleName RULE_1 = new RuleName("rule_1");
61 private static final ContractId CONTRACT_2 = new ContractId("contract_2");
62 private static final SubjectName SUBJECT_2 = new SubjectName("subject_2");
63 private static final String ADR_1 = "adr_1";
64 private static final String ADR_2 = "adr_2";
65 private static final String ADR_3 = "adr_3";
66 private static final String ADR_4 = "adr_4";
67 private static final InstanceIdentifier<?> NODE_PATH_1 = InstanceIdentifier.create(Tenants.class);
68 private static final InternalLocationCase REG_LOC_NODE_PATH_1 =
69 new InternalLocationCaseBuilder().setInternalNode(NODE_PATH_1).build();
72 private DataBroker dataProvider;
74 private RendererManager rendererManager;
78 rendererManager = new RendererManager(dataProvider);
82 * EP1--EPG_BLUE---SUBJECT_1---(P)EPG_PURPLE--EP2
85 public void testResolveRendererPolicyForEndpoint_onePolicy() {
86 ResolvedRule rule1 = TestDataFactory.defaultResolvedRule(RULE_1).build();
87 PolicyRuleGroup ruleGrp1 = TestDataFactory.defaultPolicyRuleGrp(CONTRACT_1, SUBJECT_1, rule1).build();
88 ResolvedPolicy resolvedPolicy = TestDataFactory.defaultResolvedPolicy(EPG_BLUE, EPG_PURPLE, ruleGrp1).build();
89 ResolvedPolicies resolvedPolicies =
90 new ResolvedPoliciesBuilder().setResolvedPolicy(ImmutableList.of(resolvedPolicy)).build();
91 rendererManager.resolvedPoliciesUpdated(resolvedPolicies);
93 AddressEndpoint ep1 = TestDataFactory.defaultAdrEp(ADR_1, EPG_BLUE).build();
94 AddressEndpoint ep2 = TestDataFactory.defaultAdrEp(ADR_2, EPG_PURPLE).build();
95 Endpoints endpoints = new EndpointsBuilder()
96 .setAddressEndpoints(new AddressEndpointsBuilder().setAddressEndpoint(ImmutableList.of(ep1, ep2)).build())
98 rendererManager.endpointsUpdated(endpoints);
100 AddressEndpointLocation ep1Loc = TestDataFactory.defaultAdrEpLoc(ep1.getKey(), REG_LOC_NODE_PATH_1).build();
101 AddressEndpointLocation ep2Loc = TestDataFactory.defaultAdrEpLoc(ep2.getKey(), REG_LOC_NODE_PATH_1).build();
102 EndpointLocations endpointLocations =
103 new EndpointLocationsBuilder().setAddressEndpointLocation(ImmutableList.of(ep1Loc, ep2Loc)).build();
104 rendererManager.endpointLocationsUpdated(endpointLocations);
106 RendererConfigurationBuilder rendererPolicyBuilder = new RendererConfigurationBuilder();
107 rendererManager.resolveRendererConfigForEndpoint(ep1, rendererPolicyBuilder);
108 ImmutableTable<RendererEndpointKey, PeerEndpointKey, Set<RuleGroupWithRendererEndpointParticipation>> policiesByEpAndPeerEp =
109 rendererPolicyBuilder.getPoliciesByEpAndPeerEp();
110 assertFalse(policiesByEpAndPeerEp.isEmpty());
111 assertTrue(rendererPolicyBuilder.getPoliciesByEpAndPeerExtEp().isEmpty());
112 assertTrue(rendererPolicyBuilder.getPoliciesByEpAndPeerExtConEp().isEmpty());
113 assertEquals(1, policiesByEpAndPeerEp.rowKeySet().size());
114 assertEquals(1, policiesByEpAndPeerEp.columnKeySet().size());
115 // check EP1--EPG_BLUE---SUBJECT_1---(P)EPG_PURPLE--EP2
116 RendererEndpointKey rendererEpKey = AddressEndpointUtils.toRendererEpKey(ep1.getKey());
117 assertEquals(rendererEpKey, policiesByEpAndPeerEp.rowKeySet().iterator().next());
118 PeerEndpointKey peerEpKey = AddressEndpointUtils.toPeerEpKey(ep2.getKey());
119 assertEquals(peerEpKey, policiesByEpAndPeerEp.columnKeySet().iterator().next());
120 Set<RuleGroupWithRendererEndpointParticipation> ruleGrpsWithEpPartic =
121 policiesByEpAndPeerEp.get(rendererEpKey, peerEpKey);
122 assertEquals(1, ruleGrpsWithEpPartic.size());
123 RuleGroupWithRendererEndpointParticipation ruleGrpWithEpPartic = ruleGrpsWithEpPartic.iterator().next();
124 assertEquals(ruleGrp1.getTenantId(), ruleGrpWithEpPartic.getTenantId());
125 assertEquals(ruleGrp1.getContractId(), ruleGrpWithEpPartic.getContractId());
126 assertEquals(ruleGrp1.getSubjectName(), ruleGrpWithEpPartic.getSubjectName());
127 assertEquals(EndpointPolicyParticipation.CONSUMER, ruleGrpWithEpPartic.getRendererEndpointParticipation());
131 * EP1--EPG_BLUE---SUBJECT_1---(P)EPG_PURPLE--EP2
133 * EP3--EPG_RED----SUBJECT_1---(P)EPG_GREY--EP4
136 public void testResolveRendererPolicyForEndpoint_onePolicyTwoUsage() {
137 ResolvedRule rule1 = TestDataFactory.defaultResolvedRule(RULE_1).build();
138 PolicyRuleGroup ruleGrp1 = TestDataFactory.defaultPolicyRuleGrp(CONTRACT_1, SUBJECT_1, rule1).build();
139 ResolvedPolicy bluePurplePolicy = TestDataFactory.defaultResolvedPolicy(EPG_BLUE, EPG_PURPLE, ruleGrp1).build();
140 ResolvedPolicy redGreyPolicy = TestDataFactory.defaultResolvedPolicy(EPG_RED, EPG_GREY, ruleGrp1).build();
141 ResolvedPolicies resolvedPolicies = new ResolvedPoliciesBuilder()
142 .setResolvedPolicy(ImmutableList.of(bluePurplePolicy, redGreyPolicy)).build();
143 rendererManager.resolvedPoliciesUpdated(resolvedPolicies);
145 AddressEndpoint ep1 = TestDataFactory.defaultAdrEp(ADR_1, EPG_BLUE).build();
146 AddressEndpoint ep2 = TestDataFactory.defaultAdrEp(ADR_2, EPG_PURPLE).build();
147 AddressEndpoint ep3 = TestDataFactory.defaultAdrEp(ADR_3, EPG_RED).build();
148 AddressEndpoint ep4 = TestDataFactory.defaultAdrEp(ADR_4, EPG_GREY).build();
149 Endpoints endpoints = new EndpointsBuilder()
150 .setAddressEndpoints(
151 new AddressEndpointsBuilder().setAddressEndpoint(ImmutableList.of(ep1, ep2, ep3, ep4)).build())
153 rendererManager.endpointsUpdated(endpoints);
155 AddressEndpointLocation ep1Loc = TestDataFactory.defaultAdrEpLoc(ep1.getKey(), REG_LOC_NODE_PATH_1).build();
156 AddressEndpointLocation ep2Loc = TestDataFactory.defaultAdrEpLoc(ep2.getKey(), REG_LOC_NODE_PATH_1).build();
157 AddressEndpointLocation ep3Loc = TestDataFactory.defaultAdrEpLoc(ep3.getKey(), REG_LOC_NODE_PATH_1).build();
158 AddressEndpointLocation ep4Loc = TestDataFactory.defaultAdrEpLoc(ep4.getKey(), REG_LOC_NODE_PATH_1).build();
159 EndpointLocations endpointLocations = new EndpointLocationsBuilder()
160 .setAddressEndpointLocation(ImmutableList.of(ep1Loc, ep2Loc, ep3Loc, ep4Loc)).build();
161 rendererManager.endpointLocationsUpdated(endpointLocations);
163 // EP1, EP3 as renderer endpoints
164 RendererConfigurationBuilder rendererPolicyBuilder = new RendererConfigurationBuilder();
165 rendererManager.resolveRendererConfigForEndpoint(ep1, rendererPolicyBuilder);
166 rendererManager.resolveRendererConfigForEndpoint(ep3, rendererPolicyBuilder);
167 ImmutableTable<RendererEndpointKey, PeerEndpointKey, Set<RuleGroupWithRendererEndpointParticipation>> policiesByEpAndPeerEp =
168 rendererPolicyBuilder.getPoliciesByEpAndPeerEp();
169 assertFalse(policiesByEpAndPeerEp.isEmpty());
170 assertTrue(rendererPolicyBuilder.getPoliciesByEpAndPeerExtEp().isEmpty());
171 assertTrue(rendererPolicyBuilder.getPoliciesByEpAndPeerExtConEp().isEmpty());
172 assertEquals(2, policiesByEpAndPeerEp.rowKeySet().size());
173 assertEquals(2, policiesByEpAndPeerEp.columnKeySet().size());
174 // check EPG_BLUE---SUBJECT_1---(P)EPG_PURPLE
175 RendererEndpointKey ep1RendererEpKey = AddressEndpointUtils.toRendererEpKey(ep1.getKey());
176 assertTrue(policiesByEpAndPeerEp.containsRow(ep1RendererEpKey));
177 PeerEndpointKey ep2PeerEpKey = AddressEndpointUtils.toPeerEpKey(ep2.getKey());
178 assertTrue(policiesByEpAndPeerEp.containsColumn(ep2PeerEpKey));
179 Set<RuleGroupWithRendererEndpointParticipation> ep1Ep2RuleGrpsWithEpPartic =
180 policiesByEpAndPeerEp.get(ep1RendererEpKey, ep2PeerEpKey);
181 assertEquals(1, ep1Ep2RuleGrpsWithEpPartic.size());
182 RuleGroupWithRendererEndpointParticipation ruleGrp1WithEpPartic = ep1Ep2RuleGrpsWithEpPartic.iterator().next();
183 assertEquals(ruleGrp1.getTenantId(), ruleGrp1WithEpPartic.getTenantId());
184 assertEquals(ruleGrp1.getContractId(), ruleGrp1WithEpPartic.getContractId());
185 assertEquals(ruleGrp1.getSubjectName(), ruleGrp1WithEpPartic.getSubjectName());
186 assertEquals(EndpointPolicyParticipation.CONSUMER, ruleGrp1WithEpPartic.getRendererEndpointParticipation());
187 // check EP3--EPG_RED----SUBJECT_1---(P)EPG_GREY--EP4
188 RendererEndpointKey ep3RendererEpKey = AddressEndpointUtils.toRendererEpKey(ep3.getKey());
189 assertTrue(policiesByEpAndPeerEp.containsRow(ep3RendererEpKey));
190 PeerEndpointKey ep4PeerEpKey = AddressEndpointUtils.toPeerEpKey(ep4.getKey());
191 assertTrue(policiesByEpAndPeerEp.containsColumn(ep4PeerEpKey));
192 Set<RuleGroupWithRendererEndpointParticipation> ep3Ep4RuleGrpsWithEpPartic =
193 policiesByEpAndPeerEp.get(ep3RendererEpKey, ep4PeerEpKey);
194 assertEquals(1, ep3Ep4RuleGrpsWithEpPartic.size());
195 ruleGrp1WithEpPartic = ep3Ep4RuleGrpsWithEpPartic.iterator().next();
196 assertEquals(ruleGrp1.getTenantId(), ruleGrp1WithEpPartic.getTenantId());
197 assertEquals(ruleGrp1.getContractId(), ruleGrp1WithEpPartic.getContractId());
198 assertEquals(ruleGrp1.getSubjectName(), ruleGrp1WithEpPartic.getSubjectName());
199 assertEquals(EndpointPolicyParticipation.CONSUMER, ruleGrp1WithEpPartic.getRendererEndpointParticipation());
203 * EP1--EPG_BLUE---SUBJECT_1---(P)EPG_PURPLE--EP2
205 * EP1--EPG_RED----SUBJECT_2---(P)EPG_GREY--EP2,EP3
208 public void testResolveRendererPolicyForEndpoint_twoPolicy() {
209 ResolvedRule rule1 = TestDataFactory.defaultResolvedRule(RULE_1).build();
210 PolicyRuleGroup ruleGrp1 = TestDataFactory.defaultPolicyRuleGrp(CONTRACT_1, SUBJECT_1, rule1).build();
211 PolicyRuleGroup ruleGrp2 = TestDataFactory.defaultPolicyRuleGrp(CONTRACT_2, SUBJECT_2, rule1).build();
212 ResolvedPolicy bluePurplePolicy = TestDataFactory.defaultResolvedPolicy(EPG_BLUE, EPG_PURPLE, ruleGrp1).build();
213 ResolvedPolicy redGreyPolicy = TestDataFactory.defaultResolvedPolicy(EPG_RED, EPG_GREY, ruleGrp2).build();
214 ResolvedPolicies resolvedPolicies = new ResolvedPoliciesBuilder()
215 .setResolvedPolicy(ImmutableList.of(bluePurplePolicy, redGreyPolicy)).build();
216 rendererManager.resolvedPoliciesUpdated(resolvedPolicies);
218 AddressEndpoint ep1 = TestDataFactory.defaultAdrEp(ADR_1, EPG_BLUE, EPG_RED).build();
219 AddressEndpoint ep2 = TestDataFactory.defaultAdrEp(ADR_2, EPG_PURPLE, EPG_GREY).build();
220 AddressEndpoint ep3 = TestDataFactory.defaultAdrEp(ADR_3, EPG_GREY).build();
221 Endpoints endpoints =
222 new EndpointsBuilder()
223 .setAddressEndpoints(
224 new AddressEndpointsBuilder().setAddressEndpoint(ImmutableList.of(ep1, ep2, ep3)).build())
226 rendererManager.endpointsUpdated(endpoints);
228 AddressEndpointLocation ep1Loc = TestDataFactory.defaultAdrEpLoc(ep1.getKey(), REG_LOC_NODE_PATH_1).build();
229 AddressEndpointLocation ep2Loc = TestDataFactory.defaultAdrEpLoc(ep2.getKey(), REG_LOC_NODE_PATH_1).build();
230 AddressEndpointLocation ep3Loc = TestDataFactory.defaultAdrEpLoc(ep3.getKey(), REG_LOC_NODE_PATH_1).build();
231 EndpointLocations endpointLocations = new EndpointLocationsBuilder()
232 .setAddressEndpointLocation(ImmutableList.of(ep1Loc, ep2Loc, ep3Loc)).build();
233 rendererManager.endpointLocationsUpdated(endpointLocations);
235 // EP1 as renderer endpoint
236 RendererConfigurationBuilder rendererPolicyBuilder = new RendererConfigurationBuilder();
237 rendererManager.resolveRendererConfigForEndpoint(ep1, rendererPolicyBuilder);
238 ImmutableTable<RendererEndpointKey, PeerEndpointKey, Set<RuleGroupWithRendererEndpointParticipation>> policiesByEpAndPeerEp =
239 rendererPolicyBuilder.getPoliciesByEpAndPeerEp();
240 assertFalse(policiesByEpAndPeerEp.isEmpty());
241 assertTrue(rendererPolicyBuilder.getPoliciesByEpAndPeerExtEp().isEmpty());
242 assertTrue(rendererPolicyBuilder.getPoliciesByEpAndPeerExtConEp().isEmpty());
243 assertEquals(1, policiesByEpAndPeerEp.rowKeySet().size());
244 assertEquals(2, policiesByEpAndPeerEp.columnKeySet().size());
245 // check EP1--EPG_BLUE---SUBJECT_1---(P)EPG_PURPLE--EP2
246 // check EP1--EPG_RED----SUBJECT_2---(P)EPG_GREY--EP2
247 RendererEndpointKey ep1RendererEpKey = AddressEndpointUtils.toRendererEpKey(ep1.getKey());
248 assertTrue(policiesByEpAndPeerEp.containsRow(ep1RendererEpKey));
249 PeerEndpointKey ep2PeerEpKey = AddressEndpointUtils.toPeerEpKey(ep2.getKey());
250 assertTrue(policiesByEpAndPeerEp.containsColumn(ep2PeerEpKey));
251 Set<RuleGroupWithRendererEndpointParticipation> ep1Ep2RuleGrpsWithEpPartic =
252 policiesByEpAndPeerEp.get(ep1RendererEpKey, ep2PeerEpKey);
253 assertEquals(2, ep1Ep2RuleGrpsWithEpPartic.size());
254 assertTrue(ep1Ep2RuleGrpsWithEpPartic.contains(RendererConfigurationBuilder
255 .toRuleGroupWithRendererEndpointParticipation(ruleGrp1.getKey(), EndpointPolicyParticipation.CONSUMER)));
256 assertTrue(ep1Ep2RuleGrpsWithEpPartic.contains(RendererConfigurationBuilder
257 .toRuleGroupWithRendererEndpointParticipation(ruleGrp2.getKey(), EndpointPolicyParticipation.CONSUMER)));
258 // check EP1--EPG_RED----SUBJECT_2---(P)EPG_GREY--EP3
259 PeerEndpointKey ep3PeerEpKey = AddressEndpointUtils.toPeerEpKey(ep3.getKey());
260 assertTrue(policiesByEpAndPeerEp.containsColumn(ep3PeerEpKey));
261 Set<RuleGroupWithRendererEndpointParticipation> ep1Ep3RuleGrpsWithEpPartic =
262 policiesByEpAndPeerEp.get(ep1RendererEpKey, ep3PeerEpKey);
263 assertEquals(1, ep1Ep3RuleGrpsWithEpPartic.size());
264 assertTrue(ep1Ep3RuleGrpsWithEpPartic.contains(RendererConfigurationBuilder
265 .toRuleGroupWithRendererEndpointParticipation(ruleGrp2.getKey(), EndpointPolicyParticipation.CONSUMER)));