1c2d3d86bd0fea39cfda5cb8791342aef124981e
[yangtools.git] / yang / yang-model-util / src / main / java / org / opendaylight / yangtools / yang / model / util / ModuleNameNamespaceContext.java
1 /*
2  * Copyright (c) 2019 Pantheon Technologies, s.r.o.  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.util;
9
10 import com.google.common.annotations.Beta;
11 import com.google.common.collect.ImmutableBiMap;
12 import com.google.common.collect.ImmutableBiMap.Builder;
13 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
14 import java.util.Optional;
15 import java.util.stream.Collectors;
16 import org.opendaylight.yangtools.yang.common.BiMapYangNamespaceContext;
17 import org.opendaylight.yangtools.yang.common.QNameModule;
18 import org.opendaylight.yangtools.yang.common.YangNamespaceContext;
19 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
20 import org.opendaylight.yangtools.yang.model.api.Module;
21
22 /**
23  * Utility {@link YangNamespaceContext} backed by a SchemaContext, resolving namespaces to their module names. This
24  * is useful for implementing namespace resolution according to
25  * <a href="https://tools.ietf.org/html/rfc7951#section-4">RFC7951 Section 4</a>.
26  *
27  * <p>
28  * When multiple revisions of a particular namespace are present in the backing SchemaContext, this ambiguity is
29  * resolved by using the latest revision available.
30  *
31  * @author Robert Varga
32  */
33 @Beta
34 public final class ModuleNameNamespaceContext extends AbstractSchemaContextProvider implements YangNamespaceContext {
35     private static final long serialVersionUID = 1L;
36
37     @SuppressFBWarnings(value = "SE_NO_SUITABLE_CONSTRUCTOR", justification = "Handled through writeReplace()")
38     public ModuleNameNamespaceContext(final EffectiveModelContext schemaContext) {
39         super(schemaContext);
40     }
41
42     /**
43      * Convert this object to an equivalent {@link BiMapYangNamespaceContext}.
44      *
45      * @return A BiMapYangNamespaceContext.
46      */
47     public BiMapYangNamespaceContext toBiMap() {
48         final Builder<String, QNameModule> builder = ImmutableBiMap.builder();
49         for (String name : getSchemaContext().getModules().stream().map(Module::getName).collect(Collectors.toSet())) {
50             builder.put(name, findNamespaceForPrefix(name).get());
51         }
52         return new BiMapYangNamespaceContext(builder.build());
53     }
54
55     @Override
56     public Optional<QNameModule> findNamespaceForPrefix(final String prefix) {
57         return getSchemaContext().findModules(prefix).stream().findFirst().map(Module::getQNameModule);
58     }
59
60     @Override
61     public Optional<String> findPrefixForNamespace(final QNameModule namespace) {
62         return getSchemaContext().findModule(namespace).map(Module::getName);
63     }
64
65     private Object writeReplace() {
66         return toBiMap();
67     }
68 }