2 * Copyright (c) 2015 Cisco Systems, Inc. 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.rfc7950.stmt;
10 import com.google.common.annotations.Beta;
11 import com.google.common.collect.Collections2;
12 import com.google.common.collect.ImmutableList;
13 import java.lang.invoke.MethodHandles;
14 import java.lang.invoke.VarHandle;
15 import java.util.ArrayList;
16 import java.util.Collection;
17 import java.util.List;
18 import java.util.Optional;
19 import java.util.function.Predicate;
20 import org.eclipse.jdt.annotation.NonNull;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.opendaylight.yangtools.yang.model.api.DocumentedNode;
23 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.Status;
25 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
27 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
28 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
29 import org.opendaylight.yangtools.yang.model.api.meta.StatementSource;
30 import org.opendaylight.yangtools.yang.model.api.stmt.DescriptionEffectiveStatement;
31 import org.opendaylight.yangtools.yang.model.api.stmt.ReferenceEffectiveStatement;
32 import org.opendaylight.yangtools.yang.model.api.stmt.StatusEffectiveStatement;
33 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
36 * A declared {@link AbstractEffectiveStatement} with DocumentedNode.WithStatus.
38 //FIXME: 6.0.0: use DocumentedNodeMixin.WithStatus instead of keeping any state
40 public abstract class AbstractEffectiveDocumentedNodeWithStatus<A, D extends DeclaredStatement<A>>
41 extends AbstractEffectiveStatement<A, D> implements DocumentedNode.WithStatus {
42 private static final VarHandle UNKNOWN_NODES;
46 UNKNOWN_NODES = MethodHandles.lookup().findVarHandle(AbstractEffectiveDocumentedNodeWithStatus.class,
47 "unknownNodes", ImmutableList.class);
48 } catch (NoSuchFieldException | IllegalAccessException e) {
49 throw new ExceptionInInitializerError(e);
53 private final @NonNull ImmutableList<? extends EffectiveStatement<?, ?>> substatements;
54 private final @NonNull StatementSource statementSource;
55 private final A argument;
56 private final @NonNull D declaredInstance;
57 private final @Nullable String description;
58 private final @Nullable String reference;
59 private final @NonNull Status status;
61 @SuppressWarnings("unused")
62 private volatile ImmutableList<UnknownSchemaNode> unknownNodes;
67 * @param ctx context of statement.
69 protected AbstractEffectiveDocumentedNodeWithStatus(final StmtContext<A, D, ?> ctx) {
70 argument = ctx.getStatementArgument();
71 statementSource = ctx.getStatementSource();
72 declaredInstance = ctx.buildDeclared();
73 substatements = ImmutableList.copyOf(
74 Collections2.transform(Collections2.filter(BaseStatementSupport.declaredSubstatements(ctx),
75 StmtContext::isSupportedToBuildEffective), StmtContext::buildEffective));
77 description = findFirstEffectiveSubstatementArgument(DescriptionEffectiveStatement.class).orElse(null);
78 reference = findFirstEffectiveSubstatementArgument(ReferenceEffectiveStatement.class).orElse(null);
79 status = findFirstEffectiveSubstatementArgument(StatusEffectiveStatement.class).orElse(Status.CURRENT);
83 public final StatementDefinition statementDefinition() {
84 return declaredInstance.statementDefinition();
93 public final StatementSource getStatementSource() {
94 return statementSource;
98 public final D getDeclared() {
99 return declaredInstance;
103 public final Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements() {
104 return substatements;
108 public final Optional<String> getDescription() {
109 return Optional.ofNullable(description);
113 public final Optional<String> getReference() {
114 return Optional.ofNullable(reference);
117 @SuppressWarnings("unchecked")
118 public final <T> Collection<T> allSubstatementsOfType(final Class<T> type) {
119 return Collection.class.cast(Collections2.filter(effectiveSubstatements(), type::isInstance));
123 public final Status getStatus() {
128 public final Collection<? extends UnknownSchemaNode> getUnknownSchemaNodes() {
129 final ImmutableList<UnknownSchemaNode> existing =
130 (ImmutableList<UnknownSchemaNode>) UNKNOWN_NODES.getAcquire(this);
131 return existing != null ? existing : loadUnknownSchemaNodes();
134 protected final <T> @Nullable T firstSubstatementOfType(final Class<T> type) {
135 return effectiveSubstatements().stream().filter(type::isInstance).findFirst().map(type::cast).orElse(null);
138 protected final <R> R firstSubstatementOfType(final Class<?> type, final Class<R> returnType) {
139 return effectiveSubstatements().stream()
140 .filter(((Predicate<Object>)type::isInstance).and(returnType::isInstance))
141 .findFirst().map(returnType::cast).orElse(null);
144 protected final EffectiveStatement<?, ?> firstEffectiveSubstatementOfType(final Class<?> type) {
145 return effectiveSubstatements().stream().filter(type::isInstance).findFirst().orElse(null);
148 // FIXME: rename to 'getFirstEffectiveStatement()'
149 protected final <S extends SchemaNode> S firstSchemaNode(final Class<S> type) {
150 return findFirstEffectiveSubstatement(type).orElse(null);
153 @SuppressWarnings("unchecked")
154 private @NonNull ImmutableList<UnknownSchemaNode> loadUnknownSchemaNodes() {
155 final List<UnknownSchemaNode> init = new ArrayList<>();
156 for (EffectiveStatement<?, ?> stmt : effectiveSubstatements()) {
157 if (stmt instanceof UnknownSchemaNode) {
158 init.add((UnknownSchemaNode) stmt);
162 final ImmutableList<UnknownSchemaNode> computed = ImmutableList.copyOf(init);
163 final Object witness = UNKNOWN_NODES.compareAndExchangeRelease(this, null, computed);
164 return witness == null ? computed : (ImmutableList<UnknownSchemaNode>) witness;