import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
+import java.util.List;
import java.util.Map;
+import java.util.Map.Entry;
import java.util.Objects;
import javax.annotation.Nullable;
import org.opendaylight.yangtools.concepts.Mutable;
import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.common.QNameModule;
-import org.opendaylight.yangtools.yang.common.YangConstants;
+import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour;
import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.NamespaceStorageNode;
import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.StorageNodeType;
+import org.opendaylight.yangtools.yang.parser.spi.meta.QNameCacheNamespace;
import org.opendaylight.yangtools.yang.parser.spi.meta.StatementSupport;
import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
+import org.opendaylight.yangtools.yang.parser.spi.source.BelongsToPrefixToModuleIdentifier;
+import org.opendaylight.yangtools.yang.parser.spi.source.ImpPrefixToModuleIdentifier;
+import org.opendaylight.yangtools.yang.parser.spi.source.ModuleIdentifierToModuleQName;
import org.opendaylight.yangtools.yang.parser.spi.source.PrefixToModule;
import org.opendaylight.yangtools.yang.parser.spi.source.PrefixToModuleMap;
import org.opendaylight.yangtools.yang.parser.spi.source.QNameToStatementDefinition;
return currentContext.getStatementDefinition(name);
}
- ContextBuilder<?, ?, ?> createDeclaredChild(final StatementContextBase<?, ?, ?> current, final QName name, final StatementSourceReference ref) {
+ ContextBuilder<?, ?, ?> createDeclaredChild(final StatementContextBase<?, ?, ?> current, final QName name,
+ final StatementSourceReference ref) {
StatementDefinitionContext<?, ?, ?> def = getDefinition(name);
if (def == null) {
- //unknown-stmts (from import, include or local-scope)
- if (qNameToStmtDefMap.get(Utils.trimPrefix(name)) != null) {
- QName key = Utils.qNameFromArgument(current, name.getLocalName());
- if (key != null) {
- final StatementContextBase<?,?,?> extension = (StatementContextBase<?, ?, ?>) currentContext
- .getAllFromNamespace(ExtensionNamespace.class).get(key);
- if (extension != null) {
- final QName qName = QName.create(((QName) ((SubstatementContext<?, ?, ?>) extension).getStatementArgument())
- .getModule().getNamespace(), ((QName) ((SubstatementContext<?, ?, ?>) extension).
- getStatementArgument()).getModule().getRevision(), extension.getIdentifier().getArgument());
-
- def = new StatementDefinitionContext<>(new UnknownStatementImpl.Definition
- (getNewStatementDefinition(qName)));
- } else {
- throw new IllegalArgumentException("Not found unknown statement: " + name);
- }
- }
+ // unknown-stmts (from import, include or local-scope)
+ if (qNameToStmtDefMap.get(name) != null) {
+ final StatementContextBase<?, ?, ?> extension =
+ (StatementContextBase<?, ?, ?>) currentContext.getAllFromNamespace(ExtensionNamespace.class).get(name);
+
+ SourceException.throwIfNull(extension, current.getStatementSourceReference(), "Extension %s not found",
+ name);
+
+ final QName arg = (QName) extension.getStatementArgument();
+ final QName qName = current.getFromNamespace(QNameCacheNamespace.class,
+ QName.create(arg, extension.getIdentifier().getArgument()));
+
+ def = new StatementDefinitionContext<>(new UnknownStatementImpl.Definition(
+ getNewStatementDefinition(qName)));
} else {
- //type-body-stmts
+ // type-body-stmts
def = resolveTypeBodyStmts(name.getLocalName());
}
- }
- else if (current != null && current.definition().getRepresentingClass().equals(UnknownStatementImpl.class)) {
+ } else if (current != null && current.definition().getRepresentingClass().equals(UnknownStatementImpl.class)) {
QName qName = Utils.qNameFromArgument(current, name.getLocalName());
- def = new StatementDefinitionContext<>(new UnknownStatementImpl.Definition
- (getNewStatementDefinition(qName)));
+ def = new StatementDefinitionContext<>(new UnknownStatementImpl.Definition(
+ getNewStatementDefinition(qName)));
}
Preconditions.checkArgument(def != null, "Statement %s does not have type mapping defined.", name);
}
@SuppressWarnings({"rawtypes", "unchecked"})
- private ContextBuilder<?, ?, ?> createDeclaredRoot(final StatementDefinitionContext<?, ?, ?> def, final StatementSourceReference ref) {
+ private ContextBuilder<?, ?, ?> createDeclaredRoot(final StatementDefinitionContext<?, ?, ?> def,
+ final StatementSourceReference ref) {
return new ContextBuilder(def, ref) {
@Override
- public StatementContextBase build() throws SourceException {
+ public StatementContextBase build() {
if (root == null) {
root = new RootStatementContext(this, SourceSpecificContext.this);
} else {
- Preconditions.checkState(root.getIdentifier().equals(createIdentifier()), "Root statement was already defined as %s.", root.getIdentifier());
+ Preconditions.checkState(root.getIdentifier().equals(createIdentifier()),
+ "Root statement was already defined as %s.", root.getIdentifier());
}
root.resetLists();
return root;
}
void startPhase(final ModelProcessingPhase phase) {
- @Nullable ModelProcessingPhase previousPhase = phase.getPreviousPhase();
+ @Nullable
+ ModelProcessingPhase previousPhase = phase.getPreviousPhase();
Preconditions.checkState(Objects.equals(previousPhase, finishedPhase));
Preconditions.checkState(modifiers.get(previousPhase).isEmpty());
inProgressPhase = phase;
}
@Override
- public <K, V, N extends IdentifierNamespace<K, V>> void addToLocalStorage(final Class<N> type, final K key, final V value) {
+ public <K, V, N extends IdentifierNamespace<K, V>> void addToLocalStorage(final Class<N> type, final K key,
+ final V value) {
if (ImportedNamespaceContext.class.isAssignableFrom(type)) {
importedNamespaces.add((NamespaceStorageNode) value);
}
}
@Override
- public <K, V, N extends IdentifierNamespace<K, V>> NamespaceBehaviour<K, V, N> getNamespaceBehaviour(final Class<N> type) {
+ public <K, V, N extends IdentifierNamespace<K, V>> NamespaceBehaviour<K, V, N> getNamespaceBehaviour(
+ final Class<N> type) {
return currentContext.getNamespaceBehaviour(type);
}
private static boolean tryToProgress(final Collection<ModifierImpl> currentPhaseModifiers) {
-
- Iterator<ModifierImpl> modifier = currentPhaseModifiers.iterator();
boolean hasProgressed = false;
+
+ final Iterator<ModifierImpl> modifier = currentPhaseModifiers.iterator();
while (modifier.hasNext()) {
if (modifier.next().tryApply()) {
modifier.remove();
}
SourceException failModifiers(final ModelProcessingPhase identifier) {
- InferenceException sourceEx = new InferenceException("Fail to infer source relationships", root.getStatementSourceReference());
-
-
+ final List<SourceException> exceptions = new ArrayList<>();
for (ModifierImpl mod : modifiers.get(identifier)) {
try {
mod.failModifier();
} catch (SourceException e) {
- sourceEx.addSuppressed(e);
+ exceptions.add(e);
}
}
- return sourceEx;
+
+ final String message = String.format("Yang model processing phase %s failed", identifier);
+ if (exceptions.isEmpty()) {
+ return new InferenceException(message, root.getStatementSourceReference());
+ }
+
+ final InferenceException e = new InferenceException(message, root.getStatementSourceReference(),
+ exceptions.get(0));
+ final Iterator<SourceException> it = exceptions.listIterator(1);
+ while (it.hasNext()) {
+ e.addSuppressed(it.next());
+ }
+
+ return e;
}
void loadStatements() throws SourceException {
}
}
-
private PrefixToModule prefixes() {
- Map<String, QNameModule> prefixes = currentContext.getAllFromNamespace(PrefixToModule.class);
- for (Map.Entry<String, QNameModule> prefix : prefixes.entrySet()) {
- prefixToModuleMap.put(prefix.getKey(), prefix.getValue());
+ final Map<String, ModuleIdentifier> allPrefixes = getRoot().getAllFromNamespace(ImpPrefixToModuleIdentifier
+ .class);
+ final Map<String, ModuleIdentifier> belongsToPrefixes = getRoot().getAllFromNamespace
+ (BelongsToPrefixToModuleIdentifier.class);
+ if (belongsToPrefixes != null) {
+ allPrefixes.putAll(belongsToPrefixes);
+ }
+
+ for (Entry<String, ModuleIdentifier> stringModuleIdentifierEntry : allPrefixes.entrySet()) {
+ final QNameModule namespace = getRoot().getFromNamespace(ModuleIdentifierToModuleQName.class,
+ stringModuleIdentifierEntry.getValue());
+ prefixToModuleMap.put(stringModuleIdentifierEntry.getKey(), namespace);
}
return prefixToModuleMap;
}
private QNameToStatementDefinition stmtDef() {
- //regular YANG statements added
+ // regular YANG statements added
ImmutableMap<QName, StatementSupport<?, ?, ?>> definitions = currentContext.getSupportsForPhase(
inProgressPhase).getDefinitions();
- for (Map.Entry<QName, StatementSupport<?, ?, ?>> entry : definitions.entrySet()) {
+ for (Entry<QName, StatementSupport<?, ?, ?>> entry : definitions.entrySet()) {
qNameToStmtDefMap.put(entry.getKey(), entry.getValue());
}
- //extensions added
+ // extensions added
if (inProgressPhase.equals(ModelProcessingPhase.FULL_DECLARATION)) {
- Map<QName, StmtContext<?, ExtensionStatement, EffectiveStatement<QName, ExtensionStatement>>> extensions = currentContext
- .getAllFromNamespace(ExtensionNamespace.class);
+ Map<QName, StmtContext<?, ExtensionStatement, EffectiveStatement<QName, ExtensionStatement>>> extensions =
+ currentContext.getAllFromNamespace(ExtensionNamespace.class);
if (extensions != null) {
- for (Map.Entry<QName, StmtContext<?, ExtensionStatement, EffectiveStatement<QName, ExtensionStatement>>> extension : extensions
- .entrySet()) {
- qNameToStmtDefMap
- .put(QName.create(YangConstants.RFC6020_YIN_MODULE,
- extension.getKey().getLocalName()),
- (StatementDefinition) ((StatementContextBase<?, ?, ?>) extension
- .getValue()).definition()
- .getFactory());
+ for (Entry<QName, StmtContext<?, ExtensionStatement, EffectiveStatement<QName, ExtensionStatement>>> extension :
+ extensions.entrySet()) {
+ qNameToStmtDefMap.put((extension.getKey()),
+ (StatementDefinition) ((StatementContextBase<?, ?, ?>) extension.getValue()).definition()
+ .getFactory());
}
}
}