Updated the Documentation with sample yang and json
[cps.git] / docs / cps-path.rst
1 .. This work is licensed under a Creative Commons Attribution 4.0 International License.
2 .. http://creativecommons.org/licenses/by/4.0
3 .. Copyright (C) 2021-2022 Nordix Foundation
4
5 .. DO NOT CHANGE THIS LABEL FOR RELEASE NOTES - EVEN THOUGH IT GIVES A WARNING
6 .. _design:
7
8
9 CPS Path
10 ########
11
12 .. toctree::
13    :maxdepth: 1
14
15 Introduction
16 ============
17
18 Several CPS APIs use the CPS path (or cpsPath in Java API) parameter.
19 The 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/>`_
20
21 This section describes the functionality currently supported by CPS Path.
22
23 Sample Yang Model
24 =================
25
26 .. 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
94 The json and xml below describes some basic data to be used to illustrate the CPS Path functionality.
95
96 Sample 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
145 Sample Data in XML
146 ==================
147
148 .. code-block:: xml
149
150     <shops>
151        <bookstore name="Chapters">
152           <bookstore-name>Chapters</bookstore-name>
153           <categories code=1 name="SciFi" numberOfBooks="2">
154              <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>
167           <categories code=2 name="Kids" numberOfBooks="1">
168              <books>
169                 <book title="Matilda" />
170              </books>
171           </categories>
172        </bookstore>
173     </shops>
174
175 General Notes
176 =============
177
178 - String values must be wrapped in quotation marks ``"`` (U+0022) or apostrophes ``'`` (U+0027).
179 - String comparisons are case sensitive.
180 - 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
181
182 Query Syntax
183 ============
184
185 ``( <absolute-path> | <descendant-path> ) [ <leaf-conditions> ] [ <text()-condition> ] [ <ancestor-axis> ]``
186
187 Each CPS path expression need to start with an 'absolute' or 'descendant' xpath.
188
189 absolute-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``
200   - ``/shops/bookstore/categories[@code='1']/books``
201   - ``/shops/bookstore/categories[@code='1']/books/book[@title='2001: A Space Odyssey']``
202
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   - 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>`_
208
209 descendant-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``
218   - ``//categories[@code='1']/books``
219   - ``//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
224 leaf-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.
229   - ``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]``
234   - ``//categories[@name="Kids"]``
235   - ``//categories[@name='Kids']``
236   - ``//categories[@code='1']/books/book[@title='Dune' and @price=5]``
237
238 **Limitations**
239   - 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.
243
244 **Notes**
245   - 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.
246
247 text()-condition
248 ----------------
249
250 The text()-condition  can be added to any CPS path query.
251
252 **Syntax**: ``<cps-path> ( '/' <leaf-name> '[text()=' <string-value> ']' )?``
253   - ``cps-path``: Any CPS path query.
254   - ``leaf-name``: The name of the leaf or leaf-list which value needs to be compared.
255   - ``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.
256
257 **Examples**
258   - ``//book/label[text()="classic"]``
259   - ``//book/edition[text()="1965"]``
260
261 **Limitations**
262   - 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).
263   - Only one leaf or leaf-list can be tested.
264   - Only string and integer values are supported, boolean and float values are not supported.
265   - 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.
266   - 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.
267
268 ancestor-axis
269 -------------
270
271 The ancestor axis can be added to any CPS path query but has to be the last part.
272
273 **Syntax**: ``<cps-path> ( '/ancestor::' <ancestor-path> )?``
274   - ``cps-path``: Any CPS path query.
275   - ``ancestor-path``: Partial path to ancestors of the target node. This can contain one or more ancestor nodes separated by a ``/``.
276
277 **Examples**
278   - ``//book/ancestor::categories``
279   - ``//categories[@code='2']/books/ancestor::bookstore``
280   - ``//book/ancestor::categories[@code='1']/books``
281   - ``//book/label[text()="classic"]/ancestor::shops``
282
283 **Limitations**
284   - Ancestor list elements can only be addressed using the list key leaf.
285   - List elements with compound keys are not supported.