93069256e59bc049c71c3cbf1b1cfd0faab5e6e7
[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.Iterator;
12 import org.opendaylight.yangtools.yang.common.QName;
13 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
14 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
15 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
16 import org.opendaylight.yangtools.yang.parser.spi.meta.DerivedNamespaceBehaviour;
17 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour;
18 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
19
20 class SchemaNodeIdentifierBuildNamespace extends
21         DerivedNamespaceBehaviour<SchemaNodeIdentifier, StmtContext.Mutable<?, ?, EffectiveStatement<?, ?>>, QName, SchemaNodeIdentifierBuildNamespace, ChildSchemaNodes<?, ?>>
22         implements IdentifierNamespace<SchemaNodeIdentifier, StmtContext.Mutable<?, ?, EffectiveStatement<?, ?>>> {
23
24     @SuppressWarnings({"unchecked", "rawtypes"})
25     protected SchemaNodeIdentifierBuildNamespace() {
26         super(SchemaNodeIdentifierBuildNamespace.class, (Class) ChildSchemaNodes.class);
27     }
28
29     @Override
30     public StmtContext.Mutable<?, ?, EffectiveStatement<?, ?>> get(
31             SchemaNodeIdentifier key) {
32         throw new UnsupportedOperationException("Direct access to namespace is not supported");
33     }
34
35     @Override
36     public StmtContext.Mutable<?, ?, EffectiveStatement<?, ?>> getFrom(NamespaceStorageNode storage, SchemaNodeIdentifier key) {
37
38         final NamespaceStorageNode lookupStartStorage;
39         if(key.isAbsolute() || storage.getStorageNodeType() == StorageNodeType.ROOT_STATEMENT_LOCAL) {
40             lookupStartStorage = NamespaceBehaviour.findClosestTowardsRoot(storage, StorageNodeType.GLOBAL);
41         } else {
42             lookupStartStorage = storage;
43         }
44         Iterator<QName> iterator = key.getPathFromRoot().iterator();
45         if(!iterator.hasNext()) {
46             if(lookupStartStorage instanceof StmtContext<?, ?, ?>) {
47                 return (StmtContext.Mutable<?, ?, EffectiveStatement<?, ?>>) lookupStartStorage;
48             } else {
49                 return null;
50             }
51         }
52         StmtContext.Mutable<?, ?, EffectiveStatement<?, ?>> current = (StmtContext.Mutable<?, ?, EffectiveStatement<?, ?>>) lookupStartStorage.getFromLocalStorage(ChildSchemaNodes.class, iterator.next());
53         while(current != null && iterator.hasNext()) {
54             current = (StmtContext.Mutable<?, ?, EffectiveStatement<?, ?>>) current.getFromNamespace(ChildSchemaNodes.class, iterator.next());
55         }
56         return current;
57     }
58
59     @Override
60     public QName getSignificantKey(SchemaNodeIdentifier key) {
61         return key.getLastComponent();
62     }
63
64 }