2 * Copyright (c) 2021 PANTHEON.tech, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.restconf.nb.rfc8040;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
13 import com.google.common.annotations.Beta;
14 import com.google.common.base.MoreObjects;
15 import com.google.common.collect.ImmutableList;
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;
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>.
29 public final class FieldsParam implements RestconfQueryParam<FieldsParam> {
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()}.
34 public static final class NodeSelector implements Immutable {
35 private final ImmutableList<ApiIdentifier> path;
36 private final ImmutableList<NodeSelector> subSelectors;
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");
44 NodeSelector(final ImmutableList<ApiIdentifier> path) {
45 this(path, ImmutableList.of());
49 * Return the path to the selected node. Guaranteed to have at least one element.
51 * @return path to the selected node
53 public ImmutableList<ApiIdentifier> path() {
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.
61 * @return Selectors for nested nodes.
63 public ImmutableList<NodeSelector> subSelectors() {
68 public String toString() {
69 final var helper = MoreObjects.toStringHelper(this).add("path", path);
70 if (!subSelectors.isEmpty()) {
71 helper.add("subSelectors", subSelectors);
73 return helper.toString();
77 private static final URI CAPABILITY = URI.create("urn:ietf:params:restconf:capability:fields:1.0");
79 private final ImmutableList<NodeSelector> nodeSelectors;
80 private final String paramValue;
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);
89 * Parse a {@code fields} parameter.
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.
95 public static FieldsParam parse(final String str) throws ParseException {
96 return new FieldsParam(new FieldsParameterParser().parseNodeSelectors(str), str);
100 public Class<@NonNull FieldsParam> javaClass() {
101 return FieldsParam.class;
105 public String paramName() {
109 public static String uriName() {
113 public static URI capabilityUri() {
118 * Selectors for nodes which should be reported. Guaranteed to have at least one element.
120 * @return selectors for nodes to be reported
122 public ImmutableList<NodeSelector> nodeSelectors() {
123 return nodeSelectors;
127 public String paramValue() {
132 public String toString() {
133 return MoreObjects.toStringHelper(this).add("nodeSelectors", nodeSelectors).toString();