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.util;
11 import java.util.ArrayList;
12 import java.util.Collection;
13 import java.util.HashSet;
14 import java.util.List;
17 import javax.annotation.concurrent.Immutable;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.CapabilityName;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ConditionName;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.LabelName;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.QualityName;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.RelatorName;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.RequirementName;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.SelectorName;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TargetName;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.ConsumerSelectionRelator;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.Label;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.Matcher.MatchType;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.ProviderSelectionRelator;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.Relator;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.capabilities.Capability;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.conditions.Condition;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.qualities.Quality;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.requirements.Requirement;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.target.selector.QualityMatcher;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.target.selector.quality.matcher.MatcherQuality;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.Target;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.clause.consumer.matchers.group.identification.constraints.group.requirement.constraint._case.RequirementMatcher;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.clause.consumer.matchers.group.identification.constraints.group.requirement.constraint._case.requirement.matcher.MatcherRequirement;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.clause.provider.matchers.group.identification.constraints.group.capability.constraint._case.CapabilityMatcher;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.clause.provider.matchers.group.identification.constraints.group.capability.constraint._case.capability.matcher.MatcherCapability;
45 * Utilities related to matchers and labels
47 public class MatcherUtils {
49 private MatcherUtils() {
50 throw new UnsupportedOperationException("Cannot create an instance");
54 * Apply a quality matcher to a normalized target
57 * the {@link QualityMatcher} to apply
59 * the {@link Target} to match against
60 * @return <code>true</code> if the matcher matches the target
62 public static boolean applyQualityMatcher(QualityMatcher matcher,
64 List<MatcherLabel<QualityName, TargetName>> mls = new ArrayList<>();
65 if (matcher.getMatcherQuality() != null) {
66 for (MatcherQuality ml : matcher.getMatcherQuality()) {
67 mls.add(new MatcherLabel<>(ml.getName(),
68 ml.getTargetNamespace()));
71 Set<QualityName> toMatch = new HashSet<>();
72 for (Quality q : target.getQuality()) {
73 toMatch.add(q.getName());
75 return applyLabelMatcher(mls, matcher.getMatchType(),
76 toMatch, target.getName());
80 * Apply a requirement matcher to a normalized consumer selection relator
83 * the {@link RequirementMatcher} to apply
85 * the {@link ConsumerSelectionRelator} to match against
86 * @return <code>true</code> if the matcher matches the target
88 public static boolean applyReqMatcher(RequirementMatcher matcher,
89 ConsumerSelectionRelator relator) {
90 List<MatcherLabel<RequirementName, SelectorName>> mls = new ArrayList<>();
91 if (matcher.getMatcherRequirement() != null) {
92 for (MatcherRequirement ml : matcher.getMatcherRequirement()) {
93 mls.add(new MatcherLabel<>(ml.getName(),
94 ml.getSelectorNamespace()));
97 Set<RequirementName> toMatch = new HashSet<>();
98 for (Requirement q : relator.getRequirement()) {
99 toMatch.add(q.getName());
101 return applyLabelMatcher(mls, matcher.getMatchType(),
102 toMatch, relator.getName());
106 * Apply a capability matcher to a normalized provider selection relator
109 * the {@link RequirementMatcher} to apply
111 * the {@link ProviderSelectionRelator} to match against
112 * @return <code>true</code> if the matcher matches the target
114 public static boolean applyCapMatcher(CapabilityMatcher matcher,
115 ProviderSelectionRelator relator) {
116 List<MatcherLabel<CapabilityName, SelectorName>> mls = new ArrayList<>();
117 if (matcher.getMatcherCapability() != null) {
118 for (MatcherCapability ml : matcher.getMatcherCapability()) {
119 mls.add(new MatcherLabel<>(ml.getName(),
120 ml.getSelectorNamespace()));
123 Set<CapabilityName> toMatch = new HashSet<>();
124 for (Capability q : relator.getCapability()) {
125 toMatch.add(q.getName());
127 return applyLabelMatcher(mls, matcher.getMatchType(),
128 toMatch, relator.getName());
132 * Functional interface used for generic label methods
137 * The specific label type
139 * the related label name type
141 public interface GetLabelName<L extends Label, LN extends LabelName> {
143 * Get the appropriate typed name for the given label
149 public LN getName(L label);
153 * A {@link GetLabelName} for qualities
155 public static final GetLabelName<Quality, QualityName> getQualityName =
156 new GetLabelName<Quality, QualityName>() {
158 public QualityName getName(Quality label) {
159 return label.getName();
164 * A {@link GetLabelName} for matcher qualities
166 public static final GetLabelName<MatcherQuality, QualityName> getMatcherQualityName =
167 new GetLabelName<MatcherQuality, QualityName>() {
169 public QualityName getName(MatcherQuality label) {
170 return label.getName();
175 * A {@link GetLabelName} for requirements
177 public static final GetLabelName<Requirement, RequirementName> getRequirementName =
178 new GetLabelName<Requirement, RequirementName>() {
180 public RequirementName getName(Requirement label) {
181 return label.getName();
186 * A {@link GetLabelName} for matcher requirements
188 public static final GetLabelName<MatcherRequirement, RequirementName> getMatcherRequirementName =
189 new GetLabelName<MatcherRequirement, RequirementName>() {
191 public RequirementName getName(MatcherRequirement label) {
192 return label.getName();
197 * A {@link GetLabelName} for capabilities
199 public static final GetLabelName<Capability, CapabilityName> getCapabilityName =
200 new GetLabelName<Capability, CapabilityName>() {
202 public CapabilityName getName(Capability label) {
203 return label.getName();
208 * A {@link GetLabelName} for matcher capabilities
210 public static final GetLabelName<MatcherCapability, CapabilityName> getMatcherCapabilityName =
211 new GetLabelName<MatcherCapability, CapabilityName>() {
213 public CapabilityName getName(MatcherCapability label) {
214 return label.getName();
219 * A {@link GetLabelName} for capabilities
221 public static final GetLabelName<Condition, ConditionName> getConditionName =
222 new GetLabelName<Condition, ConditionName>() {
224 public ConditionName getName(Condition label) {
225 return label.getName();
230 private static class MatcherLabel<LN extends LabelName, NS extends RelatorName> {
234 public MatcherLabel(LN name, NS namespace) {
237 this.namespace = namespace;
241 private static <LN extends LabelName, L extends Label,
242 NS extends RelatorName, R extends Relator>
243 boolean applyLabelMatcher(Collection<MatcherLabel<LN, NS>> matcherLabels,
249 if (matcherLabels != null) {
250 matchersize = matcherLabels.size();
251 for (MatcherLabel<LN, NS> matcherLabel : matcherLabels) {
252 if (matcherLabel.namespace != null &&
253 !matcherLabel.namespace.equals(matchNamespace))
255 if (!toMatch.contains(matcherLabel.name))
260 if (matchType == null)
261 matchType = MatchType.All;
269 return matches == matchersize;