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.rfc7950.stmt.BaseStatementSupport;
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 BaseStatementSupport<String, UnionSpecification, EffectiveStatement<String, UnionSpecification>> {
28 private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(YangStmtMapping
30 .addMultiple(YangStmtMapping.TYPE)
33 UnionSpecificationSupport() {
34 super(YangStmtMapping.TYPE);
38 public String parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
43 protected SubstatementValidator getSubstatementValidator() {
44 return SUBSTATEMENT_VALIDATOR;
48 protected UnionSpecification createDeclared(final StmtContext<String, UnionSpecification, ?> ctx,
49 final ImmutableList<? extends DeclaredStatement<?>> substatements) {
50 return new UnionSpecificationImpl(ctx.coerceRawStatementArgument(), substatements);
54 protected UnionSpecification createEmptyDeclared(final StmtContext<String, UnionSpecification, ?> ctx) {
59 protected EffectiveStatement<String, UnionSpecification> createEffective(
60 final Current<String, UnionSpecification> stmt,
61 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
62 if (substatements.isEmpty()) {
66 final UnionTypeBuilder builder = BaseTypes.unionTypeBuilder(stmt.getSchemaPath());
68 for (final EffectiveStatement<?, ?> subStmt : substatements) {
69 if (subStmt instanceof TypeEffectiveStatement) {
70 builder.addType(((TypeEffectiveStatement<?>)subStmt).getTypeDefinition());
74 return new TypeEffectiveStatementImpl<>(stmt.declared(), substatements, builder);
77 private static SourceException noType(final @NonNull CommonStmtCtx stmt) {
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", stmt.sourceReference());