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.deviate;
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;
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;
60 abstract class AbstractDeviateStatementSupport
61 extends AbstractStatementSupport<DeviateKind, DeviateStatement, DeviateEffectiveStatement> {
62 private static final Logger LOG = LoggerFactory.getLogger(AbstractDeviateStatementSupport.class);
64 private static final SubstatementValidator DEVIATE_NOT_SUPPORTED_SUBSTATEMENT_VALIDATOR =
65 SubstatementValidator.builder(YangStmtMapping.DEVIATE).build();
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)
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)
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)
98 private static final ImmutableMap<String, DeviateKind> KEYWORD_TO_DEVIATE_MAP =
99 Maps.uniqueIndex(Arrays.asList(DeviateKind.values()), DeviateKind::getKeyword);
101 private static final ImmutableSet<YangStmtMapping> SINGLETON_STATEMENTS = ImmutableSet.of(
102 YangStmtMapping.UNITS, YangStmtMapping.CONFIG, YangStmtMapping.MANDATORY,
103 YangStmtMapping.MIN_ELEMENTS, YangStmtMapping.MAX_ELEMENTS);
105 private static final ImmutableSet<YangStmtMapping> IMPLICIT_STATEMENTS = ImmutableSet.of(YangStmtMapping.CONFIG,
106 YangStmtMapping.MANDATORY, YangStmtMapping.MAX_ELEMENTS, YangStmtMapping.MIN_ELEMENTS);
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);
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);
121 public final void onFullDefinitionDeclared(
122 final Mutable<DeviateKind, DeviateStatement, DeviateEffectiveStatement> deviateStmtCtx) {
123 final DeviateKind deviateKind = deviateStmtCtx.argument();
124 getSubstatementValidatorForDeviate(deviateKind).validate(deviateStmtCtx);
126 final SchemaNodeIdentifier deviationTarget =
127 (SchemaNodeIdentifier) deviateStmtCtx.coerceParentContext().argument();
129 if (!isDeviationSupported(deviateStmtCtx, deviationTarget)) {
133 final ModelActionBuilder deviateAction = deviateStmtCtx.newInferenceAction(
134 ModelProcessingPhase.EFFECTIVE_MODEL);
136 final Prerequisite<StmtContext<DeviateKind, DeviateStatement,
137 DeviateEffectiveStatement>> sourceCtxPrerequisite =
138 deviateAction.requiresCtx(deviateStmtCtx, ModelProcessingPhase.EFFECTIVE_MODEL);
140 final Prerequisite<Mutable<?, ?, EffectiveStatement<?, ?>>> targetCtxPrerequisite =
141 deviateAction.mutatesEffectiveCtxPath(deviateStmtCtx.getRoot(),
142 SchemaTreeNamespace.class, deviationTarget.getNodeIdentifiers());
144 deviateAction.apply(new InferenceAction() {
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
152 final var sourceNodeStmtCtx = sourceCtxPrerequisite.resolve(ctx);
153 final var targetNodeStmtCtx = targetCtxPrerequisite.resolve(ctx);
155 switch (deviateKind) {
157 // FIXME: this can be short-circuited without an inference action
158 targetNodeStmtCtx.setUnsupported();
161 performDeviateAdd(sourceNodeStmtCtx, targetNodeStmtCtx);
164 performDeviateReplace(sourceNodeStmtCtx, targetNodeStmtCtx);
167 performDeviateDelete(sourceNodeStmtCtx, targetNodeStmtCtx);
170 throw new IllegalStateException("Unsupported deviate " + deviateKind);
175 public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
176 throw new InferenceException(deviateStmtCtx.coerceParentContext(), "Deviation target '%s' not found.",
181 public void prerequisiteUnavailable(final Prerequisite<?> unavail) {
182 if (targetCtxPrerequisite.equals(unavail)) {
183 deviateStmtCtx.setUnsupported();
185 prerequisiteFailed(List.of(unavail));
192 public String internArgument(final String rawArgument) {
193 if ("add".equals(rawArgument)) {
195 } else if ("delete".equals(rawArgument)) {
197 } else if ("replace".equals(rawArgument)) {
199 } else if ("not-supported".equals(rawArgument)) {
200 return "not-supported";
207 protected final DeviateStatement createDeclared(final BoundStmtCtx<DeviateKind> ctx,
208 final ImmutableList<DeclaredStatement<?>> substatements) {
209 return DeclaredStatements.createDeviate(ctx.getArgument(), substatements);
213 protected final DeviateStatement attachDeclarationReference(final DeviateStatement stmt,
214 final DeclarationReference reference) {
215 return DeclaredStatementDecorators.decorateDeviate(stmt, reference);
219 protected final DeviateEffectiveStatement createEffective(final Current<DeviateKind, DeviateStatement> stmt,
220 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
221 return EffectiveStatements.createDeviate(stmt.declared(), substatements);
224 protected SubstatementValidator getSubstatementValidatorForDeviate(final DeviateKind deviateKind) {
225 switch (deviateKind) {
227 return DEVIATE_NOT_SUPPORTED_SUBSTATEMENT_VALIDATOR;
229 return DEVIATE_ADD_SUBSTATEMENT_VALIDATOR;
231 return DEVIATE_REPLACE_SUBSTATEMENT_VALIDATOR;
233 return DEVIATE_DELETE_SUBSTATEMENT_VALIDATOR;
235 throw new IllegalStateException(String.format(
236 "Substatement validator for deviate %s has not been defined.", deviateKind));
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) {
249 final QNameModule currentModule = deviateStmtCtx.getFromNamespace(ModuleCtxToModuleQName.class,
250 deviateStmtCtx.getRoot());
251 final QNameModule targetModule = Iterables.getLast(deviationTarget.getNodeIdentifiers()).getModule();
253 final Set<QNameModule> deviationModulesSupportedByTargetModule = modulesDeviatedByModules.get(targetModule);
254 if (deviationModulesSupportedByTargetModule != null) {
255 return deviationModulesSupportedByTargetModule.contains(currentModule);
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);
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()),
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());
286 copyStatement(stmtCtxToBeAdded, targetCtx);
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);
299 private static void replaceStatement(final StmtContext<?, ?, ?> stmtCtxToBeReplaced,
300 final Mutable<?, ?, ?> targetCtx) {
301 final StatementDefinition stmtToBeReplaced = stmtCtxToBeReplaced.publicDefinition();
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());
311 for (StmtContext<?, ?, ?> targetCtxSubstatement : targetCtx.effectiveSubstatements()) {
312 if (stmtToBeReplaced.equals(targetCtxSubstatement.publicDefinition())) {
313 targetCtx.removeStatementFromEffectiveSubstatements(stmtToBeReplaced);
314 copyStatement(stmtCtxToBeReplaced, targetCtx);
319 for (Mutable<?, ?, ?> targetCtxSubstatement : targetCtx.mutableDeclaredSubstatements()) {
320 if (stmtToBeReplaced.equals(targetCtxSubstatement.publicDefinition())) {
321 targetCtxSubstatement.setUnsupported();
322 copyStatement(stmtCtxToBeReplaced, targetCtx);
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);
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());
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);
350 private static void deleteStatement(final StmtContext<?, ?, ?> stmtCtxToBeDeleted,
351 final Mutable<?, ?, ?> targetCtx) {
352 final StatementDefinition stmtToBeDeleted = stmtCtxToBeDeleted.publicDefinition();
353 final String stmtArgument = stmtCtxToBeDeleted.rawArgument();
355 for (Mutable<?, ?, ?> targetCtxSubstatement : targetCtx.mutableEffectiveSubstatements()) {
356 if (statementsAreEqual(stmtToBeDeleted, stmtArgument, targetCtxSubstatement.publicDefinition(),
357 targetCtxSubstatement.rawArgument())) {
358 targetCtx.removeStatementFromEffectiveSubstatements(stmtToBeDeleted, stmtArgument);
363 for (Mutable<?, ?, ?> targetCtxSubstatement : targetCtx.mutableDeclaredSubstatements()) {
364 if (statementsAreEqual(stmtToBeDeleted, stmtArgument, targetCtxSubstatement.publicDefinition(),
365 targetCtxSubstatement.rawArgument())) {
366 targetCtxSubstatement.setUnsupported();
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());
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));
383 targetCtx.addEffectiveSubstatement(targetCtx.childCopyOf(stmtCtxToBeCopied, CopyType.ORIGINAL));
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);
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());
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());
406 if (supportedDeviationTargets == null) {
407 supportedDeviationTargets = YangValidationBundles.SUPPORTED_DEVIATION_TARGETS.get(YangVersion.VERSION_1,
408 deviateSubstatementCtx.publicDefinition());
411 // if supportedDeviationTargets is null, it means that the deviate substatement is an unknown statement
412 return supportedDeviationTargets == null || supportedDeviationTargets.contains(
413 deviateTargetCtx.publicDefinition());