blob: 44662d893c2d5977a324114d18ad91e52f2dbebf [file] [log] [blame]
ToineSiebelink98c07872021-04-20 17:33:09 +01001.. This work is licensed under a Creative Commons Attribution 4.0 International License.
2.. http://creativecommons.org/licenses/by/4.0
Lathishb3550f12022-03-15 12:03:53 +00003.. Copyright (C) 2021-2022 Nordix Foundation
ToineSiebelink98c07872021-04-20 17:33:09 +01004
5.. DO NOT CHANGE THIS LABEL FOR RELEASE NOTES - EVEN THOUGH IT GIVES A WARNING
emacleeb176de22022-08-31 15:53:10 +01006.. _path:
ToineSiebelink98c07872021-04-20 17:33:09 +01007
8
9CPS Path
10########
11
12.. toctree::
13 :maxdepth: 1
14
15Introduction
16============
17
ToineSiebelinkf0527c52021-07-06 13:03:03 +010018Several CPS APIs use the CPS path (or cpsPath in Java API) parameter.
19The CPS path parameter is used for querying xpaths. CPS path is inspired by the `XML Path Language (XPath) 3.1. <https://www.w3.org/TR/2017/REC-xpath-31-20170321/>`_
ToineSiebelink98c07872021-04-20 17:33:09 +010020
21This section describes the functionality currently supported by CPS Path.
22
Lathishb3550f12022-03-15 12:03:53 +000023Sample Yang Model
24=================
ToineSiebelink98c07872021-04-20 17:33:09 +010025
Lathishb3550f12022-03-15 12:03:53 +000026.. code-block::
27
28 module stores {
29 yang-version 1.1;
30 namespace "org:onap:ccsdk:sample";
31
32 prefix book-store;
33
34 revision "2020-09-15" {
35 description
36 "Sample Model";
37 }
38 container shops {
39
40 container bookstore {
41
42 leaf bookstore-name {
43 type string;
44 }
45
46 leaf name {
47 type string;
48 }
49
50 list categories {
51
52 key "code";
53
54 leaf code {
55 type uint16;
56 }
57
58 leaf name {
59 type string;
60 }
61
62 leaf numberOfBooks {
63 type uint16;
64 }
65
66 container books {
67
68 list book {
69 key title;
70
71 leaf title {
72 type string;
73 }
74 leaf price {
75 type uint16;
76 }
77 leaf-list label {
78 type string;
79 }
80 leaf-list edition {
81 type string;
82 }
83 }
84 }
85 }
86 }
87 }
88 }
89
90**Note.** 'categories' is a Yang List and 'code' is its key leaf. All other data nodes are Yang Containers. 'label' and 'edition' are both leaf-lists.
91
92**Note.** CPS accepts only json data. The xml data presented here is for illustration purposes only.
93
94The json and xml below describes some basic data to be used to illustrate the CPS Path functionality.
95
96Sample Data in Json
97===================
98
99.. code-block:: json
100
101 {
102 "shops": {
103 "bookstore": {
104 "bookstore-name": "Chapters",
105 "name": "Chapters",
106 "categories": [
107 {
108 "code": 1,
109 "name": "SciFi",
110 "numberOfBooks": 2,
111 "books": {
112 "book": [
113 {
114 "title": "2001: A Space Odyssey",
115 "price": 5,
116 "label": ["sale", "classic"],
117 "edition": ["1968", "2018"]
118 },
119 {
120 "title": "Dune",
121 "price": 5,
122 "label": ["classic"],
123 "edition": ["1965"]
124 }
125 ]
126 }
127 },
128 {
129 "code": 2,
130 "name": "Kids",
131 "numberOfBooks": 1,
132 "books": {
133 "book": [
134 {
135 "title": "Matilda"
136 }
137 ]
138 }
139 }
140 ]
141 }
142 }
143 }
144
145Sample Data in XML
146==================
ToineSiebelink98c07872021-04-20 17:33:09 +0100147
148.. code-block:: xml
149
150 <shops>
151 <bookstore name="Chapters">
ToineSiebelinkf0527c52021-07-06 13:03:03 +0100152 <bookstore-name>Chapters</bookstore-name>
Lathishb3550f12022-03-15 12:03:53 +0000153 <categories code=1 name="SciFi" numberOfBooks="2">
ToineSiebelinkf0527c52021-07-06 13:03:03 +0100154 <books>
155 <book title="2001: A Space Odyssey" price="5">
156 <label>sale</label>
157 <label>classic</label>
158 <edition>1968</edition>
159 <edition>2018</edition>
160 </book>
161 <book title="Dune" price="5">
162 <label>classic</label>
163 <edition>1965</edition>
164 </book>
165 </books>
166 </categories>
Lathishb3550f12022-03-15 12:03:53 +0000167 <categories code=2 name="Kids" numberOfBooks="1">
ToineSiebelinkf0527c52021-07-06 13:03:03 +0100168 <books>
169 <book title="Matilda" />
170 </books>
171 </categories>
ToineSiebelink98c07872021-04-20 17:33:09 +0100172 </bookstore>
173 </shops>
174
ToineSiebelink98c07872021-04-20 17:33:09 +0100175General Notes
176=============
177
ToineSiebelinkf0527c52021-07-06 13:03:03 +0100178- String values must be wrapped in quotation marks ``"`` (U+0022) or apostrophes ``'`` (U+0027).
ToineSiebelink98c07872021-04-20 17:33:09 +0100179- String comparisons are case sensitive.
ToineSiebelinkf0527c52021-07-06 13:03:03 +0100180- List key-fields containing ``\`` or ``@[`` will not be processed correctly when being referenced with such key values in absolute or descendant paths. This means such entries will be omitted from any query result. See `CPS-500 <https://jira.onap.org/browse/CPS-500>`_ Special Character Limitations of cpsPath Queries
ToineSiebelink98c07872021-04-20 17:33:09 +0100181
ToineSiebelinkf0527c52021-07-06 13:03:03 +0100182Query Syntax
183============
ToineSiebelink98c07872021-04-20 17:33:09 +0100184
ToineSiebelinkf0527c52021-07-06 13:03:03 +0100185``( <absolute-path> | <descendant-path> ) [ <leaf-conditions> ] [ <text()-condition> ] [ <ancestor-axis> ]``
ToineSiebelink98c07872021-04-20 17:33:09 +0100186
ToineSiebelinkf0527c52021-07-06 13:03:03 +0100187Each CPS path expression need to start with an 'absolute' or 'descendant' xpath.
188
189absolute-path
190-------------
191
192**Syntax**: ``'/' <container-name> ( '[' <list-key> ']' )? ( '/' <containerName> ( '[' <list-key> ']' )? )*``
193
194 - ``container name``: Any yang container or list.
195 - ``list-key``: One or more key-value pairs, each preceded by the ``@`` symbol, combined using the ``and`` keyword.
196 - The above van repeated any number of times.
197
198**Examples**
199 - ``/shops/bookstore``
Lathishb3550f12022-03-15 12:03:53 +0000200 - ``/shops/bookstore/categories[@code='1']/books``
201 - ``/shops/bookstore/categories[@code='1']/books/book[@title='2001: A Space Odyssey']``
ToineSiebelinkf0527c52021-07-06 13:03:03 +0100202
203**Limitations**
204 - Absolute paths must start with the top element (data node) as per the model tree.
205 - Each list reference must include a valid instance reference to the key for that list. Except when it is the last element.
206
207descendant-path
208---------------
209
210**Syntax**: ``'//' <container-name> ( '[' <list-key> ']' )? ( '/' <containerName> ( '[' <list-key> ']' )? )*``
211
212 - The syntax of a descendant path is identical to a absolute path except that it is preceded by a double slash ``//``.
213
214**Examples**
215 - ``//bookstore``
Lathishb3550f12022-03-15 12:03:53 +0000216 - ``//categories[@code='1']/books``
ToineSiebelinkf0527c52021-07-06 13:03:03 +0100217 - ``//bookstore/categories``
218
219**Limitations**
220 - Each list reference must include a valid instance reference to the key for that list. Except when it is the last element.
221
222leaf-conditions
223---------------
224
225**Syntax**: ``<xpath> '[' @<leaf-name1> '=' <leaf-value1> ( ' and ' @<leaf-name> '=' <leaf-value> )* ']'``
226 - ``xpath``: Absolute or descendant or xpath to the (list) node which elements will be queried.
ToineSiebelink98c07872021-04-20 17:33:09 +0100227 - ``leaf-name``: The name of the leaf which value needs to be compared.
228 - ``leaf-value``: The required value of the leaf.
229
230**Examples**
231 - ``/shops/bookstore/categories[@numberOfBooks=1]``
ToineSiebelinkf0527c52021-07-06 13:03:03 +0100232 - ``//categories[@name="Kids"]``
233 - ``//categories[@name='Kids']``
Lathishb3550f12022-03-15 12:03:53 +0000234 - ``//categories[@code='1']/books/book[@title='Dune' and @price=5]``
Lathisha7dc9162022-04-28 16:37:00 +0100235 - ``//categories[@code=1]``
ToineSiebelink98c07872021-04-20 17:33:09 +0100236**Limitations**
ToineSiebelinkf0527c52021-07-06 13:03:03 +0100237 - Only the last list or container can be queried leaf values. Any ancestor list will have to be referenced by its key name-value pair(s).
238 - Multiple attributes can only be combined using ``and``. ``or`` and bracketing is not supported.
239 - Only leaves can be used, leaf-list are not supported.
240 - Only string and integer values are supported, boolean and float values are not supported.
Lathisha7dc9162022-04-28 16:37:00 +0100241 - The key should be supplied with correct data type for it to be queried from DB. In the last example above the attribute code is of type
242 Integer so the cps query will not work if the value is passed as string.
243 eg: ``//categories[@code="1"]`` or ``//categories[@code='1']`` will not work because the key attribute code is treated a string.
ToineSiebelink98c07872021-04-20 17:33:09 +0100244
245**Notes**
puthuparambil.aditya05316072021-04-23 12:52:09 +0100246 - For performance reasons it does not make sense to query using key leaf as attribute. If the key value is known it is better to execute a get request with the complete xpath.
ToineSiebelink98c07872021-04-20 17:33:09 +0100247
ToineSiebelinkf0527c52021-07-06 13:03:03 +0100248text()-condition
249----------------
ToineSiebelink98c07872021-04-20 17:33:09 +0100250
ToineSiebelinkf0527c52021-07-06 13:03:03 +0100251The text()-condition can be added to any CPS path query.
252
253**Syntax**: ``<cps-path> ( '/' <leaf-name> '[text()=' <string-value> ']' )?``
254 - ``cps-path``: Any CPS path query.
255 - ``leaf-name``: The name of the leaf or leaf-list which value needs to be compared.
256 - ``string-value``: The required value of the leaf or leaf-list element as a string wrapped in quotation marks (U+0022) or apostrophes (U+0027). This wil still match integer values.
ToineSiebelink98c07872021-04-20 17:33:09 +0100257
258**Examples**
ToineSiebelinkf0527c52021-07-06 13:03:03 +0100259 - ``//book/label[text()="classic"]``
260 - ``//book/edition[text()="1965"]``
ToineSiebelink98c07872021-04-20 17:33:09 +0100261
262**Limitations**
ToineSiebelinkf0527c52021-07-06 13:03:03 +0100263 - Only the last list or container can be queried for leaf values with a text() condition. Any ancestor list will have to be referenced by its key name-value pair(s).
264 - Only one leaf or leaf-list can be tested.
265 - Only string and integer values are supported, boolean and float values are not supported.
266 - Since CPS cannot return individual leaves it will always return the container with all its leaves. Ancestor-axis can be used to specify a parent higher up the tree.
267 - When querying a leaf value (instead of leaf-list) it is better, more performant to use a text value condition use @<leaf-name> as described above.
puthuparambil.adityaff714622021-04-23 11:55:24 +0100268
ToineSiebelinkf0527c52021-07-06 13:03:03 +0100269ancestor-axis
niamhcore4395c1f2021-05-05 11:42:20 +0100270-------------
271
ToineSiebelinkf0527c52021-07-06 13:03:03 +0100272The ancestor axis can be added to any CPS path query but has to be the last part.
niamhcore4395c1f2021-05-05 11:42:20 +0100273
ToineSiebelinkf0527c52021-07-06 13:03:03 +0100274**Syntax**: ``<cps-path> ( '/ancestor::' <ancestor-path> )?``
niamhcore4395c1f2021-05-05 11:42:20 +0100275 - ``cps-path``: Any CPS path query.
ToineSiebelinkf0527c52021-07-06 13:03:03 +0100276 - ``ancestor-path``: Partial path to ancestors of the target node. This can contain one or more ancestor nodes separated by a ``/``.
niamhcore4395c1f2021-05-05 11:42:20 +0100277
278**Examples**
279 - ``//book/ancestor::categories``
Lathishb3550f12022-03-15 12:03:53 +0000280 - ``//categories[@code='2']/books/ancestor::bookstore``
281 - ``//book/ancestor::categories[@code='1']/books``
282 - ``//book/label[text()="classic"]/ancestor::shops``
niamhcore4395c1f2021-05-05 11:42:20 +0100283
284**Limitations**
285 - Ancestor list elements can only be addressed using the list key leaf.
ToineSiebelinkf0527c52021-07-06 13:03:03 +0100286 - List elements with compound keys are not supported.