Added range type to subject-feature-definition/parameter
[groupbasedpolicy.git] / renderers / opflex / src / main / java / org / opendaylight / groupbasedpolicy / renderer / opflex / mit / PolicyUri.java
1 /*
2  * Copyright (C) 2014 Cisco Systems, Inc.
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  * Authors : Thomas Bachman
9  */
10
11 package org.opendaylight.groupbasedpolicy.renderer.opflex.mit;
12
13 import java.util.ArrayList;
14 import java.util.List;
15
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
17
18 /**
19  * Class used for building and extending URIs
20  *
21  * Returning the URI could be optimized by building this
22  * as we go, rather than building it when requested.
23  *
24  * This counts and levels used in this class are for
25  * the level of hierarchy, and do not include any
26  * separator characters ("/"). For example, the following
27  * URI:
28  *
29  *   /tenants/tenant/51134b1e-6047-4d51-8d07-4135afd3672f
30  *
31  * has 3 levels.
32  *
33  * @author tbachman
34  *
35  */
36 public class PolicyUri {
37
38         public final static String POLICY_URI_SEP = "/";
39
40         private List<String> uri;
41
42         public PolicyUri() {
43                 uri = new ArrayList<String>();
44         }
45
46         /**
47          * Copy Constructor
48          *
49          * @param policyUri
50          */
51         public PolicyUri(PolicyUri policyUri) {
52                 this.uri = new ArrayList<String>(policyUri.uri);
53         }
54
55         /**
56          * Constructor using a full string, which gets split
57          * into it's elements.
58          *
59          * @param uri
60          */
61         public PolicyUri(String uri) {
62         String [] tmpUri = uri.split(PolicyUri.POLICY_URI_SEP);
63         if (tmpUri.length > 0) {
64                 this.uri = new ArrayList<String>();
65             // gets rid of leading empty element
66                 for (int i = 1; i < tmpUri.length; i++) {
67                         this.push(tmpUri[i]);
68                 }
69         }
70         else {
71             this.uri = null;
72         }
73         }
74
75         /**
76          * Constructor using a list of URI elements, which
77          * excludes separator characters
78          *
79          * @param tokens
80          */
81         public PolicyUri(List<String> tokens) {
82                 if (tokens.size() > 0) {
83                         this.uri = new ArrayList<String>();
84                         for (String t: tokens) {
85                                 this.push(t);
86                         }
87                 }
88         }
89
90         /**
91          * Return the URI as a Uri object, including
92          * separator characters
93          *
94          * @return
95          */
96         public Uri getUri() {
97                 return new Uri(this.toString());
98         }
99
100         /**
101          * Push a new leaf on to the URI
102          *
103          * @param leaf
104          */
105         public void push(String leaf) {
106                 uri.add(POLICY_URI_SEP);
107                 uri.add(leaf);
108         }
109
110         /**
111          * Returns the String representation of parent object URI
112          *
113          * @return
114          */
115         public String getParent() {
116                 if (uri.size() == 0) return null;
117
118                 PolicyUri parentUri = new PolicyUri(this);
119                 parentUri.pop();
120                 return parentUri.toString();
121
122         }
123
124         /**
125          * Remove (and return) the leaf of the URI.
126          * Never pop off the "/" root element.
127          *
128          * @return
129          */
130         public String pop() {
131                 if (uri.size() <= 0) {
132                         return null;
133                 }
134                 else {
135                         // remove the node
136                         String s = uri.remove(uri.size()-1);
137                         // remove the separator
138                         uri.remove(uri.size()-1);
139                         // return just the node
140                         return s;
141                 }
142         }
143
144     /**
145      * Determine if the URI is valid.
146      *
147      * @return
148      */
149     public boolean valid() {
150         if (uri.size() > 0) {
151             return true;
152         }
153         return false;
154     }
155
156
157     /**
158      * Get the element at depth "level". Returns
159      * null if level exceeds the hierarchy depth.
160      *
161      * @param level
162      * @return
163      */
164     public String getElement(int level) {
165         if ((level*2+1) > this.uri.size()) return null;
166
167         return this.uri.get(level*2+1);
168     }
169
170     /**
171      * Return the index of the first instance
172      * where the named element is found in the URI,
173      * starting the search from the root
174      *
175      * @param needle
176      * @return
177      */
178     public int whichElement(String needle) {
179         return (this.uri.indexOf(needle)/2);
180     }
181
182     /**
183      * Return the number of levels in the parsed URI
184      * hierarchy
185      *
186      * @return
187      */
188     public int totalElements() {
189         return (this.uri.size()/2);
190     }
191
192     /**
193      * Check to see if the parsed URI contains
194      * the named element in the hierarchy
195      *
196      * @param needle
197      * @return
198      */
199     public boolean contains(String needle) {
200         return this.uri.contains(needle);
201     }
202
203     public String originalPath() {
204         return this.toString();
205     }
206
207         @Override
208         public int hashCode() {
209                 final int prime = 31;
210                 int result = 1;
211                 result = prime * result + ((uri == null) ? 0 : uri.hashCode());
212                 return result;
213         }
214
215         @Override
216         public boolean equals(Object obj) {
217                 if (this == obj)
218                         return true;
219                 if (obj == null)
220                         return false;
221                 if (getClass() != obj.getClass())
222                         return false;
223                 PolicyUri other = (PolicyUri) obj;
224                 if (uri == null) {
225                         if (other.uri != null)
226                                 return false;
227                 } else if (!uri.equals(other.uri))
228                         return false;
229                 return true;
230         }
231
232     @Override
233         public String toString() {
234                 if (uri.size() == 0) return "";
235                 StringBuffer sb = new StringBuffer();
236
237                 for (String s: uri) {
238                         sb.append(s);
239                 }
240                 return sb.toString();
241         }
242 }