Separate out ReadDataParams
[netconf.git] / restconf / restconf-nb-rfc8040 / src / main / java / org / opendaylight / restconf / nb / rfc8040 / FieldsParam.java
1 /*
2  * Copyright (c) 2021 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.restconf.nb.rfc8040;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
12
13 import com.google.common.annotations.Beta;
14 import com.google.common.base.MoreObjects;
15 import com.google.common.collect.ImmutableList;
16 import java.net.URI;
17 import java.text.ParseException;
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.opendaylight.restconf.nb.rfc8040.ApiPath.ApiIdentifier;
21 import org.opendaylight.yangtools.concepts.Immutable;
22
23 /**
24  * This class represents a {@code fields} parameter as defined in
25  * <a href="https://datatracker.ietf.org/doc/html/rfc8040#section-4.8.3">RFC8040 section 4.8.3</a>.
26  */
27 @Beta
28 @NonNullByDefault
29 public final class FieldsParam implements RestconfQueryParam<FieldsParam> {
30     /**
31      * A selector for a single node as identified by {@link #path()}. Individual child nodes are subject to further
32      * filtering based on {@link #subSelectors()}.
33      */
34     public static final class NodeSelector implements Immutable {
35         private final ImmutableList<ApiIdentifier> path;
36         private final ImmutableList<NodeSelector> subSelectors;
37
38         NodeSelector(final ImmutableList<ApiIdentifier> path, final ImmutableList<NodeSelector> subSelectors) {
39             this.path = requireNonNull(path);
40             this.subSelectors = requireNonNull(subSelectors);
41             checkArgument(!path.isEmpty(), "At least path segment is required");
42         }
43
44         NodeSelector(final ImmutableList<ApiIdentifier> path) {
45             this(path, ImmutableList.of());
46         }
47
48         /**
49          * Return the path to the selected node. Guaranteed to have at least one element.
50          *
51          * @return path to the selected node
52          */
53         public ImmutableList<ApiIdentifier> path() {
54             return path;
55         }
56
57         /**
58          * Selectors for single nodes which should be selected from the node found by interpreting {@link #path}. If
59          * there are no selectors, i.e. {@code subSelectors().isEmpty())}, all child nodes are meant to be selected.
60          *
61          * @return Selectors for nested nodes.
62          */
63         public ImmutableList<NodeSelector> subSelectors() {
64             return subSelectors;
65         }
66
67         @Override
68         public String toString() {
69             final var helper = MoreObjects.toStringHelper(this).add("path", path);
70             if (!subSelectors.isEmpty()) {
71                 helper.add("subSelectors", subSelectors);
72             }
73             return helper.toString();
74         }
75     }
76
77     private static final URI CAPABILITY = URI.create("urn:ietf:params:restconf:capability:fields:1.0");
78
79     private final ImmutableList<NodeSelector> nodeSelectors;
80     private final String paramValue;
81
82     private FieldsParam(final ImmutableList<NodeSelector> nodeSelectors, final String uriValue) {
83         this.nodeSelectors = requireNonNull(nodeSelectors);
84         checkArgument(!nodeSelectors.isEmpty(), "At least one selector is required");
85         this.paramValue = requireNonNull(uriValue);
86     }
87
88     /**
89      * Parse a {@code fields} parameter.
90      *
91      * @param str Unescaped URL string
92      * @return The contents of parameter
93      * @throws ParseException if {@code str} does not represent a valid {@code fields} parameter.
94      */
95     public static FieldsParam parse(final String str) throws ParseException {
96         return new FieldsParam(new FieldsParameterParser().parseNodeSelectors(str), str);
97     }
98
99     @Override
100     public Class<@NonNull FieldsParam> javaClass() {
101         return FieldsParam.class;
102     }
103
104     @Override
105     public String paramName() {
106         return uriName();
107     }
108
109     public static String uriName() {
110         return "fields";
111     }
112
113     public static URI capabilityUri() {
114         return CAPABILITY;
115     }
116
117     /**
118      * Selectors for nodes which should be reported. Guaranteed to have at least one element.
119      *
120      * @return selectors for nodes to be reported
121      */
122     public ImmutableList<NodeSelector> nodeSelectors() {
123         return nodeSelectors;
124     }
125
126     @Override
127     public String paramValue() {
128         return paramValue;
129     }
130
131     @Override
132     public String toString() {
133         return MoreObjects.toStringHelper(this).add("nodeSelectors", nodeSelectors).toString();
134     }
135 }