Populate parser/ hierarchy
[yangtools.git] / data / rfc8528-data-api / src / main / java / org / opendaylight / yangtools / rfc8528 / data / api / YangLibraryConstants.java
1 /*
2  * Copyright (c) 2019 PANTHEON.tech s.r.o. 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.rfc8528.data.api;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.annotations.Beta;
13 import com.google.common.collect.ImmutableMap;
14 import com.google.common.collect.Maps;
15 import java.util.Arrays;
16 import java.util.Optional;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.opendaylight.yangtools.yang.common.XMLNamespace;
19
20 /**
21  * Constants related to {@code ietf-yang-library.yang}. As schema-mount works in concert with yang-library, we need
22  * these constants to interpret correctly categorize incoming data and present them to schema resolution process.
23  *
24  * <p>
25  * While RFC7895 and RFC8525 are not strictly required by YANG, RFC7950 contains a weak reference to it when dealing
26  * with capability negotiation on protocol layers. Moreover RFC8528 makes it explicit that an instance of yang-library
27  * is mounted underneath both {@code inline} and {@code shared-schema} types of mount.
28  *
29  * <p>
30  * While we could mandate use of either RFC7895 or RFC8525 across the board, this is not feasible, as mount points may
31  * be nested and point to external systems -- hence it is completely possible to encounter both old and new information
32  * in a single mount point tree.
33  */
34 @Beta
35 @NonNullByDefault
36 public final class YangLibraryConstants {
37     /**
38      * The namespace assigned to {@code ietf-yang-library}. This constant is required for XML-like parsers, using
39      * XML namespaces to reference modules.
40      */
41     public static final XMLNamespace MODULE_NAMESPACE =
42         XMLNamespace.of("urn:ietf:params:xml:ns:yang:ietf-yang-library");
43     /**
44      * The module name assigned to {@code ietf-yang-library}. This constant is required for JSON-like parsers, using
45      * module names to reference modules.
46      */
47     public static final String MODULE_NAME = "ietf-yang-library";
48
49     /**
50      * Top-level containers which hold YANG Library information, ordered by descending preference, with more modern
51      * and/or preferred entries first.
52      */
53     public enum ContainerName {
54         // Note: order this enum from most-preferred to least-preferred name
55         /**
56          * Container in RFC8525 (NMDA) YANG Library.
57          */
58         RFC8525("yang-library"),
59         /**
60          * Container in RFC7895 (pre-NMDA) YANG Library.
61          */
62         RFC7895("modules-state");
63
64         private static final ImmutableMap<String, ContainerName> NAME_TO_ENUM = Maps.uniqueIndex(
65             Arrays.asList(values()), ContainerName::getLocalName);
66
67         private final String localName;
68
69         ContainerName(final String localName) {
70             this.localName = requireNonNull(localName);
71         }
72
73         public String getLocalName() {
74             return localName;
75         }
76
77         public static Optional<ContainerName> forLocalName(final String localName) {
78             return Optional.ofNullable(NAME_TO_ENUM.get(requireNonNull(localName)));
79         }
80     }
81
82     private YangLibraryConstants() {
83         // Hidden
84     }
85 }