2 * Copyright (c) 2022 PANTHEON.tech, s.r.o. 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
8 package org.opendaylight.yangtools.yang.parser.spi;
10 import static com.google.common.base.Verify.verifyNotNull;
11 import static java.util.Objects.requireNonNull;
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;
23 * {@link NamespaceBehaviour} handling {@link ParserNamespaces#schemaTree()}.
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());
37 * This method is analogous to {@link SchemaTreeAwareEffectiveStatement#findSchemaTreeNode(QName)}.
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);
46 // An existing node takes precedence, if it does not exist try to request it
47 return existing != null ? existing : requestFrom(storageNode, key);
51 public Map<QName, StmtContext<QName, D, E>> getAllFrom(final NamespaceStorageNode storage) {
52 // FIXME: 7.0.0: this method needs to be well-defined
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(),
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());
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)
74 private static NamespaceStorageNode globalOrStatementSpecific(final NamespaceStorageNode storage) {
75 NamespaceStorageNode current = requireNonNull(storage);
76 while (!isLocalOrGlobal(current.getStorageNodeType())) {
77 current = verifyNotNull(current.getParentNamespaceStorage());
82 private static boolean isLocalOrGlobal(final StorageNodeType type) {
83 return type == StorageNodeType.STATEMENT_LOCAL || type == StorageNodeType.GLOBAL;