Adopt odlparent-10.0.0/yangtools-8.0.0-SNAPSHOT
[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.yang.gen.v1.mdsal426.norev.BooleanCont;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns._default.value.test.norev.DecimalContainer;
18 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
19 import org.opendaylight.yangtools.yang.common.Decimal64;
20 import org.opendaylight.yangtools.yang.common.Uint64;
21
22 @SuppressWarnings("exports")
23 public class QueryBuilderExamples {
24     @Mock
25     private QueryFactory factory;
26
27     /*
28      * Return all of /foo. Equivalent to a read() of the same identifier.
29      */
30     public QueryExpression<Foo> selectFoo() {
31         return factory
32                 .querySubtree(InstanceIdentifier.create(Foo.class))
33                 .build();
34     }
35
36     /*
37      * Read all of /foo/system[name="some"]. Equivalent to a read() of the same identifier.
38      */
39     public QueryExpression<System> selectFooSystemSome() {
40         return factory
41                 .querySubtree(InstanceIdentifier.create(Foo.class).child(System.class, new SystemKey("some")))
42                 .build();
43     }
44
45     /*
46      * Read all entries in /foo/system. Equivalent to a read(/foo).get().nonnullSystem().
47      */
48     public QueryExpression<System> selectFooSystem() {
49         return factory
50                 .querySubtree(InstanceIdentifier.create(Foo.class))
51                 .extractChild(System.class)
52                 .build();
53     }
54
55     /*
56      * Read all entries in /foo/system, which have 'alias' set to 'some'.
57      */
58     public QueryExpression<System> selectFooSystemAliasSome() {
59         return factory
60                 .querySubtree(InstanceIdentifier.create(Foo.class))
61                 .extractChild(System.class)
62                 .matching()
63                     .leaf(System::getAlias)
64                     .valueEquals("some")
65                 .build();
66     }
67
68     /*
69      * Read all entries in /foo/system, which have 'alias' containing the string 'needle'.
70      */
71     public QueryExpression<System> selectFooSystemAliasWithNeedle() {
72         return factory
73                 .querySubtree(InstanceIdentifier.create(Foo.class))
74                 .extractChild(System.class)
75                 .matching()
76                     .leaf(System::getAlias)
77                     .contains("needle")
78                 .build();
79     }
80
81     /*
82      * Read all entries in /foo/system/alarms, which have 'critical' leaf present.
83      */
84     public QueryExpression<Alarms> selectFooSystemAlarmsCritical() {
85         return factory
86                 .querySubtree(InstanceIdentifier.create(Foo.class))
87                 .extractChild(System.class)
88                 .extractChild(Alarms.class)
89                 .matching()
90                     .leaf(Alarms::getCritical)
91                     .nonNull()
92                 .build();
93     }
94
95     /*
96      * Read all entries in /foo/system/alarms, which have 'critical' leaf present and have an entry in 'affected-users'
97      * with 'uid' larger than 10.
98      *
99      * Note this is the same expression as selectFooSystemCriticalUid(), but selects Alarms objects.
100      */
101     public QueryExpression<Alarms> selectFooSystemAlarmsCriticalUid() {
102         return factory
103                 .querySubtree(InstanceIdentifier.create(Foo.class))
104                 .extractChild(System.class)
105                 .extractChild(Alarms.class)
106                 .matching()
107                     .leaf(Alarms::getCritical)
108                     .nonNull()
109                 .and()
110                     .childObject(AffectedUsers.class)
111                     .leaf(AffectedUsers::getUid)
112                     .greaterThan(Uint64.TEN)
113                 .build();
114     }
115
116
117     /*
118      * Read all entries in /foo/system, which have 'critical' leaf present and have an entry in 'affected-users'
119      * with 'uid' larger than 10.
120      *
121      * Note this is the same expression as selectFooSystemAlarmsCriticalUid(), but selects System objects.
122      */
123     public QueryExpression<System> selectFooSystemCriticalUid() {
124         return factory
125                 .querySubtree(InstanceIdentifier.create(Foo.class))
126                 .extractChild(System.class)
127                 .matching()
128                     .childObject(Alarms.class)
129                     .leaf(Alarms::getCritical)
130                     .nonNull()
131                 .and()
132                     .childObject(Alarms.class)
133                     .childObject(AffectedUsers.class)
134                     .leaf(AffectedUsers::getUid)
135                     .greaterThan(Uint64.TEN)
136                 .build();
137     }
138
139     public QueryExpression<BooleanCont> selectBoolean() {
140         return factory
141                 .querySubtree(InstanceIdentifier.create(BooleanCont.class))
142                 .matching()
143                     .leaf(BooleanCont::getIsFoo)
144                     .valueEquals(true)
145                 .build();
146     }
147
148     public QueryExpression<DecimalContainer> selectDecimal64() {
149         return factory
150                 .querySubtree(InstanceIdentifier.create(DecimalContainer.class))
151                 .matching()
152                     .leaf(DecimalContainer::getDecimalLeaf5)
153                     .valueEquals(Decimal64.valueOf("1.0"))
154                 .build();
155     }
156 }