Do not use SchemaNodeIdentifierBuildNamespace prerequisites
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / namespace / 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 package org.opendaylight.yangtools.yang.parser.rfc7950.namespace;
9
10 import com.google.common.annotations.Beta;
11 import java.util.Iterator;
12 import java.util.Optional;
13 import javax.annotation.Nonnull;
14 import org.opendaylight.yangtools.yang.common.QName;
15 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
16 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
17 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
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
23 /**
24  * Legacy namespace for looking up nodes by their Schema Tree identifier.
25  *
26  * @deprecated Use path-based utilities provided around {@link ChildSchemaNodeNamespace} instead.
27  */
28 @Beta
29 @Deprecated
30 public final class SchemaNodeIdentifierBuildNamespace
31         extends DerivedNamespaceBehaviour<SchemaNodeIdentifier, Mutable<?, ?, EffectiveStatement<?, ?>>, QName,
32                 SchemaNodeIdentifierBuildNamespace, ChildSchemaNodeNamespace<?, ?>>
33         implements IdentifierNamespace<SchemaNodeIdentifier, Mutable<?, ?, EffectiveStatement<?, ?>>> {
34
35     @SuppressWarnings({"unchecked", "rawtypes"})
36     public SchemaNodeIdentifierBuildNamespace() {
37         super(SchemaNodeIdentifierBuildNamespace.class, (Class) ChildSchemaNodeNamespace.class);
38     }
39
40     /**
41      * Find statement context identified by interpreting specified {@link SchemaNodeIdentifier} starting at specified
42      * {@link StmtContext}.
43      *
44      * @param root Search root context
45      * @param identifier {@link SchemaNodeIdentifier} relative to search root
46      * @return Matching statement context, if present.
47      * @throws NullPointerException if any of the arguments is null
48      * @deprecated Use {@link ChildSchemaNodeNamespace#findNode(StmtContext, SchemaNodeIdentifier)} instead.
49      */
50     @Deprecated
51     public static Optional<StmtContext<?, ?, ?>> findNode(final StmtContext<?, ?, ?> root,
52             final SchemaNodeIdentifier identifier) {
53         return ChildSchemaNodeNamespace.findNode(root, identifier);
54     }
55
56     @Override
57     public Mutable<?, ?, EffectiveStatement<?, ?>> get(@Nonnull final SchemaNodeIdentifier key) {
58         throw new UnsupportedOperationException("Direct access to namespace is not supported");
59     }
60
61     @SuppressWarnings("unchecked")
62     @Override
63     public Mutable<?, ?, EffectiveStatement<?, ?>> getFrom(final NamespaceStorageNode storage,
64             final SchemaNodeIdentifier key) {
65         final NamespaceStorageNode lookupStartStorage;
66         if (key.isAbsolute() || storage.getStorageNodeType() == StorageNodeType.ROOT_STATEMENT_LOCAL) {
67             lookupStartStorage = NamespaceBehaviour.findClosestTowardsRoot(storage, StorageNodeType.GLOBAL);
68         } else {
69             lookupStartStorage = storage;
70         }
71         final Iterator<QName> iterator = key.getPathFromRoot().iterator();
72         if (!iterator.hasNext()) {
73             if (lookupStartStorage instanceof StmtContext<?, ?, ?>) {
74                 return (Mutable<?, ?, EffectiveStatement<?, ?>>) lookupStartStorage;
75             }
76             return null;
77         }
78         QName nextPath = iterator.next();
79         Mutable<?, ?, EffectiveStatement<?, ?>> current = lookupStartStorage.getFromLocalStorage(
80             ChildSchemaNodeNamespace.class,nextPath);
81         if (current == null && lookupStartStorage instanceof StmtContext<?, ?, ?>) {
82             return ChildSchemaNodeNamespace.tryToFindUnknownStatement(nextPath.getLocalName(),
83                 (Mutable<?, ?, EffectiveStatement<?, ?>>) lookupStartStorage);
84         }
85         while (current != null && iterator.hasNext()) {
86             nextPath = iterator.next();
87             final Mutable<?, ?, EffectiveStatement<?, ?>> nextNodeCtx = current.getFromNamespace(
88                 ChildSchemaNodeNamespace.class,nextPath);
89             if (nextNodeCtx == null) {
90                 return ChildSchemaNodeNamespace.tryToFindUnknownStatement(nextPath.getLocalName(), current);
91             }
92             current = nextNodeCtx;
93         }
94         return current;
95     }
96
97     @Override
98     public QName getSignificantKey(final SchemaNodeIdentifier key) {
99         return key.getLastComponent();
100     }
101 }