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.model.ri.stmt.impl.eff;
10 import com.google.common.collect.ImmutableList;
11 import java.lang.invoke.MethodHandles;
12 import java.lang.invoke.MethodHandles.Lookup;
13 import java.lang.invoke.VarHandle;
15 import java.util.Optional;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
19 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
20 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
21 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
22 import org.opendaylight.yangtools.yang.model.api.meta.StatementOrigin;
23 import org.opendaylight.yangtools.yang.model.api.stmt.DefaultEffectiveStatement;
24 import org.opendaylight.yangtools.yang.model.api.stmt.DescriptionEffectiveStatement;
25 import org.opendaylight.yangtools.yang.model.api.stmt.ReferenceEffectiveStatement;
26 import org.opendaylight.yangtools.yang.model.api.stmt.StatusEffectiveStatement;
27 import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement;
28 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement;
29 import org.opendaylight.yangtools.yang.model.api.stmt.TypedefEffectiveStatement;
30 import org.opendaylight.yangtools.yang.model.api.stmt.TypedefStatement;
31 import org.opendaylight.yangtools.yang.model.api.stmt.UnitsEffectiveStatement;
32 import org.opendaylight.yangtools.yang.model.ri.type.DerivedTypeBuilder;
33 import org.opendaylight.yangtools.yang.model.ri.type.DerivedTypes;
34 import org.opendaylight.yangtools.yang.model.spi.meta.AbstractDeclaredEffectiveStatement.DefaultArgument.WithSubstatements;
35 import org.opendaylight.yangtools.yang.model.spi.meta.EffectiveStatementMixins.SchemaNodeMixin;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
39 public final class TypedefEffectiveStatementImpl extends WithSubstatements<QName, TypedefStatement>
40 implements TypedefEffectiveStatement, SchemaNodeMixin<TypedefStatement> {
41 private static final Logger LOG = LoggerFactory.getLogger(TypedefEffectiveStatementImpl.class);
43 private static final VarHandle TYPE_DEFINITION;
44 private static final VarHandle TYPE_STATEMENT;
47 final Lookup lookup = MethodHandles.lookup();
49 TYPE_DEFINITION = lookup.findVarHandle(TypedefEffectiveStatementImpl.class, "typeDefinition",
50 TypeDefinition.class);
51 TYPE_STATEMENT = lookup.findVarHandle(TypedefEffectiveStatementImpl.class, "typeStatement",
52 ProxyTypeEffectiveStatement.class);
53 } catch (NoSuchFieldException | IllegalAccessException e) {
54 throw new ExceptionInInitializerError(e);
58 private final int flags;
60 // Accessed via TYPE_DEFINITION
61 @SuppressWarnings("unused")
62 private volatile TypeDefinition<?> typeDefinition;
63 // Accessed via TYPE_STATEMENT
64 @SuppressWarnings("unused")
65 private volatile ProxyTypeEffectiveStatement typeStatement;
67 public TypedefEffectiveStatementImpl(final TypedefStatement declared, final int flags,
68 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
69 super(declared, substatements);
79 public TypeDefinition<?> getTypeDefinition() {
80 final TypeDefinition<?> existing = (TypeDefinition<?>) TYPE_DEFINITION.getAcquire(this);
81 return existing != null ? existing : loadTypeDefinition();
85 public TypeEffectiveStatement<TypeStatement> asTypeEffectiveStatement() {
86 final ProxyTypeEffectiveStatement local = (ProxyTypeEffectiveStatement) TYPE_STATEMENT.getAcquire(this);
87 return local != null ? local : loadTypeStatement();
90 private @NonNull TypeDefinition<?> loadTypeDefinition() {
91 final TypeEffectiveStatement<?> type = findFirstEffectiveSubstatement(TypeEffectiveStatement.class).get();
92 final DerivedTypeBuilder<?> builder = DerivedTypes.derivedTypeBuilder(type.getTypeDefinition(), argument());
94 for (final EffectiveStatement<?, ?> stmt : effectiveSubstatements()) {
95 if (stmt instanceof DefaultEffectiveStatement) {
96 builder.setDefaultValue(((DefaultEffectiveStatement) stmt).argument());
97 } else if (stmt instanceof DescriptionEffectiveStatement) {
98 builder.setDescription(((DescriptionEffectiveStatement)stmt).argument());
99 } else if (stmt instanceof ReferenceEffectiveStatement) {
100 builder.setReference(((ReferenceEffectiveStatement)stmt).argument());
101 } else if (stmt instanceof StatusEffectiveStatement) {
102 builder.setStatus(((StatusEffectiveStatement)stmt).argument());
103 } else if (stmt instanceof UnitsEffectiveStatement) {
104 builder.setUnits(((UnitsEffectiveStatement)stmt).argument());
105 } else if (stmt instanceof UnknownSchemaNode) {
106 // FIXME: should not directly implement, I think
107 builder.addUnknownSchemaNode((UnknownSchemaNode)stmt);
108 } else if (!(stmt instanceof TypeEffectiveStatement)) {
109 LOG.debug("Ignoring statement {}", stmt);
113 final TypeDefinition<?> created = builder.build();
114 final Object witness = TYPE_DEFINITION.compareAndExchangeRelease(this, null, created);
115 return witness == null ? created : (TypeDefinition<?>) witness;
118 private @NonNull ProxyTypeEffectiveStatement loadTypeStatement() {
119 final ProxyTypeEffectiveStatement created = new ProxyTypeEffectiveStatement();
120 final Object witness = TYPE_STATEMENT.compareAndExchangeRelease(this, null, created);
121 return witness == null ? created : (ProxyTypeEffectiveStatement) witness;
124 private final class ProxyTypeEffectiveStatement implements TypeEffectiveStatement<TypeStatement> {
126 public TypeStatement getDeclared() {
131 public <K, V, N extends IdentifierNamespace<K, V>> Optional<V> get(final Class<N> namespace,
132 final K identifier) {
133 return TypedefEffectiveStatementImpl.this.get(namespace, identifier);
137 public <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAll(final Class<N> namespace) {
138 return TypedefEffectiveStatementImpl.this.getAll(namespace);
142 public ImmutableList<? extends EffectiveStatement<?, ?>> effectiveSubstatements() {
143 return TypedefEffectiveStatementImpl.this.effectiveSubstatements();
147 public QName argument() {
148 return TypedefEffectiveStatementImpl.this.argument();
152 public StatementOrigin statementOrigin() {
153 return StatementOrigin.CONTEXT;
157 public TypeDefinition<?> getTypeDefinition() {
158 return TypedefEffectiveStatementImpl.this.getTypeDefinition();