Migrate users of Optional.get()
[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 edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
13 import java.util.Optional;
14 import org.opendaylight.yangtools.yang.common.BiMapYangNamespaceContext;
15 import org.opendaylight.yangtools.yang.common.QNameModule;
16 import org.opendaylight.yangtools.yang.common.YangNamespaceContext;
17 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
18 import org.opendaylight.yangtools.yang.model.api.Module;
19 import org.opendaylight.yangtools.yang.model.spi.AbstractEffectiveModelContextProvider;
20
21 /**
22  * Utility {@link YangNamespaceContext} backed by a SchemaContext, resolving namespaces to their module names. This
23  * is useful for implementing namespace resolution according to
24  * <a href="https://tools.ietf.org/html/rfc7951#section-4">RFC7951 Section 4</a>.
25  *
26  * <p>
27  * When multiple revisions of a particular namespace are present in the backing SchemaContext, this ambiguity is
28  * resolved by using the latest revision available.
29  *
30  * @author Robert Varga
31  */
32 @Beta
33 public final class ModuleNameNamespaceContext extends AbstractEffectiveModelContextProvider
34         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 var builder = ImmutableBiMap.<String, QNameModule>builder();
49         for (var module : getEffectiveModelContext().getModuleStatements().values()) {
50             final var name = module.argument().getLocalName();
51             builder.put(name, findNamespaceForPrefix(name).orElseThrow());
52         }
53         return new BiMapYangNamespaceContext(builder.build());
54     }
55
56     @Override
57     public Optional<QNameModule> findNamespaceForPrefix(final String prefix) {
58         return getEffectiveModelContext().findModules(prefix).stream().findFirst().map(Module::getQNameModule);
59     }
60
61     @Override
62     public Optional<String> findPrefixForNamespace(final QNameModule namespace) {
63         return getEffectiveModelContext().findModule(namespace).map(Module::getName);
64     }
65
66     private Object writeReplace() {
67         return toBiMap();
68     }
69 }