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.rfc8040.parser;
10 import static com.google.common.base.Verify.verifyNotNull;
12 import com.google.common.annotations.Beta;
13 import com.google.common.collect.ImmutableList;
14 import org.eclipse.jdt.annotation.NonNull;
15 import org.opendaylight.yangtools.rfc8040.model.api.YangDataEffectiveStatement;
16 import org.opendaylight.yangtools.rfc8040.model.api.YangDataStatement;
17 import org.opendaylight.yangtools.rfc8040.model.api.YangDataStatements;
18 import org.opendaylight.yangtools.yang.common.Empty;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
21 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
22 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
23 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
24 import org.opendaylight.yangtools.yang.model.api.stmt.DataTreeEffectiveStatement;
25 import org.opendaylight.yangtools.yang.model.spi.meta.AbstractDeclaredStatement.WithRawStringArgument.WithSubstatements;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStringStatementSupport;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.InvalidSubstatementException;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.MissingSubstatementException;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
33 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
34 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
37 public final class YangDataStatementSupport
38 extends AbstractStringStatementSupport<YangDataStatement, YangDataEffectiveStatement> {
40 * Declared statement representation of 'yang-data' extension defined in
41 * <a href="https://tools.ietf.org/html/rfc8040#section-8">RFC 8040</a>.
43 private static final class Declared extends WithSubstatements implements YangDataStatement {
44 Declared(final String rawArgument, final ImmutableList<? extends DeclaredStatement<?>> substatements) {
45 super(rawArgument, substatements);
49 private static final YangDataStatementSupport INSTANCE = new YangDataStatementSupport(YangDataStatements.YANG_DATA);
51 private final SubstatementValidator declaredValidator;
53 private YangDataStatementSupport(final StatementDefinition definition) {
54 super(definition, StatementPolicy.reject());
55 declaredValidator = SubstatementValidator.builder(definition)
56 .addMandatory(YangStmtMapping.CONTAINER)
57 .addOptional(YangStmtMapping.USES)
61 public static YangDataStatementSupport getInstance() {
66 public void onStatementAdded(final Mutable<String, YangDataStatement, YangDataEffectiveStatement> ctx) {
67 // as per https://tools.ietf.org/html/rfc8040#section-8,
68 // yang-data is ignored unless it appears as a top-level statement
69 if (ctx.coerceParentContext().getParentContext() != null) {
70 ctx.setIsSupportedToBuildEffective(false);
75 public void onFullDefinitionDeclared(final Mutable<String, YangDataStatement, YangDataEffectiveStatement> ctx) {
76 // Parse and populate our argument to be picked up when we build the effecitve statement
77 final String argument = SourceException.throwIfNull(ctx.argument(), ctx, "yang-data requires an argument");
78 final QName qname = StmtContextUtils.parseIdentifier(ctx, argument);
79 ctx.addToNs(YangDataArgumentNamespace.class, Empty.getInstance(), qname);
83 public boolean isIgnoringIfFeatures() {
88 public boolean isIgnoringConfig() {
93 protected SubstatementValidator getSubstatementValidator() {
94 return declaredValidator;
98 protected YangDataStatement createDeclared(@NonNull final StmtContext<String, YangDataStatement, ?> ctx,
99 final ImmutableList<? extends DeclaredStatement<?>> substatements) {
100 return new Declared(ctx.getRawArgument(), substatements);
104 protected YangDataEffectiveStatement createEffective(final Current<String, YangDataStatement> stmt,
105 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
106 // So now we need to deal with effective validation. The requirement is that:
107 // It MUST contain data definition statements
108 // that result in exactly one container data node definition.
109 final long dataDefs = substatements.stream().filter(DataTreeEffectiveStatement.class::isInstance).count();
111 throw new MissingSubstatementException("yang-data requires exactly one container", stmt.sourceReference());
114 throw new InvalidSubstatementException(stmt,
115 "yang-data requires exactly one data definition node, found %s", dataDefs);
118 return new YangDataEffectiveStatementImpl(stmt, substatements,
119 verifyNotNull(stmt.namespaceItem(YangDataArgumentNamespace.class, Empty.getInstance())));