org.opendaylight.yangtools.yang.model.util.ModuleNameNamespaceContext Maven / Gradle / Ivy
/*
* Copyright (c) 2019 Pantheon Technologies, s.r.o. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*/
package org.opendaylight.yangtools.yang.model.util;
import com.google.common.annotations.Beta;
import com.google.common.collect.ImmutableBiMap;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.opendaylight.yangtools.yang.common.BiMapYangNamespaceContext;
import org.opendaylight.yangtools.yang.common.QNameModule;
import org.opendaylight.yangtools.yang.common.YangNamespaceContext;
import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
import org.opendaylight.yangtools.yang.model.spi.AbstractEffectiveModelContextProvider;
/**
* Utility {@link YangNamespaceContext} backed by a SchemaContext, resolving namespaces to their module names. This
* is useful for implementing namespace resolution according to
* RFC7951 Section 4.
*
*
* When multiple revisions of a particular namespace are present in the backing SchemaContext, this ambiguity is
* resolved by using the latest revision available.
*/
@Beta
public final class ModuleNameNamespaceContext extends AbstractEffectiveModelContextProvider
implements YangNamespaceContext {
@java.io.Serial
private static final long serialVersionUID = 1L;
@SuppressFBWarnings(value = "SE_NO_SUITABLE_CONSTRUCTOR", justification = "Handled through writeReplace()")
public ModuleNameNamespaceContext(final EffectiveModelContext schemaContext) {
super(schemaContext);
}
/**
* Convert this object to an equivalent {@link BiMapYangNamespaceContext}.
*
* @return A BiMapYangNamespaceContext.
*/
public BiMapYangNamespaceContext toBiMap() {
final var builder = ImmutableBiMap.builder();
for (var module : getEffectiveModelContext().getModuleStatements().values()) {
final var name = module.argument().getLocalName();
builder.put(name, findNamespaceForPrefix(name).orElseThrow());
}
return new BiMapYangNamespaceContext(builder.build());
}
@Override
public QNameModule namespaceForPrefix(final String prefix) {
final var modules = getEffectiveModelContext().findModuleStatements(prefix).iterator();
return modules.hasNext() ? modules.next().localQNameModule() : null;
}
@Override
public String prefixForNamespace(final QNameModule namespace) {
return getEffectiveModelContext().findModuleStatement(namespace)
.map(module -> module.argument().getLocalName())
.orElse(null);
}
@java.io.Serial
private Object writeReplace() {
return toBiMap();
}
}