Initial opendaylight infrastructure commit!!
[controller.git] / opendaylight / sal / yang-prototype / yang / yang-common / src / main / java / org / opendaylight / controller / yang / common / QName.java
1 /*\r
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.\r
3  *\r
4  * This program and the accompanying materials are made available under the\r
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
6  * and is available at http://www.eclipse.org/legal/epl-v10.html\r
7  */\r
8 package org.opendaylight.controller.yang.common;\r
9 \r
10 import java.net.URI;\r
11 import java.net.URISyntaxException;\r
12 \r
13 import java.text.SimpleDateFormat;\r
14 import java.util.Date;\r
15 \r
16 /**\r
17  * The QName from XML consists of local name of element and XML namespace, but\r
18  * for our use, we added module revision to it.\r
19  * \r
20  * In YANG context QName is full name of defined node, type, procedure or\r
21  * notification. QName consists of XML namespace, YANG model revision and local\r
22  * name of defined type. It is used to prevent name clashes between nodes with\r
23  * same local name, but from different schemas.\r
24  * \r
25  * <ul>\r
26  * <li><b>XMLNamespace</b> - the namespace assigned to the YANG module which\r
27  * defined element, type, procedure or notification.</li>\r
28  * <li><b>Revision</b> - the revision of the YANG module which describes the\r
29  * element</li>\r
30  * <li><b>LocalName</b> - the YANG schema identifier which were defined for this\r
31  * node in the YANG module</li>\r
32  * </ul>\r
33  * \r
34  * \r
35  */\r
36 public class QName {\r
37 \r
38     private SimpleDateFormat revisionFormat = new SimpleDateFormat("yyyy-MM-dd");\r
39 \r
40     final URI namespace;\r
41     final String localName;\r
42     final String prefix;\r
43     final Date revision;\r
44 \r
45     /**\r
46      * QName Constructor.\r
47      * \r
48      * @param namespace\r
49      *            the namespace assigned to the YANG module\r
50      * @param revision\r
51      *            the revision of the YANG module\r
52      * @param prefix\r
53      *            locally defined prefix assigned to local name\r
54      * @param localName\r
55      *            YANG schema identifier\r
56      */\r
57     public QName(URI namespace, Date revision, String prefix, String localName) {\r
58         this.namespace = namespace;\r
59         this.localName = localName;\r
60         this.revision = revision;\r
61         this.prefix = prefix;\r
62     }\r
63 \r
64     /**\r
65      * QName Constructor.\r
66      * \r
67      * @param namespace\r
68      *            the namespace assigned to the YANG module\r
69      * @param localName\r
70      *            YANG schema identifier\r
71      */\r
72     public QName(URI namespace, String localName) {\r
73         this(namespace, null, "", localName);\r
74     }\r
75 \r
76     /**\r
77      * QName Constructor.\r
78      * \r
79      * @param namespace\r
80      *            the namespace assigned to the YANG module\r
81      * @param revision\r
82      *            the revision of the YANG module\r
83      * @param localName\r
84      *            YANG schema identifier\r
85      */\r
86     public QName(URI namespace, Date revision, String localName) {\r
87         this(namespace, revision, null, localName);\r
88     }\r
89 \r
90     public QName(QName base, String localName) {\r
91         this(base.getNamespace(), base.getRevision(), base.getPrefix(),\r
92                 localName);\r
93     }\r
94 \r
95     /**\r
96      * Returns XMLNamespace assigned to the YANG module.\r
97      * \r
98      * @return XMLNamespace assigned to the YANG module.\r
99      */\r
100     public URI getNamespace() {\r
101         return namespace;\r
102     }\r
103 \r
104     /**\r
105      * Returns YANG schema identifier which were defined for this node in the\r
106      * YANG module\r
107      * \r
108      * @return YANG schema identifier which were defined for this node in the\r
109      *         YANG module\r
110      */\r
111     public String getLocalName() {\r
112         return localName;\r
113     }\r
114 \r
115     /**\r
116      * Returns revision of the YANG module if the module has defined revision,\r
117      * otherwise returns <code>null</code>\r
118      * \r
119      * @return revision of the YANG module if the module has defined revision,\r
120      *         otherwise returns <code>null</code>\r
121      */\r
122     public Date getRevision() {\r
123         return revision;\r
124     }\r
125 \r
126     /**\r
127      * Returns locally defined prefix assigned to local name\r
128      * \r
129      * @return locally defined prefix assigned to local name\r
130      */\r
131     public String getPrefix() {\r
132         return prefix;\r
133     }\r
134 \r
135     @Override\r
136     public int hashCode() {\r
137         final int prime = 31;\r
138         int result = 1;\r
139         result = prime * result\r
140                 + ((localName == null) ? 0 : localName.hashCode());\r
141         result = prime * result\r
142                 + ((namespace == null) ? 0 : namespace.hashCode());\r
143         result = prime * result\r
144                 + ((revision == null) ? 0 : revision.hashCode());\r
145         return result;\r
146     }\r
147 \r
148     @Override\r
149     public boolean equals(Object obj) {\r
150         if (this == obj)\r
151             return true;\r
152         if (obj == null)\r
153             return false;\r
154         if (getClass() != obj.getClass())\r
155             return false;\r
156         QName other = (QName) obj;\r
157         if (localName == null) {\r
158             if (other.localName != null)\r
159                 return false;\r
160         } else if (!localName.equals(other.localName))\r
161             return false;\r
162         if (namespace == null) {\r
163             if (other.namespace != null)\r
164                 return false;\r
165         } else if (!namespace.equals(other.namespace))\r
166             return false;\r
167         if (revision == null) {\r
168             if (other.revision != null)\r
169                 return false;\r
170         } else if (!revision.equals(other.revision))\r
171             return false;\r
172         return true;\r
173     }\r
174 \r
175     @Override\r
176     public String toString() {\r
177         StringBuilder sb = new StringBuilder();\r
178         if (namespace != null) {\r
179             sb.append("(" + namespace);\r
180 \r
181             if (revision != null) {\r
182                 sb.append("?revision=" + revisionFormat.format(revision));\r
183             }\r
184             sb.append(")");\r
185         }\r
186         sb.append(localName);\r
187         return sb.toString();\r
188     }\r
189 \r
190     /**\r
191      * Returns a namespace in form defined by section 5.6.4. of {@link https\r
192      * ://tools.ietf.org/html/rfc6020}, if namespace is not correctly defined,\r
193      * the method will return <code>null</code> <br>\r
194      * example "http://example.acme.com/system?revision=2008-04-01"\r
195      * \r
196      * @return namespace in form defined by section 5.6.4. of {@link https\r
197      *         ://tools.ietf.org/html/rfc6020}, if namespace is not correctly\r
198      *         defined, the method will return <code>null</code>\r
199      * \r
200      */\r
201     URI getRevisionNamespace() {\r
202 \r
203         if (namespace == null)\r
204             return null;\r
205 \r
206         String query = "";\r
207         if (revision != null) {\r
208             query = "revision=" + revisionFormat.format(revision);\r
209         }\r
210 \r
211         URI compositeURI = null;\r
212         try {\r
213             compositeURI = new URI(namespace.getScheme(),\r
214                     namespace.getUserInfo(), namespace.getHost(),\r
215                     namespace.getPort(), namespace.getPath(), query,\r
216                     namespace.getFragment());\r
217         } catch (URISyntaxException e) {\r
218             e.printStackTrace();\r
219         }\r
220         return compositeURI;\r
221     }\r
222 }\r