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 {
30 private static final long serialVersionUID = 3L;
33 * Creates new YANG Schema source identifier for sources with or without a revision.
35 * @param name Name of schema
36 * @param revision Revision of schema
37 * @throws NullPointerException if {@code name} is null
39 public SourceIdentifier {
44 * Creates new YANG Schema source identifier for sources without a revision.
46 * @param name Name of schema
47 * @throws NullPointerException if {@code name} is null
49 public SourceIdentifier(final @NonNull Unqualified name) {
54 * Creates new YANG Schema source identifier for sources without a revision.
56 * @param name Name of schema
57 * @throws NullPointerException if {@code name} is null
58 * @throws IllegalArgumentException if {@code name} is not a valid YANG identifier
60 public SourceIdentifier(final @NonNull String name) {
61 this(Unqualified.of(name));
65 * Creates new YANG Schema source identifier for sources with or without a revision.
67 * @param name Name of schema
68 * @param revision Optional schema revision
69 * @throws NullPointerException if {@code name} is null
70 * @throws IllegalArgumentException if {@code name} is not a valid YANG identifier
72 public SourceIdentifier(final @NonNull String name, final @Nullable Revision revision) {
73 this(Unqualified.of(name), revision);
77 * Creates new YANG Schema source identifier for sources with or without a revision.
79 * @param name Name of schema
80 * @param revision Optional schema revision
81 * @throws NullPointerException if {@code name} is null
82 * @throws IllegalArgumentException if {@code name} is not a valid YANG identifier
83 * @throws DateTimeParseException if {@code revision} format does not conform specification.
85 public SourceIdentifier(final @NonNull String name, final @Nullable String revision) {
86 this(name, revision != null ? Revision.of(revision) : null);
90 * Returns filename for this YANG module as specified in RFC 6020.
93 * Returns filename in format <code>name ['@' revision] '.yang'</code>, where revision is date in format YYYY-mm-dd.
96 * @see <a href="http://www.rfc-editor.org/rfc/rfc6020#section-5.2">RFC6020</a>
98 * @return Filename for this source identifier.
100 public @NonNull String toYangFilename() {
101 return toYangFileName(name.getLocalName(), revision);
105 public String toString() {
106 final var sb = new StringBuilder("SourceIdentifier [").append(name.getLocalName());
107 if (revision != null) {
108 sb.append('@').append(revision);
110 return sb.append(']').toString();
114 * Returns filename for this YANG module as specified in RFC 6020.
117 * Returns filename in format <code>moduleName ['@' revision] '.yang'</code>,
118 * where Where revision-date is in format YYYY-mm-dd.
121 * See http://www.rfc-editor.org/rfc/rfc6020#section-5.2
123 * @param moduleName module name
124 * @param revision optional revision
125 * @return Filename for this source identifier.
127 public static @NonNull String toYangFileName(final @NonNull String moduleName, final @Nullable Revision revision) {
128 final var sb = new StringBuilder(moduleName);
129 if (revision != null) {
130 sb.append('@').append(revision);
132 return sb.append(YangConstants.RFC6020_YANG_FILE_EXTENSION).toString();