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.ImmutableMap;
11 import com.google.common.collect.ImmutableMap.Builder;
12 import com.google.common.collect.ImmutableSet;
13 import com.google.common.collect.SetMultimap;
14 import java.util.Collection;
16 import java.util.Objects;
18 import org.opendaylight.yangtools.yang.common.QNameModule;
19 import org.opendaylight.yangtools.yang.common.YangVersion;
20 import org.opendaylight.yangtools.yang.model.api.DeviateKind;
21 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
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.DeviateStatement;
25 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
26 import org.opendaylight.yangtools.yang.parser.rfc7950.namespace.SchemaNodeIdentifierBuildNamespace;
27 import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.YangValidationBundles;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.CopyType;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceAction;
33 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceContext;
34 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.Prerequisite;
35 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
36 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
37 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
38 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
39 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
40 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleCtxToModuleQName;
41 import org.opendaylight.yangtools.yang.parser.spi.source.ModulesDeviatedByModules;
42 import org.opendaylight.yangtools.yang.parser.spi.source.ModulesDeviatedByModules.SupportedModules;
43 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
44 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
48 abstract class AbstractDeviateStatementSupport extends AbstractStatementSupport<DeviateKind, DeviateStatement,
49 EffectiveStatement<DeviateKind, DeviateStatement>> {
50 private static final Logger LOG = LoggerFactory.getLogger(AbstractDeviateStatementSupport.class);
52 private static final SubstatementValidator DEVIATE_NOT_SUPPORTED_SUBSTATEMENT_VALIDATOR =
53 SubstatementValidator.builder(YangStmtMapping.DEVIATE).build();
55 private static final SubstatementValidator DEVIATE_ADD_SUBSTATEMENT_VALIDATOR =
56 SubstatementValidator.builder(YangStmtMapping.DEVIATE)
57 .addOptional(YangStmtMapping.CONFIG)
58 .addOptional(YangStmtMapping.DEFAULT)
59 .addOptional(YangStmtMapping.MANDATORY)
60 .addOptional(YangStmtMapping.MAX_ELEMENTS)
61 .addOptional(YangStmtMapping.MIN_ELEMENTS)
62 .addAny(YangStmtMapping.MUST)
63 .addAny(YangStmtMapping.UNIQUE)
64 .addOptional(YangStmtMapping.UNITS)
67 private static final SubstatementValidator DEVIATE_REPLACE_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 .addOptional(YangStmtMapping.TYPE)
75 .addOptional(YangStmtMapping.UNITS)
78 private static final SubstatementValidator DEVIATE_DELETE_SUBSTATEMENT_VALIDATOR =
79 SubstatementValidator.builder(YangStmtMapping.DEVIATE)
80 .addOptional(YangStmtMapping.DEFAULT)
81 .addAny(YangStmtMapping.MUST)
82 .addAny(YangStmtMapping.UNIQUE)
83 .addOptional(YangStmtMapping.UNITS)
86 private static final Map<String, DeviateKind> KEYWORD_TO_DEVIATE_MAP;
89 final Builder<String, DeviateKind> keywordToDeviateMapBuilder = ImmutableMap.builder();
90 for (final DeviateKind deviate : DeviateKind.values()) {
91 keywordToDeviateMapBuilder.put(deviate.getKeyword(), deviate);
93 KEYWORD_TO_DEVIATE_MAP = keywordToDeviateMapBuilder.build();
96 private static final Set<YangStmtMapping> SINGLETON_STATEMENTS = ImmutableSet.of(
97 YangStmtMapping.UNITS, YangStmtMapping.CONFIG, YangStmtMapping.MANDATORY,
98 YangStmtMapping.MIN_ELEMENTS, YangStmtMapping.MAX_ELEMENTS);
100 private static final Set<YangStmtMapping> IMPLICIT_STATEMENTS = ImmutableSet.of(YangStmtMapping.CONFIG,
101 YangStmtMapping.MANDATORY, YangStmtMapping.MAX_ELEMENTS, YangStmtMapping.MIN_ELEMENTS);
103 AbstractDeviateStatementSupport() {
104 super(YangStmtMapping.DEVIATE);
108 public final DeviateKind parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
109 return SourceException.throwIfNull(KEYWORD_TO_DEVIATE_MAP.get(value),
110 ctx.getStatementSourceReference(), "String '%s' is not valid deviate argument", value);
114 public final DeviateStatement createDeclared(final StmtContext<DeviateKind, DeviateStatement, ?> ctx) {
115 return new DeviateStatementImpl(ctx);
119 public final EffectiveStatement<DeviateKind, DeviateStatement> createEffective(
120 final StmtContext<DeviateKind, DeviateStatement, EffectiveStatement<DeviateKind,
121 DeviateStatement>> ctx) {
122 return new DeviateEffectiveStatementImpl(ctx);
126 public final void onFullDefinitionDeclared(final Mutable<DeviateKind, DeviateStatement,
127 EffectiveStatement<DeviateKind, DeviateStatement>> deviateStmtCtx) {
128 final DeviateKind deviateKind = deviateStmtCtx.getStatementArgument();
129 getSubstatementValidatorForDeviate(deviateKind).validate(deviateStmtCtx);
131 final SchemaNodeIdentifier deviationTarget =
132 (SchemaNodeIdentifier) deviateStmtCtx.getParentContext().getStatementArgument();
134 if (!isDeviationSupported(deviateStmtCtx, deviationTarget)) {
138 final ModelActionBuilder deviateAction = deviateStmtCtx.newInferenceAction(
139 ModelProcessingPhase.EFFECTIVE_MODEL);
141 final Prerequisite<StmtContext<DeviateKind, DeviateStatement, EffectiveStatement<DeviateKind,
142 DeviateStatement>>> sourceCtxPrerequisite =
143 deviateAction.requiresCtx(deviateStmtCtx, ModelProcessingPhase.EFFECTIVE_MODEL);
145 final Prerequisite<Mutable<?, ?, EffectiveStatement<?, ?>>> targetCtxPrerequisite =
146 deviateAction.mutatesEffectiveCtx(deviateStmtCtx.getRoot(),
147 SchemaNodeIdentifierBuildNamespace.class, deviationTarget);
149 deviateAction.apply(new InferenceAction() {
151 public void apply(final InferenceContext ctx) throws InferenceException {
152 // FIXME once BUG-7760 gets fixed, there will be no need for these dirty casts
153 final StatementContextBase<?, ?, ?> sourceNodeStmtCtx =
154 (StatementContextBase<?, ?, ?>) sourceCtxPrerequisite.resolve(ctx);
155 final StatementContextBase<?, ?, ?> targetNodeStmtCtx =
156 (StatementContextBase<?, ?, ?>) targetCtxPrerequisite.resolve(ctx);
158 switch (deviateKind) {
160 targetNodeStmtCtx.setIsSupportedToBuildEffective(false);
163 performDeviateAdd(sourceNodeStmtCtx, targetNodeStmtCtx);
166 performDeviateReplace(sourceNodeStmtCtx, targetNodeStmtCtx);
169 performDeviateDelete(sourceNodeStmtCtx, targetNodeStmtCtx);
172 throw new IllegalStateException("Unsupported deviate " + deviateKind);
177 public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
178 throw new InferenceException(deviateStmtCtx.getParentContext().getStatementSourceReference(),
179 "Deviation target '%s' not found.", deviationTarget);
185 protected final SubstatementValidator getSubstatementValidator() {
189 protected SubstatementValidator getSubstatementValidatorForDeviate(final DeviateKind deviateKind) {
190 switch (deviateKind) {
192 return DEVIATE_NOT_SUPPORTED_SUBSTATEMENT_VALIDATOR;
194 return DEVIATE_ADD_SUBSTATEMENT_VALIDATOR;
196 return DEVIATE_REPLACE_SUBSTATEMENT_VALIDATOR;
198 return DEVIATE_DELETE_SUBSTATEMENT_VALIDATOR;
200 throw new IllegalStateException(String.format(
201 "Substatement validator for deviate %s has not been defined.", deviateKind));
205 private static boolean isDeviationSupported(final Mutable<DeviateKind, DeviateStatement,
206 EffectiveStatement<DeviateKind, DeviateStatement>> deviateStmtCtx,
207 final SchemaNodeIdentifier deviationTarget) {
208 final SetMultimap<QNameModule, QNameModule> modulesDeviatedByModules = deviateStmtCtx.getFromNamespace(
209 ModulesDeviatedByModules.class, SupportedModules.SUPPORTED_MODULES);
210 if (modulesDeviatedByModules == null) {
214 final QNameModule currentModule = deviateStmtCtx.getFromNamespace(ModuleCtxToModuleQName.class,
215 deviateStmtCtx.getRoot());
216 final QNameModule targetModule = deviationTarget.getLastComponent().getModule();
218 final Set<QNameModule> deviationModulesSupportedByTargetModule = modulesDeviatedByModules.get(targetModule);
219 if (deviationModulesSupportedByTargetModule != null) {
220 return deviationModulesSupportedByTargetModule.contains(currentModule);
226 private static void performDeviateAdd(final StatementContextBase<?, ?, ?> deviateStmtCtx,
227 final StatementContextBase<?, ?, ?> targetCtx) {
228 for (Mutable<?, ?, ?> originalStmtCtx : deviateStmtCtx.mutableDeclaredSubstatements()) {
229 validateDeviationTarget(originalStmtCtx, targetCtx);
230 addStatement(originalStmtCtx, targetCtx);
234 private static void addStatement(final Mutable<?, ?, ?> stmtCtxToBeAdded,
235 final StatementContextBase<?, ?, ?> targetCtx) {
236 if (!StmtContextUtils.isUnknownStatement(stmtCtxToBeAdded)) {
237 final StatementDefinition stmtToBeAdded = stmtCtxToBeAdded.getPublicDefinition();
238 if (SINGLETON_STATEMENTS.contains(stmtToBeAdded) || YangStmtMapping.DEFAULT.equals(stmtToBeAdded)
239 && YangStmtMapping.LEAF.equals(targetCtx.getPublicDefinition())) {
240 for (final StmtContext<?, ?, ?> targetCtxSubstatement : targetCtx.allSubstatements()) {
241 InferenceException.throwIf(stmtToBeAdded.equals(targetCtxSubstatement.getPublicDefinition()),
242 stmtCtxToBeAdded.getStatementSourceReference(),
243 "Deviation cannot add substatement %s to target node %s because it is already defined "
244 + "in target and can appear only once.",
245 stmtToBeAdded.getStatementName(), targetCtx.getStatementArgument());
250 copyStatement(stmtCtxToBeAdded, targetCtx);
253 private static void performDeviateReplace(final StatementContextBase<?, ?, ?> deviateStmtCtx,
254 final StatementContextBase<?, ?, ?> targetCtx) {
255 for (Mutable<?, ?, ?> originalStmtCtx : deviateStmtCtx.mutableDeclaredSubstatements()) {
256 validateDeviationTarget(originalStmtCtx, targetCtx);
257 replaceStatement(originalStmtCtx, targetCtx);
261 private static void replaceStatement(final Mutable<?, ?, ?> stmtCtxToBeReplaced,
262 final StatementContextBase<?, ?, ?> targetCtx) {
263 final StatementDefinition stmtToBeReplaced = stmtCtxToBeReplaced.getPublicDefinition();
265 if (YangStmtMapping.DEFAULT.equals(stmtToBeReplaced)
266 && YangStmtMapping.LEAF_LIST.equals(targetCtx.getPublicDefinition())) {
267 LOG.error("Deviation cannot replace substatement {} in target leaf-list {} because a leaf-list can "
268 + "have multiple default statements. At line: {}", stmtToBeReplaced.getStatementName(),
269 targetCtx.getStatementArgument(), stmtCtxToBeReplaced.getStatementSourceReference());
273 for (final StmtContext<?, ?, ?> targetCtxSubstatement : targetCtx.effectiveSubstatements()) {
274 if (stmtToBeReplaced.equals(targetCtxSubstatement.getPublicDefinition())) {
275 targetCtx.removeStatementFromEffectiveSubstatements(stmtToBeReplaced);
276 copyStatement(stmtCtxToBeReplaced, targetCtx);
281 for (final Mutable<?, ?, ?> targetCtxSubstatement : targetCtx.mutableDeclaredSubstatements()) {
282 if (stmtToBeReplaced.equals(targetCtxSubstatement.getPublicDefinition())) {
283 targetCtxSubstatement.setIsSupportedToBuildEffective(false);
284 copyStatement(stmtCtxToBeReplaced, targetCtx);
289 // This is a special case when deviate replace of a config/mandatory/max/min-elements substatement targets
290 // a node which does not contain an explicitly declared config/mandatory/max/min-elements.
291 // However, according to RFC6020/RFC7950, these properties are always implicitly present.
292 if (IMPLICIT_STATEMENTS.contains(stmtToBeReplaced)) {
293 addStatement(stmtCtxToBeReplaced, targetCtx);
297 throw new InferenceException(stmtCtxToBeReplaced.getStatementSourceReference(), "Deviation cannot replace "
298 + "substatement %s in target node %s because it does not exist in target node.",
299 stmtToBeReplaced.getStatementName(), targetCtx.getStatementArgument());
302 private static void performDeviateDelete(final StatementContextBase<?, ?, ?> deviateStmtCtx,
303 final StatementContextBase<?, ?, ?> targetCtx) {
304 for (Mutable<?, ?, ?> originalStmtCtx : deviateStmtCtx.mutableDeclaredSubstatements()) {
305 validateDeviationTarget(originalStmtCtx, targetCtx);
306 deleteStatement(originalStmtCtx, targetCtx);
310 private static void deleteStatement(final StmtContext<?, ?, ?> stmtCtxToBeDeleted,
311 final StatementContextBase<?, ?, ?> targetCtx) {
312 final StatementDefinition stmtToBeDeleted = stmtCtxToBeDeleted.getPublicDefinition();
313 final String stmtArgument = stmtCtxToBeDeleted.rawStatementArgument();
315 for (final Mutable<?, ?, ?> targetCtxSubstatement : targetCtx.mutableEffectiveSubstatements()) {
316 if (statementsAreEqual(stmtToBeDeleted, stmtArgument, targetCtxSubstatement.getPublicDefinition(),
317 targetCtxSubstatement.rawStatementArgument())) {
318 targetCtx.removeStatementFromEffectiveSubstatements(stmtToBeDeleted, stmtArgument);
323 for (final Mutable<?, ?, ?> targetCtxSubstatement : targetCtx.mutableDeclaredSubstatements()) {
324 if (statementsAreEqual(stmtToBeDeleted, stmtArgument, targetCtxSubstatement.getPublicDefinition(),
325 targetCtxSubstatement.rawStatementArgument())) {
326 targetCtxSubstatement.setIsSupportedToBuildEffective(false);
331 LOG.error("Deviation cannot delete substatement {} with argument '{}' in target node {} because it does "
332 + "not exist in the target node. At line: {}", stmtToBeDeleted.getStatementName(), stmtArgument,
333 targetCtx.getStatementArgument(), stmtCtxToBeDeleted.getStatementSourceReference());
336 private static void copyStatement(final Mutable<?, ?, ?> stmtCtxToBeCopied,
337 final StatementContextBase<?, ?, ?> targetCtx) {
338 // we need to make a copy of the statement context only if it is an unknown statement, otherwise
339 // we can reuse the original statement context
340 if (!StmtContextUtils.isUnknownStatement(stmtCtxToBeCopied)) {
341 targetCtx.addEffectiveSubstatement(stmtCtxToBeCopied);
343 targetCtx.addEffectiveSubstatement(targetCtx.childCopyOf(stmtCtxToBeCopied, CopyType.ORIGINAL));
347 private static boolean statementsAreEqual(final StatementDefinition firstStmtDef, final String firstStmtArg,
348 final StatementDefinition secondStmtDef, final String secondStmtArg) {
349 return firstStmtDef.equals(secondStmtDef) && Objects.equals(firstStmtArg, secondStmtArg);
352 private static void validateDeviationTarget(final StmtContext<?, ?, ?> deviateSubStmtCtx,
353 final StmtContext<?, ?, ?> targetCtx) {
354 InferenceException.throwIf(!isSupportedDeviationTarget(deviateSubStmtCtx, targetCtx,
355 targetCtx.getRootVersion()), deviateSubStmtCtx.getStatementSourceReference(),
356 "%s is not a valid deviation target for substatement %s.",
357 targetCtx.getStatementArgument(), deviateSubStmtCtx.getPublicDefinition().getStatementName());
360 private static boolean isSupportedDeviationTarget(final StmtContext<?, ?, ?> deviateSubstatementCtx,
361 final StmtContext<?, ?, ?> deviateTargetCtx, final YangVersion yangVersion) {
362 Set<StatementDefinition> supportedDeviationTargets =
363 YangValidationBundles.SUPPORTED_DEVIATION_TARGETS.get(deviateTargetCtx.getRootVersion(),
364 deviateSubstatementCtx.getPublicDefinition());
366 if (supportedDeviationTargets == null) {
367 supportedDeviationTargets = YangValidationBundles.SUPPORTED_DEVIATION_TARGETS.get(YangVersion.VERSION_1,
368 deviateSubstatementCtx.getPublicDefinition());
371 // if supportedDeviationTargets is null, it means that the deviate substatement is an unknown statement
372 return supportedDeviationTargets == null || supportedDeviationTargets.contains(
373 deviateTargetCtx.getPublicDefinition());