2 * Copyright (c) 2014 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.resolver;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14 import static org.mockito.Matchers.any;
15 import static org.mockito.Mockito.mock;
16 import static org.mockito.Mockito.when;
18 import java.util.HashMap;
19 import java.util.HashSet;
20 import java.util.List;
23 import java.util.concurrent.ScheduledExecutorService;
25 import org.junit.Assert;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
29 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
30 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
31 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
32 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
33 import org.opendaylight.groupbasedpolicy.resolver.ContractResolverUtils.ContractMatch;
34 import org.opendaylight.groupbasedpolicy.resolver.PolicyResolver.TenantContext;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.CapabilityMatcherName;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.CapabilityName;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ClauseName;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ConditionMatcherName;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ConditionName;
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.QualityMatcherName;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.QualityName;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.RequirementMatcherName;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.RequirementName;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.RuleName;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.SelectorName;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.SubjectName;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TargetName;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.Matcher.MatchType;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.capabilities.Capability;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.capabilities.CapabilityBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.condition.matchers.ConditionMatcher;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.condition.matchers.ConditionMatcherBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.conditions.Condition;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.conditions.ConditionBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.qualities.Quality;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.qualities.QualityBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.requirements.Requirement;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.requirements.RequirementBuilder;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.target.selector.QualityMatcher;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.target.selector.QualityMatcherBuilder;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.target.selector.quality.matcher.MatcherQualityBuilder;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.Tenant;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.TenantBuilder;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.Contract;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.ContractBuilder;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.EndpointGroup;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.EndpointGroupBuilder;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.contract.Clause;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.contract.ClauseBuilder;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.contract.Subject;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.contract.SubjectBuilder;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.contract.Target;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.contract.TargetBuilder;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.contract.clause.ConsumerMatchersBuilder;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.contract.clause.ProviderMatchersBuilder;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.contract.clause.consumer.matchers.group.identification.constraints.GroupRequirementConstraintCaseBuilder;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.contract.clause.consumer.matchers.group.identification.constraints.group.requirement.constraint._case.RequirementMatcher;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.contract.clause.consumer.matchers.group.identification.constraints.group.requirement.constraint._case.RequirementMatcherBuilder;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.contract.clause.consumer.matchers.group.identification.constraints.group.requirement.constraint._case.requirement.matcher.MatcherRequirementBuilder;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.contract.clause.provider.matchers.group.identification.constraints.GroupCapabilityConstraintCaseBuilder;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.contract.clause.provider.matchers.group.identification.constraints.group.capability.constraint._case.CapabilityMatcher;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.contract.clause.provider.matchers.group.identification.constraints.group.capability.constraint._case.CapabilityMatcherBuilder;
86 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.contract.clause.provider.matchers.group.identification.constraints.group.capability.constraint._case.capability.matcher.MatcherCapabilityBuilder;
87 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.contract.subject.Rule;
88 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.contract.subject.RuleBuilder;
89 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.endpoint.group.ConsumerNamedSelector;
90 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.endpoint.group.ConsumerNamedSelectorBuilder;
91 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.endpoint.group.ConsumerTargetSelector;
92 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.endpoint.group.ConsumerTargetSelectorBuilder;
93 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.endpoint.group.ProviderNamedSelector;
94 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.endpoint.group.ProviderNamedSelectorBuilder;
95 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.endpoint.group.ProviderTargetSelector;
96 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.endpoint.group.ProviderTargetSelectorBuilder;
97 import org.opendaylight.yangtools.concepts.ListenerRegistration;
98 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
100 import com.google.common.collect.ImmutableList;
101 import com.google.common.collect.ImmutableSet;
102 import com.google.common.collect.Table;
103 import com.google.common.util.concurrent.CheckedFuture;
105 public class PolicyResolverTest {
106 Quality q1 = new QualityBuilder()
107 .setName(new QualityName("q1"))
109 Quality q2 = new QualityBuilder()
110 .setName(new QualityName("q2"))
112 Quality q3 = new QualityBuilder()
113 .setName(new QualityName("q3"))
116 Requirement r1 = new RequirementBuilder()
117 .setName(new RequirementName("r1"))
119 Requirement r3 = new RequirementBuilder()
120 .setName(new RequirementName("r3"))
123 Capability cap1 = new CapabilityBuilder()
124 .setName(new CapabilityName("cap1"))
126 Capability cap3 = new CapabilityBuilder()
127 .setName(new CapabilityName("cap3"))
130 Condition cond1 = new ConditionBuilder()
131 .setName(new ConditionName("cond1"))
133 Condition cond2 = new ConditionBuilder()
134 .setName(new ConditionName("cond2"))
136 Condition cond3 = new ConditionBuilder()
137 .setName(new ConditionName("cond3"))
140 Target t1 = new TargetBuilder()
141 .setName(new TargetName("t1"))
142 .setQuality(ImmutableList.of(q1,q2))
144 Target t2 = new TargetBuilder()
145 .setName(new TargetName("t1"))
146 .setQuality(ImmutableList.of(q3))
148 Target t0 = new TargetBuilder()
149 .setName(new TargetName("t1"))
152 Rule rule1 = new RuleBuilder()
153 .setName(new RuleName("r1"))
154 .setOrder(Integer.valueOf(5))
156 Rule rule2 = new RuleBuilder()
157 .setName(new RuleName("r2"))
159 Rule rule3 = new RuleBuilder()
160 .setName(new RuleName("r3"))
162 Subject s1 = new SubjectBuilder()
163 .setName(new SubjectName("s1"))
164 .setRule(ImmutableList.of(rule1))
166 Subject s2 = new SubjectBuilder()
167 .setName(new SubjectName("s1"))
168 .setRule(ImmutableList.of(rule2))
169 .setOrder(Integer.valueOf(3))
171 Subject s3 = new SubjectBuilder()
172 .setName(new SubjectName("s3"))
173 .setRule(ImmutableList.of(rule3))
174 .setOrder(Integer.valueOf(3))
177 RequirementMatcher rm1 = new RequirementMatcherBuilder()
178 .setName(new RequirementMatcherName("rm1"))
179 .setMatcherRequirement(ImmutableList.of(new MatcherRequirementBuilder(r1).build()))
181 CapabilityMatcher capm1 = new CapabilityMatcherBuilder()
182 .setName(new CapabilityMatcherName("cap1"))
183 .setMatcherCapability(ImmutableList.of(new MatcherCapabilityBuilder(cap1).build()))
185 ConditionMatcher condm1 = new ConditionMatcherBuilder()
186 .setName(new ConditionMatcherName("condm1"))
187 .setCondition(ImmutableList.of(cond1))
188 .setMatchType(MatchType.All)
190 ConditionMatcher condm2 = new ConditionMatcherBuilder()
191 .setName(new ConditionMatcherName("condm2"))
192 .setCondition(ImmutableList.of(cond1, cond2))
193 .setMatchType(MatchType.Any)
195 ConditionMatcher condm3 = new ConditionMatcherBuilder()
196 .setName(new ConditionMatcherName("condm3"))
197 .setCondition(ImmutableList.of(cond3))
198 .setMatchType(MatchType.None)
201 Clause clause1 = new ClauseBuilder()
202 .setName(new ClauseName("clause1"))
203 .setConsumerMatchers(new ConsumerMatchersBuilder()
204 .setGroupIdentificationConstraints(new GroupRequirementConstraintCaseBuilder()
205 .setRequirementMatcher(ImmutableList.of(rm1)).build())
206 .setConditionMatcher(ImmutableList.of(condm1, condm2, condm3))
208 .setProviderMatchers(new ProviderMatchersBuilder()
209 .setGroupIdentificationConstraints(new GroupCapabilityConstraintCaseBuilder()
210 .setCapabilityMatcher(ImmutableList.of(capm1)).build())
212 .setSubjectRefs(ImmutableList.of(s1.getName()))
214 Clause clause3 = new ClauseBuilder()
215 .setName(new ClauseName("clause3"))
216 .setSubjectRefs(ImmutableList.of(s3.getName(), s2.getName()))
218 Clause clause0 = new ClauseBuilder()
219 .setName(new ClauseName("clause0"))
221 Clause clause00 = new ClauseBuilder()
222 .setName(new ClauseName("clause00"))
223 .setConsumerMatchers(new ConsumerMatchersBuilder().build())
224 .setProviderMatchers(new ProviderMatchersBuilder().build())
227 Contract contract1 = new ContractBuilder()
228 .setId(new ContractId("c9eea992-ba51-4e11-b797-986853832ad9"))
229 .setTarget(ImmutableList.of(t1))
230 .setClause(ImmutableList.of(clause1, clause0, clause00))
231 .setSubject(ImmutableList.of(s1))
233 Contract contract2 = new ContractBuilder()
234 .setId(new ContractId("3a3b67ff-1795-4dc0-a7b2-2c3453872e4e"))
235 .setTarget(ImmutableList.of(t1, t2))
236 .setClause(ImmutableList.of(clause3))
237 .setSubject(ImmutableList.of(s2))
239 Contract contract0 = new ContractBuilder()
240 .setId(new ContractId("ce467a3c-2c7b-4e9e-a575-7da1fbdf1833"))
242 Contract contract00 = new ContractBuilder()
243 .setId(new ContractId("79de88e8-b37f-4764-a1a3-7f3b37b15433"))
244 .setTarget(ImmutableList.of(t0))
247 ConsumerNamedSelector cns1 = new ConsumerNamedSelectorBuilder()
248 .setName(new SelectorName("cns1"))
249 .setContract(ImmutableList.of(contract1.getId()))
250 .setRequirement(ImmutableList.of(r1, r3))
252 ConsumerNamedSelector cns2 = new ConsumerNamedSelectorBuilder()
253 .setName(new SelectorName("cns2"))
254 .setContract(ImmutableList.of(contract2.getId()))
255 .setRequirement(ImmutableList.of(r1, r3))
257 ProviderNamedSelector pns1 = new ProviderNamedSelectorBuilder()
258 .setName(new SelectorName("pns1"))
259 .setContract(ImmutableList.of(contract1.getId(), contract2.getId()))
260 .setCapability(ImmutableList.of(cap1, cap3))
263 QualityMatcher qm1 = new QualityMatcherBuilder()
264 .setName(new QualityMatcherName("qm1"))
265 .setMatcherQuality(ImmutableList.of(new MatcherQualityBuilder(q1).build()))
267 QualityMatcher qm3 = new QualityMatcherBuilder()
268 .setName(new QualityMatcherName("qm3"))
269 .setMatcherQuality(ImmutableList.of(new MatcherQualityBuilder(q3).build()))
271 ConsumerTargetSelector cts1 = new ConsumerTargetSelectorBuilder()
272 .setName(new SelectorName("cts1"))
273 .setQualityMatcher(ImmutableList.of(qm1))
275 ProviderTargetSelector pts1 = new ProviderTargetSelectorBuilder()
276 .setName(new SelectorName("pts1"))
277 .setQualityMatcher(ImmutableList.of(qm3))
280 EndpointGroup eg1 = new EndpointGroupBuilder()
281 .setId(new EndpointGroupId("12802e21-8602-40ec-91d3-a75a296881ab"))
282 .setConsumerNamedSelector(ImmutableList.of(cns1))
284 EndpointGroup eg2 = new EndpointGroupBuilder()
285 .setId(new EndpointGroupId("66bb92ff-6e4c-41f1-8c7d-baa322016ab5"))
286 .setProviderNamedSelector(ImmutableList.of(pns1))
288 EndpointGroup eg3 = new EndpointGroupBuilder()
289 .setId(new EndpointGroupId("0ed93cb5-28ee-46bd-a5a1-41d6aa88dae5"))
290 .setConsumerNamedSelector(ImmutableList.of(cns1, cns2))
292 EndpointGroup eg4 = new EndpointGroupBuilder()
293 .setId(new EndpointGroupId("51eaf011-94a9-4cb1-b12d-149b77c5c016"))
294 .setConsumerTargetSelector(ImmutableList.of(cts1))
296 EndpointGroup eg5 = new EndpointGroupBuilder()
297 .setId(new EndpointGroupId("92344738-ba37-4d69-b9e5-904eebdad585"))
298 .setProviderTargetSelector(ImmutableList.of(pts1))
300 EndpointGroup eg0 = new EndpointGroupBuilder()
301 .setId(new EndpointGroupId("64e03313-d6d8-43cb-ae4d-5a9b0a410c91"))
304 Tenant tenant1 = new TenantBuilder()
305 .setId(new TenantId("144b9aec-ef06-44f1-a50c-2fe5be456feb"))
306 .setContract(ImmutableList.of(contract1, contract2))
307 .setEndpointGroup(ImmutableList.of(eg1, eg2))
309 Tenant tenant2 = new TenantBuilder()
310 .setId(new TenantId("138a2bc3-d3cb-4588-ad7a-63c9f19ce3e5"))
311 .setContract(ImmutableList.of(contract1, contract2))
312 .setEndpointGroup(ImmutableList.of(eg1, eg2, eg3))
314 Tenant tenant3 = new TenantBuilder()
315 .setId(new TenantId("d1feede4-c31f-4232-ace2-93fcd065af1d"))
316 .setContract(ImmutableList.of(contract1, contract2))
317 .setEndpointGroup(ImmutableList.of(eg4, eg5))
319 Tenant tenant0 = new TenantBuilder().build();
320 Tenant tenant00 = new TenantBuilder()
321 .setContract(ImmutableList.of(contract0, contract00))
322 .setEndpointGroup(ImmutableList.of(eg0))
325 PolicyResolver resolver;
328 public void setup() throws Exception {
329 resolver = new PolicyResolver(null, null);
333 public void listenerTest() {
334 PolicyScope policyScope;
335 PolicyListener policyListener = mock(PolicyListener.class);
336 Assert.assertTrue(resolver.policyListenerScopes.isEmpty());
338 policyScope = resolver.registerListener(policyListener);
339 Assert.assertNotNull(policyScope);
340 Assert.assertFalse(resolver.policyListenerScopes.isEmpty());
342 resolver.removeListener(policyScope);
343 Assert.assertTrue(resolver.policyListenerScopes.isEmpty());
346 @SuppressWarnings({"unchecked", "rawtypes"})
348 public void tenantTest() {
349 DataBroker dataProvider = mock(DataBroker.class);
350 ScheduledExecutorService executor = mock(ScheduledExecutorService.class);
351 resolver = new PolicyResolver(dataProvider, executor);
353 TenantId tenantId = mock(TenantId.class);
354 Assert.assertTrue(resolver.resolvedTenants.isEmpty());
356 ListenerRegistration<DataChangeListener> registration = mock(ListenerRegistration.class);
358 dataProvider.registerDataChangeListener(any(LogicalDatastoreType.class), any(InstanceIdentifier.class),
359 any(DataChangeListener.class), any(DataChangeScope.class))).thenReturn(registration);
361 ReadOnlyTransaction transaction = mock(ReadOnlyTransaction.class);
362 when(dataProvider.newReadOnlyTransaction()).thenReturn(transaction);
363 CheckedFuture unresolved = mock(CheckedFuture.class);
364 when(transaction.read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class))).thenReturn(unresolved);
366 resolver.subscribeTenant(tenantId);
367 Assert.assertFalse(resolver.resolvedTenants.isEmpty());
369 resolver.unsubscribeTenant(tenantId);
370 Assert.assertTrue(resolver.resolvedTenants.isEmpty());
373 public void verifyMatches(List<ContractId> contrids,
374 List<TenantId> contrtids,
375 List<ContractMatch> matches) {
376 HashSet<ContractMatchKey> v = new HashSet<>();
377 for (int i = 0; i < contrids.size(); i++) {
378 v.add(new ContractMatchKey(contrtids.get(i), contrids.get(i)));
380 assertEquals(contrids.size(), matches.size());
381 for (ContractMatch m : matches) {
383 new ContractMatchKey(m.contractTenant.getId(),
385 assertTrue(v.contains(k));
390 public void testContractSelection() throws Exception {
392 TenantContext tc = new TenantContext(null);
394 tc.tenant.set(new IndexedTenant(tenant1));
395 Table<EgKey, EgKey, List<ContractMatch>> contractMatches =
396 ContractResolverUtils.selectContracts(ImmutableSet.of(tc.tenant.get()));
397 assertEquals(1, contractMatches.size());
398 List<ContractMatch> matches =
399 contractMatches.get(new EgKey(tenant1.getId(), eg1.getId()),
400 new EgKey(tenant1.getId(), eg2.getId()));
401 verifyMatches(ImmutableList.of(contract1.getId()),
402 ImmutableList.of(tenant1.getId()),
406 tc.tenant.set(new IndexedTenant(tenant2));
407 contractMatches = ContractResolverUtils.selectContracts(ImmutableSet.of(tc.tenant.get()));
408 assertEquals(2, contractMatches.size());
409 matches = contractMatches.get(new EgKey(tenant2.getId(), eg1.getId()),
410 new EgKey(tenant2.getId(), eg2.getId()));
411 verifyMatches(ImmutableList.of(contract1.getId()),
412 ImmutableList.of(tenant2.getId()),
415 matches = contractMatches.get(new EgKey(tenant2.getId(), eg3.getId()),
416 new EgKey(tenant2.getId(), eg2.getId()));
417 verifyMatches(ImmutableList.of(contract2.getId(), contract1.getId()),
418 ImmutableList.of(tenant2.getId(), tenant2.getId()),
422 tc.tenant.set(new IndexedTenant(tenant3));
423 contractMatches = ContractResolverUtils.selectContracts(ImmutableSet.of(tc.tenant.get()));
424 assertEquals(1, contractMatches.size());
425 matches = contractMatches.get(new EgKey(tenant3.getId(), eg4.getId()),
426 new EgKey(tenant3.getId(), eg5.getId()));
427 verifyMatches(ImmutableList.of(contract2.getId()),
428 ImmutableList.of(tenant3.getId()),
432 tc.tenant.set(new IndexedTenant(tenant0));
433 contractMatches = ContractResolverUtils.selectContracts(ImmutableSet.of(tc.tenant.get()));
434 assertEquals(0, contractMatches.size());
436 tc.tenant.set(new IndexedTenant(tenant00));
437 contractMatches = ContractResolverUtils.selectContracts(ImmutableSet.of(tc.tenant.get()));
438 assertEquals(0, contractMatches.size());
442 public void testSubjectSelection() throws Exception {
444 new ConditionSet(ImmutableSet.of(cond1.getName()),
445 ImmutableSet.of(cond3.getName()),
446 ImmutableSet.of(ImmutableSet.of(cond1.getName(),
448 EndpointConstraint ec1 =
449 new EndpointConstraint(cs, null);
450 EndpointConstraint ec2 =
451 new EndpointConstraint(ConditionSet.EMPTY, null);
452 TenantContext tc = new TenantContext(null);
454 tc.tenant.set(new IndexedTenant(tenant1));
455 Table<EgKey, EgKey, List<ContractMatch>> contractMatches =
456 ContractResolverUtils.selectContracts(ImmutableSet.of(tc.tenant.get()));
457 Map<EgKey, Set<ConditionSet>> egConditions = new HashMap<>();
458 Table<EgKey, EgKey, Policy> policy =
459 SubjectResolverUtils.selectSubjects(contractMatches, egConditions);
460 assertEquals(1, policy.size());
461 Policy p = policy.get(new EgKey(tenant1.getId(), eg1.getId()), new EgKey(tenant1.getId(), eg2.getId()));
462 List<RuleGroup> rules = p.getRuleMap().get(ec1, ec2);
463 assertNotNull(rules);
464 assertEquals(1, rules.size());
465 RuleGroup rg = rules.get(0);
466 assertEquals(tenant1.getId(), rg.contractTenant.getId());
467 assertEquals(contract1.getId(), rg.relatedContract.getId());
468 assertEquals(s1.getName(), rg.relatedSubject);
469 assertEquals(1, rg.rules.size());
470 assertEquals(rule1.getName(), rg.rules.get(0).getName());
472 tc.tenant.set(new IndexedTenant(tenant2));
473 contractMatches = ContractResolverUtils.selectContracts(ImmutableSet.of(tc.tenant.get()));
474 egConditions = new HashMap<>();
475 policy = SubjectResolverUtils.selectSubjects(contractMatches, egConditions);
477 assertEquals(2, policy.size());
478 p = policy.get(new EgKey(tenant2.getId(), eg3.getId()),
479 new EgKey(tenant2.getId(), eg2.getId()));
480 rules = p.getRuleMap().get(ec1, ec2);
481 assertNotNull(rules);
482 assertEquals(1, rules.size());
484 assertEquals(tenant2.getId(), rg.contractTenant.getId());
485 assertEquals(contract1.getId(), rg.relatedContract.getId());
486 assertEquals(s1.getName(), rg.relatedSubject);
487 assertEquals(1, rg.rules.size());
488 assertEquals(rule1.getName(), rg.rules.get(0).getName());
490 rules = p.getRuleMap().get(ec2, ec2);
491 assertNotNull(rules);
492 assertEquals(1, rules.size());
494 assertEquals(tenant2.getId(), rg.contractTenant.getId());
495 assertEquals(contract2.getId(), rg.relatedContract.getId());
496 assertEquals(s2.getName(), rg.relatedSubject);
497 assertEquals(1, rg.rules.size());
498 assertEquals(rule2.getName(), rg.rules.get(0).getName());
500 p = policy.get(new EgKey(tenant2.getId(), eg1.getId()),
501 new EgKey(tenant2.getId(), eg2.getId()));
502 rules = p.getRuleMap().get(ec1, ec2);
503 assertNotNull(rules);
504 assertEquals(1, rules.size());
506 assertEquals(tenant2.getId(), rg.contractTenant.getId());
507 assertEquals(contract1.getId(), rg.relatedContract.getId());
508 assertEquals(s1.getName(), rg.relatedSubject);
509 assertEquals(1, rg.rules.size());
510 assertEquals(rule1.getName(), rg.rules.get(0).getName());
512 tc.tenant.set(new IndexedTenant(tenant3));
513 contractMatches = ContractResolverUtils.selectContracts(ImmutableSet.of(tc.tenant.get()));
514 egConditions = new HashMap<>();
515 policy = SubjectResolverUtils.selectSubjects(contractMatches, egConditions);
517 assertEquals(1, policy.size());
518 p = policy.get(new EgKey(tenant3.getId(), eg4.getId()),
519 new EgKey(tenant3.getId(), eg5.getId()));
520 rules = p.getRuleMap().get(ec2, ec2);
521 assertNotNull(rules);
522 assertEquals(1, rules.size());
524 assertEquals(tenant3.getId(), rg.contractTenant.getId());
525 assertEquals(contract2.getId(), rg.relatedContract.getId());
526 assertEquals(s2.getName(), rg.relatedSubject);
527 assertEquals(1, rg.rules.size());
528 assertEquals(rule2.getName(), rg.rules.get(0).getName());
531 private static class ContractMatchKey {
534 public ContractMatchKey(TenantId tenant, ContractId contract) {
536 this.tenant = tenant;
537 this.contract = contract;
540 public int hashCode() {
541 final int prime = 31;
543 result = prime * result +
544 ((contract == null) ? 0 : contract.hashCode());
545 result = prime * result +
546 ((tenant == null) ? 0 : tenant.hashCode());
550 public boolean equals(Object obj) {
555 if (getClass() != obj.getClass())
557 ContractMatchKey other = (ContractMatchKey) obj;
558 if (contract == null) {
559 if (other.contract != null)
561 } else if (!contract.equals(other.contract))
563 if (tenant == null) {
564 if (other.tenant != null)
566 } else if (!tenant.equals(other.tenant))
571 public String toString() {
572 return "ContractMatchKey [tenant=" + tenant + ", contract=" +