Security group in VPP renderer
[groupbasedpolicy.git] / groupbasedpolicy / src / main / java / org / opendaylight / groupbasedpolicy / util / MatcherUtils.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.util;
10
11 import java.util.ArrayList;
12 import java.util.Collection;
13 import java.util.HashSet;
14 import java.util.List;
15 import java.util.Set;
16
17 import javax.annotation.concurrent.Immutable;
18
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;
43
44 /**
45  * Utilities related to matchers and labels
46  */
47 public class MatcherUtils {
48
49     private MatcherUtils() {
50         throw new UnsupportedOperationException("Cannot create an instance");
51     }
52
53     /**
54      * Apply a quality matcher to a normalized target
55      *
56      * @param matcher
57      *            the {@link QualityMatcher} to apply
58      * @param target
59      *            the {@link Target} to match against
60      * @return <code>true</code> if the matcher matches the target
61      */
62     public static boolean applyQualityMatcher(QualityMatcher matcher,
63             Target target) {
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()));
69             }
70         }
71         Set<QualityName> toMatch = new HashSet<>();
72         for (Quality q : target.getQuality()) {
73             toMatch.add(q.getName());
74         }
75         return applyLabelMatcher(mls, matcher.getMatchType(),
76                 toMatch, target.getName());
77     }
78
79     /**
80      * Apply a requirement matcher to a normalized consumer selection relator
81      *
82      * @param matcher
83      *            the {@link RequirementMatcher} to apply
84      * @param relator
85      *            the {@link ConsumerSelectionRelator} to match against
86      * @return <code>true</code> if the matcher matches the target
87      */
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()));
95             }
96         }
97         Set<RequirementName> toMatch = new HashSet<>();
98         for (Requirement q : relator.getRequirement()) {
99             toMatch.add(q.getName());
100         }
101         return applyLabelMatcher(mls, matcher.getMatchType(),
102                 toMatch, relator.getName());
103     }
104
105     /**
106      * Apply a capability matcher to a normalized provider selection relator
107      *
108      * @param matcher
109      *            the {@link RequirementMatcher} to apply
110      * @param relator
111      *            the {@link ProviderSelectionRelator} to match against
112      * @return <code>true</code> if the matcher matches the target
113      */
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()));
121             }
122         }
123         Set<CapabilityName> toMatch = new HashSet<>();
124         for (Capability q : relator.getCapability()) {
125             toMatch.add(q.getName());
126         }
127         return applyLabelMatcher(mls, matcher.getMatchType(),
128                 toMatch, relator.getName());
129     }
130
131     /**
132      * Functional interface used for generic label methods
133      *
134      * @author readams
135      *
136      * @param <L>
137      *            The specific label type
138      * @param <LN>
139      *            the related label name type
140      */
141     public interface GetLabelName<L extends Label, LN extends LabelName> {
142         /**
143          * Get the appropriate typed name for the given label
144          *
145          * @param label
146          *            the label
147          * @return the name
148          */
149         public LN getName(L label);
150     }
151
152     /**
153      * A {@link GetLabelName} for qualities
154      */
155     public static final GetLabelName<Quality, QualityName> getQualityName =
156             new GetLabelName<Quality, QualityName>() {
157                 @Override
158                 public QualityName getName(Quality label) {
159                     return label.getName();
160                 }
161             };
162
163     /**
164      * A {@link GetLabelName} for matcher qualities
165      */
166     public static final GetLabelName<MatcherQuality, QualityName> getMatcherQualityName =
167             new GetLabelName<MatcherQuality, QualityName>() {
168                 @Override
169                 public QualityName getName(MatcherQuality label) {
170                     return label.getName();
171                 }
172             };
173
174     /**
175      * A {@link GetLabelName} for requirements
176      */
177     public static final GetLabelName<Requirement, RequirementName> getRequirementName =
178             new GetLabelName<Requirement, RequirementName>() {
179                 @Override
180                 public RequirementName getName(Requirement label) {
181                     return label.getName();
182                 }
183             };
184
185     /**
186      * A {@link GetLabelName} for matcher requirements
187      */
188     public static final GetLabelName<MatcherRequirement, RequirementName> getMatcherRequirementName =
189             new GetLabelName<MatcherRequirement, RequirementName>() {
190                 @Override
191                 public RequirementName getName(MatcherRequirement label) {
192                     return label.getName();
193                 }
194             };
195
196     /**
197      * A {@link GetLabelName} for capabilities
198      */
199     public static final GetLabelName<Capability, CapabilityName> getCapabilityName =
200             new GetLabelName<Capability, CapabilityName>() {
201                 @Override
202                 public CapabilityName getName(Capability label) {
203                     return label.getName();
204                 }
205             };
206
207     /**
208      * A {@link GetLabelName} for matcher capabilities
209      */
210     public static final GetLabelName<MatcherCapability, CapabilityName> getMatcherCapabilityName =
211             new GetLabelName<MatcherCapability, CapabilityName>() {
212                 @Override
213                 public CapabilityName getName(MatcherCapability label) {
214                     return label.getName();
215                 }
216             };
217
218     /**
219      * A {@link GetLabelName} for capabilities
220      */
221     public static final GetLabelName<Condition, ConditionName> getConditionName =
222             new GetLabelName<Condition, ConditionName>() {
223                 @Override
224                 public ConditionName getName(Condition label) {
225                     return label.getName();
226                 }
227             };
228
229     @Immutable
230     private static class MatcherLabel<LN extends LabelName, NS extends RelatorName> {
231         final LN name;
232         final NS namespace;
233
234         public MatcherLabel(LN name, NS namespace) {
235             super();
236             this.name = name;
237             this.namespace = namespace;
238         }
239     }
240
241     private static <LN extends LabelName, L extends Label,
242             NS extends RelatorName, R extends Relator>
243             boolean applyLabelMatcher(Collection<MatcherLabel<LN, NS>> matcherLabels,
244                     MatchType matchType,
245                     Set<LN> toMatch,
246                     NS matchNamespace) {
247         int matches = 0;
248         int matchersize = 0;
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))
254                     continue;
255                 if (!toMatch.contains(matcherLabel.name))
256                     continue;
257                 matches += 1;
258             }
259         }
260         if (matchType == null)
261             matchType = MatchType.All;
262         switch (matchType) {
263         case Any:
264             return matches > 0;
265         case None:
266             return matches == 0;
267         case All:
268         default:
269             return matches == matchersize;
270         }
271     }
272 }