f3b314384d4bd5a4fc988ba31b39954fc6088345
[yangtools.git] / parser / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / deviate / AbstractDeviateStatementSupport.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.deviate;
9
10 import com.google.common.collect.ImmutableList;
11 import com.google.common.collect.ImmutableMap;
12 import com.google.common.collect.ImmutableSet;
13 import com.google.common.collect.Iterables;
14 import com.google.common.collect.Maps;
15 import com.google.common.collect.SetMultimap;
16 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
17 import java.util.Arrays;
18 import java.util.Collection;
19 import java.util.List;
20 import java.util.Objects;
21 import java.util.Set;
22 import org.opendaylight.yangtools.yang.common.Empty;
23 import org.opendaylight.yangtools.yang.common.QNameModule;
24 import org.opendaylight.yangtools.yang.common.YangVersion;
25 import org.opendaylight.yangtools.yang.model.api.DeviateKind;
26 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
27 import org.opendaylight.yangtools.yang.model.api.meta.DeclarationReference;
28 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
29 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
30 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
31 import org.opendaylight.yangtools.yang.model.api.stmt.DeviateEffectiveStatement;
32 import org.opendaylight.yangtools.yang.model.api.stmt.DeviateStatement;
33 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
34 import org.opendaylight.yangtools.yang.model.ri.stmt.DeclaredStatementDecorators;
35 import org.opendaylight.yangtools.yang.model.ri.stmt.DeclaredStatements;
36 import org.opendaylight.yangtools.yang.model.ri.stmt.EffectiveStatements;
37 import org.opendaylight.yangtools.yang.parser.api.YangParserConfiguration;
38 import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.YangValidationBundles;
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.BoundStmtCtx;
42 import org.opendaylight.yangtools.yang.parser.spi.meta.CopyType;
43 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
44 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
45 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
46 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceAction;
47 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceContext;
48 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.Prerequisite;
49 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
50 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
51 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
52 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
53 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
54 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleCtxToModuleQName;
55 import org.opendaylight.yangtools.yang.parser.spi.source.ModulesDeviatedByModules;
56 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
57 import org.slf4j.Logger;
58 import org.slf4j.LoggerFactory;
59
60 abstract class AbstractDeviateStatementSupport
61         extends AbstractStatementSupport<DeviateKind, DeviateStatement, DeviateEffectiveStatement> {
62     private static final Logger LOG = LoggerFactory.getLogger(AbstractDeviateStatementSupport.class);
63
64     private static final SubstatementValidator DEVIATE_NOT_SUPPORTED_SUBSTATEMENT_VALIDATOR =
65             SubstatementValidator.builder(YangStmtMapping.DEVIATE).build();
66
67     private static final SubstatementValidator DEVIATE_ADD_SUBSTATEMENT_VALIDATOR =
68             SubstatementValidator.builder(YangStmtMapping.DEVIATE)
69                 .addOptional(YangStmtMapping.CONFIG)
70                 .addOptional(YangStmtMapping.DEFAULT)
71                 .addOptional(YangStmtMapping.MANDATORY)
72                 .addOptional(YangStmtMapping.MAX_ELEMENTS)
73                 .addOptional(YangStmtMapping.MIN_ELEMENTS)
74                 .addAny(YangStmtMapping.MUST)
75                 .addAny(YangStmtMapping.UNIQUE)
76                 .addOptional(YangStmtMapping.UNITS)
77                 .build();
78
79     private static final SubstatementValidator DEVIATE_REPLACE_SUBSTATEMENT_VALIDATOR =
80             SubstatementValidator.builder(YangStmtMapping.DEVIATE)
81                 .addOptional(YangStmtMapping.CONFIG)
82                 .addOptional(YangStmtMapping.DEFAULT)
83                 .addOptional(YangStmtMapping.MANDATORY)
84                 .addOptional(YangStmtMapping.MAX_ELEMENTS)
85                 .addOptional(YangStmtMapping.MIN_ELEMENTS)
86                 .addOptional(YangStmtMapping.TYPE)
87                 .addOptional(YangStmtMapping.UNITS)
88                 .build();
89
90     private static final SubstatementValidator DEVIATE_DELETE_SUBSTATEMENT_VALIDATOR =
91             SubstatementValidator.builder(YangStmtMapping.DEVIATE)
92                 .addOptional(YangStmtMapping.DEFAULT)
93                 .addAny(YangStmtMapping.MUST)
94                 .addAny(YangStmtMapping.UNIQUE)
95                 .addOptional(YangStmtMapping.UNITS)
96                 .build();
97
98     private static final ImmutableMap<String, DeviateKind> KEYWORD_TO_DEVIATE_MAP =
99             Maps.uniqueIndex(Arrays.asList(DeviateKind.values()), DeviateKind::getKeyword);
100
101     private static final ImmutableSet<YangStmtMapping> SINGLETON_STATEMENTS = ImmutableSet.of(
102             YangStmtMapping.UNITS, YangStmtMapping.CONFIG, YangStmtMapping.MANDATORY,
103             YangStmtMapping.MIN_ELEMENTS, YangStmtMapping.MAX_ELEMENTS);
104
105     private static final ImmutableSet<YangStmtMapping> IMPLICIT_STATEMENTS = ImmutableSet.of(YangStmtMapping.CONFIG,
106             YangStmtMapping.MANDATORY, YangStmtMapping.MAX_ELEMENTS, YangStmtMapping.MIN_ELEMENTS);
107
108     AbstractDeviateStatementSupport(final YangParserConfiguration config) {
109         // Note: we are performing our own validation based on deviate kind.
110         // TODO: perhaps we should do argumentSpecificSupport?
111         super(YangStmtMapping.DEVIATE, StatementPolicy.contextIndependent(), config, null);
112     }
113
114     @Override
115     public final DeviateKind parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
116         return SourceException.throwIfNull(KEYWORD_TO_DEVIATE_MAP.get(value), ctx,
117             "String '%s' is not valid deviate argument", value);
118     }
119
120     @Override
121     public final void onFullDefinitionDeclared(
122             final Mutable<DeviateKind, DeviateStatement, DeviateEffectiveStatement> deviateStmtCtx) {
123         final DeviateKind deviateKind = deviateStmtCtx.argument();
124         getSubstatementValidatorForDeviate(deviateKind).validate(deviateStmtCtx);
125
126         final SchemaNodeIdentifier deviationTarget =
127                 (SchemaNodeIdentifier) deviateStmtCtx.coerceParentContext().argument();
128
129         if (!isDeviationSupported(deviateStmtCtx, deviationTarget)) {
130             return;
131         }
132
133         final ModelActionBuilder deviateAction = deviateStmtCtx.newInferenceAction(
134                 ModelProcessingPhase.EFFECTIVE_MODEL);
135
136         final Prerequisite<StmtContext<DeviateKind, DeviateStatement,
137             DeviateEffectiveStatement>> sourceCtxPrerequisite =
138                 deviateAction.requiresCtx(deviateStmtCtx, ModelProcessingPhase.EFFECTIVE_MODEL);
139
140         final Prerequisite<Mutable<?, ?, EffectiveStatement<?, ?>>> targetCtxPrerequisite =
141                 deviateAction.mutatesEffectiveCtxPath(deviateStmtCtx.getRoot(),
142                     SchemaTreeNamespace.class, deviationTarget.getNodeIdentifiers());
143
144         deviateAction.apply(new InferenceAction() {
145             @Override
146             public void apply(final InferenceContext ctx) {
147                 if (!deviateStmtCtx.isSupportedToBuildEffective()) {
148                     // We are not building effective model, hence we should not be performing any effects
149                     return;
150                 }
151
152                 final var sourceNodeStmtCtx = sourceCtxPrerequisite.resolve(ctx);
153                 final var targetNodeStmtCtx = targetCtxPrerequisite.resolve(ctx);
154
155                 switch (deviateKind) {
156                     case NOT_SUPPORTED:
157                         // FIXME: this can be short-circuited without an inference action
158                         targetNodeStmtCtx.setUnsupported();
159                         break;
160                     case ADD:
161                         performDeviateAdd(sourceNodeStmtCtx, targetNodeStmtCtx);
162                         break;
163                     case REPLACE:
164                         performDeviateReplace(sourceNodeStmtCtx, targetNodeStmtCtx);
165                         break;
166                     case DELETE:
167                         performDeviateDelete(sourceNodeStmtCtx, targetNodeStmtCtx);
168                         break;
169                     default:
170                         throw new IllegalStateException("Unsupported deviate " + deviateKind);
171                 }
172             }
173
174             @Override
175             public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
176                 throw new InferenceException(deviateStmtCtx.coerceParentContext(), "Deviation target '%s' not found.",
177                     deviationTarget);
178             }
179
180             @Override
181             public void prerequisiteUnavailable(final Prerequisite<?> unavail) {
182                 if (targetCtxPrerequisite.equals(unavail)) {
183                     deviateStmtCtx.setUnsupported();
184                 } else {
185                     prerequisiteFailed(List.of(unavail));
186                 }
187             }
188         });
189     }
190
191     @Override
192     public String internArgument(final String rawArgument) {
193         if ("add".equals(rawArgument)) {
194             return "add";
195         } else if ("delete".equals(rawArgument)) {
196             return "delete";
197         } else if ("replace".equals(rawArgument)) {
198             return "replace";
199         } else if ("not-supported".equals(rawArgument)) {
200             return "not-supported";
201         } else {
202             return rawArgument;
203         }
204     }
205
206     @Override
207     protected final DeviateStatement createDeclared(final BoundStmtCtx<DeviateKind> ctx,
208             final ImmutableList<DeclaredStatement<?>> substatements) {
209         return DeclaredStatements.createDeviate(ctx.getArgument(), substatements);
210     }
211
212     @Override
213     protected final DeviateStatement attachDeclarationReference(final DeviateStatement stmt,
214             final DeclarationReference reference) {
215         return DeclaredStatementDecorators.decorateDeviate(stmt, reference);
216     }
217
218     @Override
219     protected final DeviateEffectiveStatement createEffective(final Current<DeviateKind, DeviateStatement> stmt,
220             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
221         return EffectiveStatements.createDeviate(stmt.declared(), substatements);
222     }
223
224     protected SubstatementValidator getSubstatementValidatorForDeviate(final DeviateKind deviateKind) {
225         switch (deviateKind) {
226             case NOT_SUPPORTED:
227                 return DEVIATE_NOT_SUPPORTED_SUBSTATEMENT_VALIDATOR;
228             case ADD:
229                 return DEVIATE_ADD_SUBSTATEMENT_VALIDATOR;
230             case REPLACE:
231                 return DEVIATE_REPLACE_SUBSTATEMENT_VALIDATOR;
232             case DELETE:
233                 return DEVIATE_DELETE_SUBSTATEMENT_VALIDATOR;
234             default:
235                 throw new IllegalStateException(String.format(
236                         "Substatement validator for deviate %s has not been defined.", deviateKind));
237         }
238     }
239
240     private static boolean isDeviationSupported(
241             final Mutable<DeviateKind, DeviateStatement, DeviateEffectiveStatement> deviateStmtCtx,
242             final SchemaNodeIdentifier deviationTarget) {
243         final SetMultimap<QNameModule, QNameModule> modulesDeviatedByModules = deviateStmtCtx.getFromNamespace(
244                 ModulesDeviatedByModules.class, Empty.value());
245         if (modulesDeviatedByModules == null) {
246             return true;
247         }
248
249         final QNameModule currentModule = deviateStmtCtx.getFromNamespace(ModuleCtxToModuleQName.class,
250                 deviateStmtCtx.getRoot());
251         final QNameModule targetModule = Iterables.getLast(deviationTarget.getNodeIdentifiers()).getModule();
252
253         final Set<QNameModule> deviationModulesSupportedByTargetModule = modulesDeviatedByModules.get(targetModule);
254         if (deviationModulesSupportedByTargetModule != null) {
255             return deviationModulesSupportedByTargetModule.contains(currentModule);
256         }
257
258         return false;
259     }
260
261     @SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD",
262             justification = "https://github.com/spotbugs/spotbugs/issues/811")
263     private static void performDeviateAdd(final StmtContext<?, ?, ?> deviateStmtCtx,
264             final Mutable<?, ?, ?> targetCtx) {
265         for (StmtContext<?, ?, ?> originalStmtCtx : deviateStmtCtx.declaredSubstatements()) {
266             validateDeviationTarget(originalStmtCtx, targetCtx);
267             addStatement(originalStmtCtx, targetCtx);
268         }
269     }
270
271     private static void addStatement(final StmtContext<?, ?, ?> stmtCtxToBeAdded, final Mutable<?, ?, ?> targetCtx) {
272         if (!StmtContextUtils.isUnknownStatement(stmtCtxToBeAdded)) {
273             final StatementDefinition stmtToBeAdded = stmtCtxToBeAdded.publicDefinition();
274             if (SINGLETON_STATEMENTS.contains(stmtToBeAdded) || YangStmtMapping.DEFAULT.equals(stmtToBeAdded)
275                     && YangStmtMapping.LEAF.equals(targetCtx.publicDefinition())) {
276                 for (StmtContext<?, ?, ?> targetCtxSubstatement : targetCtx.allSubstatements()) {
277                     InferenceException.throwIf(stmtToBeAdded.equals(targetCtxSubstatement.publicDefinition()),
278                         stmtCtxToBeAdded,
279                         "Deviation cannot add substatement %s to target node %s because it is already defined "
280                         + "in target and can appear only once.",
281                         stmtToBeAdded.getStatementName(), targetCtx.argument());
282                 }
283             }
284         }
285
286         copyStatement(stmtCtxToBeAdded, targetCtx);
287     }
288
289     @SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD",
290             justification = "https://github.com/spotbugs/spotbugs/issues/811")
291     private static void performDeviateReplace(final StmtContext<?, ?, ?> deviateStmtCtx,
292             final Mutable<?, ?, ?> targetCtx) {
293         for (StmtContext<?, ?, ?> originalStmtCtx : deviateStmtCtx.declaredSubstatements()) {
294             validateDeviationTarget(originalStmtCtx, targetCtx);
295             replaceStatement(originalStmtCtx, targetCtx);
296         }
297     }
298
299     private static void replaceStatement(final StmtContext<?, ?, ?> stmtCtxToBeReplaced,
300             final Mutable<?, ?, ?> targetCtx) {
301         final StatementDefinition stmtToBeReplaced = stmtCtxToBeReplaced.publicDefinition();
302
303         if (YangStmtMapping.DEFAULT.equals(stmtToBeReplaced)
304                 && YangStmtMapping.LEAF_LIST.equals(targetCtx.publicDefinition())) {
305             LOG.error("Deviation cannot replace substatement {} in target leaf-list {} because a leaf-list can "
306                     + "have multiple default statements. At line: {}", stmtToBeReplaced.getStatementName(),
307                     targetCtx.argument(), stmtCtxToBeReplaced.sourceReference());
308             return;
309         }
310
311         for (StmtContext<?, ?, ?> targetCtxSubstatement : targetCtx.effectiveSubstatements()) {
312             if (stmtToBeReplaced.equals(targetCtxSubstatement.publicDefinition())) {
313                 targetCtx.removeStatementFromEffectiveSubstatements(stmtToBeReplaced);
314                 copyStatement(stmtCtxToBeReplaced, targetCtx);
315                 return;
316             }
317         }
318
319         for (Mutable<?, ?, ?> targetCtxSubstatement : targetCtx.mutableDeclaredSubstatements()) {
320             if (stmtToBeReplaced.equals(targetCtxSubstatement.publicDefinition())) {
321                 targetCtxSubstatement.setUnsupported();
322                 copyStatement(stmtCtxToBeReplaced, targetCtx);
323                 return;
324             }
325         }
326
327         // This is a special case when deviate replace of a config/mandatory/max/min-elements substatement targets
328         // a node which does not contain an explicitly declared config/mandatory/max/min-elements.
329         // However, according to RFC6020/RFC7950, these properties are always implicitly present.
330         if (IMPLICIT_STATEMENTS.contains(stmtToBeReplaced)) {
331             addStatement(stmtCtxToBeReplaced, targetCtx);
332             return;
333         }
334
335         throw new InferenceException(stmtCtxToBeReplaced,
336             "Deviation cannot replace substatement %s in target node %s because it does not exist in target node.",
337             stmtToBeReplaced.getStatementName(), targetCtx.argument());
338     }
339
340     @SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD",
341             justification = "https://github.com/spotbugs/spotbugs/issues/811")
342     private static void performDeviateDelete(final StmtContext<?, ?, ?> deviateStmtCtx,
343                 final Mutable<?, ?, ?> targetCtx) {
344         for (StmtContext<?, ?, ?> originalStmtCtx : deviateStmtCtx.declaredSubstatements()) {
345             validateDeviationTarget(originalStmtCtx, targetCtx);
346             deleteStatement(originalStmtCtx, targetCtx);
347         }
348     }
349
350     private static void deleteStatement(final StmtContext<?, ?, ?> stmtCtxToBeDeleted,
351             final Mutable<?, ?, ?> targetCtx) {
352         final StatementDefinition stmtToBeDeleted = stmtCtxToBeDeleted.publicDefinition();
353         final String stmtArgument = stmtCtxToBeDeleted.rawArgument();
354
355         for (Mutable<?, ?, ?> targetCtxSubstatement : targetCtx.mutableEffectiveSubstatements()) {
356             if (statementsAreEqual(stmtToBeDeleted, stmtArgument, targetCtxSubstatement.publicDefinition(),
357                     targetCtxSubstatement.rawArgument())) {
358                 targetCtx.removeStatementFromEffectiveSubstatements(stmtToBeDeleted, stmtArgument);
359                 return;
360             }
361         }
362
363         for (Mutable<?, ?, ?> targetCtxSubstatement : targetCtx.mutableDeclaredSubstatements()) {
364             if (statementsAreEqual(stmtToBeDeleted, stmtArgument, targetCtxSubstatement.publicDefinition(),
365                     targetCtxSubstatement.rawArgument())) {
366                 targetCtxSubstatement.setUnsupported();
367                 return;
368             }
369         }
370
371         LOG.error("Deviation cannot delete substatement {} with argument '{}' in target node {} because it does "
372                 + "not exist in the target node. At line: {}", stmtToBeDeleted.getStatementName(), stmtArgument,
373                 targetCtx.argument(), stmtCtxToBeDeleted.sourceReference());
374     }
375
376     private static void copyStatement(final StmtContext<?, ?, ?> stmtCtxToBeCopied, final Mutable<?, ?, ?> targetCtx) {
377         // we need to make a copy of the statement context only if it is an unknown statement, otherwise
378         // we can reuse the original statement context
379         if (!StmtContextUtils.isUnknownStatement(stmtCtxToBeCopied)) {
380             // FIXME: I think this should be handled by the corresponding support's copy policy
381             targetCtx.addEffectiveSubstatement(stmtCtxToBeCopied.replicaAsChildOf(targetCtx));
382         } else {
383             targetCtx.addEffectiveSubstatement(targetCtx.childCopyOf(stmtCtxToBeCopied, CopyType.ORIGINAL));
384         }
385     }
386
387     private static boolean statementsAreEqual(final StatementDefinition firstStmtDef, final String firstStmtArg,
388             final StatementDefinition secondStmtDef, final String secondStmtArg) {
389         return firstStmtDef.equals(secondStmtDef) && Objects.equals(firstStmtArg, secondStmtArg);
390     }
391
392     private static void validateDeviationTarget(final StmtContext<?, ?, ?> deviateSubStmtCtx,
393             final StmtContext<?, ?, ?> targetCtx) {
394         InferenceException.throwIf(!isSupportedDeviationTarget(deviateSubStmtCtx, targetCtx,
395             targetCtx.yangVersion()), deviateSubStmtCtx,
396             "%s is not a valid deviation target for substatement %s.", targetCtx.argument(),
397             deviateSubStmtCtx.publicDefinition().getStatementName());
398     }
399
400     private static boolean isSupportedDeviationTarget(final StmtContext<?, ?, ?> deviateSubstatementCtx,
401             final StmtContext<?, ?, ?> deviateTargetCtx, final YangVersion yangVersion) {
402         Set<StatementDefinition> supportedDeviationTargets =
403                 YangValidationBundles.SUPPORTED_DEVIATION_TARGETS.get(yangVersion,
404                         deviateSubstatementCtx.publicDefinition());
405
406         if (supportedDeviationTargets == null) {
407             supportedDeviationTargets = YangValidationBundles.SUPPORTED_DEVIATION_TARGETS.get(YangVersion.VERSION_1,
408                     deviateSubstatementCtx.publicDefinition());
409         }
410
411         // if supportedDeviationTargets is null, it means that the deviate substatement is an unknown statement
412         return supportedDeviationTargets == null || supportedDeviationTargets.contains(
413                 deviateTargetCtx.publicDefinition());
414     }
415 }