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 com.google.common.annotations.Beta;
13 import com.google.common.collect.Interner;
14 import com.google.common.collect.Interners;
15 import java.util.Optional;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.eclipse.jdt.annotation.Nullable;
18 import org.opendaylight.yangtools.concepts.Identifier;
19 import org.opendaylight.yangtools.concepts.Immutable;
20 import org.opendaylight.yangtools.yang.common.Revision;
21 import org.opendaylight.yangtools.yang.common.YangConstants;
24 * Base class of YANG Schema source identifiers. Source identifiers are designated to be carry only necessary
25 * information to look up YANG module (or submodule) source and to be used by various SchemaSourceProviders.
28 * For further reference see: <a href="https://tools.ietf.org/html/rfc6020#section-5.2">RFC6020</a>
29 * and <a href="https://tools.ietf.org/html/rfc6022#section-3.1">RFC6022</a>.
32 public abstract class SourceIdentifier implements Identifier, Immutable {
33 private static final Interner<SourceIdentifier> INTERNER = Interners.newWeakInterner();
34 private static final long serialVersionUID = 2L;
36 private final @Nullable Revision revision;
37 private final @NonNull String name;
40 * Creates new YANG Schema source identifier for sources without revision.
42 * @param name Name of schema
44 SourceIdentifier(final String name) {
45 this(name, (Revision) null);
49 * Creates new YANG Schema source identifier.
51 * @param name Name of schema
52 * @param revision Revision of source, may be null
54 SourceIdentifier(final String name, final @Nullable Revision revision) {
55 this.name = requireNonNull(name);
56 this.revision = revision;
60 * Creates new YANG Schema source identifier.
62 * @param name Name of schema
63 * @param revision Revision of source, possibly not present
65 SourceIdentifier(final String name, final Optional<Revision> revision) {
66 this(name, revision.orElse(null));
70 * Return an interned reference to a equivalent SemVerSourceIdentifier.
72 * @return Interned reference, or this object if it was interned.
74 public @NonNull SourceIdentifier intern() {
75 return INTERNER.intern(this);
83 public @NonNull String getName() {
88 * Returns revision of source or null if revision was not supplied.
90 * @return revision of source or null if revision was not supplied.
92 public Optional<Revision> getRevision() {
93 return Optional.ofNullable(revision);
97 * Returns filename for this YANG module as specified in RFC 6020.
100 * Returns filename in format <code>name ['@' revision] '.yang'</code>, where revision is date in format YYYY-mm-dd.
103 * @see <a href="http://tools.ietf.org/html/rfc6020#section-5.2">RFC6020</a>
105 * @return Filename for this source identifier.
107 public @NonNull String toYangFilename() {
108 return toYangFileName(name, Optional.ofNullable(revision));
112 * Returns filename for this YANG module as specified in RFC 6020.
115 * Returns filename in format <code>moduleName ['@' revision] '.yang'</code>,
116 * where Where revision-date is in format YYYY-mm-dd.
119 * See http://tools.ietf.org/html/rfc6020#section-5.2
121 * @param moduleName module name
122 * @param revision optional revision
123 * @return Filename for this source identifier.
125 public static @NonNull String toYangFileName(final String moduleName, final Optional<Revision> revision) {
126 final StringBuilder sb = new StringBuilder(moduleName);
127 if (revision.isPresent()) {
128 sb.append('@').append(revision.orElseThrow());
130 return sb.append(YangConstants.RFC6020_YANG_FILE_EXTENSION).toString();