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