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.augment;
10 import com.google.common.collect.ImmutableList;
11 import java.util.regex.Pattern;
12 import java.util.stream.Stream;
13 import org.opendaylight.yangtools.yang.common.Empty;
14 import org.opendaylight.yangtools.yang.model.api.Status;
15 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
16 import org.opendaylight.yangtools.yang.model.api.meta.DeclarationReference;
17 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
18 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
19 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
20 import org.opendaylight.yangtools.yang.model.api.stmt.AugmentEffectiveStatement;
21 import org.opendaylight.yangtools.yang.model.api.stmt.AugmentStatement;
22 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
23 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
24 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Descendant;
25 import org.opendaylight.yangtools.yang.model.api.stmt.StatusEffectiveStatement;
26 import org.opendaylight.yangtools.yang.model.api.stmt.WhenEffectiveStatement;
27 import org.opendaylight.yangtools.yang.model.ri.stmt.DeclaredStatementDecorators;
28 import org.opendaylight.yangtools.yang.model.ri.stmt.DeclaredStatements;
29 import org.opendaylight.yangtools.yang.model.ri.stmt.EffectiveStatements;
30 import org.opendaylight.yangtools.yang.model.spi.meta.EffectiveStatementMixins.EffectiveStatementWithFlags.FlagsBuilder;
31 import org.opendaylight.yangtools.yang.model.spi.meta.SubstatementIndexingException;
32 import org.opendaylight.yangtools.yang.parser.api.YangParserConfiguration;
33 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.ArgumentUtils;
34 import org.opendaylight.yangtools.yang.parser.spi.ParserNamespaces;
35 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
36 import org.opendaylight.yangtools.yang.parser.spi.meta.BoundStmtCtx;
37 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
38 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
39 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.Prerequisite;
40 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
41 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
42 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
43 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
44 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
45 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
47 abstract class AbstractAugmentStatementSupport
48 extends AbstractStatementSupport<SchemaNodeIdentifier, AugmentStatement, AugmentEffectiveStatement> {
49 private static final Pattern PATH_REL_PATTERN1 = Pattern.compile("\\.\\.?\\s*/(.+)");
50 private static final Pattern PATH_REL_PATTERN2 = Pattern.compile("//.*");
52 AbstractAugmentStatementSupport(final YangParserConfiguration config, final SubstatementValidator validator) {
53 super(YangStmtMapping.AUGMENT, StatementPolicy.copyDeclared(
54 (copy, current, substatements) -> copy.getArgument().equals(current.getArgument())),
59 public final SchemaNodeIdentifier parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
60 SourceException.throwIf(PATH_REL_PATTERN1.matcher(value).matches()
61 || PATH_REL_PATTERN2.matcher(value).matches(), ctx,
62 "Augment argument \'%s\' is not valid, it can be only absolute path; or descendant if used in uses", value);
65 // https://www.rfc-editor.org/rfc/rfc6020#section-7.15
66 // https://www.rfc-editor.org/rfc/rfc7950#section-7.17
68 // The argument is either Absolute or Descendant based on whether the statement is declared within a 'uses'
69 // statement. The mechanics differs wildly between the two cases, so let's start by ensuring our argument
70 // is in the correct domain.
71 final SchemaNodeIdentifier result = ArgumentUtils.nodeIdentifierFromPath(ctx, value);
72 final StatementDefinition parent = ctx.coerceParentContext().publicDefinition();
73 if (parent == YangStmtMapping.USES) {
74 SourceException.throwIf(result instanceof Absolute, ctx,
75 "Absolute schema node identifier is not allowed when used within a uses statement");
77 SourceException.throwIf(result instanceof Descendant, ctx,
78 "Descendant schema node identifier is not allowed when used outside of a uses statement");
84 public final void onFullDefinitionDeclared(
85 final Mutable<SchemaNodeIdentifier, AugmentStatement, AugmentEffectiveStatement> augmentNode) {
86 if (!augmentNode.isSupportedByFeatures()) {
87 // We need this augment node to be present, but it should not escape to effective world
88 augmentNode.setUnsupported();
91 super.onFullDefinitionDeclared(augmentNode);
93 if (StmtContextUtils.isInExtensionBody(augmentNode)) {
97 final ModelActionBuilder augmentAction = augmentNode.newInferenceAction(ModelProcessingPhase.EFFECTIVE_MODEL);
98 augmentAction.requiresCtx(augmentNode, ModelProcessingPhase.EFFECTIVE_MODEL);
99 final Prerequisite<Mutable<?, ?, EffectiveStatement<?, ?>>> target = augmentAction.mutatesEffectiveCtxPath(
100 getSearchRoot(augmentNode), ParserNamespaces.schemaTree(), augmentNode.getArgument().getNodeIdentifiers());
102 augmentAction.apply(new AugmentInferenceAction(this, augmentNode, target));
106 protected final AugmentStatement createDeclared(final BoundStmtCtx<SchemaNodeIdentifier> ctx,
107 final ImmutableList<DeclaredStatement<?>> substatements) {
108 return DeclaredStatements.createAugment(ctx.getRawArgument(), ctx.getArgument(), substatements);
112 protected final AugmentStatement attachDeclarationReference(final AugmentStatement stmt,
113 final DeclarationReference reference) {
114 return DeclaredStatementDecorators.decorateAugment(stmt, reference);
118 protected final Stream<? extends StmtContext<?, ?, ?>> statementsToBuild(
119 final Current<SchemaNodeIdentifier, AugmentStatement> stmt,
120 final Stream<? extends StmtContext<?, ?, ?>> substatements) {
121 // Pick up the marker left by onFullDefinitionDeclared() inference action. If it is present we need to pass our
122 // children through target's implicit wrapping.
123 final var implicitDef = stmt.namespaceItem(AugmentImplicitHandlingNamespace.INSTANCE, Empty.value());
124 return implicitDef == null ? substatements
125 : substatements.map(subCtx -> implicitDef.wrapWithImplicit(subCtx));
129 protected final AugmentEffectiveStatement createEffective(
130 final Current<SchemaNodeIdentifier, AugmentStatement> stmt,
131 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
132 final int flags = new FlagsBuilder()
133 .setStatus(findFirstArgument(substatements, StatusEffectiveStatement.class, Status.CURRENT))
137 return EffectiveStatements.createAugment(stmt.declared(), stmt.getArgument(), flags, substatements);
138 } catch (SubstatementIndexingException e) {
139 throw new SourceException(e.getMessage(), stmt, e);
143 abstract boolean allowsMandatory(StmtContext<?, ?, ?> ctx);
145 static StmtContext<?, ?, ?> getSearchRoot(final StmtContext<?, ?, ?> augmentContext) {
146 // Augment is in uses - we need to augment instantiated nodes in parent.
147 final StmtContext<?, ?, ?> parent = augmentContext.coerceParentContext();
148 if (YangStmtMapping.USES == parent.publicDefinition()) {
149 return parent.getParentContext();
154 static boolean hasWhenSubstatement(final StmtContext<?, ?, ?> ctx) {
155 return ctx.hasSubstatement(WhenEffectiveStatement.class);