2 * Copyright (c) 2014 Cisco Systems, Inc. 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.yangtools.yang.model.api.source;
10 import static java.util.Objects.requireNonNull;
12 import java.time.format.DateTimeParseException;
13 import org.eclipse.jdt.annotation.NonNull;
14 import org.eclipse.jdt.annotation.Nullable;
15 import org.opendaylight.yangtools.concepts.Identifier;
16 import org.opendaylight.yangtools.yang.common.Revision;
17 import org.opendaylight.yangtools.yang.common.UnresolvedQName.Unqualified;
18 import org.opendaylight.yangtools.yang.common.YangConstants;
21 * Base class of YANG Schema source identifiers. Source identifiers are designated to be carry only necessary
22 * information to look up YANG module (or submodule) source and to be used by various SchemaSourceProviders.
25 * For further reference see: <a href="https://www.rfc-editor.org/rfc/rfc6020#section-5.2">RFC6020</a>
26 * and <a href="https://www.rfc-editor.org/rfc/rfc6022#section-3.1">RFC6022</a>.
28 public record SourceIdentifier(@NonNull Unqualified name, @Nullable Revision revision) implements Identifier {
29 private static final long serialVersionUID = 3L;
32 * Creates new YANG Schema source identifier for sources with or without a revision.
34 * @param name Name of schema
35 * @param revision Revision of schema
36 * @throws NullPointerException if {@code name} is null
38 public SourceIdentifier {
43 * Creates new YANG Schema source identifier for sources without a revision.
45 * @param name Name of schema
46 * @throws NullPointerException if {@code name} is null
48 public SourceIdentifier(final @NonNull Unqualified name) {
53 * Creates new YANG Schema source identifier for sources without a revision.
55 * @param name Name of schema
56 * @throws NullPointerException if {@code name} is null
57 * @throws IllegalArgumentException if {@code name} is not a valid YANG identifier
59 public SourceIdentifier(final @NonNull String name) {
60 this(Unqualified.of(name));
64 * Creates new YANG Schema source identifier for sources with or without a revision.
66 * @param name Name of schema
67 * @param revision Optional schema revision
68 * @throws NullPointerException if {@code name} is null
69 * @throws IllegalArgumentException if {@code name} is not a valid YANG identifier
71 public SourceIdentifier(final @NonNull String name, final @Nullable Revision revision) {
72 this(Unqualified.of(name), revision);
76 * Creates new YANG Schema source identifier for sources with or without a revision.
78 * @param name Name of schema
79 * @param revision Optional schema revision
80 * @throws NullPointerException if {@code name} is null
81 * @throws IllegalArgumentException if {@code name} is not a valid YANG identifier
82 * @throws DateTimeParseException if {@code revision} format does not conform specification.
84 public SourceIdentifier(final @NonNull String name, final @Nullable String revision) {
85 this(name, revision != null ? Revision.of(revision) : null);
89 * Returns filename for this YANG module as specified in RFC 6020.
92 * Returns filename in format <code>name ['@' revision] '.yang'</code>, where revision is date in format YYYY-mm-dd.
95 * @see <a href="http://www.rfc-editor.org/rfc/rfc6020#section-5.2">RFC6020</a>
97 * @return Filename for this source identifier.
99 public @NonNull String toYangFilename() {
100 return toYangFileName(name.getLocalName(), revision);
104 public String toString() {
105 final var sb = new StringBuilder("SourceIdentifier [").append(name.getLocalName());
106 if (revision != null) {
107 sb.append('@').append(revision);
109 return sb.append(']').toString();
113 * Returns filename for this YANG module as specified in RFC 6020.
116 * Returns filename in format <code>moduleName ['@' revision] '.yang'</code>,
117 * where Where revision-date is in format YYYY-mm-dd.
120 * See http://www.rfc-editor.org/rfc/rfc6020#section-5.2
122 * @param moduleName module name
123 * @param revision optional revision
124 * @return Filename for this source identifier.
126 public static @NonNull String toYangFileName(final @NonNull String moduleName, final @Nullable Revision revision) {
127 final StringBuilder sb = new StringBuilder(moduleName);
128 if (revision != null) {
129 sb.append('@').append(revision);
131 return sb.append(YangConstants.RFC6020_YANG_FILE_EXTENSION).toString();