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.api;
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 java.util.Objects;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.opendaylight.yangtools.concepts.Immutable;
22 import org.opendaylight.yangtools.yang.common.UnresolvedQName;
23 import org.opendaylight.yangtools.yang.common.UnresolvedQName.Unqualified;
26 * Intermediate representation of a parsed {@code api-path} string as defined in
27 * <a href="https://datatracker.ietf.org/doc/html/rfc8040#section-3.5.3.1">RFC section 3.5.3.1</a>. It models the
28 * path as a series of {@link Step}s.
32 public final class ApiPath implements Immutable {
34 * A single step in an {@link ApiPath}.
36 public abstract static class Step implements Immutable {
37 private final @Nullable String module;
38 private final Unqualified identifier;
40 Step(final @Nullable String module, final String identifier) {
41 this.identifier = verifyNotNull(UnresolvedQName.tryLocalName(identifier),
42 "Unexpected invalid identifier %s", identifier);
46 public Unqualified identifier() {
50 public @Nullable String module() {
55 public abstract int hashCode();
58 public abstract boolean equals(@Nullable Object obj);
60 final boolean equals(final Step other) {
61 return Objects.equals(module, other.module) && identifier.equals(other.identifier);
65 public final String toString() {
66 return addToStringAttributes(MoreObjects.toStringHelper(this).omitNullValues()).toString();
69 ToStringHelper addToStringAttributes(final ToStringHelper helper) {
70 return helper.add("module", module).add("identifier", identifier);
75 * An {@code api-identifier} step in a {@link ApiPath}.
77 public static final class ApiIdentifier extends Step {
78 public ApiIdentifier(final @Nullable String module, final String identifier) {
79 super(module, identifier);
83 public int hashCode() {
84 return Objects.hash(module(), identifier());
88 public boolean equals(final @Nullable Object obj) {
89 return this == obj || obj instanceof ApiIdentifier other && equals(other);
94 * A {@code list-instance} step in a {@link ApiPath}.
96 public static final class ListInstance extends Step {
97 private final ImmutableList<String> keyValues;
99 ListInstance(final @Nullable String module, final String identifier, final ImmutableList<String> keyValues) {
100 super(module, identifier);
101 this.keyValues = requireNonNull(keyValues);
104 public ImmutableList<String> keyValues() {
109 public int hashCode() {
110 return Objects.hash(module(), identifier(), keyValues);
114 public boolean equals(final @Nullable Object obj) {
115 return this == obj || obj instanceof ListInstance other && equals(other)
116 && keyValues.equals(other.keyValues);
120 ToStringHelper addToStringAttributes(final ToStringHelper helper) {
121 return super.addToStringAttributes(helper).add("keyValues", keyValues);
125 private static final ApiPath EMPTY = new ApiPath(ImmutableList.of());
127 private final ImmutableList<Step> steps;
129 private ApiPath(final ImmutableList<Step> steps) {
130 this.steps = requireNonNull(steps);
134 * Return an empty ApiPath.
136 * @return An empty ApiPath.
138 public static ApiPath empty() {
143 * Parse an {@link ApiPath} from a raw Request URI fragment or another source. The string is expected to contain
144 * percent-encoded bytes. Any sequence of such bytes is interpreted as a {@code UTF-8}-encoded string. Invalid
145 * sequences are rejected.
147 * @param str Request URI part
148 * @return An {@link ApiPath}
149 * @throws NullPointerException if {@code str} is {@code null}
150 * @throws ParseException if the string cannot be parsed
152 public static ApiPath parse(final String str) throws ParseException {
153 return str.isEmpty() ? EMPTY : parseString(ApiPathParser.newStrict(), str);
157 * Parse an {@link ApiPath} from a raw Request URI fragment. The string is expected to contain percent-encoded
158 * bytes. Any sequence of such bytes is interpreted as a {@code UTF-8}-encoded string. Invalid sequences are
159 * rejected, but consecutive slashes may be tolerated, depending on runtime configuration.
161 * @param str Request URI part
162 * @return An {@link ApiPath}
163 * @throws NullPointerException if {@code str} is {@code null}
164 * @throws ParseException if the string cannot be parsed
166 public static ApiPath parseUrl(final String str) throws ParseException {
167 return str.isEmpty() ? EMPTY : parseString(ApiPathParser.newUrl(), str);
170 public ImmutableList<Step> steps() {
174 public int indexOf(final String module, final String identifier) {
175 final var m = requireNonNull(module);
176 final var id = requireNonNull(identifier);
177 for (int i = 0, size = steps.size(); i < size; ++i) {
178 final var step = steps.get(i);
179 if (m.equals(step.module) && id.equals(step.identifier.getLocalName())) {
186 public ApiPath subPath(final int fromIndex) {
187 return subPath(fromIndex, steps.size());
190 public ApiPath subPath(final int fromIndex, final int toIndex) {
191 final var subList = steps.subList(fromIndex, toIndex);
192 if (subList == steps) {
194 } else if (subList.isEmpty()) {
197 return new ApiPath(subList);
202 public int hashCode() {
203 return steps.hashCode();
207 public boolean equals(final @Nullable Object obj) {
208 return obj == this || obj instanceof ApiPath other && steps.equals(other.steps());
211 private static ApiPath parseString(final ApiPathParser parser, final String str) throws ParseException {
212 final var steps = parser.parseSteps(str);
213 return steps.isEmpty() ? EMPTY : new ApiPath(steps);