2 * Copyright (c) 2017 Pantheon Technologies, s.r.o. 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.type;
10 import com.google.common.collect.ImmutableList;
11 import org.eclipse.jdt.annotation.NonNull;
12 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
13 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
14 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
15 import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement;
16 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement.UnionSpecification;
17 import org.opendaylight.yangtools.yang.model.util.type.BaseTypes;
18 import org.opendaylight.yangtools.yang.model.util.type.UnionTypeBuilder;
19 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStringStatementSupport;
20 import org.opendaylight.yangtools.yang.parser.spi.meta.CommonStmtCtx;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
24 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
26 final class UnionSpecificationSupport
27 extends AbstractStringStatementSupport<UnionSpecification, EffectiveStatement<String, UnionSpecification>> {
28 private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(YangStmtMapping
30 .addMultiple(YangStmtMapping.TYPE)
33 UnionSpecificationSupport() {
34 super(YangStmtMapping.TYPE, StatementPolicy.exactReplica());
38 protected SubstatementValidator getSubstatementValidator() {
39 return SUBSTATEMENT_VALIDATOR;
43 protected UnionSpecification createDeclared(final StmtContext<String, UnionSpecification, ?> ctx,
44 final ImmutableList<? extends DeclaredStatement<?>> substatements) {
45 return new UnionSpecificationImpl(ctx.getRawArgument(), substatements);
49 protected UnionSpecification createEmptyDeclared(final StmtContext<String, UnionSpecification, ?> ctx) {
54 protected EffectiveStatement<String, UnionSpecification> createEffective(
55 final Current<String, UnionSpecification> stmt,
56 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
57 if (substatements.isEmpty()) {
61 final UnionTypeBuilder builder = BaseTypes.unionTypeBuilder(stmt.argumentAsTypeQName());
63 for (final EffectiveStatement<?, ?> subStmt : substatements) {
64 if (subStmt instanceof TypeEffectiveStatement) {
65 builder.addType(((TypeEffectiveStatement<?>)subStmt).getTypeDefinition());
69 return new TypeEffectiveStatementImpl<>(stmt.declared(), substatements, builder);
72 private static SourceException noType(final @NonNull CommonStmtCtx stmt) {
74 * https://tools.ietf.org/html/rfc7950#section-9.12
76 * When the type is "union", the "type" statement (Section 7.4) MUST be
79 return new SourceException("At least one type statement has to be present", stmt);