Expose parameter names as String constants
[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     // API consistency: must not be confused with enum constants
78     @SuppressWarnings("checkstyle:ConstantName")
79     public static final String uriName = "fields";
80     private static final URI CAPABILITY = URI.create("urn:ietf:params:restconf:capability:fields:1.0");
81
82     private final ImmutableList<NodeSelector> nodeSelectors;
83     private final String paramValue;
84
85     private FieldsParam(final ImmutableList<NodeSelector> nodeSelectors, final String uriValue) {
86         this.nodeSelectors = requireNonNull(nodeSelectors);
87         checkArgument(!nodeSelectors.isEmpty(), "At least one selector is required");
88         paramValue = requireNonNull(uriValue);
89     }
90
91     /**
92      * Parse a {@code fields} parameter.
93      *
94      * @param str Unescaped URL string
95      * @return The contents of parameter
96      * @throws ParseException if {@code str} does not represent a valid {@code fields} parameter.
97      */
98     public static FieldsParam parse(final String str) throws ParseException {
99         return new FieldsParam(new FieldsParameterParser().parseNodeSelectors(str), str);
100     }
101
102     @Override
103     public Class<@NonNull FieldsParam> javaClass() {
104         return FieldsParam.class;
105     }
106
107     @Override
108     public String paramName() {
109         return uriName;
110     }
111
112     public static URI capabilityUri() {
113         return CAPABILITY;
114     }
115
116     /**
117      * Selectors for nodes which should be reported. Guaranteed to have at least one element.
118      *
119      * @return selectors for nodes to be reported
120      */
121     public ImmutableList<NodeSelector> nodeSelectors() {
122         return nodeSelectors;
123     }
124
125     @Override
126     public String paramValue() {
127         return paramValue;
128     }
129
130     @Override
131     public String toString() {
132         return MoreObjects.toStringHelper(this).add("nodeSelectors", nodeSelectors).toString();
133     }
134 }