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;
15 import com.google.common.collect.ImmutableList;
16 import com.google.common.collect.ImmutableTable;
18 import java.util.Collections;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.junit.runner.RunWith;
24 import org.mockito.Mock;
25 import org.mockito.Mockito;
26 import org.mockito.runners.MockitoJUnitRunner;
27 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
28 import org.opendaylight.groupbasedpolicy.base_endpoint.EndpointAugmentorRegistryImpl;
29 import org.opendaylight.groupbasedpolicy.forwarding.NetworkDomainAugmentorRegistryImpl;
30 import org.opendaylight.groupbasedpolicy.renderer.util.AddressEndpointUtils;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.EndpointLocations;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.EndpointLocationsBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.Endpoints;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.EndpointsBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.endpoint.locations.AddressEndpointLocation;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.endpoints.AddressEndpointsBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.endpoints.address.endpoints.AddressEndpoint;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.absolute.location.absolute.location.location.type.InternalLocationCase;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.absolute.location.absolute.location.location.type.InternalLocationCaseBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ContractId;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.EndpointGroupId;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.RuleName;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.SubjectName;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.Tenants;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.EndpointPolicyParticipation;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.has.rule.group.with.renderer.endpoint.participation.RuleGroupWithRendererEndpointParticipation;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.renderer.policy.configuration.renderer.endpoints.RendererEndpointKey;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.renderer.policy.configuration.renderer.endpoints.renderer.endpoint.PeerEndpointKey;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.resolved.policy.rev150828.ResolvedPolicies;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.resolved.policy.rev150828.ResolvedPoliciesBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.resolved.policy.rev150828.has.resolved.rules.ResolvedRule;
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.resolved.policy.policy.rule.group.with.endpoint.constraints.PolicyRuleGroup;
54 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
56 @RunWith(MockitoJUnitRunner.class)
57 public class RendererManagerTest {
59 private static final EndpointGroupId EPG_BLUE = new EndpointGroupId("blue_epg");
60 private static final EndpointGroupId EPG_PURPLE = new EndpointGroupId("purple_epg");
61 private static final EndpointGroupId EPG_RED = new EndpointGroupId("red_epg");
62 private static final EndpointGroupId EPG_GREY = new EndpointGroupId("grey_epg");
63 private static final ContractId CONTRACT_1 = new ContractId("contract_1");
64 private static final SubjectName SUBJECT_1 = new SubjectName("subject_1");
65 private static final RuleName RULE_1 = new RuleName("rule_1");
66 private static final ContractId CONTRACT_2 = new ContractId("contract_2");
67 private static final SubjectName SUBJECT_2 = new SubjectName("subject_2");
68 private static final String ADR_1 = "adr_1";
69 private static final String ADR_2 = "adr_2";
70 private static final String ADR_3 = "adr_3";
71 private static final String ADR_4 = "adr_4";
72 private static final InstanceIdentifier<?> NODE_PATH_1 = InstanceIdentifier.create(Tenants.class);
73 private static final InternalLocationCase REG_LOC_NODE_PATH_1 =
74 new InternalLocationCaseBuilder().setInternalNode(NODE_PATH_1).build();
77 private DataBroker dataProvider;
79 private NetworkDomainAugmentorRegistryImpl netDomainAugmentorRegistry;
81 private EndpointAugmentorRegistryImpl epAugmentorRegistry;
83 private RendererManager rendererManager;
87 Mockito.when(netDomainAugmentorRegistry.getNetworkDomainAugmentors()).thenReturn(Collections.emptySet());
88 Mockito.when(epAugmentorRegistry.getEndpointAugmentors()).thenReturn(Collections.emptySet());
89 rendererManager = new RendererManager(dataProvider, netDomainAugmentorRegistry, epAugmentorRegistry);
93 * EP1--EPG_BLUE---SUBJECT_1---(P)EPG_PURPLE--EP2
96 public void testResolveRendererPolicyForEndpoint_onePolicy() {
97 ResolvedRule rule1 = TestDataFactory.defaultResolvedRule(RULE_1).build();
98 PolicyRuleGroup ruleGrp1 = TestDataFactory.defaultPolicyRuleGrp(CONTRACT_1, SUBJECT_1, rule1).build();
99 ResolvedPolicy resolvedPolicy = TestDataFactory.defaultResolvedPolicy(EPG_BLUE, EPG_PURPLE, ruleGrp1).build();
100 ResolvedPolicies resolvedPolicies =
101 new ResolvedPoliciesBuilder().setResolvedPolicy(ImmutableList.of(resolvedPolicy)).build();
102 rendererManager.resolvedPoliciesUpdated(resolvedPolicies);
104 AddressEndpoint ep1 = TestDataFactory.defaultAdrEp(ADR_1, EPG_BLUE).build();
105 AddressEndpoint ep2 = TestDataFactory.defaultAdrEp(ADR_2, EPG_PURPLE).build();
106 Endpoints endpoints = new EndpointsBuilder()
107 .setAddressEndpoints(new AddressEndpointsBuilder().setAddressEndpoint(ImmutableList.of(ep1, ep2)).build())
109 rendererManager.endpointsUpdated(endpoints);
111 AddressEndpointLocation ep1Loc = TestDataFactory.defaultAdrEpLoc(ep1.getKey(), REG_LOC_NODE_PATH_1).build();
112 AddressEndpointLocation ep2Loc = TestDataFactory.defaultAdrEpLoc(ep2.getKey(), REG_LOC_NODE_PATH_1).build();
113 EndpointLocations endpointLocations =
114 new EndpointLocationsBuilder().setAddressEndpointLocation(ImmutableList.of(ep1Loc, ep2Loc)).build();
115 rendererManager.endpointLocationsUpdated(endpointLocations);
117 RendererConfigurationBuilder rendererPolicyBuilder = new RendererConfigurationBuilder();
118 rendererManager.resolveRendererConfigForEndpoint(ep1, rendererPolicyBuilder);
119 ImmutableTable<RendererEndpointKey, PeerEndpointKey, Set<RuleGroupWithRendererEndpointParticipation>> policiesByEpAndPeerEp =
120 rendererPolicyBuilder.getPoliciesByEpAndPeerEp();
121 assertFalse(policiesByEpAndPeerEp.isEmpty());
122 assertTrue(rendererPolicyBuilder.getPoliciesByEpAndPeerExtEp().isEmpty());
123 assertTrue(rendererPolicyBuilder.getPoliciesByEpAndPeerExtConEp().isEmpty());
124 assertEquals(1, policiesByEpAndPeerEp.rowKeySet().size());
125 assertEquals(1, policiesByEpAndPeerEp.columnKeySet().size());
126 // check EP1--EPG_BLUE---SUBJECT_1---(P)EPG_PURPLE--EP2
127 RendererEndpointKey rendererEpKey = AddressEndpointUtils.toRendererEpKey(ep1.getKey());
128 assertEquals(rendererEpKey, policiesByEpAndPeerEp.rowKeySet().iterator().next());
129 PeerEndpointKey peerEpKey = AddressEndpointUtils.toPeerEpKey(ep2.getKey());
130 assertEquals(peerEpKey, policiesByEpAndPeerEp.columnKeySet().iterator().next());
131 Set<RuleGroupWithRendererEndpointParticipation> ruleGrpsWithEpPartic =
132 policiesByEpAndPeerEp.get(rendererEpKey, peerEpKey);
133 assertEquals(1, ruleGrpsWithEpPartic.size());
134 RuleGroupWithRendererEndpointParticipation ruleGrpWithEpPartic = ruleGrpsWithEpPartic.iterator().next();
135 assertEquals(ruleGrp1.getTenantId(), ruleGrpWithEpPartic.getTenantId());
136 assertEquals(ruleGrp1.getContractId(), ruleGrpWithEpPartic.getContractId());
137 assertEquals(ruleGrp1.getSubjectName(), ruleGrpWithEpPartic.getSubjectName());
138 assertEquals(EndpointPolicyParticipation.CONSUMER, ruleGrpWithEpPartic.getRendererEndpointParticipation());
142 * EP1--EPG_BLUE---SUBJECT_1---(P)EPG_PURPLE--EP2
144 * EP3--EPG_RED----SUBJECT_1---(P)EPG_GREY--EP4
147 public void testResolveRendererPolicyForEndpoint_onePolicyTwoUsage() {
148 ResolvedRule rule1 = TestDataFactory.defaultResolvedRule(RULE_1).build();
149 PolicyRuleGroup ruleGrp1 = TestDataFactory.defaultPolicyRuleGrp(CONTRACT_1, SUBJECT_1, rule1).build();
150 ResolvedPolicy bluePurplePolicy = TestDataFactory.defaultResolvedPolicy(EPG_BLUE, EPG_PURPLE, ruleGrp1).build();
151 ResolvedPolicy redGreyPolicy = TestDataFactory.defaultResolvedPolicy(EPG_RED, EPG_GREY, ruleGrp1).build();
152 ResolvedPolicies resolvedPolicies = new ResolvedPoliciesBuilder()
153 .setResolvedPolicy(ImmutableList.of(bluePurplePolicy, redGreyPolicy)).build();
154 rendererManager.resolvedPoliciesUpdated(resolvedPolicies);
156 AddressEndpoint ep1 = TestDataFactory.defaultAdrEp(ADR_1, EPG_BLUE).build();
157 AddressEndpoint ep2 = TestDataFactory.defaultAdrEp(ADR_2, EPG_PURPLE).build();
158 AddressEndpoint ep3 = TestDataFactory.defaultAdrEp(ADR_3, EPG_RED).build();
159 AddressEndpoint ep4 = TestDataFactory.defaultAdrEp(ADR_4, EPG_GREY).build();
160 Endpoints endpoints = new EndpointsBuilder()
161 .setAddressEndpoints(
162 new AddressEndpointsBuilder().setAddressEndpoint(ImmutableList.of(ep1, ep2, ep3, ep4)).build())
164 rendererManager.endpointsUpdated(endpoints);
166 AddressEndpointLocation ep1Loc = TestDataFactory.defaultAdrEpLoc(ep1.getKey(), REG_LOC_NODE_PATH_1).build();
167 AddressEndpointLocation ep2Loc = TestDataFactory.defaultAdrEpLoc(ep2.getKey(), REG_LOC_NODE_PATH_1).build();
168 AddressEndpointLocation ep3Loc = TestDataFactory.defaultAdrEpLoc(ep3.getKey(), REG_LOC_NODE_PATH_1).build();
169 AddressEndpointLocation ep4Loc = TestDataFactory.defaultAdrEpLoc(ep4.getKey(), REG_LOC_NODE_PATH_1).build();
170 EndpointLocations endpointLocations = new EndpointLocationsBuilder()
171 .setAddressEndpointLocation(ImmutableList.of(ep1Loc, ep2Loc, ep3Loc, ep4Loc)).build();
172 rendererManager.endpointLocationsUpdated(endpointLocations);
174 // EP1, EP3 as renderer endpoints
175 RendererConfigurationBuilder rendererPolicyBuilder = new RendererConfigurationBuilder();
176 rendererManager.resolveRendererConfigForEndpoint(ep1, rendererPolicyBuilder);
177 rendererManager.resolveRendererConfigForEndpoint(ep3, rendererPolicyBuilder);
178 ImmutableTable<RendererEndpointKey, PeerEndpointKey, Set<RuleGroupWithRendererEndpointParticipation>> policiesByEpAndPeerEp =
179 rendererPolicyBuilder.getPoliciesByEpAndPeerEp();
180 assertFalse(policiesByEpAndPeerEp.isEmpty());
181 assertTrue(rendererPolicyBuilder.getPoliciesByEpAndPeerExtEp().isEmpty());
182 assertTrue(rendererPolicyBuilder.getPoliciesByEpAndPeerExtConEp().isEmpty());
183 assertEquals(2, policiesByEpAndPeerEp.rowKeySet().size());
184 assertEquals(2, policiesByEpAndPeerEp.columnKeySet().size());
185 // check EPG_BLUE---SUBJECT_1---(P)EPG_PURPLE
186 RendererEndpointKey ep1RendererEpKey = AddressEndpointUtils.toRendererEpKey(ep1.getKey());
187 assertTrue(policiesByEpAndPeerEp.containsRow(ep1RendererEpKey));
188 PeerEndpointKey ep2PeerEpKey = AddressEndpointUtils.toPeerEpKey(ep2.getKey());
189 assertTrue(policiesByEpAndPeerEp.containsColumn(ep2PeerEpKey));
190 Set<RuleGroupWithRendererEndpointParticipation> ep1Ep2RuleGrpsWithEpPartic =
191 policiesByEpAndPeerEp.get(ep1RendererEpKey, ep2PeerEpKey);
192 assertEquals(1, ep1Ep2RuleGrpsWithEpPartic.size());
193 RuleGroupWithRendererEndpointParticipation ruleGrp1WithEpPartic = ep1Ep2RuleGrpsWithEpPartic.iterator().next();
194 assertEquals(ruleGrp1.getTenantId(), ruleGrp1WithEpPartic.getTenantId());
195 assertEquals(ruleGrp1.getContractId(), ruleGrp1WithEpPartic.getContractId());
196 assertEquals(ruleGrp1.getSubjectName(), ruleGrp1WithEpPartic.getSubjectName());
197 assertEquals(EndpointPolicyParticipation.CONSUMER, ruleGrp1WithEpPartic.getRendererEndpointParticipation());
198 // check EP3--EPG_RED----SUBJECT_1---(P)EPG_GREY--EP4
199 RendererEndpointKey ep3RendererEpKey = AddressEndpointUtils.toRendererEpKey(ep3.getKey());
200 assertTrue(policiesByEpAndPeerEp.containsRow(ep3RendererEpKey));
201 PeerEndpointKey ep4PeerEpKey = AddressEndpointUtils.toPeerEpKey(ep4.getKey());
202 assertTrue(policiesByEpAndPeerEp.containsColumn(ep4PeerEpKey));
203 Set<RuleGroupWithRendererEndpointParticipation> ep3Ep4RuleGrpsWithEpPartic =
204 policiesByEpAndPeerEp.get(ep3RendererEpKey, ep4PeerEpKey);
205 assertEquals(1, ep3Ep4RuleGrpsWithEpPartic.size());
206 ruleGrp1WithEpPartic = ep3Ep4RuleGrpsWithEpPartic.iterator().next();
207 assertEquals(ruleGrp1.getTenantId(), ruleGrp1WithEpPartic.getTenantId());
208 assertEquals(ruleGrp1.getContractId(), ruleGrp1WithEpPartic.getContractId());
209 assertEquals(ruleGrp1.getSubjectName(), ruleGrp1WithEpPartic.getSubjectName());
210 assertEquals(EndpointPolicyParticipation.CONSUMER, ruleGrp1WithEpPartic.getRendererEndpointParticipation());
214 * EP1--EPG_BLUE---SUBJECT_1---(P)EPG_PURPLE--EP2
216 * EP1--EPG_RED----SUBJECT_2---(P)EPG_GREY--EP2,EP3
219 public void testResolveRendererPolicyForEndpoint_twoPolicy() {
220 ResolvedRule rule1 = TestDataFactory.defaultResolvedRule(RULE_1).build();
221 PolicyRuleGroup ruleGrp1 = TestDataFactory.defaultPolicyRuleGrp(CONTRACT_1, SUBJECT_1, rule1).build();
222 PolicyRuleGroup ruleGrp2 = TestDataFactory.defaultPolicyRuleGrp(CONTRACT_2, SUBJECT_2, rule1).build();
223 ResolvedPolicy bluePurplePolicy = TestDataFactory.defaultResolvedPolicy(EPG_BLUE, EPG_PURPLE, ruleGrp1).build();
224 ResolvedPolicy redGreyPolicy = TestDataFactory.defaultResolvedPolicy(EPG_RED, EPG_GREY, ruleGrp2).build();
225 ResolvedPolicies resolvedPolicies = new ResolvedPoliciesBuilder()
226 .setResolvedPolicy(ImmutableList.of(bluePurplePolicy, redGreyPolicy)).build();
227 rendererManager.resolvedPoliciesUpdated(resolvedPolicies);
229 AddressEndpoint ep1 = TestDataFactory.defaultAdrEp(ADR_1, EPG_BLUE, EPG_RED).build();
230 AddressEndpoint ep2 = TestDataFactory.defaultAdrEp(ADR_2, EPG_PURPLE, EPG_GREY).build();
231 AddressEndpoint ep3 = TestDataFactory.defaultAdrEp(ADR_3, EPG_GREY).build();
232 Endpoints endpoints =
233 new EndpointsBuilder()
234 .setAddressEndpoints(
235 new AddressEndpointsBuilder().setAddressEndpoint(ImmutableList.of(ep1, ep2, ep3)).build())
237 rendererManager.endpointsUpdated(endpoints);
239 AddressEndpointLocation ep1Loc = TestDataFactory.defaultAdrEpLoc(ep1.getKey(), REG_LOC_NODE_PATH_1).build();
240 AddressEndpointLocation ep2Loc = TestDataFactory.defaultAdrEpLoc(ep2.getKey(), REG_LOC_NODE_PATH_1).build();
241 AddressEndpointLocation ep3Loc = TestDataFactory.defaultAdrEpLoc(ep3.getKey(), REG_LOC_NODE_PATH_1).build();
242 EndpointLocations endpointLocations = new EndpointLocationsBuilder()
243 .setAddressEndpointLocation(ImmutableList.of(ep1Loc, ep2Loc, ep3Loc)).build();
244 rendererManager.endpointLocationsUpdated(endpointLocations);
246 // EP1 as renderer endpoint
247 RendererConfigurationBuilder rendererPolicyBuilder = new RendererConfigurationBuilder();
248 rendererManager.resolveRendererConfigForEndpoint(ep1, rendererPolicyBuilder);
249 ImmutableTable<RendererEndpointKey, PeerEndpointKey, Set<RuleGroupWithRendererEndpointParticipation>> policiesByEpAndPeerEp =
250 rendererPolicyBuilder.getPoliciesByEpAndPeerEp();
251 assertFalse(policiesByEpAndPeerEp.isEmpty());
252 assertTrue(rendererPolicyBuilder.getPoliciesByEpAndPeerExtEp().isEmpty());
253 assertTrue(rendererPolicyBuilder.getPoliciesByEpAndPeerExtConEp().isEmpty());
254 assertEquals(1, policiesByEpAndPeerEp.rowKeySet().size());
255 assertEquals(2, policiesByEpAndPeerEp.columnKeySet().size());
256 // check EP1--EPG_BLUE---SUBJECT_1---(P)EPG_PURPLE--EP2
257 // check EP1--EPG_RED----SUBJECT_2---(P)EPG_GREY--EP2
258 RendererEndpointKey ep1RendererEpKey = AddressEndpointUtils.toRendererEpKey(ep1.getKey());
259 assertTrue(policiesByEpAndPeerEp.containsRow(ep1RendererEpKey));
260 PeerEndpointKey ep2PeerEpKey = AddressEndpointUtils.toPeerEpKey(ep2.getKey());
261 assertTrue(policiesByEpAndPeerEp.containsColumn(ep2PeerEpKey));
262 Set<RuleGroupWithRendererEndpointParticipation> ep1Ep2RuleGrpsWithEpPartic =
263 policiesByEpAndPeerEp.get(ep1RendererEpKey, ep2PeerEpKey);
264 assertEquals(2, ep1Ep2RuleGrpsWithEpPartic.size());
265 assertTrue(ep1Ep2RuleGrpsWithEpPartic.contains(RendererConfigurationBuilder
266 .toRuleGroupWithRendererEndpointParticipation(ruleGrp1.getKey(), EndpointPolicyParticipation.CONSUMER)));
267 assertTrue(ep1Ep2RuleGrpsWithEpPartic.contains(RendererConfigurationBuilder
268 .toRuleGroupWithRendererEndpointParticipation(ruleGrp2.getKey(), EndpointPolicyParticipation.CONSUMER)));
269 // check EP1--EPG_RED----SUBJECT_2---(P)EPG_GREY--EP3
270 PeerEndpointKey ep3PeerEpKey = AddressEndpointUtils.toPeerEpKey(ep3.getKey());
271 assertTrue(policiesByEpAndPeerEp.containsColumn(ep3PeerEpKey));
272 Set<RuleGroupWithRendererEndpointParticipation> ep1Ep3RuleGrpsWithEpPartic =
273 policiesByEpAndPeerEp.get(ep1RendererEpKey, ep3PeerEpKey);
274 assertEquals(1, ep1Ep3RuleGrpsWithEpPartic.size());
275 assertTrue(ep1Ep3RuleGrpsWithEpPartic.contains(RendererConfigurationBuilder
276 .toRuleGroupWithRendererEndpointParticipation(ruleGrp2.getKey(), EndpointPolicyParticipation.CONSUMER)));