Bug 4640: Change semantic-version to openconfig-version
[yangtools.git] / yang / yang-model-api / src / main / java / org / opendaylight / yangtools / yang / model / repo / api / OpenconfigVerSourceIdentifier.java
1 /*
2  * Copyright (c) 2016 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 java.util.Objects;
13 import org.opendaylight.yangtools.concepts.SemVer;
14 import org.opendaylight.yangtools.yang.model.api.Module;
15
16 /**
17  * YANG Schema source identifier with specified openconfig version
18  *
19  * Simple transfer object represents identifier of source for YANG schema
20  * (module or submodule), which consists of
21  * <ul>
22  * <li>YANG schema name {@link #getName()}
23  * <li>Openconfig version of yang schema {@link #getOpenconfigVersion()}
24  * <li>(Optional) Module revision ({link {@link #getRevision()}
25  * </ul>
26  *
27  * Source identifier is designated to be carry only necessary information to
28  * look-up YANG model source and to be used by various SchemaSourceProviders.
29  *
30  * <b>Note:</b>On source retrieval layer it is impossible to distinguish between
31  * YANG module and/or submodule unless source is present.
32  *
33  * <p>
34  * (For further reference see: http://tools.ietf.org/html/rfc6020#section-5.2
35  * and http://tools.ietf.org/html/rfc6022#section-3.1 ).
36  */
37 @Beta
38 public final class OpenconfigVerSourceIdentifier extends SourceIdentifier {
39     private static final long serialVersionUID = 1L;
40     private final SemVer semVer;
41
42     /**
43      * Creates new YANG Schema openconfig-version source identifier.
44      *
45      * @param name
46      *            Name of schema
47      * @param formattedRevision
48      *            Optional of source revision in format YYYY-mm-dd. If not
49      *            present, default value will be used.
50      * @param semVer
51      *            openconfig version of source
52      */
53     OpenconfigVerSourceIdentifier(final String name, final Optional<String> formattedRevision, final SemVer semVer) {
54         super(name, formattedRevision);
55         this.semVer = semVer == null ? Module.DEFAULT_OPENCONFIG_VERSION : semVer;
56     }
57
58     /**
59      * Creates new YANG Schema openconfig-version source identifier.
60      *
61      * @param name
62      *            Name of schema
63      * @param semVer
64      *            openconfig version of source
65      */
66     OpenconfigVerSourceIdentifier(final String name, final SemVer semVer) {
67         this(name, Optional.absent(), semVer);
68     }
69
70     /**
71      * Returns openconfig version of source or
72      * {@link Module#DEFAULT_OPENCONFIG_VERSION} if openconfig version was not
73      * supplied.
74      *
75      * @return revision of source or {@link Module#DEFAULT_OPENCONFIG_VERSION} if
76      *         revision was not supplied.
77      */
78     public SemVer getOpenconfigVersion() {
79         return semVer;
80     }
81
82     /**
83      * Creates new YANG Schema openconfig-version source identifier.
84      *
85      * @param moduleName
86      *            Name of schema
87      * @param semVer
88      *            openconfig version of source
89      */
90     public static OpenconfigVerSourceIdentifier create(final String moduleName, final SemVer semVer) {
91         return new OpenconfigVerSourceIdentifier(moduleName, semVer);
92     }
93
94     /**
95      * Creates new YANG Schema openconfig-version source identifier.
96      *
97      * @param moduleName
98      *            Name of schema
99      * @param revision
100      *            Revision of source in format YYYY-mm-dd
101      * @param semVer
102      *            openconfig version of source
103      */
104     public static OpenconfigVerSourceIdentifier create(final String moduleName, final String revision,
105             final SemVer semVer) {
106         return new OpenconfigVerSourceIdentifier(moduleName, Optional.of(revision), semVer);
107     }
108
109     /**
110      * Creates new YANG Schema openconfig-version source identifier.
111      *
112      * @param moduleName
113      *            Name of schema
114      * @param revision
115      *            Optional of source revision in format YYYY-mm-dd. If not
116      *            present, default value will be used.
117      * @param semVer
118      *            openconfig version of source
119      */
120     public static OpenconfigVerSourceIdentifier create(final String moduleName,
121             final Optional<String> revision, final SemVer semVer) {
122         return new OpenconfigVerSourceIdentifier(moduleName, revision, semVer);
123     }
124
125     @Override
126     public int hashCode() {
127         final int prime = 31;
128         int result = 1;
129         result = prime * result + Objects.hashCode(getName());
130         result = prime * result + Objects.hashCode(semVer);
131         return result;
132     }
133
134     @Override
135     public boolean equals(final Object obj) {
136         if (this == obj) {
137             return true;
138         }
139         if (!(obj instanceof OpenconfigVerSourceIdentifier)) {
140             return false;
141         }
142         final OpenconfigVerSourceIdentifier other = (OpenconfigVerSourceIdentifier) obj;
143         return Objects.equals(getName(), other.getName()) && Objects.equals(semVer, other.semVer);
144     }
145
146     @Override
147     public String toString() {
148         return "OpenconfigVerSourceIdentifier [name=" + getName() + "(" + semVer + ")" + "@" + getRevision() + "]";
149     }
150 }