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.repo.api;
10 import static java.util.Objects.requireNonNull;
12 import org.eclipse.jdt.annotation.NonNull;
13 import org.eclipse.jdt.annotation.Nullable;
14 import org.opendaylight.yangtools.concepts.Identifier;
15 import org.opendaylight.yangtools.yang.common.Revision;
16 import org.opendaylight.yangtools.yang.common.UnresolvedQName.Unqualified;
17 import org.opendaylight.yangtools.yang.common.YangConstants;
20 * Base class of YANG Schema source identifiers. Source identifiers are designated to be carry only necessary
21 * information to look up YANG module (or submodule) source and to be used by various SchemaSourceProviders.
24 * For further reference see: <a href="https://tools.ietf.org/html/rfc6020#section-5.2">RFC6020</a>
25 * and <a href="https://tools.ietf.org/html/rfc6022#section-3.1">RFC6022</a>.
27 public record SourceIdentifier(@NonNull Unqualified name, @Nullable Revision revision) implements Identifier {
28 private static final long serialVersionUID = 3L;
31 * Creates new YANG Schema source identifier for sources with or without a revision.
33 * @param name Name of schema
34 * @param revision Revision of schema
35 * @throws NullPointerException if {@code name} is null
37 public SourceIdentifier {
42 * Creates new YANG Schema source identifier for sources without a revision.
44 * @param name Name of schema
45 * @throws NullPointerException if {@code name} is null
47 public SourceIdentifier(final @NonNull Unqualified name) {
52 * Creates new YANG Schema source identifier for sources without a revision.
54 * @param name Name of schema
55 * @throws NullPointerException if {@code name} is null
56 * @throws IllegalArgumentException if {@code name} is not a valid YANG identifier
58 public SourceIdentifier(final @NonNull String name) {
59 this(Unqualified.of(name));
63 * Creates new YANG Schema source identifier for sources with or without a revision.
65 * @param name Name of schema
66 * @throws NullPointerException if {@code name} is null
67 * @throws IllegalArgumentException if {@code name} is not a valid YANG identifier
69 public SourceIdentifier(final @NonNull String name, final @Nullable String revision) {
70 this(Unqualified.of(name), revision != null ? Revision.of(revision) : null);
74 * Returns filename for this YANG module as specified in RFC 6020.
77 * Returns filename in format <code>name ['@' revision] '.yang'</code>, where revision is date in format YYYY-mm-dd.
80 * @see <a href="http://tools.ietf.org/html/rfc6020#section-5.2">RFC6020</a>
82 * @return Filename for this source identifier.
84 public @NonNull String toYangFilename() {
85 return toYangFileName(name.getLocalName(), revision);
89 public String toString() {
90 final var sb = new StringBuilder("SourceIdentifier [").append(name.getLocalName());
91 if (revision != null) {
92 sb.append('@').append(revision);
94 return sb.append(']').toString();
98 * Returns filename for this YANG module as specified in RFC 6020.
101 * Returns filename in format <code>moduleName ['@' revision] '.yang'</code>,
102 * where Where revision-date is in format YYYY-mm-dd.
105 * See http://tools.ietf.org/html/rfc6020#section-5.2
107 * @param moduleName module name
108 * @param revision optional revision
109 * @return Filename for this source identifier.
111 public static @NonNull String toYangFileName(final @NonNull String moduleName, final @Nullable Revision revision) {
112 final StringBuilder sb = new StringBuilder(moduleName);
113 if (revision != null) {
114 sb.append('@').append(revision);
116 return sb.append(YangConstants.RFC6020_YANG_FILE_EXTENSION).toString();