BUG-997: Introduce SchemaSource respository and related APIs
[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/eplv10.html
7  */
8 package org.opendaylight.yangtools.yang.model.repo.api;
9
10 import com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12
13 import org.opendaylight.yangtools.concepts.Identifier;
14 import org.opendaylight.yangtools.concepts.Immutable;
15
16 /**
17  *
18  * YANG Schema source identifier
19  *
20  * Simple transfer object represents identifier of source for YANG schema (module or submodule),
21  * which consists of
22  * <ul>
23  * <li>YANG schema name ({@link #getName()}
24  * <li>Module revision (optional) ({link {@link #getRevision()})
25  * </ul>
26  *
27  * Source identifier is designated to be carry only necessary information
28  * to look-up YANG model source and to be used by {@link AdvancedSchemaSourceProvider}
29  * and similar.
30  *
31  * <b>Note:</b>On source retrieval layer it is impossible to distinguish
32  * between YANG module and/or submodule unless source is present.
33  *
34  * <p>
35  * (For further reference see: http://tools.ietf.org/html/rfc6020#section-5.2 and
36  * http://tools.ietf.org/html/rfc6022#section-3.1 ).
37  *
38  *
39  */
40 public final class SourceIdentifier implements Identifier, Immutable {
41     private static final long serialVersionUID = 1L;
42     private final String revision;
43     private final String name;
44
45     /**
46      *
47      * Creates new YANG Schema source identifier.
48      *
49      * @param name Name of schema
50      * @param formattedRevision Revision of source in format YYYY-mm-dd
51      */
52     public SourceIdentifier(final String name, final Optional<String> formattedRevision) {
53         super();
54         this.name = Preconditions.checkNotNull(name);
55         this.revision = formattedRevision.orNull();
56     }
57
58     /**
59      * Returns model name
60      *
61      * @return model name
62      */
63     public String getName() {
64         return name;
65     }
66
67     /**
68      * Returns revision of source or null if revision was not supplied.
69      *
70      * @return revision of source or null if revision was not supplied.
71      */
72     public String getRevision() {
73         return revision;
74     }
75
76     @Override
77     public int hashCode() {
78         final int prime = 31;
79         int result = 1;
80         result = prime * result + ((name == null) ? 0 : name.hashCode());
81         result = prime * result + ((revision == null) ? 0 : revision.hashCode());
82         return result;
83     }
84
85     @Override
86     public boolean equals(final Object obj) {
87         if (this == obj) {
88             return true;
89         }
90         if (obj == null) {
91             return false;
92         }
93         if (getClass() != obj.getClass()) {
94             return false;
95         }
96         SourceIdentifier other = (SourceIdentifier) obj;
97         if (name == null) {
98             if (other.name != null) {
99                 return false;
100             }
101         } else if (!name.equals(other.name)) {
102             return false;
103         }
104         if (revision == null) {
105             if (other.revision != null) {
106                 return false;
107             }
108         } else if (!revision.equals(other.revision)) {
109             return false;
110         }
111         return true;
112     }
113
114     public static SourceIdentifier create(final String moduleName, final Optional<String> revision) {
115         return new SourceIdentifier(moduleName, revision);
116     }
117
118     /**
119      * Returns filename for this YANG module as specified in RFC 6020.
120      *
121      * Returns filename in format
122      * <code>name ['@' revision] '.yang'</code>
123      * <p>
124      * Where revision is  date in format YYYY-mm-dd.
125      * <p>
126      *
127      * @see http://tools.ietf.org/html/rfc6020#section-5.2
128      *
129      * @return Filename for this source identifier.
130      */
131     public String toYangFilename() {
132         return toYangFileName(name, Optional.fromNullable(revision));
133     }
134
135     @Override
136     public String toString() {
137         return "SourceIdentifier [name=" + name + "@" + revision + "]";
138     }
139
140     /**
141      * Returns filename for this YANG module as specified in RFC 6020.
142      *
143      * Returns filename in format
144      * <code>moduleName ['@' revision] '.yang'</code>
145      *
146      * Where Where revision-date is in format YYYY-mm-dd.
147      *
148      * <p>
149      * See
150      * http://tools.ietf.org/html/rfc6020#section-5.2
151      *
152      * @return Filename for this source identifier.
153      */
154     public static final String toYangFileName(final String moduleName, final Optional<String> revision) {
155         StringBuilder filename = new StringBuilder(moduleName);
156         if (revision.isPresent()) {
157             filename.append('@');
158             filename.append(revision.get());
159         }
160         filename.append(".yang");
161         return filename.toString();
162     }
163
164 }