3decdf81ae9628fb90e6818d4569e2b27b5ccef0
[yangtools.git] / parser / rfc7952-parser-support / src / main / java / org / opendaylight / yangtools / rfc7952 / parser / AnnotationEffectiveStatementImpl.java
1 /*
2  * Copyright (c) 2020 PANTHEON.tech, 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.rfc7952.parser;
9
10 import static com.google.common.base.Verify.verifyNotNull;
11
12 import com.google.common.collect.ImmutableList;
13 import org.eclipse.jdt.annotation.NonNull;
14 import org.opendaylight.yangtools.rfc7952.model.api.AnnotationEffectiveStatement;
15 import org.opendaylight.yangtools.rfc7952.model.api.AnnotationSchemaNode;
16 import org.opendaylight.yangtools.rfc7952.model.api.AnnotationStatement;
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.meta.EffectiveStatement;
20 import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement;
21 import org.opendaylight.yangtools.yang.model.api.stmt.UnitsEffectiveStatement;
22 import org.opendaylight.yangtools.yang.model.ri.type.ConcreteTypeBuilder;
23 import org.opendaylight.yangtools.yang.model.ri.type.ConcreteTypes;
24 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.UnknownEffectiveStatementBase;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
26 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
27
28 final class AnnotationEffectiveStatementImpl extends UnknownEffectiveStatementBase<QName, AnnotationStatement>
29         implements AnnotationEffectiveStatement, AnnotationSchemaNode {
30     private final @NonNull TypeDefinition<?> type;
31
32     AnnotationEffectiveStatementImpl(final Current<QName, AnnotationStatement> stmt,
33             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
34         super(stmt, substatements);
35         final QName qname = stmt.getArgument();
36
37         // FIXME: move this into onFullDefinitionDeclared()
38         final TypeEffectiveStatement<?> typeStmt = SourceException.throwIfNull(
39             firstSubstatementOfType(TypeEffectiveStatement.class), stmt,
40             "AnnotationStatementSupport %s is missing a 'type' statement", qname);
41
42         final ConcreteTypeBuilder<?> builder = ConcreteTypes.concreteTypeBuilder(typeStmt.getTypeDefinition(),
43             qname);
44         final UnitsEffectiveStatement unitsStmt = firstSubstatementOfType(UnitsEffectiveStatement.class);
45         if (unitsStmt != null) {
46             builder.setUnits(unitsStmt.argument());
47         }
48         type = builder.build();
49     }
50
51     @Override
52     public QName getQName() {
53         return verifyNotNull(argument());
54     }
55
56     @Override
57     public TypeDefinition<?> getType() {
58         return type;
59     }
60
61     @Override
62     public TypeDefinition<?> getTypeDefinition() {
63         return type;
64     }
65
66     @Override
67     public AnnotationEffectiveStatement asEffectiveStatement() {
68         return this;
69     }
70 }