Merge "BUG-997: Evolve the SchemaRegistry concepts"
[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.annotations.Beta;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13
14 import org.opendaylight.yangtools.concepts.Identifier;
15 import org.opendaylight.yangtools.concepts.Immutable;
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 @Beta
39 public final class SourceIdentifier implements Identifier, Immutable {
40     private static final long serialVersionUID = 1L;
41     private final String revision;
42     private final String name;
43
44     /**
45      *
46      * Creates new YANG Schema source identifier.
47      *
48      * @param name Name of schema
49      * @param formattedRevision Revision of source in format YYYY-mm-dd
50      */
51     public SourceIdentifier(final String name, final Optional<String> formattedRevision) {
52         super();
53         this.name = Preconditions.checkNotNull(name);
54         this.revision = formattedRevision.orNull();
55     }
56
57     /**
58      * Returns model name
59      *
60      * @return model name
61      */
62     public String getName() {
63         return name;
64     }
65
66     /**
67      * Returns revision of source or null if revision was not supplied.
68      *
69      * @return revision of source or null if revision was not supplied.
70      */
71     public String getRevision() {
72         return revision;
73     }
74
75     @Override
76     public int hashCode() {
77         final int prime = 31;
78         int result = 1;
79         result = prime * result + ((name == null) ? 0 : name.hashCode());
80         result = prime * result + ((revision == null) ? 0 : revision.hashCode());
81         return result;
82     }
83
84     @Override
85     public boolean equals(final Object obj) {
86         if (this == obj) {
87             return true;
88         }
89         if (obj == null) {
90             return false;
91         }
92         if (getClass() != obj.getClass()) {
93             return false;
94         }
95         SourceIdentifier other = (SourceIdentifier) obj;
96         if (name == null) {
97             if (other.name != null) {
98                 return false;
99             }
100         } else if (!name.equals(other.name)) {
101             return false;
102         }
103         if (revision == null) {
104             if (other.revision != null) {
105                 return false;
106             }
107         } else if (!revision.equals(other.revision)) {
108             return false;
109         }
110         return true;
111     }
112
113     public static SourceIdentifier create(final String moduleName, final Optional<String> revision) {
114         return new SourceIdentifier(moduleName, revision);
115     }
116
117     /**
118      * Returns filename for this YANG module as specified in RFC 6020.
119      *
120      * Returns filename in format
121      * <code>name ['@' revision] '.yang'</code>
122      * <p>
123      * Where revision is  date in format YYYY-mm-dd.
124      * <p>
125      *
126      * @see http://tools.ietf.org/html/rfc6020#section-5.2
127      *
128      * @return Filename for this source identifier.
129      */
130     public String toYangFilename() {
131         return toYangFileName(name, Optional.fromNullable(revision));
132     }
133
134     @Override
135     public String toString() {
136         return "SourceIdentifier [name=" + name + "@" + revision + "]";
137     }
138
139     /**
140      * Returns filename for this YANG module as specified in RFC 6020.
141      *
142      * Returns filename in format
143      * <code>moduleName ['@' revision] '.yang'</code>
144      *
145      * Where Where revision-date is in format YYYY-mm-dd.
146      *
147      * <p>
148      * See
149      * http://tools.ietf.org/html/rfc6020#section-5.2
150      *
151      * @return Filename for this source identifier.
152      */
153     public static final String toYangFileName(final String moduleName, final Optional<String> revision) {
154         StringBuilder filename = new StringBuilder(moduleName);
155         if (revision.isPresent()) {
156             filename.append('@');
157             filename.append(revision.get());
158         }
159         filename.append(".yang");
160         return filename.toString();
161     }
162
163 }