Move more statements
[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.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;
47
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("//.*");
52
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())
59             ));
60     }
61
62     @Override
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);
67
68         // As per:
69         //   https://tools.ietf.org/html/rfc6020#section-7.15
70         //   https://tools.ietf.org/html/rfc7950#section-7.17
71         //
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");
80         } else {
81             SourceException.throwIf(result instanceof Descendant, ctx,
82                 "Descendant schema node identifier is not allowed when used outside of a uses statement");
83         }
84         return result;
85     }
86
87     @Override
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);
93         }
94
95         super.onFullDefinitionDeclared(augmentNode);
96
97         if (StmtContextUtils.isInExtensionBody(augmentNode)) {
98             return;
99         }
100
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());
105
106         augmentAction.apply(new AugmentInferenceAction(this, augmentNode, target));
107     }
108
109     @Override
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);
113     }
114
115     @Override
116     protected final AugmentStatement createEmptyDeclared(
117             final StmtContext<SchemaNodeIdentifier, AugmentStatement, ?> ctx) {
118         return DeclaredStatements.createAugment(ctx.getRawArgument(), ctx.getArgument());
119     }
120
121     @Override
122     protected final List<? extends StmtContext<?, ?, ?>> statementsToBuild(
123             final Current<SchemaNodeIdentifier, AugmentStatement> stmt,
124             final List<? extends StmtContext<?, ?, ?>> substatements) {
125         // Pick up the marker left by onFullDefinitionDeclared() inference action. If it is present we need to pass our
126         // children through target's implicit wrapping.
127         final StatementContextBase<?, ?, ?> implicitDef = stmt.getFromNamespace(AugmentImplicitHandlingNamespace.class,
128             Empty.getInstance());
129         return implicitDef == null ? substatements : Lists.transform(substatements, subCtx -> {
130             verify(subCtx instanceof StatementContextBase);
131             return implicitDef.wrapWithImplicit((StatementContextBase<?, ?, ?>) subCtx);
132         });
133     }
134
135     @Override
136     protected final AugmentEffectiveStatement createEffective(
137             final Current<SchemaNodeIdentifier, AugmentStatement> stmt,
138             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
139         final int flags = new FlagsBuilder()
140                 .setStatus(findFirstArgument(substatements, StatusEffectiveStatement.class, Status.CURRENT))
141                 .toFlags();
142
143         try {
144             return EffectiveStatements.createAugment(stmt.declared(), stmt.getArgument(), flags,
145                 stmt.moduleName().getModule(), substatements, stmt.original(AugmentationSchemaNode.class));
146         } catch (SubstatementIndexingException e) {
147             throw new SourceException(e.getMessage(), stmt, e);
148         }
149     }
150
151     abstract boolean allowsMandatory(StmtContext<?, ?, ?> ctx);
152
153     static StmtContext<?, ?, ?> getSearchRoot(final StmtContext<?, ?, ?> augmentContext) {
154         // Augment is in uses - we need to augment instantiated nodes in parent.
155         final StmtContext<?, ?, ?> parent = augmentContext.coerceParentContext();
156         if (YangStmtMapping.USES == parent.publicDefinition()) {
157             return parent.getParentContext();
158         }
159         return parent;
160     }
161
162     static boolean hasWhenSubstatement(final StmtContext<?, ?, ?> ctx) {
163         return ctx.hasSubstatement(WhenEffectiveStatement.class);
164     }
165 }