Eliminate SourceException throws declarations
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / AugmentStatementImpl.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. 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.stmt.rfc6020;
9
10 import static org.opendaylight.yangtools.yang.parser.spi.SubstatementValidator.MAX;
11 import com.google.common.base.Preconditions;
12 import java.util.Collection;
13 import java.util.regex.Pattern;
14 import javax.annotation.Nonnull;
15 import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping;
16 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
17 import org.opendaylight.yangtools.yang.model.api.stmt.AugmentStatement;
18 import org.opendaylight.yangtools.yang.model.api.stmt.DataDefinitionStatement;
19 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
20 import org.opendaylight.yangtools.yang.parser.spi.SubstatementValidator;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractDeclaredStatement;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.Prerequisite;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
30 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
31 import org.opendaylight.yangtools.yang.parser.spi.source.StmtOrderingNamespace;
32 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
33 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.AugmentEffectiveStatementImpl;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 public class AugmentStatementImpl extends AbstractDeclaredStatement<SchemaNodeIdentifier> implements AugmentStatement {
38     private static final Logger LOG = LoggerFactory.getLogger(AugmentStatementImpl.class);
39     private static final Pattern PATH_REL_PATTERN1 = Pattern.compile("\\.\\.?\\s*/(.+)");
40     private static final Pattern PATH_REL_PATTERN2 = Pattern.compile("//.*");
41     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator
42             .builder(Rfc6020Mapping.AUGMENT)
43             .add(Rfc6020Mapping.ANYXML, 0, MAX)
44             .add(Rfc6020Mapping.CASE, 0, MAX)
45             .add(Rfc6020Mapping.CHOICE, 0, MAX)
46             .add(Rfc6020Mapping.CONTAINER, 0, MAX)
47             .add(Rfc6020Mapping.DESCRIPTION, 0, 1)
48             .add(Rfc6020Mapping.IF_FEATURE, 0, MAX)
49             .add(Rfc6020Mapping.LEAF, 0, MAX)
50             .add(Rfc6020Mapping.LEAF_LIST, 0, MAX)
51             .add(Rfc6020Mapping.LIST, 0, MAX)
52             .add(Rfc6020Mapping.REFERENCE, 0, 1)
53             .add(Rfc6020Mapping.STATUS, 0, 1)
54             .add(Rfc6020Mapping.USES, 0, MAX)
55             .add(Rfc6020Mapping.WHEN, 0, 1)
56             .build();
57
58     protected AugmentStatementImpl(final StmtContext<SchemaNodeIdentifier, AugmentStatement, ?> context) {
59         super(context);
60     }
61
62     public static class Definition extends
63             AbstractStatementSupport<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> {
64
65         public Definition() {
66             super(Rfc6020Mapping.AUGMENT);
67         }
68
69         @Override
70         public SchemaNodeIdentifier parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
71             Preconditions.checkArgument(!PATH_REL_PATTERN1.matcher(value).matches()
72                 && !PATH_REL_PATTERN2.matcher(value).matches(),
73                 "An argument for augment can be only absolute path; or descendant if used in uses");
74
75             return Utils.nodeIdentifierFromPath(ctx, value);
76         }
77
78         @Override
79         public AugmentStatement createDeclared(
80                 final StmtContext<SchemaNodeIdentifier, AugmentStatement, ?> ctx) {
81             return new AugmentStatementImpl(ctx);
82         }
83
84         @Override
85         public EffectiveStatement<SchemaNodeIdentifier, AugmentStatement> createEffective(
86                 final StmtContext<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> ctx) {
87             return new AugmentEffectiveStatementImpl(ctx);
88         }
89
90         @Override
91         public void onFullDefinitionDeclared(
92                 final StmtContext.Mutable<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> augmentNode) {
93             SUBSTATEMENT_VALIDATOR.validate(augmentNode);
94
95             if (StmtContextUtils.isInExtensionBody(augmentNode)) {
96                 return;
97             }
98
99             final ModelActionBuilder augmentAction = augmentNode.newInferenceAction(
100                 ModelProcessingPhase.EFFECTIVE_MODEL);
101             final ModelActionBuilder.Prerequisite<StmtContext<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>>> sourceCtxPrereq =
102                     augmentAction.requiresCtx(augmentNode, ModelProcessingPhase.EFFECTIVE_MODEL);
103             final Prerequisite<Mutable<?, ?, EffectiveStatement<?, ?>>> target =
104                     augmentAction.mutatesEffectiveCtx(getSearchRoot(augmentNode), SchemaNodeIdentifierBuildNamespace.class, augmentNode.getStatementArgument());
105             augmentAction.apply(new ModelActionBuilder.InferenceAction() {
106
107                 @Override
108                 public void apply() {
109                     final StatementContextBase<?, ?, ?> augmentTargetCtx = (StatementContextBase<?, ?, ?>) target.get();
110                     if (!AugmentUtils.isSupportedAugmentTarget(augmentTargetCtx) || StmtContextUtils.isInExtensionBody(augmentTargetCtx)) {
111                         augmentNode.setIsSupportedToBuildEffective(false);
112                         return;
113                     }
114
115                     // FIXME: this is a workaround for models which augment a node which is added via an extension
116                     //        which we do not handle. This needs to be reworked in terms of unknown schema nodes.
117                     final StatementContextBase<?, ?, ?> augmentSourceCtx = (StatementContextBase<?, ?, ?>) augmentNode;
118                     try {
119                         AugmentUtils.copyFromSourceToTarget(augmentSourceCtx, augmentTargetCtx);
120                         augmentTargetCtx.addEffectiveSubstatement(augmentSourceCtx);
121                         updateAugmentOrder(augmentSourceCtx);
122                     } catch (SourceException e) {
123                         LOG.debug("Failed to add augmentation %s defined at {}",
124                             augmentSourceCtx.getStatementSourceReference(), e);
125                     }
126                 }
127
128                 private void updateAugmentOrder(final StatementContextBase<?, ?, ?> augmentSourceCtx) {
129                     Integer currentOrder = augmentSourceCtx.getFromNamespace(StmtOrderingNamespace.class,
130                         Rfc6020Mapping.AUGMENT);
131                     if (currentOrder == null) {
132                         currentOrder = 1;
133                     } else {
134                         currentOrder++;
135                     }
136
137                     augmentSourceCtx.setOrder(currentOrder);
138                     augmentSourceCtx.addToNs(StmtOrderingNamespace.class, Rfc6020Mapping.AUGMENT, currentOrder);
139                 }
140
141                 @Override
142                 public void prerequisiteFailed(final Collection<? extends ModelActionBuilder.Prerequisite<?>> failed) {
143                     throw new InferenceException(augmentNode.getStatementSourceReference(),
144                         "Augment target '%s' not found", augmentNode.getStatementArgument());
145                 }
146             });
147         }
148
149         private static Mutable<?, ?, ?> getSearchRoot(final Mutable<?, ?, ?> augmentContext) {
150             Mutable<?, ?, ?> parent = augmentContext.getParentContext();
151             // Augment is in uses - we need to augment instantiated nodes in parent.
152             if (Rfc6020Mapping.USES.equals(parent.getPublicDefinition())) {
153                 return parent.getParentContext();
154             }
155             return parent;
156         }
157     }
158
159     @Nonnull
160     @Override
161     public SchemaNodeIdentifier getTargetNode() {
162         return argument();
163     }
164
165     @Override
166     public Collection<? extends DataDefinitionStatement> getDataDefinitions() {
167         return allDeclared(DataDefinitionStatement.class);
168     }
169 }