40c0751167568302296bd2b2e459c7fb01b26f74
[yangtools.git] / yang / yang-model-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 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;
22
23 /**
24  * Base class of YANG Schema source identifiers.
25  *
26  * <p>
27  * Source identifiers are designated to be carry only necessary information to
28  * look-up YANG model source and to be used by various SchemaSourceProviders.
29  *
30  * <p>
31  * (For further reference see: http://tools.ietf.org/html/rfc6020#section-5.2
32  * and http://tools.ietf.org/html/rfc6022#section-3.1 ).
33  */
34 @Beta
35 public abstract class SourceIdentifier implements Identifier, Immutable {
36     /**
37      * Default revision for sources without specified revision. This should be used for comparisons
38      * based on revision when a SourceIdentifier does not have a revision.
39      */
40     public static final String NOT_PRESENT_FORMATTED_REVISION = "0000-00-00";
41
42     @RegEx
43     private static final String REVISION_PATTERN_STR = "\\d\\d\\d\\d-\\d\\d-\\d\\d";
44
45     /**
46      * Simplified compiled revision pattern in format YYYY-mm-dd, which checks
47      * only distribution of number elements.
48      *
49      * <p>
50      * For checking if supplied string is real date, use {@link SimpleDateFormatUtil} instead.
51      */
52     public static final Pattern REVISION_PATTERN = Pattern.compile(REVISION_PATTERN_STR);
53
54     private static final Interner<SourceIdentifier> INTERNER = Interners.newWeakInterner();
55
56     private static final long serialVersionUID = 1L;
57     private final String revision;
58     private final String name;
59
60     /**
61      * Creates new YANG Schema source identifier for sources without revision.
62      *
63      * @param name
64      *            Name of schema
65      */
66     SourceIdentifier(final String name) {
67         this.name = Preconditions.checkNotNull(name);
68         this.revision = null;
69     }
70
71     /**
72      * Creates new YANG Schema source identifier.
73      *
74      * @param name
75      *            Name of schema
76      * @param formattedRevision
77      *            Revision of source in format YYYY-mm-dd
78      */
79     SourceIdentifier(final String name, final String formattedRevision) {
80         this.name = Preconditions.checkNotNull(name);
81         this.revision = Preconditions.checkNotNull(formattedRevision);
82     }
83
84     /**
85      * Creates new YANG Schema source identifier.
86      *
87      * @param name
88      *            Name of schema
89      * @param formattedRevision
90      *            Revision of source in format YYYY-mm-dd. If not present,
91      *            default value will be used.
92      */
93     SourceIdentifier(final String name, final Optional<String> formattedRevision) {
94         this.name = Preconditions.checkNotNull(name);
95         this.revision = formattedRevision.orNull();
96     }
97
98     /**
99      * Return an interned reference to a equivalent SemVerSourceIdentifier.
100      *
101      * @return Interned reference, or this object if it was interned.
102      */
103     public SourceIdentifier intern() {
104         return INTERNER.intern(this);
105     }
106
107     /**
108      * Returns model name.
109      *
110      * @return model name
111      */
112     public String getName() {
113         return name;
114     }
115
116     /**
117      * Returns revision of source or null if revision was not supplied.
118      *
119      * @return revision of source or null if revision was not supplied.
120      */
121     // FIXME: version 2.0.0: this should return Optional<String>
122     public String getRevision() {
123         return revision;
124     }
125
126     /**
127      * Since we've got two ways of model versioning (revision &amp; 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
131      * <ul>
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>
135      * </ul>or
136      * <ul>
137      * <li>{@link RevisionSourceIdentifier#create(String)}</li>
138      * <li>{@link RevisionSourceIdentifier#create(String, String)}</li>
139      * <li>{@link RevisionSourceIdentifier#create(String, Optional)}</li>
140      * </ul>
141      *
142      * @param moduleName
143      *            Name of schema
144      * @param revision
145      *            Revision of source in format YYYY-mm-dd. If not present,
146      *            default value will be used.
147      * @return particular SourceIdentifier instance
148      */
149     @Deprecated
150     public static SourceIdentifier create(final String moduleName, final Optional<String> revision) {
151         return new RevisionSourceIdentifier(moduleName, revision);
152     }
153
154     /**
155      * Returns filename for this YANG module as specified in RFC 6020.
156      *
157      * <p>
158      * Returns filename in format <code>name ['@' revision] '.yang'</code>,
159      * where revision is date in format YYYY-mm-dd.
160      *
161      * <p>
162      * @see <a href="http://tools.ietf.org/html/rfc6020#section-5.2">RFC6020</a>
163      *
164      * @return Filename for this source identifier.
165      */
166     public String toYangFilename() {
167         final String rev = NOT_PRESENT_FORMATTED_REVISION.equals(revision) ? null : revision;
168         return toYangFileName(name, Optional.fromNullable(rev));
169     }
170
171     /**
172      * Returns filename for this YANG module as specified in RFC 6020.
173      *
174      * <p>
175      * Returns filename in format <code>moduleName ['@' revision] '.yang'</code>,
176      * where Where revision-date is in format YYYY-mm-dd.
177      *
178      * <p>
179      * See http://tools.ietf.org/html/rfc6020#section-5.2
180      *
181      * @return Filename for this source identifier.
182      */
183     public static String toYangFileName(final String moduleName, final Optional<String> revision) {
184         StringBuilder filename = new StringBuilder(moduleName);
185         if (revision.isPresent()) {
186             filename.append('@');
187             filename.append(revision.get());
188         }
189         filename.append(YangConstants.RFC6020_YANG_FILE_EXTENSION);
190         return filename.toString();
191     }
192 }