a656d7bd81435459fd388ce3df4fdedfbde956fe
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / augment / AbstractAugmentStatementSupport.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies, s.r.o. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.parser.rfc7950.stmt.augment;
9
10 import static com.google.common.base.Verify.verify;
11
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.DeclarationReference;
22 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
23 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
24 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
25 import org.opendaylight.yangtools.yang.model.api.stmt.AugmentEffectiveStatement;
26 import org.opendaylight.yangtools.yang.model.api.stmt.AugmentStatement;
27 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
28 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
29 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Descendant;
30 import org.opendaylight.yangtools.yang.model.api.stmt.StatusEffectiveStatement;
31 import org.opendaylight.yangtools.yang.model.api.stmt.WhenEffectiveStatement;
32 import org.opendaylight.yangtools.yang.model.ri.stmt.DeclaredStatementDecorators;
33 import org.opendaylight.yangtools.yang.model.ri.stmt.DeclaredStatements;
34 import org.opendaylight.yangtools.yang.model.ri.stmt.EffectiveStatements;
35 import org.opendaylight.yangtools.yang.model.spi.meta.EffectiveStatementMixins.EffectiveStatementWithFlags.FlagsBuilder;
36 import org.opendaylight.yangtools.yang.model.spi.meta.SubstatementIndexingException;
37 import org.opendaylight.yangtools.yang.parser.api.YangParserConfiguration;
38 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.ArgumentUtils;
39 import org.opendaylight.yangtools.yang.parser.spi.SchemaTreeNamespace;
40 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
41 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
42 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
43 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.Prerequisite;
44 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
45 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
46 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
47 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
48 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
49 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
50
51 abstract class AbstractAugmentStatementSupport
52         extends AbstractStatementSupport<SchemaNodeIdentifier, AugmentStatement, AugmentEffectiveStatement> {
53     private static final Pattern PATH_REL_PATTERN1 = Pattern.compile("\\.\\.?\\s*/(.+)");
54     private static final Pattern PATH_REL_PATTERN2 = Pattern.compile("//.*");
55
56     AbstractAugmentStatementSupport(final YangParserConfiguration config) {
57         super(YangStmtMapping.AUGMENT, StatementPolicy.copyDeclared(
58             (copy, current, substatements) ->
59                 copy.getArgument().equals(current.getArgument())
60                 && copy.moduleName().getModule().equals(current.moduleName().getModule())
61                 && Objects.equals(copy.original(), current.original())
62             ), config);
63     }
64
65     @Override
66     public final SchemaNodeIdentifier parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
67         SourceException.throwIf(PATH_REL_PATTERN1.matcher(value).matches()
68             || PATH_REL_PATTERN2.matcher(value).matches(), ctx,
69             "Augment argument \'%s\' is not valid, it can be only absolute path; or descendant if used in uses", value);
70
71         // As per:
72         //   https://tools.ietf.org/html/rfc6020#section-7.15
73         //   https://tools.ietf.org/html/rfc7950#section-7.17
74         //
75         // The argument is either Absolute or Descendant based on whether the statement is declared within a 'uses'
76         // statement. The mechanics differs wildly between the two cases, so let's start by ensuring our argument
77         // is in the correct domain.
78         final SchemaNodeIdentifier result = ArgumentUtils.nodeIdentifierFromPath(ctx, value);
79         final StatementDefinition parent = ctx.coerceParentContext().publicDefinition();
80         if (parent == YangStmtMapping.USES) {
81             SourceException.throwIf(result instanceof Absolute, ctx,
82                 "Absolute schema node identifier is not allowed when used within a uses statement");
83         } else {
84             SourceException.throwIf(result instanceof Descendant, ctx,
85                 "Descendant schema node identifier is not allowed when used outside of a uses statement");
86         }
87         return result;
88     }
89
90     @Override
91     public final void onFullDefinitionDeclared(
92             final Mutable<SchemaNodeIdentifier, AugmentStatement, AugmentEffectiveStatement> augmentNode) {
93         if (!augmentNode.isSupportedByFeatures()) {
94             // We need this augment node to be present, but it should not escape to effective world
95             augmentNode.setIsSupportedToBuildEffective(false);
96         }
97
98         super.onFullDefinitionDeclared(augmentNode);
99
100         if (StmtContextUtils.isInExtensionBody(augmentNode)) {
101             return;
102         }
103
104         final ModelActionBuilder augmentAction = augmentNode.newInferenceAction(ModelProcessingPhase.EFFECTIVE_MODEL);
105         augmentAction.requiresCtx(augmentNode, ModelProcessingPhase.EFFECTIVE_MODEL);
106         final Prerequisite<Mutable<?, ?, EffectiveStatement<?, ?>>> target = augmentAction.mutatesEffectiveCtxPath(
107             getSearchRoot(augmentNode), SchemaTreeNamespace.class, augmentNode.getArgument().getNodeIdentifiers());
108
109         augmentAction.apply(new AugmentInferenceAction(this, augmentNode, target));
110     }
111
112     @Override
113     protected final AugmentStatement createDeclared(final StmtContext<SchemaNodeIdentifier, AugmentStatement, ?> ctx,
114             final ImmutableList<? extends DeclaredStatement<?>> substatements) {
115         return DeclaredStatements.createAugment(ctx.getRawArgument(), ctx.getArgument(), substatements);
116     }
117
118     @Override
119     protected final AugmentStatement attachDeclarationReference(final AugmentStatement stmt,
120             final DeclarationReference reference) {
121         return DeclaredStatementDecorators.decorateAugment(stmt, reference);
122     }
123
124     @Override
125     protected final List<? extends StmtContext<?, ?, ?>> statementsToBuild(
126             final Current<SchemaNodeIdentifier, AugmentStatement> stmt,
127             final List<? extends StmtContext<?, ?, ?>> substatements) {
128         // Pick up the marker left by onFullDefinitionDeclared() inference action. If it is present we need to pass our
129         // children through target's implicit wrapping.
130         final StatementContextBase<?, ?, ?> implicitDef = stmt.getFromNamespace(AugmentImplicitHandlingNamespace.class,
131             Empty.getInstance());
132         return implicitDef == null ? substatements : Lists.transform(substatements, subCtx -> {
133             verify(subCtx instanceof StatementContextBase);
134             return implicitDef.wrapWithImplicit((StatementContextBase<?, ?, ?>) subCtx);
135         });
136     }
137
138     @Override
139     protected final AugmentEffectiveStatement createEffective(
140             final Current<SchemaNodeIdentifier, AugmentStatement> stmt,
141             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
142         final int flags = new FlagsBuilder()
143                 .setStatus(findFirstArgument(substatements, StatusEffectiveStatement.class, Status.CURRENT))
144                 .toFlags();
145
146         try {
147             return EffectiveStatements.createAugment(stmt.declared(), stmt.getArgument(), flags,
148                 stmt.moduleName().getModule(), substatements, stmt.original(AugmentationSchemaNode.class));
149         } catch (SubstatementIndexingException e) {
150             throw new SourceException(e.getMessage(), stmt, e);
151         }
152     }
153
154     abstract boolean allowsMandatory(StmtContext<?, ?, ?> ctx);
155
156     static StmtContext<?, ?, ?> getSearchRoot(final StmtContext<?, ?, ?> augmentContext) {
157         // Augment is in uses - we need to augment instantiated nodes in parent.
158         final StmtContext<?, ?, ?> parent = augmentContext.coerceParentContext();
159         if (YangStmtMapping.USES == parent.publicDefinition()) {
160             return parent.getParentContext();
161         }
162         return parent;
163     }
164
165     static boolean hasWhenSubstatement(final StmtContext<?, ?, ?> ctx) {
166         return ctx.hasSubstatement(WhenEffectiveStatement.class);
167     }
168 }