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 static com.google.common.base.Verify.verify;
12 import com.google.common.collect.ImmutableList;
13 import com.google.common.collect.Lists;
14 import java.util.List;
15 import java.util.Objects;
16 import java.util.regex.Pattern;
17 import org.opendaylight.yangtools.yang.common.Empty;
18 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
19 import org.opendaylight.yangtools.yang.model.api.Status;
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.AugmentEffectiveStatement;
25 import org.opendaylight.yangtools.yang.model.api.stmt.AugmentStatement;
26 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
27 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
28 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Descendant;
29 import org.opendaylight.yangtools.yang.model.api.stmt.StatusEffectiveStatement;
30 import org.opendaylight.yangtools.yang.model.api.stmt.WhenEffectiveStatement;
31 import org.opendaylight.yangtools.yang.model.ri.stmt.DeclaredStatements;
32 import org.opendaylight.yangtools.yang.model.ri.stmt.EffectiveStatements;
33 import org.opendaylight.yangtools.yang.model.spi.meta.EffectiveStatementMixins.EffectiveStatementWithFlags.FlagsBuilder;
34 import org.opendaylight.yangtools.yang.model.spi.meta.SubstatementIndexingException;
35 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.ArgumentUtils;
36 import org.opendaylight.yangtools.yang.parser.spi.SchemaTreeNamespace;
37 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
38 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
39 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
40 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.Prerequisite;
41 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
42 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
43 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
44 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
45 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
46 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
48 abstract class AbstractAugmentStatementSupport
49 extends AbstractStatementSupport<SchemaNodeIdentifier, AugmentStatement, AugmentEffectiveStatement> {
50 private static final Pattern PATH_REL_PATTERN1 = Pattern.compile("\\.\\.?\\s*/(.+)");
51 private static final Pattern PATH_REL_PATTERN2 = Pattern.compile("//.*");
53 AbstractAugmentStatementSupport() {
54 super(YangStmtMapping.AUGMENT, StatementPolicy.copyDeclared(
55 (copy, current, substatements) ->
56 copy.getArgument().equals(current.getArgument())
57 && copy.moduleName().getModule().equals(current.moduleName().getModule())
58 && Objects.equals(copy.original(), current.original())
63 public final SchemaNodeIdentifier parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
64 SourceException.throwIf(PATH_REL_PATTERN1.matcher(value).matches()
65 || PATH_REL_PATTERN2.matcher(value).matches(), ctx,
66 "Augment argument \'%s\' is not valid, it can be only absolute path; or descendant if used in uses", value);
69 // https://tools.ietf.org/html/rfc6020#section-7.15
70 // https://tools.ietf.org/html/rfc7950#section-7.17
72 // The argument is either Absolute or Descendant based on whether the statement is declared within a 'uses'
73 // statement. The mechanics differs wildly between the two cases, so let's start by ensuring our argument
74 // is in the correct domain.
75 final SchemaNodeIdentifier result = ArgumentUtils.nodeIdentifierFromPath(ctx, value);
76 final StatementDefinition parent = ctx.coerceParentContext().publicDefinition();
77 if (parent == YangStmtMapping.USES) {
78 SourceException.throwIf(result instanceof Absolute, ctx,
79 "Absolute schema node identifier is not allowed when used within a uses statement");
81 SourceException.throwIf(result instanceof Descendant, ctx,
82 "Descendant schema node identifier is not allowed when used outside of a uses statement");
88 public final void onFullDefinitionDeclared(
89 final Mutable<SchemaNodeIdentifier, AugmentStatement, AugmentEffectiveStatement> augmentNode) {
90 if (!augmentNode.isSupportedByFeatures()) {
91 // We need this augment node to be present, but it should not escape to effective world
92 augmentNode.setIsSupportedToBuildEffective(false);
95 super.onFullDefinitionDeclared(augmentNode);
97 if (StmtContextUtils.isInExtensionBody(augmentNode)) {
101 final ModelActionBuilder augmentAction = augmentNode.newInferenceAction(ModelProcessingPhase.EFFECTIVE_MODEL);
102 augmentAction.requiresCtx(augmentNode, ModelProcessingPhase.EFFECTIVE_MODEL);
103 final Prerequisite<Mutable<?, ?, EffectiveStatement<?, ?>>> target = augmentAction.mutatesEffectiveCtxPath(
104 getSearchRoot(augmentNode), SchemaTreeNamespace.class, augmentNode.getArgument().getNodeIdentifiers());
106 augmentAction.apply(new AugmentInferenceAction(this, augmentNode, target));
110 protected final AugmentStatement createDeclared(final StmtContext<SchemaNodeIdentifier, AugmentStatement, ?> ctx,
111 final ImmutableList<? extends DeclaredStatement<?>> substatements) {
112 return DeclaredStatements.createAugment(ctx.getRawArgument(), ctx.getArgument(), substatements);
116 protected final List<? extends StmtContext<?, ?, ?>> statementsToBuild(
117 final Current<SchemaNodeIdentifier, AugmentStatement> stmt,
118 final List<? extends StmtContext<?, ?, ?>> substatements) {
119 // Pick up the marker left by onFullDefinitionDeclared() inference action. If it is present we need to pass our
120 // children through target's implicit wrapping.
121 final StatementContextBase<?, ?, ?> implicitDef = stmt.getFromNamespace(AugmentImplicitHandlingNamespace.class,
122 Empty.getInstance());
123 return implicitDef == null ? substatements : Lists.transform(substatements, subCtx -> {
124 verify(subCtx instanceof StatementContextBase);
125 return implicitDef.wrapWithImplicit((StatementContextBase<?, ?, ?>) subCtx);
130 protected final AugmentEffectiveStatement createEffective(
131 final Current<SchemaNodeIdentifier, AugmentStatement> stmt,
132 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
133 final int flags = new FlagsBuilder()
134 .setStatus(findFirstArgument(substatements, StatusEffectiveStatement.class, Status.CURRENT))
138 return EffectiveStatements.createAugment(stmt.declared(), stmt.getArgument(), flags,
139 stmt.moduleName().getModule(), substatements, stmt.original(AugmentationSchemaNode.class));
140 } catch (SubstatementIndexingException e) {
141 throw new SourceException(e.getMessage(), stmt, e);
145 abstract boolean allowsMandatory(StmtContext<?, ?, ?> ctx);
147 static StmtContext<?, ?, ?> getSearchRoot(final StmtContext<?, ?, ?> augmentContext) {
148 // Augment is in uses - we need to augment instantiated nodes in parent.
149 final StmtContext<?, ?, ?> parent = augmentContext.coerceParentContext();
150 if (YangStmtMapping.USES == parent.publicDefinition()) {
151 return parent.getParentContext();
156 static boolean hasWhenSubstatement(final StmtContext<?, ?, ?> ctx) {
157 return ctx.hasSubstatement(WhenEffectiveStatement.class);