2 * Copyright (c) 2019 Pantheon Technologies, s.r.o. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.common;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static com.google.common.base.Preconditions.checkState;
12 import static java.util.Objects.requireNonNull;
14 import com.google.common.annotations.Beta;
15 import java.io.Serializable;
16 import java.util.Optional;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.opendaylight.yangtools.concepts.Immutable;
21 * Interface for mapping between {@link String} prefixes and {@link QNameModule} namespaces. The conceptual model
22 * matches prefix mapping inside a YANG {@code module} as defined through the use of {@code prefix} and {@code import}
23 * statements and detailed in <a href="https://tools.ietf.org/html/rfc7950#section-7.1.4">RFC7950 Section 7.1.4</a>.
26 * Each namespace context can have a default namespace and a set of prefix/namespace mappings. A namespace can be bound
27 * to multiple prefixes at the same time. The default namespace must also have a prefix assigned.
29 * @author Robert Varga
32 public interface YangNamespaceContext extends Immutable, Serializable {
34 * Return the default namespace in this context.
36 * @return Default namespace, if supported.
38 @NonNull Optional<QNameModule> getDefaultNamespace();
41 * Return QNameModule to which a particular prefix is bound.
43 * @param prefix Prefix to look up
44 * @return QNameModule bound to specified prefix
45 * @throws NullPointerException if {@code prefix} is null
47 @NonNull Optional<QNameModule> findNamespaceForPrefix(String prefix);
50 * Return a prefix to which a particular QNameModule is bound. If a namespace is bound to multiple prefixes, it is
51 * left unspecified which of those prefixes is returned.
53 * @param namespace QNameModule to look up
54 * @return Prefix to which the QNameModule is bound
55 * @throws NullPointerException if {@code module} is null
57 @NonNull Optional<String> findPrefixForNamespace(QNameModule namespace);
60 * Create a {@link QName} in the default namespace.
62 * @param localName QName local name
64 * @throws NullPointerException if {@code localName} is null
65 * @throws IllegalArgumentException if {@code localName} does not conform to local name requirements
66 * @throws IllegalStateException if this context does not have default namespace
68 default @NonNull QName createQName(final String localName) {
69 final Optional<QNameModule> namespace = getDefaultNamespace();
70 checkState(namespace.isPresent(), "%s does not have a default namespace", this);
71 return QName.create(namespace.get(), requireNonNull(localName));
75 * Create a {@link QName} by resolving a prefix against currently-bound prefixes and combining it with specified
78 * @param prefix Namespace prefix
79 * @param localName QName local name
81 * @throws NullPointerException if any argument is null
82 * @throws IllegalArgumentException if {@code localName} does not conform to local name requirements or if the
83 * prefix is not bound in this context.
85 default @NonNull QName createQName(final String prefix, final String localName) {
86 final Optional<QNameModule> namespace = findNamespaceForPrefix(prefix);
87 checkArgument(namespace.isPresent(), "Prefix %s is not bound", prefix);
88 return QName.create(namespace.get(), localName);