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;
14 import java.util.Collection;
16 import java.util.Optional;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
20 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
22 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
23 import org.opendaylight.yangtools.yang.model.api.meta.StatementOrigin;
24 import org.opendaylight.yangtools.yang.model.api.stmt.DefaultEffectiveStatement;
25 import org.opendaylight.yangtools.yang.model.api.stmt.DescriptionEffectiveStatement;
26 import org.opendaylight.yangtools.yang.model.api.stmt.ReferenceEffectiveStatement;
27 import org.opendaylight.yangtools.yang.model.api.stmt.StatusEffectiveStatement;
28 import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement;
29 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement;
30 import org.opendaylight.yangtools.yang.model.api.stmt.TypedefEffectiveStatement;
31 import org.opendaylight.yangtools.yang.model.api.stmt.TypedefStatement;
32 import org.opendaylight.yangtools.yang.model.api.stmt.UnitsEffectiveStatement;
33 import org.opendaylight.yangtools.yang.model.ri.type.DerivedTypeBuilder;
34 import org.opendaylight.yangtools.yang.model.ri.type.DerivedTypes;
35 import org.opendaylight.yangtools.yang.model.spi.meta.AbstractDeclaredEffectiveStatement.DefaultArgument.WithSubstatements;
36 import org.opendaylight.yangtools.yang.model.spi.meta.EffectiveStatementMixins.SchemaNodeMixin;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
40 public final class TypedefEffectiveStatementImpl extends WithSubstatements<QName, TypedefStatement>
41 implements TypedefEffectiveStatement, SchemaNodeMixin<TypedefStatement> {
42 private static final Logger LOG = LoggerFactory.getLogger(TypedefEffectiveStatementImpl.class);
44 private static final VarHandle TYPE_DEFINITION;
45 private static final VarHandle TYPE_STATEMENT;
48 final Lookup lookup = MethodHandles.lookup();
50 TYPE_DEFINITION = lookup.findVarHandle(TypedefEffectiveStatementImpl.class, "typeDefinition",
51 TypeDefinition.class);
52 TYPE_STATEMENT = lookup.findVarHandle(TypedefEffectiveStatementImpl.class, "typeStatement",
53 ProxyTypeEffectiveStatement.class);
54 } catch (NoSuchFieldException | IllegalAccessException e) {
55 throw new ExceptionInInitializerError(e);
59 private final int flags;
61 // Accessed via TYPE_DEFINITION
62 @SuppressWarnings("unused")
63 private volatile TypeDefinition<?> typeDefinition;
64 // Accessed via TYPE_STATEMENT
65 @SuppressWarnings("unused")
66 private volatile ProxyTypeEffectiveStatement typeStatement;
68 public TypedefEffectiveStatementImpl(final TypedefStatement declared, final int flags,
69 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
70 super(declared, substatements);
80 public TypeDefinition<?> getTypeDefinition() {
81 final TypeDefinition<?> existing = (TypeDefinition<?>) TYPE_DEFINITION.getAcquire(this);
82 return existing != null ? existing : loadTypeDefinition();
86 public TypeEffectiveStatement<TypeStatement> asTypeEffectiveStatement() {
87 final ProxyTypeEffectiveStatement local = (ProxyTypeEffectiveStatement) TYPE_STATEMENT.getAcquire(this);
88 return local != null ? local : loadTypeStatement();
91 private @NonNull TypeDefinition<?> loadTypeDefinition() {
92 final TypeEffectiveStatement<?> type = findFirstEffectiveSubstatement(TypeEffectiveStatement.class).get();
93 final DerivedTypeBuilder<?> builder = DerivedTypes.derivedTypeBuilder(type.getTypeDefinition(), argument());
95 for (final EffectiveStatement<?, ?> stmt : effectiveSubstatements()) {
96 if (stmt instanceof DefaultEffectiveStatement) {
97 builder.setDefaultValue(((DefaultEffectiveStatement) stmt).argument());
98 } else if (stmt instanceof DescriptionEffectiveStatement) {
99 builder.setDescription(((DescriptionEffectiveStatement)stmt).argument());
100 } else if (stmt instanceof ReferenceEffectiveStatement) {
101 builder.setReference(((ReferenceEffectiveStatement)stmt).argument());
102 } else if (stmt instanceof StatusEffectiveStatement) {
103 builder.setStatus(((StatusEffectiveStatement)stmt).argument());
104 } else if (stmt instanceof UnitsEffectiveStatement) {
105 builder.setUnits(((UnitsEffectiveStatement)stmt).argument());
106 } else if (stmt instanceof UnknownSchemaNode) {
107 // FIXME: should not directly implement, I think
108 builder.addUnknownSchemaNode((UnknownSchemaNode)stmt);
109 } else if (!(stmt instanceof TypeEffectiveStatement)) {
110 LOG.debug("Ignoring statement {}", stmt);
114 final TypeDefinition<?> created = builder.build();
115 final Object witness = TYPE_DEFINITION.compareAndExchangeRelease(this, null, created);
116 return witness == null ? created : (TypeDefinition<?>) witness;
119 private @NonNull ProxyTypeEffectiveStatement loadTypeStatement() {
120 final ProxyTypeEffectiveStatement created = new ProxyTypeEffectiveStatement();
121 final Object witness = TYPE_STATEMENT.compareAndExchangeRelease(this, null, created);
122 return witness == null ? created : (ProxyTypeEffectiveStatement) witness;
125 private final class ProxyTypeEffectiveStatement implements TypeEffectiveStatement<TypeStatement> {
127 public TypeStatement getDeclared() {
132 public <K, V, N extends IdentifierNamespace<K, V>> Optional<V> get(final Class<N> namespace,
133 final K identifier) {
134 return TypedefEffectiveStatementImpl.this.get(namespace, identifier);
138 public <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAll(final Class<N> namespace) {
139 return TypedefEffectiveStatementImpl.this.getAll(namespace);
143 public Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements() {
144 return TypedefEffectiveStatementImpl.this.effectiveSubstatements();
148 public QName argument() {
149 return TypedefEffectiveStatementImpl.this.argument();
153 public StatementOrigin statementOrigin() {
154 return StatementOrigin.CONTEXT;
158 public TypeDefinition<?> getTypeDefinition() {
159 return TypedefEffectiveStatementImpl.this.getTypeDefinition();