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 com.google.common.annotations.Beta;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import com.google.common.collect.Interner;
14 import com.google.common.collect.Interners;
15 import java.util.regex.Pattern;
16 import javax.annotation.RegEx;
17 import org.opendaylight.yangtools.concepts.Identifier;
18 import org.opendaylight.yangtools.concepts.Immutable;
19 import org.opendaylight.yangtools.concepts.SemVer;
20 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
21 import org.opendaylight.yangtools.yang.common.YangConstants;
24 * Base class of YANG Schema source identifiers.
26 * Source identifiers are designated to be carry only necessary information to
27 * look-up YANG model source and to be used by various SchemaSourceProviders.
29 * (For further reference see: http://tools.ietf.org/html/rfc6020#section-5.2
30 * and http://tools.ietf.org/html/rfc6022#section-3.1 ).
33 public abstract class SourceIdentifier implements Identifier, Immutable {
35 * Default revision for sources without specified revision. This should be used for comparisons
36 * based on revision when a SourceIdentifier does not have a revision.
38 public static final String NOT_PRESENT_FORMATTED_REVISION = "0000-00-00";
41 private static final String REVISION_PATTERN_STR = "\\d\\d\\d\\d-\\d\\d-\\d\\d";
44 * Simplified compiled revision pattern in format YYYY-mm-dd, which checks
45 * only distribution of number elements.
47 * For checking if supplied string is real date, use
48 * {@link SimpleDateFormatUtil} instead.
50 public static final Pattern REVISION_PATTERN = Pattern.compile(REVISION_PATTERN_STR);
52 private static final Interner<SourceIdentifier> INTERNER = Interners.newWeakInterner();
54 private static final long serialVersionUID = 1L;
55 private final String revision;
56 private final String name;
60 * Creates new YANG Schema source identifier for sources without revision.
65 SourceIdentifier(final String name) {
66 this.name = Preconditions.checkNotNull(name);
71 * Creates new YANG Schema source identifier.
75 * @param formattedRevision
76 * Revision of source in format YYYY-mm-dd
78 SourceIdentifier(final String name, final String formattedRevision) {
79 this.name = Preconditions.checkNotNull(name);
80 this.revision = Preconditions.checkNotNull(formattedRevision);
84 * Creates new YANG Schema source identifier.
88 * @param formattedRevision
89 * Revision of source in format YYYY-mm-dd. If not present,
90 * default value will be used.
92 SourceIdentifier(final String name, final Optional<String> formattedRevision) {
93 this.name = Preconditions.checkNotNull(name);
94 this.revision = formattedRevision.orNull();
98 * Return an interned reference to a equivalent SemVerSourceIdentifier.
100 * @return Interned reference, or this object if it was interned.
102 public SourceIdentifier intern() {
103 return INTERNER.intern(this);
111 public String getName() {
116 * Returns revision of source or null if revision was not supplied.
118 * @return revision of source or null if revision was not supplied.
120 // FIXME: version 2.0.0: this should return Optional<String>
121 public String getRevision() {
127 * Since we've got two ways of model versioning (revision & semantic version),
128 * this method shouldn't be called directly anymore. Eventually, callers of this method
129 * should be notified before method gets deleted.
130 * @deprecated use either
132 * <li>{@link SemVerSourceIdentifier#create(String, SemVer)}</li>
133 * <li>{@link SemVerSourceIdentifier#create(String, Optional, SemVer)}</li>
134 * <li>{@link SemVerSourceIdentifier#create(String, String, SemVer)}</li>
138 * <li>{@link RevisionSourceIdentifier#create(String)}</li>
139 * <li>{@link RevisionSourceIdentifier#create(String, String)}</li>
140 * <li>{@link RevisionSourceIdentifier#create(String, Optional)}</li>
147 * Revision of source in format YYYY-mm-dd. If not present,
148 * default value will be used.
150 * @return particular SourceIdentifier instance
154 public static SourceIdentifier create(final String moduleName, final Optional<String> revision) {
155 return new RevisionSourceIdentifier(moduleName, revision);
159 * Returns filename for this YANG module as specified in RFC 6020.
161 * Returns filename in format <code>name ['@' revision] '.yang'</code>
163 * Where revision is date in format YYYY-mm-dd.
166 * @see <a href="http://tools.ietf.org/html/rfc6020#section-5.2">RFC6020</a>
168 * @return Filename for this source identifier.
170 public String toYangFilename() {
171 final String rev = NOT_PRESENT_FORMATTED_REVISION.equals(revision) ? null : revision;
172 return toYangFileName(name, Optional.fromNullable(rev));
176 * Returns filename for this YANG module as specified in RFC 6020.
178 * Returns filename in format <code>moduleName ['@' revision] '.yang'</code>
180 * Where Where revision-date is in format YYYY-mm-dd.
183 * See http://tools.ietf.org/html/rfc6020#section-5.2
185 * @return Filename for this source identifier.
187 public static String toYangFileName(final String moduleName, final Optional<String> revision) {
188 StringBuilder filename = new StringBuilder(moduleName);
189 if (revision.isPresent()) {
190 filename.append('@');
191 filename.append(revision.get());
193 filename.append(YangConstants.RFC6020_YANG_FILE_EXTENSION);
194 return filename.toString();