Make ParserNamespace final
[yangtools.git] / parser / yang-parser-spi / src / main / java / org / opendaylight / yangtools / yang / parser / spi / SchemaTreeNamespaceBehaviour.java
1 /*
2  * Copyright (c) 2022 PANTHEON.tech, s.r.o. 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.spi;
9
10 import static com.google.common.base.Verify.verifyNotNull;
11 import static java.util.Objects.requireNonNull;
12
13 import java.util.Map;
14 import org.opendaylight.yangtools.yang.common.QName;
15 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
16 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeAwareEffectiveStatement;
17 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeEffectiveStatement;
18 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour;
19 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
20 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
21
22 /**
23  * {@link NamespaceBehaviour} handling {@link ParserNamespaces#schemaTree()}.
24  */
25 // FIXME: 11.0.0: this contract seems to fall on the reactor side of things rather than parser-spi. Consider moving this
26 //                into yang-(parser-)reactor-api.
27 final class SchemaTreeNamespaceBehaviour<D extends DeclaredStatement<QName>, E extends SchemaTreeEffectiveStatement<D>>
28         extends NamespaceBehaviour<QName, StmtContext<QName, D, E>> {
29     SchemaTreeNamespaceBehaviour() {
30         super(ParserNamespaces.schemaTree());
31     }
32
33     /**
34      * {@inheritDoc}
35      *
36      * <p>
37      * This method is analogous to {@link SchemaTreeAwareEffectiveStatement#findSchemaTreeNode(QName)}.
38      */
39     @Override
40     public StmtContext<QName, D, E> getFrom(final NamespaceStorageNode storage, final QName key) {
41         // Get the backing storage node for the requested storage
42         final NamespaceStorageNode storageNode = globalOrStatementSpecific(storage);
43         // Check try to look up existing node
44         final StmtContext<QName, D, E> existing = storageNode.getFromLocalStorage(getIdentifier(), key);
45
46         // An existing node takes precedence, if it does not exist try to request it
47         return existing != null ? existing : requestFrom(storageNode, key);
48     }
49
50     @Override
51     public Map<QName, StmtContext<QName, D, E>> getAllFrom(final NamespaceStorageNode storage) {
52         // FIXME: 7.0.0: this method needs to be well-defined
53         return null;
54     }
55
56     @Override
57     public void addTo(final NamespaceStorageNode storage, final QName key, final StmtContext<QName, D, E> value) {
58         final StmtContext<?, D, E> prev = globalOrStatementSpecific(storage).putToLocalStorageIfAbsent(getIdentifier(),
59             key, value);
60
61         if (prev != null) {
62             throw new SourceException(value,
63                 "Error in module '%s': cannot add '%s'. Node name collision: '%s' already declared at %s",
64                 value.getRoot().rawArgument(), key, prev.argument(), prev.sourceReference());
65         }
66     }
67
68     private static <D extends DeclaredStatement<QName>, E extends SchemaTreeEffectiveStatement<D>>
69             StmtContext<QName, D, E> requestFrom(final NamespaceStorageNode storageNode, final QName key) {
70         return storageNode instanceof OnDemandSchemaTreeStorageNode ondemand ? ondemand.requestSchemaTreeChild(key)
71             : null;
72     }
73
74     private static NamespaceStorageNode globalOrStatementSpecific(final NamespaceStorageNode storage) {
75         NamespaceStorageNode current = requireNonNull(storage);
76         while (!isLocalOrGlobal(current.getStorageNodeType())) {
77             current = verifyNotNull(current.getParentNamespaceStorage());
78         }
79         return current;
80     }
81
82     private static boolean isLocalOrGlobal(final StorageNodeType type) {
83         return type == StorageNodeType.STATEMENT_LOCAL || type == StorageNodeType.GLOBAL;
84     }
85 }