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.NonNullByDefault;
19 import org.opendaylight.restconf.nb.rfc8040.ApiPath.ApiIdentifier;
20 import org.opendaylight.yangtools.concepts.Immutable;
23 * This class represents a {@code fields} parameter as defined in
24 * <a href="https://datatracker.ietf.org/doc/html/rfc8040#section-4.8.3">RFC8040 section 4.8.3</a>.
28 public final class FieldsParameter implements Immutable {
30 * A selector for a single node as identified by {@link #path()}. Individual child nodes are subject to further
31 * filtering based on {@link #subSelectors()}.
33 public static final class NodeSelector implements Immutable {
34 private final ImmutableList<ApiIdentifier> path;
35 private final ImmutableList<NodeSelector> subSelectors;
37 NodeSelector(final ImmutableList<ApiIdentifier> path, final ImmutableList<NodeSelector> subSelectors) {
38 this.path = requireNonNull(path);
39 this.subSelectors = requireNonNull(subSelectors);
40 checkArgument(!path.isEmpty(), "At least path segment is required");
43 NodeSelector(final ImmutableList<ApiIdentifier> path) {
44 this(path, ImmutableList.of());
48 * Return the path to the selected node. Guaranteed to have at least one element.
50 * @return path to the selected node
52 public ImmutableList<ApiIdentifier> path() {
57 * Selectors for single nodes which should be selected from the node found by interpreting {@link #path}. If
58 * there are no selectors, i.e. {@code subSelectors().isEmpty())}, all child nodes are meant to be selected.
60 * @return Selectors for nested nodes.
62 public ImmutableList<NodeSelector> subSelectors() {
67 public String toString() {
68 final var helper = MoreObjects.toStringHelper(this).add("path", path);
69 if (!subSelectors.isEmpty()) {
70 helper.add("subSelectors", subSelectors);
72 return helper.toString();
76 private static final URI CAPABILITY = URI.create("urn:ietf:params:restconf:capability:fields:1.0");
78 private final ImmutableList<NodeSelector> nodeSelectors;
80 FieldsParameter(final ImmutableList<NodeSelector> nodeSelectors) {
81 this.nodeSelectors = requireNonNull(nodeSelectors);
82 checkArgument(!nodeSelectors.isEmpty(), "At least one selector is required");
86 * Parse a {@code fields} parameter.
88 * @param str Unescaped URL string
89 * @return The contents of parameter
90 * @throws ParseException if {@code str} does not represent a valid {@code fields} parameter.
92 public static FieldsParameter parse(final String str) throws ParseException {
93 return new FieldsParameterParser().parse(str);
96 public static String uriName() {
100 public static URI capabilityUri() {
105 * Selectors for nodes which should be reported. Guaranteed to have at least one element.
107 * @return selectors for nodes to be reported
109 public ImmutableList<NodeSelector> nodeSelectors() {
110 return nodeSelectors;
114 public String toString() {
115 return MoreObjects.toStringHelper(this).add("nodeSelectors", nodeSelectors).toString();