Bug 6867: Extend yang statement parser to support different yang versions
[yangtools.git] / yang / yang-common / src / main / java / org / opendaylight / yangtools / yang / common / YangVersion.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.common;
9
10 import com.google.common.annotations.Beta;
11 import com.google.common.base.Preconditions;
12 import javax.annotation.Nonnull;
13
14 /**
15  * Enumeration of supported YANG versions.
16  *
17  * @author Robert Varga
18  */
19 @Beta
20 public enum YangVersion {
21     /**
22      * Version 1, as defined in RFC6020.
23      */
24     VERSION_1("1", "RFC6020"),
25     /**
26      * Version 1.1, as defined in RFC7950.
27      */
28     VERSION_1_1("1.1", "RFC7950");
29
30     private final String str;
31     private String reference;
32
33     private YangVersion(final String str, final String reference) {
34         this.str = Preconditions.checkNotNull(str);
35         this.reference = Preconditions.checkNotNull(reference);
36     }
37
38     /**
39      * Parse a YANG version from its textual representation.
40      *
41      * @param str String to parse
42      * @return YANG version
43      * @throws IllegalArgumentException if the string is malformed
44      * @throws NullPointerException if the string is null
45      */
46     public static YangVersion parse(@Nonnull final String str) {
47         switch (str) {
48             case "1":
49                 return VERSION_1;
50             case "1.1":
51                 return VERSION_1_1;
52             default:
53                 throw new IllegalArgumentException("Invalid YANG version '" + str + "'");
54         }
55     }
56
57     /**
58      * Return the normative reference defining this YANG version.
59      *
60      * @return Normative reference.
61      */
62     @Nonnull public String getReference() {
63         return reference;
64     }
65
66     /**
67      * Return the canonical string represetation of this YANG version.
68      * @return Canonical string
69      */
70     @Nonnull public String toCanonicalString() {
71         return str;
72     }
73 }