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