Rename {Data,Schema}TreeAwareEffectiveStatement.Namespace
[yangtools.git] / parser / rfc8040-parser-support / src / main / java / org / opendaylight / yangtools / rfc8040 / parser / YangDataEffectiveStatementImpl.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies, 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.rfc8040.parser;
9
10 import static com.google.common.base.Verify.verify;
11 import static java.util.Objects.requireNonNull;
12
13 import com.google.common.annotations.Beta;
14 import com.google.common.collect.ImmutableList;
15 import java.util.Map;
16 import java.util.Optional;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.opendaylight.yangtools.rfc8040.model.api.YangDataEffectiveStatement;
19 import org.opendaylight.yangtools.rfc8040.model.api.YangDataSchemaNode;
20 import org.opendaylight.yangtools.rfc8040.model.api.YangDataStatement;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
24 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
25 import org.opendaylight.yangtools.yang.model.api.stmt.ContainerEffectiveStatement;
26 import org.opendaylight.yangtools.yang.model.spi.meta.AbstractEffectiveUnknownSchmemaNode;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
28
29 @Beta
30 final class YangDataEffectiveStatementImpl extends AbstractEffectiveUnknownSchmemaNode<String, YangDataStatement>
31         implements YangDataEffectiveStatement, YangDataSchemaNode {
32     private final @NonNull QName argumentQName;
33     private final @NonNull ContainerEffectiveStatement container;
34
35     YangDataEffectiveStatementImpl(final Current<String, YangDataStatement> stmt,
36              final ImmutableList<? extends EffectiveStatement<?, ?>> substatements, final QName qname) {
37         super(stmt.declared(), stmt.argument(), stmt.history(), substatements);
38         argumentQName = requireNonNull(qname);
39
40         container = findFirstEffectiveSubstatement(ContainerEffectiveStatement.class).get();
41
42         // TODO: this is strong binding of two API contracts. Unfortunately ContainerEffectiveStatement design is
43         //       incomplete.
44         verify(container instanceof ContainerSchemaNode, "Incompatible container %s", container);
45     }
46
47     @Override
48     public QName getQName() {
49         return argumentQName;
50     }
51
52     @Override
53     public ContainerEffectiveStatement getContainer() {
54         return container;
55     }
56
57     @Override
58     public ContainerSchemaNode getContainerSchemaNode() {
59         // Verified in the constructor
60         return (ContainerSchemaNode) container;
61     }
62
63     @Override
64     public YangDataEffectiveStatement asEffectiveStatement() {
65         return this;
66     }
67
68     @Override
69     protected <K, V, N extends IdentifierNamespace<K, V>> Optional<? extends Map<K, V>> getNamespaceContents(
70             final Class<N> namespace) {
71         if (SchemaTreeNamespace.class.equals(namespace) || DataTreeNamespace.class.equals(namespace)) {
72             @SuppressWarnings("unchecked")
73             final Map<K, V> ns = (Map<K, V>)Map.of(container.argument(), container);
74             return Optional.of(ns);
75         }
76         return super.getNamespaceContents(namespace);
77     }
78 }