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.Verify.verifyNotNull;
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.base.MoreObjects.ToStringHelper;
16 import com.google.common.collect.ImmutableList;
17 import java.text.ParseException;
18 import javax.ws.rs.PathParam;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
22 import org.opendaylight.yangtools.concepts.Immutable;
23 import org.opendaylight.yangtools.yang.common.ErrorTag;
24 import org.opendaylight.yangtools.yang.common.ErrorType;
25 import org.opendaylight.yangtools.yang.common.UnqualifiedQName;
28 * Intermediate representation of a parsed {@code api-path} string as defined in
29 * <a href="https://datatracker.ietf.org/doc/html/rfc8040#section-3.5.3.1">RFC section 3.5.3.1</a>. It models the
30 * path as a series of {@link Step}s.
34 public final class ApiPath implements Immutable {
36 * A single step in an {@link ApiPath}.
38 public abstract static class Step implements Immutable {
39 private final @Nullable String module;
40 private final UnqualifiedQName identifier;
42 Step(final @Nullable String module, final String identifier) {
43 this.identifier = verifyNotNull(UnqualifiedQName.tryCreate(identifier), "Unexpected invalid identifier %s",
48 public UnqualifiedQName identifier() {
52 public @Nullable String module() {
57 public final String toString() {
58 return addToStringAttributes(MoreObjects.toStringHelper(this).omitNullValues()).toString();
61 ToStringHelper addToStringAttributes(final ToStringHelper helper) {
62 return helper.add("module", module).add("identifier", identifier);
67 * An {@code api-identifier} step in a {@link ApiPath}.
69 public static final class ApiIdentifier extends Step {
70 ApiIdentifier(final @Nullable String module, final String identifier) {
71 super(module, identifier);
76 * A {@code list-instance} step in a {@link ApiPath}.
78 public static final class ListInstance extends Step {
79 private final ImmutableList<String> keyValues;
81 ListInstance(final @Nullable String module, final String identifier, final ImmutableList<String> keyValues) {
82 super(module, identifier);
83 this.keyValues = requireNonNull(keyValues);
86 public ImmutableList<String> keyValues() {
91 ToStringHelper addToStringAttributes(final ToStringHelper helper) {
92 return super.addToStringAttributes(helper).add("keyValues", keyValues);
96 private static final ApiPath EMPTY = new ApiPath(ImmutableList.of());
98 private final ImmutableList<Step> steps;
100 private ApiPath(final ImmutableList<Step> steps) {
101 this.steps = requireNonNull(steps);
105 * Return an empty ApiPath.
107 * @return An empty ApiPath.
109 public static ApiPath empty() {
114 * Parse an {@link ApiPath} from a raw Request URI fragment or another source. The string is expected to contain
115 * percent-encoded bytes. Any sequence of such bytes is interpreted as a {@code UTF-8}-encoded string. Invalid
116 * sequences are rejected.
118 * @param str Request URI part
119 * @return An {@link ApiPath}
120 * @throws NullPointerException if {@code str} is {@code null}
121 * @throws ParseException if the string cannot be parsed
123 public static ApiPath parse(final String str) throws ParseException {
124 return str.isEmpty() ? EMPTY : parseString(ApiPathParser.newStrict(), str);
128 * Parse an {@link ApiPath} from a raw Request URI fragment. The string is expected to contain percent-encoded
129 * bytes. Any sequence of such bytes is interpreted as a {@code UTF-8}-encoded string. Invalid sequences are
130 * rejected, but consecutive slashes may be tolerated, depending on runtime configuration.
132 * @param str Request URI part
133 * @return An {@link ApiPath}
134 * @throws NullPointerException if {@code str} is {@code null}
135 * @throws ParseException if the string cannot be parsed
137 public static ApiPath parseUrl(final String str) throws ParseException {
138 return str.isEmpty() ? EMPTY : parseString(ApiPathParser.newUrl(), str);
142 * Parse an {@link ApiPath} from a raw Request URI. The string is expected to contain percent-encoded bytes. Any
143 * sequence of such bytes is interpreted as a {@code UTF-8}-encoded string. Invalid sequences are rejected.
145 * @param str Request URI part
146 * @return An {@link ApiPath}
147 * @throws RestconfDocumentedException if the string cannot be parsed
150 public static ApiPath valueOf(final @Nullable String str) {
156 return parseUrl(str);
157 } catch (ParseException e) {
158 throw new RestconfDocumentedException("Invalid path '" + str + "'", ErrorType.APPLICATION,
159 ErrorTag.MALFORMED_MESSAGE, e);
163 public ImmutableList<Step> steps() {
167 public int indexOf(final String module, final String identifier) {
168 final var m = requireNonNull(module);
169 final var id = requireNonNull(identifier);
170 for (int i = 0, size = steps.size(); i < size; ++i) {
171 final var step = steps.get(i);
172 if (m.equals(step.module) && id.equals(step.identifier.getLocalName())) {
179 public ApiPath subPath(final int fromIndex) {
180 return subPath(fromIndex, steps.size());
183 public ApiPath subPath(final int fromIndex, final int toIndex) {
184 final var subList = steps.subList(fromIndex, toIndex);
185 if (subList == steps) {
187 } else if (subList.isEmpty()) {
190 return new ApiPath(subList);
195 public int hashCode() {
196 return steps.hashCode();
200 public boolean equals(final @Nullable Object obj) {
201 return obj == this || obj instanceof ApiPath && steps.equals(((ApiPath) obj).steps());
204 private static ApiPath parseString(final ApiPathParser parser, final String str) throws ParseException {
205 final var steps = parser.parseSteps(str);
206 return steps.isEmpty() ? EMPTY : new ApiPath(steps);