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.opendaylight.yangtools.yang.model.api.YangStmtMapping;
12 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
13 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
14 import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement;
15 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement.UnionSpecification;
16 import org.opendaylight.yangtools.yang.model.util.type.BaseTypes;
17 import org.opendaylight.yangtools.yang.model.util.type.UnionTypeBuilder;
18 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.BaseStatementSupport;
19 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
20 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
21 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
23 final class UnionSpecificationSupport
24 extends BaseStatementSupport<String, UnionSpecification, EffectiveStatement<String, UnionSpecification>> {
25 private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(YangStmtMapping
27 .addMultiple(YangStmtMapping.TYPE)
30 UnionSpecificationSupport() {
31 super(YangStmtMapping.TYPE);
35 public String parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
40 protected SubstatementValidator getSubstatementValidator() {
41 return SUBSTATEMENT_VALIDATOR;
45 protected UnionSpecification createDeclared(final StmtContext<String, UnionSpecification, ?> ctx,
46 final ImmutableList<? extends DeclaredStatement<?>> substatements) {
47 return new UnionSpecificationImpl(ctx, substatements);
51 protected UnionSpecification createEmptyDeclared(final StmtContext<String, UnionSpecification, ?> ctx) {
56 protected EffectiveStatement<String, UnionSpecification> createEffective(
57 final StmtContext<String, UnionSpecification, EffectiveStatement<String, UnionSpecification>> ctx,
58 final UnionSpecification declared, final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
59 final UnionTypeBuilder builder = BaseTypes.unionTypeBuilder(ctx.getSchemaPath().get());
61 for (final EffectiveStatement<?, ?> stmt : substatements) {
62 if (stmt instanceof TypeEffectiveStatement) {
63 builder.addType(((TypeEffectiveStatement<?>)stmt).getTypeDefinition());
67 return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
71 protected EffectiveStatement<String, UnionSpecification> createEmptyEffective(
72 final StmtContext<String, UnionSpecification, EffectiveStatement<String, UnionSpecification>> ctx,
73 final UnionSpecification declared) {
77 private static SourceException noType(final StmtContext<?, ?, ?> ctx) {
79 * https://tools.ietf.org/html/rfc7950#section-9.12
81 * When the type is "union", the "type" statement (Section 7.4) MUST be
84 return new SourceException("At least one type statement has to be present", ctx.getStatementSourceReference());