Populate data/ hierarchy
[yangtools.git] / model / 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 org.opendaylight.yangtools.yang.common.BiMapYangNamespaceContext;
16 import org.opendaylight.yangtools.yang.common.QNameModule;
17 import org.opendaylight.yangtools.yang.common.YangNamespaceContext;
18 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
19 import org.opendaylight.yangtools.yang.model.api.Module;
20 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement;
21 import org.opendaylight.yangtools.yang.model.spi.AbstractEffectiveModelContextProvider;
22
23 /**
24  * Utility {@link YangNamespaceContext} backed by a SchemaContext, resolving namespaces to their module names. This
25  * is useful for implementing namespace resolution according to
26  * <a href="https://tools.ietf.org/html/rfc7951#section-4">RFC7951 Section 4</a>.
27  *
28  * <p>
29  * When multiple revisions of a particular namespace are present in the backing SchemaContext, this ambiguity is
30  * resolved by using the latest revision available.
31  *
32  * @author Robert Varga
33  */
34 @Beta
35 public final class ModuleNameNamespaceContext extends AbstractEffectiveModelContextProvider
36         implements YangNamespaceContext {
37     private static final long serialVersionUID = 1L;
38
39     @SuppressFBWarnings(value = "SE_NO_SUITABLE_CONSTRUCTOR", justification = "Handled through writeReplace()")
40     public ModuleNameNamespaceContext(final EffectiveModelContext schemaContext) {
41         super(schemaContext);
42     }
43
44     /**
45      * Convert this object to an equivalent {@link BiMapYangNamespaceContext}.
46      *
47      * @return A BiMapYangNamespaceContext.
48      */
49     public BiMapYangNamespaceContext toBiMap() {
50         final Builder<String, QNameModule> builder = ImmutableBiMap.builder();
51         for (ModuleEffectiveStatement module : getEffectiveModelContext().getModuleStatements().values()) {
52             final String name = module.argument().getLocalName();
53             builder.put(name, findNamespaceForPrefix(name).get());
54         }
55         return new BiMapYangNamespaceContext(builder.build());
56     }
57
58     @Override
59     public Optional<QNameModule> findNamespaceForPrefix(final String prefix) {
60         return getEffectiveModelContext().findModules(prefix).stream().findFirst().map(Module::getQNameModule);
61     }
62
63     @Override
64     public Optional<String> findPrefixForNamespace(final QNameModule namespace) {
65         return getEffectiveModelContext().findModule(namespace).map(Module::getName);
66     }
67
68     private Object writeReplace() {
69         return toBiMap();
70     }
71 }