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.spi.meta.SubstatementIndexingException;
32 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.ArgumentUtils;
33 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.EffectiveStatementMixins.EffectiveStatementWithFlags.FlagsBuilder;
34 import org.opendaylight.yangtools.yang.parser.spi.SchemaTreeNamespace;
35 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
36 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
37 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
38 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.Prerequisite;
39 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
40 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
41 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
42 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
43 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
44 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
46 abstract class AbstractAugmentStatementSupport
47 extends AbstractStatementSupport<SchemaNodeIdentifier, AugmentStatement, AugmentEffectiveStatement> {
48 private static final Pattern PATH_REL_PATTERN1 = Pattern.compile("\\.\\.?\\s*/(.+)");
49 private static final Pattern PATH_REL_PATTERN2 = Pattern.compile("//.*");
51 AbstractAugmentStatementSupport() {
52 super(YangStmtMapping.AUGMENT, StatementPolicy.copyDeclared(
53 (copy, current, substatements) ->
54 copy.getArgument().equals(current.getArgument())
55 && copy.moduleName().getModule().equals(current.moduleName().getModule())
56 && Objects.equals(copy.original(), current.original())
61 public final SchemaNodeIdentifier parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
62 SourceException.throwIf(PATH_REL_PATTERN1.matcher(value).matches()
63 || PATH_REL_PATTERN2.matcher(value).matches(), ctx,
64 "Augment argument \'%s\' is not valid, it can be only absolute path; or descendant if used in uses", value);
67 // https://tools.ietf.org/html/rfc6020#section-7.15
68 // https://tools.ietf.org/html/rfc7950#section-7.17
70 // The argument is either Absolute or Descendant based on whether the statement is declared within a 'uses'
71 // statement. The mechanics differs wildly between the two cases, so let's start by ensuring our argument
72 // is in the correct domain.
73 final SchemaNodeIdentifier result = ArgumentUtils.nodeIdentifierFromPath(ctx, value);
74 final StatementDefinition parent = ctx.coerceParentContext().publicDefinition();
75 if (parent == YangStmtMapping.USES) {
76 SourceException.throwIf(result instanceof Absolute, ctx,
77 "Absolute schema node identifier is not allowed when used within a uses statement");
79 SourceException.throwIf(result instanceof Descendant, ctx,
80 "Descendant schema node identifier is not allowed when used outside of a uses statement");
86 public final void onFullDefinitionDeclared(
87 final Mutable<SchemaNodeIdentifier, AugmentStatement, AugmentEffectiveStatement> augmentNode) {
88 if (!augmentNode.isSupportedByFeatures()) {
89 // We need this augment node to be present, but it should not escape to effective world
90 augmentNode.setIsSupportedToBuildEffective(false);
93 super.onFullDefinitionDeclared(augmentNode);
95 if (StmtContextUtils.isInExtensionBody(augmentNode)) {
99 final ModelActionBuilder augmentAction = augmentNode.newInferenceAction(ModelProcessingPhase.EFFECTIVE_MODEL);
100 augmentAction.requiresCtx(augmentNode, ModelProcessingPhase.EFFECTIVE_MODEL);
101 final Prerequisite<Mutable<?, ?, EffectiveStatement<?, ?>>> target = augmentAction.mutatesEffectiveCtxPath(
102 getSearchRoot(augmentNode), SchemaTreeNamespace.class, augmentNode.getArgument().getNodeIdentifiers());
104 augmentAction.apply(new AugmentInferenceAction(this, augmentNode, target));
108 protected final AugmentStatement createDeclared(final StmtContext<SchemaNodeIdentifier, AugmentStatement, ?> ctx,
109 final ImmutableList<? extends DeclaredStatement<?>> substatements) {
110 return new RegularAugmentStatement(ctx.getRawArgument(), ctx.getArgument(), substatements);
114 protected final AugmentStatement createEmptyDeclared(
115 final StmtContext<SchemaNodeIdentifier, AugmentStatement, ?> ctx) {
116 return new EmptyAugmentStatement(ctx.getRawArgument(), ctx.getArgument());
120 protected final List<? extends StmtContext<?, ?, ?>> statementsToBuild(
121 final Current<SchemaNodeIdentifier, AugmentStatement> stmt,
122 final List<? extends StmtContext<?, ?, ?>> substatements) {
123 // Pick up the marker left by onFullDefinitionDeclared() inference action. If it is present we need to pass our
124 // children through target's implicit wrapping.
125 final StatementContextBase<?, ?, ?> implicitDef = stmt.getFromNamespace(AugmentImplicitHandlingNamespace.class,
126 Empty.getInstance());
127 return implicitDef == null ? substatements : Lists.transform(substatements, subCtx -> {
128 verify(subCtx instanceof StatementContextBase);
129 return implicitDef.wrapWithImplicit((StatementContextBase<?, ?, ?>) subCtx);
134 protected final AugmentEffectiveStatement createEffective(
135 final Current<SchemaNodeIdentifier, AugmentStatement> stmt,
136 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
137 final int flags = new FlagsBuilder()
138 .setStatus(findFirstArgument(substatements, StatusEffectiveStatement.class, Status.CURRENT))
142 return new AugmentEffectiveStatementImpl(stmt.declared(), stmt.getArgument(), flags,
143 stmt.moduleName().getModule(), substatements, (AugmentationSchemaNode) stmt.original());
144 } catch (SubstatementIndexingException e) {
145 throw new SourceException(e.getMessage(), stmt, e);
149 abstract boolean allowsMandatory(StmtContext<?, ?, ?> ctx);
151 static StmtContext<?, ?, ?> getSearchRoot(final StmtContext<?, ?, ?> augmentContext) {
152 // Augment is in uses - we need to augment instantiated nodes in parent.
153 final StmtContext<?, ?, ?> parent = augmentContext.coerceParentContext();
154 if (YangStmtMapping.USES == parent.publicDefinition()) {
155 return parent.getParentContext();
160 static boolean hasWhenSubstatement(final StmtContext<?, ?, ?> ctx) {
161 return ctx.hasSubstatement(WhenEffectiveStatement.class);