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