6344beecc8583f403c6c2206f3c3d933b697513e
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / SchemaNodeIdentifierBuildNamespace.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  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
9 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020;
10
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;
23
24 public class SchemaNodeIdentifierBuildNamespace extends
25         DerivedNamespaceBehaviour<SchemaNodeIdentifier, StmtContext.Mutable<?, ?, EffectiveStatement<?, ?>>, QName, SchemaNodeIdentifierBuildNamespace, ChildSchemaNodes<?, ?>>
26         implements IdentifierNamespace<SchemaNodeIdentifier, StmtContext.Mutable<?, ?, EffectiveStatement<?, ?>>> {
27
28     @SuppressWarnings({"unchecked", "rawtypes"})
29     public SchemaNodeIdentifierBuildNamespace() {
30         super(SchemaNodeIdentifierBuildNamespace.class, (Class) ChildSchemaNodes.class);
31     }
32
33     @Override
34     public StmtContext.Mutable<?, ?, EffectiveStatement<?, ?>> get(
35             final SchemaNodeIdentifier key) {
36         throw new UnsupportedOperationException("Direct access to namespace is not supported");
37     }
38
39     @SuppressWarnings("unchecked")
40     @Override
41     public StmtContext.Mutable<?, ?, EffectiveStatement<?, ?>> getFrom(final NamespaceStorageNode storage, final SchemaNodeIdentifier key) {
42
43         final NamespaceStorageNode lookupStartStorage;
44         if (key.isAbsolute() || storage.getStorageNodeType() == StorageNodeType.ROOT_STATEMENT_LOCAL) {
45             lookupStartStorage = NamespaceBehaviour.findClosestTowardsRoot(storage, StorageNodeType.GLOBAL);
46         } else {
47             lookupStartStorage = storage;
48         }
49         final Iterator<QName> iterator = key.getPathFromRoot().iterator();
50         if (!iterator.hasNext()) {
51             if (lookupStartStorage instanceof StmtContext<?, ?, ?>) {
52                 return (StmtContext.Mutable<?, ?, EffectiveStatement<?, ?>>) lookupStartStorage;
53             }
54             return null;
55         }
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);
61         }
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);
68             }
69             current = nextNodeCtx;
70         }
71         return current;
72     }
73
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;
82             }
83         }
84         return null;
85     }
86
87     @Override
88     public QName getSignificantKey(final SchemaNodeIdentifier key) {
89         return key.getLastComponent();
90     }
91
92 }