2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020;
11 import java.util.Collection;
12 import java.util.Iterator;
13 import org.opendaylight.yangtools.yang.common.QName;
14 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
15 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
16 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
17 import org.opendaylight.yangtools.yang.model.api.stmt.UnknownStatement;
18 import org.opendaylight.yangtools.yang.parser.spi.meta.DerivedNamespaceBehaviour;
19 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour;
20 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
24 public class SchemaNodeIdentifierBuildNamespace extends
25 DerivedNamespaceBehaviour<SchemaNodeIdentifier, StmtContext.Mutable<?, ?, EffectiveStatement<?, ?>>, QName, SchemaNodeIdentifierBuildNamespace, ChildSchemaNodes<?, ?>>
26 implements IdentifierNamespace<SchemaNodeIdentifier, StmtContext.Mutable<?, ?, EffectiveStatement<?, ?>>> {
28 @SuppressWarnings({"unchecked", "rawtypes"})
29 public SchemaNodeIdentifierBuildNamespace() {
30 super(SchemaNodeIdentifierBuildNamespace.class, (Class) ChildSchemaNodes.class);
34 public StmtContext.Mutable<?, ?, EffectiveStatement<?, ?>> get(
35 final SchemaNodeIdentifier key) {
36 throw new UnsupportedOperationException("Direct access to namespace is not supported");
39 @SuppressWarnings("unchecked")
41 public StmtContext.Mutable<?, ?, EffectiveStatement<?, ?>> getFrom(final NamespaceStorageNode storage, final SchemaNodeIdentifier key) {
43 final NamespaceStorageNode lookupStartStorage;
44 if (key.isAbsolute() || storage.getStorageNodeType() == StorageNodeType.ROOT_STATEMENT_LOCAL) {
45 lookupStartStorage = NamespaceBehaviour.findClosestTowardsRoot(storage, StorageNodeType.GLOBAL);
47 lookupStartStorage = storage;
49 final Iterator<QName> iterator = key.getPathFromRoot().iterator();
50 if (!iterator.hasNext()) {
51 if (lookupStartStorage instanceof StmtContext<?, ?, ?>) {
52 return (StmtContext.Mutable<?, ?, EffectiveStatement<?, ?>>) lookupStartStorage;
56 QName nextPath = iterator.next();
57 StmtContext.Mutable<?, ?, EffectiveStatement<?, ?>> current = lookupStartStorage
58 .getFromLocalStorage(ChildSchemaNodes.class, nextPath);
59 if (current == null && lookupStartStorage instanceof StmtContext<?, ?, ?>) {
60 return tryToFindUnknownStatement(nextPath.getLocalName(), (Mutable<?, ?, EffectiveStatement<?, ?>>) lookupStartStorage);
62 while (current != null && iterator.hasNext()) {
63 nextPath = iterator.next();
64 final StmtContext.Mutable<?, ?, EffectiveStatement<?, ?>> nextNodeCtx = current
65 .getFromNamespace(ChildSchemaNodes.class, nextPath);
66 if (nextNodeCtx == null) {
67 return tryToFindUnknownStatement(nextPath.getLocalName(), current);
69 current = nextNodeCtx;
74 @SuppressWarnings({"unchecked", "rawtypes"})
75 private static Mutable<?, ?, EffectiveStatement<?, ?>> tryToFindUnknownStatement(final String localName,
76 final Mutable<?, ?, EffectiveStatement<?, ?>> current) {
77 final Collection<StmtContext<?, ?, ?>> unknownSubstatements = (Collection)StmtContextUtils.findAllSubstatements(current,
78 UnknownStatement.class);
79 for (final StmtContext<?, ?, ?> unknownSubstatement : unknownSubstatements) {
80 if (localName.equals(unknownSubstatement.rawStatementArgument())) {
81 return (Mutable<?, ?, EffectiveStatement<?, ?>>) unknownSubstatement;
88 public QName getSignificantKey(final SchemaNodeIdentifier key) {
89 return key.getLastComponent();