blob: fba21f38c67bac3964b17da43fd48ab6bc024627 [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
6.. _design:
7
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.
Lathishb3550f12022-03-15 12:03:53 +0000206 - The Absolute path to list with integer key will not work. It needs to be surrounded with a single quote ([@code='1'])
207 as if it is a string. This will be fixed in `CPS-961 <https://jira.onap.org/browse/CPS-961>`_
ToineSiebelinkf0527c52021-07-06 13:03:03 +0100208
209descendant-path
210---------------
211
212**Syntax**: ``'//' <container-name> ( '[' <list-key> ']' )? ( '/' <containerName> ( '[' <list-key> ']' )? )*``
213
214 - The syntax of a descendant path is identical to a absolute path except that it is preceded by a double slash ``//``.
215
216**Examples**
217 - ``//bookstore``
Lathishb3550f12022-03-15 12:03:53 +0000218 - ``//categories[@code='1']/books``
ToineSiebelinkf0527c52021-07-06 13:03:03 +0100219 - ``//bookstore/categories``
220
221**Limitations**
222 - Each list reference must include a valid instance reference to the key for that list. Except when it is the last element.
223
224leaf-conditions
225---------------
226
227**Syntax**: ``<xpath> '[' @<leaf-name1> '=' <leaf-value1> ( ' and ' @<leaf-name> '=' <leaf-value> )* ']'``
228 - ``xpath``: Absolute or descendant or xpath to the (list) node which elements will be queried.
ToineSiebelink98c07872021-04-20 17:33:09 +0100229 - ``leaf-name``: The name of the leaf which value needs to be compared.
230 - ``leaf-value``: The required value of the leaf.
231
232**Examples**
233 - ``/shops/bookstore/categories[@numberOfBooks=1]``
ToineSiebelinkf0527c52021-07-06 13:03:03 +0100234 - ``//categories[@name="Kids"]``
235 - ``//categories[@name='Kids']``
Lathishb3550f12022-03-15 12:03:53 +0000236 - ``//categories[@code='1']/books/book[@title='Dune' and @price=5]``
Lathisha7dc9162022-04-28 16:37:00 +0100237 - ``//categories[@code=1]``
ToineSiebelink98c07872021-04-20 17:33:09 +0100238**Limitations**
ToineSiebelinkf0527c52021-07-06 13:03:03 +0100239 - 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).
240 - Multiple attributes can only be combined using ``and``. ``or`` and bracketing is not supported.
241 - Only leaves can be used, leaf-list are not supported.
242 - Only string and integer values are supported, boolean and float values are not supported.
Lathisha7dc9162022-04-28 16:37:00 +0100243 - 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
244 Integer so the cps query will not work if the value is passed as string.
245 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 +0100246
247**Notes**
puthuparambil.aditya05316072021-04-23 12:52:09 +0100248 - 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 +0100249
ToineSiebelinkf0527c52021-07-06 13:03:03 +0100250text()-condition
251----------------
ToineSiebelink98c07872021-04-20 17:33:09 +0100252
ToineSiebelinkf0527c52021-07-06 13:03:03 +0100253The text()-condition can be added to any CPS path query.
254
255**Syntax**: ``<cps-path> ( '/' <leaf-name> '[text()=' <string-value> ']' )?``
256 - ``cps-path``: Any CPS path query.
257 - ``leaf-name``: The name of the leaf or leaf-list which value needs to be compared.
258 - ``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 +0100259
260**Examples**
ToineSiebelinkf0527c52021-07-06 13:03:03 +0100261 - ``//book/label[text()="classic"]``
262 - ``//book/edition[text()="1965"]``
ToineSiebelink98c07872021-04-20 17:33:09 +0100263
264**Limitations**
ToineSiebelinkf0527c52021-07-06 13:03:03 +0100265 - 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).
266 - Only one leaf or leaf-list can be tested.
267 - Only string and integer values are supported, boolean and float values are not supported.
268 - 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.
269 - 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 +0100270
ToineSiebelinkf0527c52021-07-06 13:03:03 +0100271ancestor-axis
niamhcore4395c1f2021-05-05 11:42:20 +0100272-------------
273
ToineSiebelinkf0527c52021-07-06 13:03:03 +0100274The ancestor axis can be added to any CPS path query but has to be the last part.
niamhcore4395c1f2021-05-05 11:42:20 +0100275
ToineSiebelinkf0527c52021-07-06 13:03:03 +0100276**Syntax**: ``<cps-path> ( '/ancestor::' <ancestor-path> )?``
niamhcore4395c1f2021-05-05 11:42:20 +0100277 - ``cps-path``: Any CPS path query.
ToineSiebelinkf0527c52021-07-06 13:03:03 +0100278 - ``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 +0100279
280**Examples**
281 - ``//book/ancestor::categories``
Lathishb3550f12022-03-15 12:03:53 +0000282 - ``//categories[@code='2']/books/ancestor::bookstore``
283 - ``//book/ancestor::categories[@code='1']/books``
284 - ``//book/label[text()="classic"]/ancestor::shops``
niamhcore4395c1f2021-05-05 11:42:20 +0100285
286**Limitations**
287 - Ancestor list elements can only be addressed using the list key leaf.
ToineSiebelinkf0527c52021-07-06 13:03:03 +0100288 - List elements with compound keys are not supported.