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.Collections;
14 import java.util.HashMap;
15 import java.util.HashSet;
16 import java.util.List;
18 import org.opendaylight.groupbasedpolicy.util.MatcherUtils.GetLabelName;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.CapabilityMatcherName;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.CapabilityName;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ClauseName;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ConditionMatcherName;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ConditionName;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ContractId;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.EndpointGroupId;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.LabelName;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.NetworkDomainId;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.QualityMatcherName;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.QualityName;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.RequirementMatcherName;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.RequirementName;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.SelectorName;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.SubjectName;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TargetName;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.Label;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.capabilities.Capability;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.condition.matchers.ConditionMatcher;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.condition.matchers.ConditionMatcherBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.conditions.Condition;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.qualities.Quality;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.requirements.Requirement;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.target.selector.QualityMatcher;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.target.selector.QualityMatcherBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.target.selector.quality.matcher.MatcherQuality;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.Tenant;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.TenantBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.Policy;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.PolicyBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.Contract;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.ContractBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.EndpointGroup;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.EndpointGroup.IntraGroupPolicy;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.EndpointGroupBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.Clause;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.ClauseBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.Subject;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.SubjectBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.Target;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.TargetBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.clause.ConsumerMatchers;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.clause.ConsumerMatchersBuilder;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.clause.ProviderMatchers;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.clause.ProviderMatchersBuilder;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.clause.consumer.matchers.GroupIdentificationConstraints;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.clause.consumer.matchers.group.identification.constraints.GroupRequirementConstraintCase;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.clause.consumer.matchers.group.identification.constraints.GroupRequirementConstraintCaseBuilder;
67 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;
68 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.RequirementMatcherBuilder;
69 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;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.clause.provider.matchers.group.identification.constraints.GroupCapabilityConstraintCase;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.clause.provider.matchers.group.identification.constraints.GroupCapabilityConstraintCaseBuilder;
72 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;
73 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.CapabilityMatcherBuilder;
74 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;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.subject.Rule;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.subject.RuleBuilder;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.endpoint.group.ConsumerNamedSelector;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.endpoint.group.ConsumerNamedSelectorBuilder;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.endpoint.group.ConsumerTargetSelector;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.endpoint.group.ConsumerTargetSelectorBuilder;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.endpoint.group.ProviderNamedSelector;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.endpoint.group.ProviderNamedSelectorBuilder;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.endpoint.group.ProviderTargetSelector;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.endpoint.group.ProviderTargetSelectorBuilder;
86 import com.google.common.base.Preconditions;
87 import com.google.common.collect.ImmutableList;
90 * Utilities useful for resolving the inheritance rules for the various objects
96 public class InheritanceUtils {
98 private InheritanceUtils() {
99 throw new UnsupportedOperationException("Cannot create an instance");
103 * Fully resolve the specified {@link Tenant}, returning a tenant with all
104 * items fully normalized. This means that no items will have parent/child
105 * relationships and can be interpreted simply without regard to inheritance
108 * @param unresolvedTenant
109 * the {@link Tenant} unresolved tenant to resolve
110 * @return the fully-resolved {@link Tenant}
111 * @throws NullPointerException if parameter {@code unresolvedTenant} is {@code null}
113 public static Tenant resolveTenant(Tenant unresolvedTenant) {
114 Preconditions.checkNotNull(unresolvedTenant);
115 HashMap<EndpointGroupId, EndpointGroup> resolvedEgs = new HashMap<>();
116 HashMap<ContractId, Contract> resolvedContracts = new HashMap<>();
117 TenantBuilder resolvedTenantBuilder = new TenantBuilder().setId(unresolvedTenant.getId())
118 .setName(unresolvedTenant.getName())
119 .setDescription(unresolvedTenant.getDescription());
121 Policy unresolvedPolicy = unresolvedTenant.getPolicy();
122 if (unresolvedPolicy != null) {
123 if (unresolvedPolicy.getEndpointGroup() != null) {
124 for (EndpointGroup eg : unresolvedPolicy.getEndpointGroup()) {
125 resolveEndpointGroup(unresolvedTenant, eg, resolvedEgs);
128 if (unresolvedPolicy.getContract() != null) {
129 for (Contract c : unresolvedPolicy.getContract()) {
130 resolveContract(unresolvedTenant, c, resolvedContracts);
133 resolvedTenantBuilder
134 .setPolicy(new PolicyBuilder().setEndpointGroup(ImmutableList.copyOf(resolvedEgs.values()))
135 .setContract(ImmutableList.copyOf(resolvedContracts.values()))
136 .setContractRef(unresolvedPolicy.getContractRef())
137 .setExternalImplicitGroup(unresolvedPolicy.getExternalImplicitGroup())
138 .setSubjectFeatureInstances(unresolvedPolicy.getSubjectFeatureInstances())
142 // XXX TODO - inherit from common tenant
144 resolvedTenantBuilder.setForwardingContext(unresolvedTenant.getForwardingContext());
145 return resolvedTenantBuilder.build();
152 private static void resolveEndpointGroup(Tenant unresolvedTenant,
153 EndpointGroup unresolvedEg,
154 HashMap<EndpointGroupId,
155 EndpointGroup> resolvedEgs) {
156 // put the unresolved object into the data structure to avoid loops
157 resolvedEgs.put(unresolvedEg.getId(), unresolvedEg);
159 // resolve parent if it hasn't been resolved already
160 EndpointGroup parent = null;
161 if (unresolvedEg.getParent() != null) {
162 if (!resolvedEgs.containsKey(unresolvedEg.getParent())) {
163 parent = TenantUtils.findEndpointGroup(unresolvedTenant,
164 unresolvedEg.getParent());
166 resolveEndpointGroup(unresolvedTenant, parent, resolvedEgs);
168 parent = resolvedEgs.get(unresolvedEg.getParent());
171 HashMap<SelectorName, ConsumerTargetSelector> resolvedCts =
173 HashMap<SelectorName, ConsumerNamedSelector> resolvedCns =
175 HashMap<SelectorName, ProviderTargetSelector> resolvedPts =
177 HashMap<SelectorName, ProviderNamedSelector> resolvedPns =
179 NetworkDomainId domain = unresolvedEg.getNetworkDomain();
180 IntraGroupPolicy igp = unresolvedEg.getIntraGroupPolicy();
182 if (unresolvedEg.getConsumerTargetSelector() != null) {
183 for (ConsumerTargetSelector s : unresolvedEg.getConsumerTargetSelector()) {
184 resolveCts(unresolvedTenant, unresolvedEg, s, resolvedCts);
187 if (unresolvedEg.getConsumerNamedSelector() != null) {
188 for (ConsumerNamedSelector s : unresolvedEg.getConsumerNamedSelector()) {
189 resolveCns(unresolvedTenant, unresolvedEg, s, resolvedCns);
192 if (unresolvedEg.getProviderTargetSelector() != null) {
193 for (ProviderTargetSelector s : unresolvedEg.getProviderTargetSelector()) {
194 resolvePts(unresolvedTenant, unresolvedEg, s, resolvedPts);
197 if (unresolvedEg.getProviderNamedSelector() != null) {
198 for (ProviderNamedSelector s : unresolvedEg.getProviderNamedSelector()) {
199 resolvePns(unresolvedTenant, unresolvedEg, s, resolvedPns);
203 if (parent != null) {
204 if (parent.getConsumerTargetSelector() != null) {
205 for (ConsumerTargetSelector cts : parent.getConsumerTargetSelector()) {
206 if (!resolvedCts.containsKey(cts.getName()))
207 resolvedCts.put(cts.getName(), cts);
210 if (parent.getConsumerNamedSelector() != null) {
211 for (ConsumerNamedSelector cns : parent.getConsumerNamedSelector()) {
212 if (!resolvedCns.containsKey(cns.getName()))
213 resolvedCns.put(cns.getName(), cns);
216 if (parent.getProviderTargetSelector() != null) {
217 for (ProviderTargetSelector pts : parent.getProviderTargetSelector()) {
218 if (!resolvedPts.containsKey(pts.getName()))
219 resolvedPts.put(pts.getName(), pts);
222 if (parent.getProviderNamedSelector() != null) {
223 for (ProviderNamedSelector pns : parent.getProviderNamedSelector()) {
224 if (!resolvedPns.containsKey(pns.getName()))
225 resolvedPns.put(pns.getName(), pns);
228 if (domain == null) {
229 domain = parent.getNetworkDomain();
232 igp = parent.getIntraGroupPolicy();
236 // Note: do not set parent, or any of the values that only exist
238 EndpointGroup resolvedEg = new EndpointGroupBuilder()
239 .setId(unresolvedEg.getId())
240 .setDescription(unresolvedEg.getDescription())
241 .setName(unresolvedEg.getName())
242 .setConsumerTargetSelector(ImmutableList.copyOf(resolvedCts.values()))
243 .setConsumerNamedSelector(ImmutableList.copyOf(resolvedCns.values()))
244 .setProviderTargetSelector(ImmutableList.copyOf(resolvedPts.values()))
245 .setProviderNamedSelector(ImmutableList.copyOf(resolvedPns.values()))
246 .setNetworkDomain(domain)
247 .setIntraGroupPolicy(igp)
249 resolvedEgs.put(resolvedEg.getId(), resolvedEg);
252 private static void resolveCts(Tenant unresolvedTenant,
253 EndpointGroup unresolvedEg,
254 ConsumerTargetSelector unresolvedTs,
255 HashMap<SelectorName,
256 ConsumerTargetSelector> resolvedCts) {
257 HashMap<QualityMatcherName, QualityMatcher> matchers = new HashMap<>();
258 HashMap<RequirementName, Requirement> requirements = new HashMap<>();
259 HashSet<EndpointGroupId> visited = new HashSet<>();
261 resolveCtsAttr(unresolvedTenant, unresolvedEg, unresolvedTs.getName(),
262 matchers, requirements, visited);
264 ConsumerTargetSelector resolved = new ConsumerTargetSelectorBuilder()
265 .setName(unresolvedTs.getName())
266 .setQualityMatcher(ImmutableList.copyOf(matchers.values()))
267 .setRequirement(ImmutableList.copyOf(requirements.values()))
269 resolvedCts.put(resolved.getName(), resolved);
272 private static void resolveCtsAttr(Tenant unresolvedTenant,
273 EndpointGroup unresolvedEg,
275 HashMap<QualityMatcherName,
276 QualityMatcher> matchers,
277 HashMap<RequirementName,
278 Requirement> requirements,
279 HashSet<EndpointGroupId> visited) {
280 if (unresolvedEg == null)
282 if (visited.contains(unresolvedEg.getId()))
284 visited.add(unresolvedEg.getId());
285 if (unresolvedEg.getParent() != null) {
286 resolveCtsAttr(unresolvedTenant,
287 TenantUtils.findEndpointGroup(unresolvedTenant,
288 unresolvedEg.getParent()),
294 resolveLabels(unresolvedEg.getRequirement(), requirements,
295 MatcherUtils.getRequirementName);
296 ConsumerTargetSelector unresolvedSelector =
297 TenantUtils.findCts(unresolvedEg, name);
298 if (unresolvedSelector == null)
300 resolveLabels(unresolvedSelector.getRequirement(), requirements,
301 MatcherUtils.getRequirementName);
302 resolveQualityMatcher(unresolvedSelector.getQualityMatcher(), matchers);
305 private static void resolveCns(Tenant unresolvedTenant,
306 EndpointGroup unresolvedEg,
307 ConsumerNamedSelector unresolvedTs,
308 HashMap<SelectorName,
309 ConsumerNamedSelector> resolvedCns) {
310 HashMap<RequirementName, Requirement> requirements = new HashMap<>();
311 HashSet<ContractId> contracts = new HashSet<>();
312 HashSet<EndpointGroupId> visited = new HashSet<>();
314 resolveCnsAttr(unresolvedTenant, unresolvedEg, unresolvedTs.getName(),
315 requirements, contracts, visited);
317 ConsumerNamedSelector resolved = new ConsumerNamedSelectorBuilder()
318 .setName(unresolvedTs.getName())
319 .setRequirement(ImmutableList.copyOf(requirements.values()))
320 .setContract(ImmutableList.copyOf(contracts))
322 resolvedCns.put(resolved.getName(), resolved);
325 private static void resolveCnsAttr(Tenant unresolvedTenant,
326 EndpointGroup unresolvedEg,
328 HashMap<RequirementName,
329 Requirement> requirements,
330 HashSet<ContractId> contracts,
331 HashSet<EndpointGroupId> visited) {
332 if (unresolvedEg == null)
334 if (visited.contains(unresolvedEg.getId()))
336 visited.add(unresolvedEg.getId());
337 if (unresolvedEg.getParent() != null) {
338 resolveCnsAttr(unresolvedTenant,
339 TenantUtils.findEndpointGroup(unresolvedTenant,
340 unresolvedEg.getParent()),
341 name, requirements, contracts, visited);
343 resolveLabels(unresolvedEg.getRequirement(), requirements,
344 MatcherUtils.getRequirementName);
345 ConsumerNamedSelector unresolvedSelector =
346 TenantUtils.findCns(unresolvedEg, name);
347 if (unresolvedSelector == null)
349 resolveLabels(unresolvedSelector.getRequirement(), requirements,
350 MatcherUtils.getRequirementName);
351 if (unresolvedSelector.getContract() != null) {
352 contracts.addAll(unresolvedSelector.getContract());
356 private static void resolvePts(Tenant unresolvedTenant,
357 EndpointGroup unresolvedEg,
358 ProviderTargetSelector unresolvedTs,
359 HashMap<SelectorName,
360 ProviderTargetSelector> resolvedCts) {
361 HashMap<QualityMatcherName, QualityMatcher> matchers = new HashMap<>();
362 HashMap<CapabilityName, Capability> capabilities = new HashMap<>();
363 HashSet<EndpointGroupId> visited = new HashSet<>();
365 resolvePtsAttr(unresolvedTenant, unresolvedEg, unresolvedTs.getName(),
366 matchers, capabilities, visited);
368 ProviderTargetSelector resolved = new ProviderTargetSelectorBuilder()
369 .setName(unresolvedTs.getName())
370 .setQualityMatcher(ImmutableList.copyOf(matchers.values()))
371 .setCapability(ImmutableList.copyOf(capabilities.values()))
373 resolvedCts.put(resolved.getName(), resolved);
376 private static void resolvePtsAttr(Tenant unresolvedTenant,
377 EndpointGroup unresolvedEg,
379 HashMap<QualityMatcherName,
380 QualityMatcher> matchers,
381 HashMap<CapabilityName,
382 Capability> capabilities,
383 HashSet<EndpointGroupId> visited) {
384 if (unresolvedEg == null)
386 if (visited.contains(unresolvedEg.getId()))
388 visited.add(unresolvedEg.getId());
389 if (unresolvedEg.getParent() != null) {
390 resolvePtsAttr(unresolvedTenant,
391 TenantUtils.findEndpointGroup(unresolvedTenant,
392 unresolvedEg.getParent()),
395 capabilities, visited);
397 resolveLabels(unresolvedEg.getCapability(), capabilities,
398 MatcherUtils.getCapabilityName);
399 ProviderTargetSelector unresolvedSelector =
400 TenantUtils.findPts(unresolvedEg, name);
401 if (unresolvedSelector == null)
403 resolveLabels(unresolvedSelector.getCapability(), capabilities,
404 MatcherUtils.getCapabilityName);
405 resolveQualityMatcher(unresolvedSelector.getQualityMatcher(), matchers);
408 private static void resolvePns(Tenant unresolvedTenant,
409 EndpointGroup unresolvedEg,
410 ProviderNamedSelector unresolvedTs,
411 HashMap<SelectorName,
412 ProviderNamedSelector> resolvedCns) {
413 HashMap<CapabilityName, Capability> capabilities = new HashMap<>();
414 HashSet<ContractId> contracts = new HashSet<>();
415 HashSet<EndpointGroupId> visited = new HashSet<>();
417 resolvePnsAttr(unresolvedTenant, unresolvedEg, unresolvedTs.getName(),
418 capabilities, contracts, visited);
420 ProviderNamedSelector resolved = new ProviderNamedSelectorBuilder()
421 .setName(unresolvedTs.getName())
422 .setCapability(ImmutableList.copyOf(capabilities.values()))
423 .setContract(ImmutableList.copyOf(contracts))
425 resolvedCns.put(resolved.getName(), resolved);
428 private static void resolvePnsAttr(Tenant unresolvedTenant,
429 EndpointGroup unresolvedEg,
431 HashMap<CapabilityName,
432 Capability> capabilities,
433 HashSet<ContractId> contracts,
434 HashSet<EndpointGroupId> visited) {
435 if (unresolvedEg == null)
437 if (visited.contains(unresolvedEg.getId()))
439 visited.add(unresolvedEg.getId());
440 if (unresolvedEg.getParent() != null) {
441 resolvePnsAttr(unresolvedTenant,
442 TenantUtils.findEndpointGroup(unresolvedTenant,
443 unresolvedEg.getParent()),
444 name, capabilities, contracts, visited);
446 resolveLabels(unresolvedEg.getCapability(), capabilities,
447 MatcherUtils.getCapabilityName);
448 ProviderNamedSelector unresolvedSelector =
449 TenantUtils.findPns(unresolvedEg, name);
450 if (unresolvedSelector == null)
452 resolveLabels(unresolvedSelector.getCapability(), capabilities,
453 MatcherUtils.getCapabilityName);
454 if (unresolvedSelector.getContract() != null) {
455 contracts.addAll(unresolvedSelector.getContract());
459 private static void resolveContract(Tenant unresolvedTenant,
460 Contract unresolvedContract,
462 Contract> resolvedContracts) {
463 // put the unresolved object into the data structure to avoid loops
464 resolvedContracts.put(unresolvedContract.getId(), unresolvedContract);
466 // resolve parent if it hasn't been resolved already
467 Contract parent = null;
468 if (unresolvedContract.getParent() != null) {
469 if (!resolvedContracts.containsKey(unresolvedContract.getParent())) {
470 parent = TenantUtils.findContract(unresolvedTenant,
471 unresolvedContract.getParent());
473 resolveContract(unresolvedTenant,
477 parent = resolvedContracts.get(unresolvedContract.getParent());
480 HashMap<TargetName, Target> resolvedTargets = new HashMap<>();
481 HashMap<ClauseName, Clause> resolvedClauses = new HashMap<>();
482 HashMap<SubjectName, Subject> resolvedSubjects = new HashMap<>();
484 if (unresolvedContract.getTarget() != null) {
485 for (Target t : unresolvedContract.getTarget()) {
486 resolveTarget(unresolvedTenant, unresolvedContract,
490 if (unresolvedContract.getClause() != null) {
491 for (Clause c : unresolvedContract.getClause()) {
492 resolveClause(unresolvedTenant, unresolvedContract,
496 if (unresolvedContract.getSubject() != null) {
497 for (Subject s : unresolvedContract.getSubject()) {
498 resolveSubject(unresolvedTenant, unresolvedContract,
499 s, resolvedSubjects);
503 if (parent != null) {
504 if (parent.getTarget() != null) {
505 for (Target t : parent.getTarget()) {
506 if (!resolvedTargets.containsKey(t.getName()))
507 resolvedTargets.put(t.getName(), t);
510 if (parent.getClause() != null) {
511 for (Clause c : parent.getClause()) {
512 if (!resolvedClauses.containsKey(c.getName()))
513 resolvedClauses.put(c.getName(), c);
516 if (parent.getSubject() != null) {
517 for (Subject s : parent.getSubject()) {
518 if (!resolvedSubjects.containsKey(s.getName()))
519 resolvedSubjects.put(s.getName(), s);
524 Contract resolvedContract = new ContractBuilder()
525 .setId(unresolvedContract.getId())
526 .setDescription(unresolvedContract.getDescription())
527 .setTarget(ImmutableList.copyOf(resolvedTargets.values()))
528 .setClause(ImmutableList.copyOf(resolvedClauses.values()))
529 .setSubject(ImmutableList.copyOf(resolvedSubjects.values()))
531 resolvedContracts.put(resolvedContract.getId(), resolvedContract);
534 private static void resolveTarget(Tenant unresolvedTenant,
535 Contract unresolvedContract,
536 Target unresolvedTarget,
537 HashMap<TargetName, Target> resolvedTargets) {
538 HashMap<QualityName, Quality> qualities = new HashMap<>();
539 HashSet<ContractId> visited = new HashSet<>();
541 resolveTargetAttrs(unresolvedTenant,
543 unresolvedTarget.getName(),
546 Target resolved = new TargetBuilder()
547 .setName(unresolvedTarget.getName())
548 .setQuality(ImmutableList.copyOf(qualities.values()))
550 resolvedTargets.put(resolved.getName(), resolved);
553 private static void resolveTargetAttrs(Tenant unresolvedTenant,
554 Contract unresolvedContract,
555 TargetName targetName,
556 HashMap<QualityName, Quality> qualities,
557 HashSet<ContractId> visited) {
558 if (unresolvedContract == null)
560 if (visited.contains(unresolvedContract.getId()))
562 visited.add(unresolvedContract.getId());
563 if (unresolvedContract.getParent() != null) {
564 resolveTargetAttrs(unresolvedTenant,
565 TenantUtils.findContract(unresolvedTenant,
566 unresolvedContract.getParent()),
570 resolveLabels(unresolvedContract.getQuality(), qualities,
571 MatcherUtils.getQualityName);
572 Target unresolvedTarget =
573 TenantUtils.findTarget(unresolvedContract, targetName);
574 resolveLabels(unresolvedTarget.getQuality(), qualities,
575 MatcherUtils.getQualityName);
579 resolveQualityMatcher(Collection<QualityMatcher> toResolve,
580 HashMap<QualityMatcherName,
581 QualityMatcher> matchers) {
582 if (toResolve == null)
584 for (QualityMatcher qm : toResolve) {
585 if (matchers.containsKey(qm.getName())) {
586 QualityMatcher oqm = matchers.get(qm.getName());
587 QualityMatcherBuilder qmb = new QualityMatcherBuilder();
588 qmb.setName(qm.getName());
589 qmb.setMatchType(oqm.getMatchType());
590 if (qm.getMatchType() != null)
591 qmb.setMatchType(qm.getMatchType());
593 HashMap<QualityName, MatcherQuality> qualities =
595 resolveLabels(oqm.getMatcherQuality(), qualities,
596 MatcherUtils.getMatcherQualityName);
597 resolveLabels(qm.getMatcherQuality(), qualities,
598 MatcherUtils.getMatcherQualityName);
600 qmb.setMatcherQuality(ImmutableList.copyOf(qualities.values()));
601 matchers.put(qm.getName(), qmb.build());
603 matchers.put(qm.getName(), qm);
609 resolveCapabilityMatcher(Collection<CapabilityMatcher> toResolve,
610 HashMap<CapabilityMatcherName,
611 CapabilityMatcher> matchers) {
612 if (toResolve == null)
614 for (CapabilityMatcher m : toResolve) {
615 if (matchers.containsKey(m.getName())) {
616 CapabilityMatcher om = matchers.get(m.getName());
617 CapabilityMatcherBuilder mb = new CapabilityMatcherBuilder();
618 mb.setName(m.getName());
619 mb.setMatchType(om.getMatchType());
620 if (m.getMatchType() != null)
621 mb.setMatchType(m.getMatchType());
623 HashMap<CapabilityName, MatcherCapability> labels =
625 resolveLabels(om.getMatcherCapability(), labels,
626 MatcherUtils.getMatcherCapabilityName);
627 resolveLabels(m.getMatcherCapability(), labels,
628 MatcherUtils.getMatcherCapabilityName);
630 mb.setMatcherCapability(ImmutableList.copyOf(labels.values()));
631 matchers.put(m.getName(), mb.build());
633 matchers.put(m.getName(), m);
639 resolveRequirementMatcher(Collection<RequirementMatcher> toResolve,
640 HashMap<RequirementMatcherName,
641 RequirementMatcher> matchers) {
642 if (toResolve == null)
644 for (RequirementMatcher m : toResolve) {
645 if (matchers.containsKey(m.getName())) {
646 RequirementMatcher om = matchers.get(m.getName());
647 RequirementMatcherBuilder mb = new RequirementMatcherBuilder();
648 mb.setName(m.getName());
649 mb.setMatchType(om.getMatchType());
650 if (m.getMatchType() != null)
651 mb.setMatchType(m.getMatchType());
653 HashMap<RequirementName, MatcherRequirement> labels =
655 resolveLabels(om.getMatcherRequirement(), labels,
656 MatcherUtils.getMatcherRequirementName);
657 resolveLabels(m.getMatcherRequirement(), labels,
658 MatcherUtils.getMatcherRequirementName);
660 mb.setMatcherRequirement(ImmutableList.copyOf(labels.values()));
661 matchers.put(m.getName(), mb.build());
663 matchers.put(m.getName(), m);
669 resolveConditionMatcher(Collection<ConditionMatcher> toResolve,
670 HashMap<ConditionMatcherName,
671 ConditionMatcher> matchers) {
672 if (toResolve == null)
674 for (ConditionMatcher m : toResolve) {
675 if (matchers.containsKey(m.getName())) {
676 ConditionMatcher om = matchers.get(m.getName());
677 ConditionMatcherBuilder mb = new ConditionMatcherBuilder();
678 mb.setName(m.getName());
679 mb.setMatchType(om.getMatchType());
680 if (m.getMatchType() != null)
681 mb.setMatchType(m.getMatchType());
683 HashMap<ConditionName, Condition> labels =
685 resolveLabels(om.getCondition(), labels,
686 MatcherUtils.getConditionName);
687 resolveLabels(m.getCondition(), labels,
688 MatcherUtils.getConditionName);
690 mb.setCondition(ImmutableList.copyOf(labels.values()));
691 matchers.put(m.getName(), mb.build());
693 matchers.put(m.getName(), m);
698 private static void resolveClause(Tenant unresolvedTenant,
699 Contract unresolvedContract,
700 Clause unresolvedClause,
701 HashMap<ClauseName, Clause> resolvedClauses) {
702 HashMap<CapabilityMatcherName, CapabilityMatcher> capMatchers = new HashMap<>();
703 HashMap<ConditionMatcherName, ConditionMatcher> provCondMatchers = new HashMap<>();
704 HashMap<RequirementMatcherName, RequirementMatcher> reqMatchers = new HashMap<>();
705 HashMap<ConditionMatcherName, ConditionMatcher> consCondMatchers = new HashMap<>();
706 HashSet<SubjectName> subjectRefs = new HashSet<>();
707 HashSet<ContractId> visited = new HashSet<>();
709 // TODO: Add GIC choices GroupNameConstraint and GroupAny
710 // TODO: Add Inheritance for EIC (ie L3Prefix) constraint.
712 resolveClauseAttr(unresolvedTenant, unresolvedContract,
713 unresolvedClause.getName(), subjectRefs,
714 capMatchers, provCondMatchers,
715 reqMatchers, consCondMatchers, visited);
717 Clause resolved = new ClauseBuilder()
718 .setName(unresolvedClause.getName())
719 .setSubjectRefs(ImmutableList.copyOf(subjectRefs))
720 .setProviderMatchers(new ProviderMatchersBuilder()
721 .setGroupIdentificationConstraints(new GroupCapabilityConstraintCaseBuilder()
722 .setCapabilityMatcher(ImmutableList.copyOf(capMatchers.values())).build())
723 .setConditionMatcher(ImmutableList.copyOf(provCondMatchers.values()))
724 .setEndpointIdentificationConstraints(
725 unresolvedClause.getProviderMatchers() == null ? null :
726 unresolvedClause.getProviderMatchers().getEndpointIdentificationConstraints())
728 .setConsumerMatchers(new ConsumerMatchersBuilder()
729 .setGroupIdentificationConstraints(new GroupRequirementConstraintCaseBuilder()
730 .setRequirementMatcher(ImmutableList.copyOf(reqMatchers.values())).build())
731 .setConditionMatcher(ImmutableList.copyOf(consCondMatchers.values()))
732 .setEndpointIdentificationConstraints(
733 unresolvedClause.getConsumerMatchers() == null ? null :
734 unresolvedClause.getConsumerMatchers().getEndpointIdentificationConstraints())
737 resolvedClauses.put(resolved.getName(), resolved);
740 private static void resolveClauseAttr(Tenant unresolvedTenant,
741 Contract unresolvedContract,
742 ClauseName clauseName,
743 HashSet<SubjectName> subjectRefs,
744 HashMap<CapabilityMatcherName,
745 CapabilityMatcher> capMatchers,
746 HashMap<ConditionMatcherName,
747 ConditionMatcher> provCondMatchers,
748 HashMap<RequirementMatcherName,
749 RequirementMatcher> reqMatchers,
750 HashMap<ConditionMatcherName,
751 ConditionMatcher> consCondMatchers,
752 HashSet<ContractId> visited) {
753 if (unresolvedContract == null)
755 if (visited.contains(unresolvedContract.getId()))
757 visited.add(unresolvedContract.getId());
758 if (unresolvedContract.getParent() != null) {
759 resolveClauseAttr(unresolvedTenant,
760 TenantUtils.findContract(unresolvedTenant,
761 unresolvedContract.getParent()),
767 consCondMatchers, visited);
770 Clause unresolvedClause =
771 TenantUtils.findClause(unresolvedContract, clauseName);
772 if (unresolvedClause == null)
775 if (unresolvedClause.getProviderMatchers() != null) {
776 ProviderMatchers pms = unresolvedClause.getProviderMatchers();
777 org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.clause.provider.matchers.GroupIdentificationConstraints groupIdentificationConstraintsProvider = pms
778 .getGroupIdentificationConstraints();
779 if (groupIdentificationConstraintsProvider instanceof GroupCapabilityConstraintCase) {
780 resolveCapabilityMatcher(
781 ((GroupCapabilityConstraintCase) groupIdentificationConstraintsProvider).getCapabilityMatcher(),
784 resolveConditionMatcher(pms.getConditionMatcher(), provCondMatchers);
787 if (unresolvedClause.getConsumerMatchers() != null) {
788 ConsumerMatchers cms = unresolvedClause.getConsumerMatchers();
789 GroupIdentificationConstraints groupIdentificiationConstrainsConsumer = cms
790 .getGroupIdentificationConstraints();
791 if (groupIdentificiationConstrainsConsumer instanceof GroupRequirementConstraintCase) {
792 resolveRequirementMatcher(
793 ((GroupRequirementConstraintCase) groupIdentificiationConstrainsConsumer)
794 .getRequirementMatcher(),
797 resolveConditionMatcher(cms.getConditionMatcher(), consCondMatchers);
800 if (unresolvedClause.getSubjectRefs() != null)
801 subjectRefs.addAll(unresolvedClause.getSubjectRefs());
804 private static class Mutable<O> {
808 private static void resolveSubject(Tenant unresolvedTenant,
809 Contract unresolvedContract,
810 Subject unresolvedSubject,
811 HashMap<SubjectName, Subject> resolvedSubjects) {
812 Mutable<Integer> order = new Mutable<>();
813 Mutable<List<Rule>> rules = new Mutable<>();
814 rules.value = Collections.emptyList();
815 HashSet<ContractId> visited = new HashSet<>();
817 resolveSubjectAttr(unresolvedTenant, unresolvedContract,
818 unresolvedSubject.getName(), order, rules, visited);
820 Subject resolved = new SubjectBuilder()
821 .setName(unresolvedSubject.getName())
822 .setOrder(order.value)
823 .setRule(rules.value)
825 resolvedSubjects.put(resolved.getName(), resolved);
828 private static Rule makeRule(Rule r, int order) {
829 return new RuleBuilder()
830 .setName(r.getName())
831 .setActionRef(r.getActionRef())
832 .setClassifierRef(r.getClassifierRef())
837 private static void resolveSubjectAttr(Tenant unresolvedTenant,
838 Contract unresolvedContract,
839 SubjectName subjectName,
840 Mutable<Integer> order,
841 Mutable<List<Rule>> rules,
842 HashSet<ContractId> visited) {
843 if (unresolvedContract == null)
845 if (visited.contains(unresolvedContract.getId()))
847 visited.add(unresolvedContract.getId());
848 if (unresolvedContract.getParent() != null) {
849 resolveSubjectAttr(unresolvedTenant,
850 TenantUtils.findContract(unresolvedTenant,
851 unresolvedContract.getParent()),
857 Subject unresolvedSubject =
858 TenantUtils.findSubject(unresolvedContract, subjectName);
859 if (unresolvedSubject == null)
861 if (unresolvedSubject.getOrder() != null)
862 order.value = unresolvedSubject.getOrder();
863 if (unresolvedSubject.getRule() != null) {
864 ImmutableList.Builder<Rule> rbuilder =
865 new ImmutableList.Builder<Rule>();
866 ArrayList<Rule> nrules =
867 new ArrayList<>(unresolvedSubject.getRule());
868 Collections.sort(nrules, TenantUtils.RULE_COMPARATOR);
870 for (Rule r : nrules) {
871 rbuilder.add(makeRule(r, index++));
873 for (Rule r : rules.value) {
874 rbuilder.add(makeRule(r, index++));
876 rules.value = rbuilder.build();
881 * Given a partially-resolved set of labels, add the next item in the
882 * inheritance ordering to the set of resolved labels.
887 * the partially-resolved set
889 * a function object to get the appropriate typed label name
891 private static <L extends Label, LN extends LabelName> void
892 resolveLabels(Collection<L> toResolve, HashMap<LN, L> labels,
893 GetLabelName<L, LN> getName) {
894 if (toResolve == null)
896 for (L l : toResolve) {
897 if (l.getInclusionRule() != null) {
898 switch (l.getInclusionRule()) {
901 labels.put(getName.getName(l), l);
905 labels.remove(getName.getName(l));
909 // default to Include
910 labels.put(getName.getName(l), l);