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.resolver;
11 import java.io.Serializable;
12 import java.util.Comparator;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ClauseName;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ContractId;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.EndpointGroupId;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.SelectorName;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.SubjectName;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TargetName;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.Tenants;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.Tenant;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.TenantKey;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.Contract;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.EndpointGroup;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.contract.Clause;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.contract.Subject;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.contract.Target;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.contract.subject.Rule;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.endpoint.group.ConsumerNamedSelector;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.endpoint.group.ConsumerTargetSelector;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.endpoint.group.ProviderNamedSelector;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.endpoint.group.ProviderTargetSelector;
34 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
36 import com.google.common.collect.ComparisonChain;
37 import com.google.common.collect.Ordering;
40 * Static methods for manipulating group-based policy tenants
43 public class TenantUtils {
45 * A comparator that assigns the natural ordering for rules, null-aware
48 public static class RuleComparator
49 implements Comparator<Rule>, Serializable {
50 private static final long serialVersionUID = -994507116060864552L;
53 public int compare(Rule o1, Rule o2) {
54 return ComparisonChain.start()
55 .compare(o1.getOrder(), o2.getOrder(),
56 Ordering.natural().nullsLast())
63 * An instance of RuleComparator
65 public static final RuleComparator RULE_COMPARATOR = new RuleComparator();
68 * Generate an {@link InstanceIdentifier} for an {@link Tenant}
69 * @param tenantKey a tenant key
70 * @return the {@link InstanceIdentifier}
72 public static InstanceIdentifier<Tenant> tenantIid(TenantKey tenantKey) {
73 return InstanceIdentifier.builder(Tenants.class)
74 .child(Tenant.class, tenantKey)
79 * Generate an {@link InstanceIdentifier} for an {@link Tenant}
80 * @param tenantId a tenant id
81 * @return the {@link InstanceIdentifier}
83 public static InstanceIdentifier<Tenant> tenantIid(TenantId tenantId) {
84 return tenantIid(new TenantKey(tenantId));
88 * Find a contract with a specified ID within a tenant
89 * @param tenant the {@link Tenant} to search
90 * @param contractId the {@link ContractId} to search for
91 * @return the {@link Contract} if it exists, null otherwise
93 public static Contract findContract(Tenant tenant,
94 ContractId contractId) {
95 if (tenant.getContract() != null) {
96 for (Contract c : tenant.getContract()) {
97 if (contractId.equals(c.getId())) {
106 * Find a clause with a specified name within a contract
107 * @param contract the {@link Contract} to search
108 * @param clauseName the {@link ClauseName} to search for
109 * @return the {@link Clause} if it exists, null otherwise
111 public static Clause findClause(Contract contract,
112 ClauseName clauseName) {
113 if (contract.getClause() != null) {
114 for (Clause c : contract.getClause()) {
115 if (clauseName.equals(c.getName())) {
124 * Find a subject with a specified name within a contract
125 * @param contract the {@link Contract} to search
126 * @param subjectName the {@link SubjectName} to search for
127 * @return the {@link Subject} if it exists, null otherwise
129 public static Subject findSubject(Contract contract,
130 SubjectName subjectName) {
131 if (contract.getSubject() != null) {
132 for (Subject c : contract.getSubject()) {
133 if (subjectName.equals(c.getName())) {
142 * Find a target with a specified name within a contract
143 * @param contract the {@link Contract} to search
144 * @param targetName the {@link TargetName} to search for
145 * @return the {@link Target} if it exists, null otherwise
147 public static Target findTarget(Contract contract,
148 TargetName targetName) {
149 if (contract.getTarget() != null) {
150 for (Target t : contract.getTarget()) {
151 if (targetName.equals(t.getName())) {
160 * Find an endpoint group with a specified ID within a tenant
161 * @param tenant the {@link Tenant} to search
162 * @param egId the {@link EndpointGroupId} to search for
163 * @return the {@link EndpointGroup} if it exists, null otherwise
165 public static EndpointGroup findEndpointGroup(Tenant tenant,
166 EndpointGroupId egId) {
167 if (tenant.getEndpointGroup() != null) {
168 for (EndpointGroup eg : tenant.getEndpointGroup()) {
169 if (egId.equals(eg.getId())) {
178 * Find a consumer named selector in an endpoint group
179 * @param eg the {@link EndpointGroup} to search
180 * @param name the {@link SelectorName} to search for
181 * @return the {@link ConsumerNamedSelector} if it exists, null otherwise
183 public static ConsumerNamedSelector findCns(EndpointGroup eg,
185 if (eg.getConsumerNamedSelector() != null) {
186 for (ConsumerNamedSelector s : eg.getConsumerNamedSelector()) {
187 if (name.equals(s.getName()))
195 * Find a consumer target selector in an endpoint group
196 * @param eg the {@link EndpointGroup} to search
197 * @param name the {@link SelectorName} to search for
198 * @return the {@link ConsumerNamedSelector} if it exists, null otherwise
200 public static ConsumerTargetSelector findCts(EndpointGroup eg,
202 if (eg.getConsumerTargetSelector() != null) {
203 for (ConsumerTargetSelector s : eg.getConsumerTargetSelector()) {
204 if (name.equals(s.getName()))
212 * Find a provider named selector in an endpoint group
213 * @param eg the {@link EndpointGroup} to search
214 * @param name the {@link SelectorName} to search for
215 * @return the {@link ProviderNamedSelector} if it exists, null otherwise
217 public static ProviderNamedSelector findPns(EndpointGroup eg,
219 if (eg.getProviderNamedSelector() != null) {
220 for (ProviderNamedSelector s : eg.getProviderNamedSelector()) {
221 if (name.equals(s.getName()))
229 * Find a provider target selector in an endpoint group
230 * @param eg the {@link EndpointGroup} to search
231 * @param name the {@link SelectorName} to search for
232 * @return the {@link ProviderNamedSelector} if it exists, null otherwise
234 public static ProviderTargetSelector findPts(EndpointGroup eg,
236 if (eg.getProviderTargetSelector() != null) {
237 for (ProviderTargetSelector s : eg.getProviderTargetSelector()) {
238 if (name.equals(s.getName()))