274012874c469f946d07a8d35c0be27010477f02
[yangtools.git] / yang / yang-repo-api / src / main / java / org / opendaylight / yangtools / yang / model / repo / api / SourceIdentifier.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.model.repo.api;
9
10 import static java.util.Objects.requireNonNull;
11
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;
22
23 /**
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.
26  *
27  * <p>
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>.
30  */
31 @Beta
32 public abstract class SourceIdentifier implements Identifier, Immutable {
33     private static final Interner<SourceIdentifier> INTERNER = Interners.newWeakInterner();
34     private static final long serialVersionUID = 2L;
35
36     private final @Nullable Revision revision;
37     private final @NonNull String name;
38
39     /**
40      * Creates new YANG Schema source identifier for sources without revision.
41      *
42      * @param name Name of schema
43      */
44     SourceIdentifier(final String name) {
45         this(name, (Revision) null);
46     }
47
48     /**
49      * Creates new YANG Schema source identifier.
50      *
51      * @param name Name of schema
52      * @param revision Revision of source, may be null
53      */
54     SourceIdentifier(final String name, final @Nullable Revision revision) {
55         this.name = requireNonNull(name);
56         this.revision = revision;
57     }
58
59     /**
60      * Creates new YANG Schema source identifier.
61      *
62      * @param name Name of schema
63      * @param revision Revision of source, possibly not present
64      */
65     SourceIdentifier(final String name, final Optional<Revision> revision) {
66         this(name, revision.orElse(null));
67     }
68
69     /**
70      * Return an interned reference to an equivalent SourceIdentifier.
71      *
72      * @return Interned reference, or this object if it was interned.
73      */
74     public @NonNull SourceIdentifier intern() {
75         return INTERNER.intern(this);
76     }
77
78     /**
79      * Returns model name.
80      *
81      * @return model name
82      */
83     public @NonNull String getName() {
84         return name;
85     }
86
87     /**
88      * Returns revision of source or null if revision was not supplied.
89      *
90      * @return revision of source or null if revision was not supplied.
91      */
92     public Optional<Revision> getRevision() {
93         return Optional.ofNullable(revision);
94     }
95
96     /**
97      * Returns filename for this YANG module as specified in RFC 6020.
98      *
99      * <p>
100      * Returns filename in format <code>name ['@' revision] '.yang'</code>, where revision is date in format YYYY-mm-dd.
101      *
102      * <p>
103      * @see <a href="http://tools.ietf.org/html/rfc6020#section-5.2">RFC6020</a>
104      *
105      * @return Filename for this source identifier.
106      */
107     public @NonNull String toYangFilename() {
108         return toYangFileName(name, Optional.ofNullable(revision));
109     }
110
111     /**
112      * Returns filename for this YANG module as specified in RFC 6020.
113      *
114      * <p>
115      * Returns filename in format <code>moduleName ['@' revision] '.yang'</code>,
116      * where Where revision-date is in format YYYY-mm-dd.
117      *
118      * <p>
119      * See http://tools.ietf.org/html/rfc6020#section-5.2
120      *
121      * @param moduleName module name
122      * @param revision optional revision
123      * @return Filename for this source identifier.
124      */
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());
129         }
130         return sb.append(YangConstants.RFC6020_YANG_FILE_EXTENSION).toString();
131     }
132 }