1 package org.opendaylight.groupbasedpolicy.resolver;
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.HashMap;
6 import java.util.HashSet;
11 import org.opendaylight.groupbasedpolicy.resolver.ContractResolverUtils.ContractMatch;
12 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ConditionName;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.SubjectName;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.Matcher.MatchType;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.condition.matchers.ConditionMatcher;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.conditions.Condition;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.Tenant;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.Contract;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.contract.Clause;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.contract.Subject;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.contract.clause.consumer.matchers.GroupIdentificationConstraints;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.contract.clause.consumer.matchers.group.identification.constraints.GroupRequirementConstraintCase;
23 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;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.contract.clause.provider.matchers.group.identification.constraints.GroupCapabilityConstraintCase;
25 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;
27 import com.google.common.collect.HashBasedTable;
28 import com.google.common.collect.ImmutableSet;
29 import com.google.common.collect.Table;
30 import com.google.common.collect.Table.Cell;
33 public class SubjectResolverUtils {
35 private SubjectResolverUtils() {
36 throw new UnsupportedOperationException("Cannot create an instance");
40 * Choose the set of subjects that in scope for each possible set of
43 // TODO Li msunal do we really need contractMatches to be a type Table<EgKey, EgKey, List<ContractMatch>>
44 // it should be sufficient to be just List<ContractMatch>
45 protected static Table<EgKey, EgKey, Policy> selectSubjects(
46 Table<EgKey, EgKey, List<ContractMatch>> contractMatches, Map<EgKey, Set<ConditionSet>> egConditions) {
47 // TODO: Note that it's possible to further simplify the resulting
49 // in the case of things like repeated rules, condition sets that
50 // cover other condition sets, etc. This would be a good thing to do
52 Table<EgKey, EgKey, Policy> policy = HashBasedTable.create();
54 for (List<ContractMatch> matches : contractMatches.values()) {
55 for (ContractMatch match : matches) {
56 List<Clause> clauses = match.contract.getClause();
60 List<Subject> subjectList = match.contract.getSubject();
61 if (subjectList == null)
64 EgKey ckey = new EgKey(match.consumerTenant.getId(),
65 match.consumer.getId());
66 EgKey pkey = new EgKey(match.providerTenant.getId(),
67 match.provider.getId());
68 Policy existing = policy.get(ckey, pkey);
70 HashMap<SubjectName, Subject> subjects = new HashMap<>();
71 for (Subject s : subjectList) {
72 subjects.put(s.getName(), s);
75 Table<EndpointConstraint, EndpointConstraint, List<Subject>> subjectMap =
76 HashBasedTable.create();
78 for (Clause clause : clauses) {
79 if (clause.getSubjectRefs() != null &&
80 clauseMatchesByGroupReqAndCapConstraints(clause, match)) {
81 ConditionSet consCSet = buildConsConditionSet(clause);
82 addConditionSet(ckey, consCSet, egConditions);
83 EndpointConstraint consEpConstraint = new EndpointConstraint(consCSet,
84 clause.getConsumerMatchers() == null ? null : clause.getConsumerMatchers()
85 .getEndpointIdentificationConstraints());
86 ConditionSet provCSet = buildProvConditionSet(clause);
87 addConditionSet(pkey, provCSet, egConditions);
88 EndpointConstraint provEpConstraint = new EndpointConstraint(provCSet,
89 clause.getProviderMatchers() == null ? null : clause.getProviderMatchers()
90 .getEndpointIdentificationConstraints());
91 List<Subject> clauseSubjects = subjectMap.get(consEpConstraint, provEpConstraint);
92 if (clauseSubjects == null) {
93 clauseSubjects = new ArrayList<>();
94 subjectMap.put(consEpConstraint, provEpConstraint, clauseSubjects);
96 for (SubjectName sn : clause.getSubjectRefs()) {
97 Subject s = subjects.get(sn);
99 clauseSubjects.add(s);
104 policy.put(ckey, pkey,
105 resolvePolicy(match.contractTenant,
115 private static boolean clauseMatchesByGroupReqAndCapConstraints(Clause clause, ContractMatch match) {
116 if (clause.getConsumerMatchers() != null) {
117 GroupIdentificationConstraints groupIdentificationConstraintsConsumer = clause.getConsumerMatchers()
118 .getGroupIdentificationConstraints();
119 if (groupIdentificationConstraintsConsumer instanceof GroupRequirementConstraintCase) {
120 List<RequirementMatcher> reqMatchers = ((GroupRequirementConstraintCase) groupIdentificationConstraintsConsumer)
121 .getRequirementMatcher();
122 if (reqMatchers != null) {
123 for (RequirementMatcher reqMatcher : reqMatchers) {
124 if (!MatcherUtils.applyReqMatcher(reqMatcher,
125 match.consumerRelator)) {
132 if (clause.getProviderMatchers() != null) {
133 org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.contract.clause.provider.matchers.GroupIdentificationConstraints groupIdentificationConstraintsProvider = clause
134 .getProviderMatchers().getGroupIdentificationConstraints();
135 if (groupIdentificationConstraintsProvider instanceof GroupCapabilityConstraintCase) {
136 List<CapabilityMatcher> capMatchers = ((GroupCapabilityConstraintCase) groupIdentificationConstraintsProvider)
137 .getCapabilityMatcher();
139 if (capMatchers != null) {
140 for (CapabilityMatcher capMatcher : capMatchers) {
141 if (!MatcherUtils.applyCapMatcher(capMatcher,
142 match.providerRelator)) {
152 private static void addConditionSet(EgKey eg, ConditionSet cs,
153 Map<EgKey, Set<ConditionSet>> egConditions) {
154 if (egConditions == null)
156 Set<ConditionSet> cset = egConditions.get(eg);
158 egConditions.put(eg, cset = new HashSet<>());
163 private static ConditionSet buildConsConditionSet(Clause clause) {
164 if (clause.getConsumerMatchers() != null) {
165 List<ConditionMatcher> condMatchers =
166 clause.getConsumerMatchers().getConditionMatcher();
167 return buildConditionSet(condMatchers);
169 return ConditionSet.EMPTY;
172 private static ConditionSet buildProvConditionSet(Clause clause) {
173 if (clause.getProviderMatchers() != null) {
174 List<ConditionMatcher> condMatchers =
175 clause.getProviderMatchers().getConditionMatcher();
176 return buildConditionSet(condMatchers);
178 return ConditionSet.EMPTY;
181 private static ConditionSet buildConditionSet(List<ConditionMatcher> condMatchers) {
182 if (condMatchers == null)
183 return ConditionSet.EMPTY;
185 ImmutableSet.Builder<ConditionName> allb = ImmutableSet.builder();
186 ImmutableSet.Builder<ConditionName> noneb = ImmutableSet.builder();
187 ImmutableSet.Builder<Set<ConditionName>> anyb =
188 ImmutableSet.builder();
189 for (ConditionMatcher condMatcher : condMatchers) {
190 if (condMatcher.getCondition() == null)
192 MatchType type = condMatcher.getMatchType();
194 type = MatchType.All;
195 if (type.equals(MatchType.Any)) {
196 ImmutableSet.Builder<ConditionName> a =
197 ImmutableSet.builder();
198 for (Condition c : condMatcher.getCondition()) {
203 for (Condition c : condMatcher.getCondition()) {
208 noneb.add(c.getName());
212 allb.add(c.getName());
218 return new ConditionSet(allb.build(), noneb.build(), anyb.build());
221 private static Policy resolvePolicy(Tenant contractTenant,
224 Table<EndpointConstraint, EndpointConstraint,
225 List<Subject>> subjectMap) {
226 Table<EndpointConstraint, EndpointConstraint, List<RuleGroup>> ruleMap =
227 HashBasedTable.create();
229 ruleMap.putAll(merge.getRuleMap());
231 for (Cell<EndpointConstraint, EndpointConstraint, List<Subject>> entry : subjectMap.cellSet()) {
232 List<RuleGroup> rules = new ArrayList<>();
233 EndpointConstraint consEpConstraint = entry.getRowKey();
234 EndpointConstraint provEpConstraint = entry.getColumnKey();
235 List<RuleGroup> oldrules = ruleMap.get(consEpConstraint, provEpConstraint);
236 if (oldrules != null) {
237 rules.addAll(oldrules);
239 for (Subject s : entry.getValue()) {
240 if (s.getRule() == null)
243 RuleGroup rg = new RuleGroup(s.getRule(), s.getOrder(),
244 contractTenant, contract,
248 Collections.sort(rules);
249 ruleMap.put(consEpConstraint, provEpConstraint, Collections.unmodifiableList(rules));
251 return new Policy(ruleMap);