Add a simple Binding query language
[mdsal.git] / binding / mdsal-binding-api / src / test / java / org / opendaylight / mdsal / binding / api / query / QueryBuilderExamples.java
1 /*
2  * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others.  All rights reserved.
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 package org.opendaylight.mdsal.binding.api.query;
9
10 import org.mockito.Mock;
11 import org.opendaylight.yang.gen.v1.mdsal.query.norev.Foo;
12 import org.opendaylight.yang.gen.v1.mdsal.query.norev.first.grp.System;
13 import org.opendaylight.yang.gen.v1.mdsal.query.norev.first.grp.SystemKey;
14 import org.opendaylight.yang.gen.v1.mdsal.query.norev.second.grp.Alarms;
15 import org.opendaylight.yang.gen.v1.mdsal.query.norev.third.grp.AffectedUsers;
16 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
17 import org.opendaylight.yangtools.yang.common.Uint64;
18
19 public class QueryBuilderExamples {
20     @Mock
21     private QueryFactory factory;
22
23     /*
24      * Return all of /foo. Equivalent to a read() of the same identifier.
25      */
26     public QueryExpression<Foo> selectFoo() {
27         return factory
28                 .querySubtree(InstanceIdentifier.create(Foo.class))
29                 .build();
30     }
31
32     /*
33      * Read all of /foo/system[name="some"]. Equivalent to a read() of the same identifier.
34      */
35     public QueryExpression<System> selectFooSystemSome() {
36         return factory
37                 .querySubtree(InstanceIdentifier.create(Foo.class).child(System.class, new SystemKey("some")))
38                 .build();
39     }
40
41     /*
42      * Read all entries in /foo/system. Equivalent to a read(/foo).get().nonnullSystem().
43      */
44     public QueryExpression<System> selectFooSystem() {
45         return factory
46                 .querySubtree(InstanceIdentifier.create(Foo.class))
47                 .extractChild(System.class)
48                 .build();
49     }
50
51     /*
52      * Read all entries in /foo/system, which have 'alias' set to 'some'.
53      */
54     public QueryExpression<System> selectFooSystemAliasSome() {
55         return factory
56                 .querySubtree(InstanceIdentifier.create(Foo.class))
57                 .extractChild(System.class)
58                 .matching()
59                     .leaf(System::getAlias)
60                     .valueEquals("some")
61                 .build();
62     }
63
64     /*
65      * Read all entries in /foo/system, which have 'alias' containing the string 'needle'.
66      */
67     public QueryExpression<System> selectFooSystemAliasWithNeedle() {
68         return factory
69                 .querySubtree(InstanceIdentifier.create(Foo.class))
70                 .extractChild(System.class)
71                 .matching()
72                     .leaf(System::getAlias)
73                     .contains("needle")
74                 .build();
75     }
76
77     /*
78      * Read all entries in /foo/system/alarms, which have 'critical' leaf present.
79      */
80     public QueryExpression<Alarms> selectFooSystemAlarmsCritical() {
81         return factory
82                 .querySubtree(InstanceIdentifier.create(Foo.class))
83                 .extractChild(System.class)
84                 .extractChild(Alarms.class)
85                 .matching()
86                     .leaf(Alarms::getCritical)
87                     .nonNull()
88                 .build();
89     }
90
91     /*
92      * Read all entries in /foo/system/alarms, which have 'critical' leaf present and have an entry in 'affected-users'
93      * with 'uid' larger than 10.
94      *
95      * Note this is the same expression as selectFooSystemCriticalUid(), but selects Alarms objects.
96      */
97     public QueryExpression<Alarms> selectFooSystemAlarmsCriticalUid() {
98         return factory
99                 .querySubtree(InstanceIdentifier.create(Foo.class))
100                 .extractChild(System.class)
101                 .extractChild(Alarms.class)
102                 .matching()
103                     .leaf(Alarms::getCritical)
104                     .nonNull()
105                 .and()
106                     .childObject(AffectedUsers.class)
107                     .leaf(AffectedUsers::getUid)
108                     .greaterThan(Uint64.TEN)
109                 .build();
110     }
111
112
113     /*
114      * Read all entries in /foo/system, which have 'critical' leaf present and have an entry in 'affected-users'
115      * with 'uid' larger than 10.
116      *
117      * Note this is the same expression as selectFooSystemAlarmsCriticalUid(), but selects System objects.
118      */
119     public QueryExpression<System> selectFooSystemCriticalUid() {
120         return factory
121                 .querySubtree(InstanceIdentifier.create(Foo.class))
122                 .extractChild(System.class)
123                 .matching()
124                     .childObject(Alarms.class)
125                     .leaf(Alarms::getCritical)
126                     .nonNull()
127                 .and()
128                     .childObject(Alarms.class)
129                     .childObject(AffectedUsers.class)
130                     .leaf(AffectedUsers::getUid)
131                     .greaterThan(Uint64.TEN)
132                 .build();
133     }
134 }