Fix copyright header
[groupbasedpolicy.git] / groupbasedpolicy / src / main / java / org / opendaylight / groupbasedpolicy / resolver / TenantUtils.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.resolver;
10
11 import java.io.Serializable;
12 import java.util.Comparator;
13
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;
35
36 import com.google.common.collect.ComparisonChain;
37 import com.google.common.collect.Ordering;
38
39 /**
40  * Static methods for manipulating group-based policy tenants
41  * @author readams
42  */
43 public class TenantUtils {
44     /**
45      * A comparator that assigns the natural ordering for rules, null-aware
46      * @author readams
47      */
48     public static class RuleComparator 
49             implements Comparator<Rule>, Serializable {
50         private static final long serialVersionUID = -994507116060864552L;
51
52         @Override
53         public int compare(Rule o1, Rule o2) {
54             return ComparisonChain.start()
55                 .compare(o1.getOrder(), o2.getOrder(), 
56                          Ordering.natural().nullsLast())
57                 .result();
58         }
59         
60     }
61
62     /**
63      * An instance of RuleComparator
64      */
65     public static final RuleComparator RULE_COMPARATOR = new RuleComparator();
66
67     /**
68      * Generate an {@link InstanceIdentifier} for an {@link Tenant}
69      * @param tenantKey a tenant key
70      * @return the {@link InstanceIdentifier}
71      */
72     public static InstanceIdentifier<Tenant> tenantIid(TenantKey tenantKey) {
73         return InstanceIdentifier.builder(Tenants.class)
74                 .child(Tenant.class, tenantKey)
75                 .build();
76     }
77
78     /**
79      * Generate an {@link InstanceIdentifier} for an {@link Tenant}
80      * @param tenantId a tenant id
81      * @return the {@link InstanceIdentifier}
82      */
83     public static InstanceIdentifier<Tenant> tenantIid(TenantId tenantId) {
84         return tenantIid(new TenantKey(tenantId));
85     }
86     
87     /**
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
92      */
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())) {
98                     return c;
99                 }
100             }
101         }
102         return null;
103     }
104
105     /**
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
110      */
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())) {
116                     return c;
117                 }
118             }
119         }
120         return null;
121     }
122
123     /**
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
128      */
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())) {
134                     return c;
135                 }
136             }
137         }
138         return null;
139     }
140
141     /**
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
146      */
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())) {
152                     return t;
153                 }
154             }
155         }
156         return null;
157     }
158     
159     /**
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
164      */
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())) {
170                     return eg;
171                 }
172             }
173         }
174         return null;
175     }
176     
177     /**
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
182      */
183     public static ConsumerNamedSelector findCns(EndpointGroup eg,
184                                                 SelectorName name) {
185         if (eg.getConsumerNamedSelector() != null) {
186             for (ConsumerNamedSelector s : eg.getConsumerNamedSelector()) {
187                 if (name.equals(s.getName()))
188                     return s;
189             }
190         }
191         return null;
192     }
193     
194     /**
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
199      */
200     public static ConsumerTargetSelector findCts(EndpointGroup eg,
201                                                  SelectorName name) {
202         if (eg.getConsumerTargetSelector() != null) {
203             for (ConsumerTargetSelector s : eg.getConsumerTargetSelector()) {
204                 if (name.equals(s.getName()))
205                     return s;
206             }
207         }
208         return null;
209     }
210
211     /**
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
216      */
217     public static ProviderNamedSelector findPns(EndpointGroup eg,
218                                                 SelectorName name) {
219         if (eg.getProviderNamedSelector() != null) {
220             for (ProviderNamedSelector s : eg.getProviderNamedSelector()) {
221                 if (name.equals(s.getName()))
222                     return s;
223             }
224         }
225         return null;
226     }
227     
228     /**
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
233      */
234     public static ProviderTargetSelector findPts(EndpointGroup eg,
235                                                  SelectorName name) {
236         if (eg.getProviderTargetSelector() != null) {
237             for (ProviderTargetSelector s : eg.getProviderTargetSelector()) {
238                 if (name.equals(s.getName()))
239                     return s;
240             }
241         }
242         return null;
243     }    
244 }