Merge "Adding EIC."
[groupbasedpolicy.git] / groupbasedpolicy / src / test / java / org / opendaylight / groupbasedpolicy / resolver / PolicyResolverTest.java
1 /*
2  * Copyright (c) 2014 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.resolver;
10
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;
17
18 import java.util.HashMap;
19 import java.util.HashSet;
20 import java.util.List;
21 import java.util.Map;
22 import java.util.Set;
23 import java.util.concurrent.ScheduledExecutorService;
24
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;
99
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;
104
105 public class PolicyResolverTest {
106     Quality q1 = new QualityBuilder()
107         .setName(new QualityName("q1"))
108         .build();
109     Quality q2 = new QualityBuilder()
110         .setName(new QualityName("q2"))
111         .build();
112     Quality q3 = new QualityBuilder()
113         .setName(new QualityName("q3"))
114         .build();
115
116     Requirement r1 = new RequirementBuilder()
117         .setName(new RequirementName("r1"))
118         .build();
119     Requirement r3 = new RequirementBuilder()
120         .setName(new RequirementName("r3"))
121         .build();
122
123     Capability cap1 = new CapabilityBuilder()
124         .setName(new CapabilityName("cap1"))
125         .build();
126     Capability cap3 = new CapabilityBuilder()
127         .setName(new CapabilityName("cap3"))
128         .build();
129
130     Condition cond1 = new ConditionBuilder()
131         .setName(new ConditionName("cond1"))
132         .build();
133     Condition cond2 = new ConditionBuilder()
134         .setName(new ConditionName("cond2"))
135         .build();
136     Condition cond3 = new ConditionBuilder()
137         .setName(new ConditionName("cond3"))
138         .build();
139
140     Target t1 = new TargetBuilder()
141         .setName(new TargetName("t1"))
142         .setQuality(ImmutableList.of(q1,q2))
143         .build();
144     Target t2 = new TargetBuilder()
145         .setName(new TargetName("t1"))
146         .setQuality(ImmutableList.of(q3))
147         .build();
148     Target t0 = new TargetBuilder()
149         .setName(new TargetName("t1"))
150         .build();
151
152     Rule rule1 = new RuleBuilder()
153         .setName(new RuleName("r1"))
154         .setOrder(Integer.valueOf(5))
155         .build();
156     Rule rule2 = new RuleBuilder()
157         .setName(new RuleName("r2"))
158         .build();
159     Rule rule3 = new RuleBuilder()
160         .setName(new RuleName("r3"))
161         .build();
162     Subject s1 = new SubjectBuilder()
163         .setName(new SubjectName("s1"))
164         .setRule(ImmutableList.of(rule1))
165         .build();
166     Subject s2 = new SubjectBuilder()
167         .setName(new SubjectName("s1"))
168         .setRule(ImmutableList.of(rule2))
169         .setOrder(Integer.valueOf(3))
170         .build();
171     Subject s3 = new SubjectBuilder()
172         .setName(new SubjectName("s3"))
173         .setRule(ImmutableList.of(rule3))
174         .setOrder(Integer.valueOf(3))
175         .build();
176
177     RequirementMatcher rm1 = new RequirementMatcherBuilder()
178         .setName(new RequirementMatcherName("rm1"))
179         .setMatcherRequirement(ImmutableList.of(new MatcherRequirementBuilder(r1).build()))
180         .build();
181     CapabilityMatcher capm1 = new CapabilityMatcherBuilder()
182         .setName(new CapabilityMatcherName("cap1"))
183         .setMatcherCapability(ImmutableList.of(new MatcherCapabilityBuilder(cap1).build()))
184         .build();
185     ConditionMatcher condm1 = new ConditionMatcherBuilder()
186         .setName(new ConditionMatcherName("condm1"))
187         .setCondition(ImmutableList.of(cond1))
188         .setMatchType(MatchType.All)
189         .build();
190     ConditionMatcher condm2 = new ConditionMatcherBuilder()
191         .setName(new ConditionMatcherName("condm2"))
192         .setCondition(ImmutableList.of(cond1, cond2))
193         .setMatchType(MatchType.Any)
194         .build();
195     ConditionMatcher condm3 = new ConditionMatcherBuilder()
196         .setName(new ConditionMatcherName("condm3"))
197         .setCondition(ImmutableList.of(cond3))
198         .setMatchType(MatchType.None)
199         .build();
200
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))
207             .build())
208         .setProviderMatchers(new ProviderMatchersBuilder()
209             .setGroupIdentificationConstraints(new GroupCapabilityConstraintCaseBuilder()
210             .setCapabilityMatcher(ImmutableList.of(capm1)).build())
211             .build())
212         .setSubjectRefs(ImmutableList.of(s1.getName()))
213         .build();
214     Clause clause3 = new ClauseBuilder()
215         .setName(new ClauseName("clause3"))
216         .setSubjectRefs(ImmutableList.of(s3.getName(), s2.getName()))
217         .build();
218     Clause clause0 = new ClauseBuilder()
219         .setName(new ClauseName("clause0"))
220         .build();
221     Clause clause00 = new ClauseBuilder()
222         .setName(new ClauseName("clause00"))
223         .setConsumerMatchers(new ConsumerMatchersBuilder().build())
224         .setProviderMatchers(new ProviderMatchersBuilder().build())
225         .build();
226
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))
232         .build();
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))
238         .build();
239     Contract contract0 = new ContractBuilder()
240         .setId(new ContractId("ce467a3c-2c7b-4e9e-a575-7da1fbdf1833"))
241         .build();
242     Contract contract00 = new ContractBuilder()
243         .setId(new ContractId("79de88e8-b37f-4764-a1a3-7f3b37b15433"))
244         .setTarget(ImmutableList.of(t0))
245         .build();
246
247     ConsumerNamedSelector cns1 = new ConsumerNamedSelectorBuilder()
248         .setName(new SelectorName("cns1"))
249         .setContract(ImmutableList.of(contract1.getId()))
250         .setRequirement(ImmutableList.of(r1, r3))
251         .build();
252     ConsumerNamedSelector cns2 = new ConsumerNamedSelectorBuilder()
253         .setName(new SelectorName("cns2"))
254         .setContract(ImmutableList.of(contract2.getId()))
255         .setRequirement(ImmutableList.of(r1, r3))
256         .build();
257     ProviderNamedSelector pns1 = new ProviderNamedSelectorBuilder()
258         .setName(new SelectorName("pns1"))
259         .setContract(ImmutableList.of(contract1.getId(), contract2.getId()))
260         .setCapability(ImmutableList.of(cap1, cap3))
261         .build();
262
263     QualityMatcher qm1 = new QualityMatcherBuilder()
264         .setName(new QualityMatcherName("qm1"))
265         .setMatcherQuality(ImmutableList.of(new MatcherQualityBuilder(q1).build()))
266         .build();
267     QualityMatcher qm3 = new QualityMatcherBuilder()
268          .setName(new QualityMatcherName("qm3"))
269          .setMatcherQuality(ImmutableList.of(new MatcherQualityBuilder(q3).build()))
270          .build();
271     ConsumerTargetSelector cts1 = new ConsumerTargetSelectorBuilder()
272         .setName(new SelectorName("cts1"))
273         .setQualityMatcher(ImmutableList.of(qm1))
274         .build();
275     ProviderTargetSelector pts1 = new ProviderTargetSelectorBuilder()
276         .setName(new SelectorName("pts1"))
277         .setQualityMatcher(ImmutableList.of(qm3))
278         .build();
279
280     EndpointGroup eg1 = new EndpointGroupBuilder()
281         .setId(new EndpointGroupId("12802e21-8602-40ec-91d3-a75a296881ab"))
282         .setConsumerNamedSelector(ImmutableList.of(cns1))
283         .build();
284     EndpointGroup eg2 = new EndpointGroupBuilder()
285         .setId(new EndpointGroupId("66bb92ff-6e4c-41f1-8c7d-baa322016ab5"))
286         .setProviderNamedSelector(ImmutableList.of(pns1))
287         .build();
288     EndpointGroup eg3 = new EndpointGroupBuilder()
289         .setId(new EndpointGroupId("0ed93cb5-28ee-46bd-a5a1-41d6aa88dae5"))
290         .setConsumerNamedSelector(ImmutableList.of(cns1, cns2))
291         .build();
292     EndpointGroup eg4 = new EndpointGroupBuilder()
293         .setId(new EndpointGroupId("51eaf011-94a9-4cb1-b12d-149b77c5c016"))
294         .setConsumerTargetSelector(ImmutableList.of(cts1))
295         .build();
296     EndpointGroup eg5 = new EndpointGroupBuilder()
297         .setId(new EndpointGroupId("92344738-ba37-4d69-b9e5-904eebdad585"))
298         .setProviderTargetSelector(ImmutableList.of(pts1))
299         .build();
300     EndpointGroup eg0 = new EndpointGroupBuilder()
301         .setId(new EndpointGroupId("64e03313-d6d8-43cb-ae4d-5a9b0a410c91"))
302         .build();
303
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))
308         .build();
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))
313         .build();
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))
318         .build();
319     Tenant tenant0 = new TenantBuilder().build();
320     Tenant tenant00 = new TenantBuilder()
321         .setContract(ImmutableList.of(contract0, contract00))
322         .setEndpointGroup(ImmutableList.of(eg0))
323         .build();
324
325     PolicyResolver resolver;
326
327     @Before
328     public void setup() throws Exception {
329         resolver = new PolicyResolver(null, null);
330     }
331
332     @Test
333     public void listenerTest() {
334         PolicyScope policyScope;
335         PolicyListener policyListener = mock(PolicyListener.class);
336         Assert.assertTrue(resolver.policyListenerScopes.isEmpty());
337
338         policyScope = resolver.registerListener(policyListener);
339         Assert.assertNotNull(policyScope);
340         Assert.assertFalse(resolver.policyListenerScopes.isEmpty());
341
342         resolver.removeListener(policyScope);
343         Assert.assertTrue(resolver.policyListenerScopes.isEmpty());
344     }
345
346     @SuppressWarnings({"unchecked", "rawtypes"})
347     @Test
348     public void tenantTest() {
349         DataBroker dataProvider = mock(DataBroker.class);
350         ScheduledExecutorService executor = mock(ScheduledExecutorService.class);
351         resolver = new PolicyResolver(dataProvider, executor);
352
353         TenantId tenantId = mock(TenantId.class);
354         Assert.assertTrue(resolver.resolvedTenants.isEmpty());
355
356         ListenerRegistration<DataChangeListener> registration = mock(ListenerRegistration.class);
357         when(
358                 dataProvider.registerDataChangeListener(any(LogicalDatastoreType.class), any(InstanceIdentifier.class),
359                         any(DataChangeListener.class), any(DataChangeScope.class))).thenReturn(registration);
360
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);
365
366         resolver.subscribeTenant(tenantId);
367         Assert.assertFalse(resolver.resolvedTenants.isEmpty());
368
369         resolver.unsubscribeTenant(tenantId);
370         Assert.assertTrue(resolver.resolvedTenants.isEmpty());
371     }
372
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)));
379         }
380         assertEquals(contrids.size(), matches.size());
381         for (ContractMatch m : matches) {
382             ContractMatchKey k =
383                     new ContractMatchKey(m.contractTenant.getId(),
384                                          m.contract.getId());
385             assertTrue(v.contains(k));
386         }
387     }
388
389     @Test
390     public void testContractSelection() throws Exception {
391         // named selectors
392         TenantContext tc = new TenantContext(null);
393
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()),
403                       matches);
404
405
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()),
413                       matches);
414
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()),
419                       matches);
420
421         // target selectors
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()),
429                       matches);
430
431         // empty matches
432         tc.tenant.set(new IndexedTenant(tenant0));
433         contractMatches = ContractResolverUtils.selectContracts(ImmutableSet.of(tc.tenant.get()));
434         assertEquals(0, contractMatches.size());
435
436         tc.tenant.set(new IndexedTenant(tenant00));
437         contractMatches = ContractResolverUtils.selectContracts(ImmutableSet.of(tc.tenant.get()));
438         assertEquals(0, contractMatches.size());
439     }
440
441     @Test
442     public void testSubjectSelection() throws Exception {
443         ConditionSet cs =
444                 new ConditionSet(ImmutableSet.of(cond1.getName()),
445                                  ImmutableSet.of(cond3.getName()),
446                                  ImmutableSet.of(ImmutableSet.of(cond1.getName(),
447                                                                  cond2.getName())));
448         EndpointConstraint ec1 =
449                 new EndpointConstraint(cs, null);
450         EndpointConstraint ec2 =
451                 new EndpointConstraint(ConditionSet.EMPTY, null);
452         TenantContext tc = new TenantContext(null);
453
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());
471
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);
476
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());
483         rg = rules.get(0);
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());
489
490         rules = p.getRuleMap().get(ec2, ec2);
491         assertNotNull(rules);
492         assertEquals(1, rules.size());
493         rg = rules.get(0);
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());
499
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());
505         rg = rules.get(0);
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());
511
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);
516
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());
523         rg = rules.get(0);
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());
529     }
530
531     private static class ContractMatchKey {
532         TenantId tenant;
533         ContractId contract;
534         public ContractMatchKey(TenantId tenant, ContractId contract) {
535             super();
536             this.tenant = tenant;
537             this.contract = contract;
538         }
539         @Override
540         public int hashCode() {
541             final int prime = 31;
542             int result = 1;
543             result = prime * result +
544                      ((contract == null) ? 0 : contract.hashCode());
545             result = prime * result +
546                      ((tenant == null) ? 0 : tenant.hashCode());
547             return result;
548         }
549         @Override
550         public boolean equals(Object obj) {
551             if (this == obj)
552                 return true;
553             if (obj == null)
554                 return false;
555             if (getClass() != obj.getClass())
556                 return false;
557             ContractMatchKey other = (ContractMatchKey) obj;
558             if (contract == null) {
559                 if (other.contract != null)
560                     return false;
561             } else if (!contract.equals(other.contract))
562                 return false;
563             if (tenant == null) {
564                 if (other.tenant != null)
565                     return false;
566             } else if (!tenant.equals(other.tenant))
567                 return false;
568             return true;
569         }
570         @Override
571         public String toString() {
572             return "ContractMatchKey [tenant=" + tenant + ", contract=" +
573                    contract + "]";
574         }
575     }
576
577 }