2 * Copyright (c) 2015 Cisco Systems, Inc. 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.stmt.rfc6020;
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.Iterables;
14 import java.util.Collection;
16 import java.util.Objects;
18 import javax.annotation.Nonnull;
19 import org.opendaylight.yangtools.yang.common.QNameModule;
20 import org.opendaylight.yangtools.yang.common.YangVersion;
21 import org.opendaylight.yangtools.yang.model.api.DeviateKind;
22 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
23 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
24 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
25 import org.opendaylight.yangtools.yang.model.api.stmt.DeviateStatement;
26 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
27 import org.opendaylight.yangtools.yang.parser.spi.SubstatementValidator;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractDeclaredStatement;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.CopyType;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
33 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceAction;
34 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceContext;
35 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.Prerequisite;
36 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
37 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
38 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
39 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
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.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.DeviateEffectiveStatementImpl;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
49 public class DeviateStatementImpl extends AbstractDeclaredStatement<DeviateKind> implements DeviateStatement {
50 private static final Logger LOG = LoggerFactory.getLogger(DeviateStatementImpl.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 protected DeviateStatementImpl(final StmtContext<DeviateKind, DeviateStatement, ?> context) {
90 public static class Definition extends AbstractStatementSupport<DeviateKind, DeviateStatement,
91 EffectiveStatement<DeviateKind, DeviateStatement>> {
92 private static final Map<String, DeviateKind> KEYWORD_TO_DEVIATE_MAP;
94 final Builder<String, DeviateKind> keywordToDeviateMapBuilder = ImmutableMap.builder();
95 for (final DeviateKind deviate : DeviateKind.values()) {
96 keywordToDeviateMapBuilder.put(deviate.getKeyword(), deviate);
98 KEYWORD_TO_DEVIATE_MAP = keywordToDeviateMapBuilder.build();
101 private static final Set<YangStmtMapping> SINGLETON_STATEMENTS = ImmutableSet.of(
102 YangStmtMapping.UNITS, YangStmtMapping.CONFIG, YangStmtMapping.MANDATORY,
103 YangStmtMapping.MIN_ELEMENTS, YangStmtMapping.MAX_ELEMENTS);
105 public Definition() {
106 super(YangStmtMapping.DEVIATE);
110 public DeviateKind parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
111 return SourceException.throwIfNull(KEYWORD_TO_DEVIATE_MAP.get(value),
112 ctx.getStatementSourceReference(), "String '%s' is not valid deviate argument", value);
116 public DeviateStatement createDeclared(final StmtContext<DeviateKind, DeviateStatement, ?> ctx) {
117 return new DeviateStatementImpl(ctx);
121 public EffectiveStatement<DeviateKind, DeviateStatement> createEffective(
122 final StmtContext<DeviateKind, DeviateStatement, EffectiveStatement<DeviateKind,
123 DeviateStatement>> ctx) {
124 return new DeviateEffectiveStatementImpl(ctx);
128 public void onFullDefinitionDeclared(final Mutable<DeviateKind, DeviateStatement,
129 EffectiveStatement<DeviateKind, DeviateStatement>> deviateStmtCtx) {
130 final DeviateKind deviateKind = deviateStmtCtx.getStatementArgument();
131 getSubstatementValidatorForDeviate(deviateKind).validate(deviateStmtCtx);
133 final SchemaNodeIdentifier deviationTarget =
134 (SchemaNodeIdentifier) deviateStmtCtx.getParentContext().getStatementArgument();
136 if (!isDeviationSupported(deviateStmtCtx, deviationTarget)) {
140 final ModelActionBuilder deviateAction = deviateStmtCtx.newInferenceAction(
141 ModelProcessingPhase.EFFECTIVE_MODEL);
143 final Prerequisite<StmtContext<DeviateKind, DeviateStatement, EffectiveStatement<DeviateKind,
144 DeviateStatement>>> sourceCtxPrerequisite =
145 deviateAction.requiresCtx(deviateStmtCtx, ModelProcessingPhase.EFFECTIVE_MODEL);
147 final Prerequisite<Mutable<?, ?, EffectiveStatement<?, ?>>> targetCtxPrerequisite =
148 deviateAction.mutatesEffectiveCtx(deviateStmtCtx.getRoot(),
149 SchemaNodeIdentifierBuildNamespace.class, deviationTarget);
151 deviateAction.apply(new InferenceAction() {
153 public void apply(final InferenceContext ctx) throws InferenceException {
154 // FIXME once BUG-7760 gets fixed, there will be no need for these dirty casts
155 final StatementContextBase<?, ?, ?> sourceNodeStmtCtx =
156 (StatementContextBase<?, ?, ?>) sourceCtxPrerequisite.resolve(ctx);
157 final StatementContextBase<?, ?, ?> targetNodeStmtCtx =
158 (StatementContextBase<?, ?, ?>) targetCtxPrerequisite.resolve(ctx);
160 switch (deviateKind) {
162 targetNodeStmtCtx.setIsSupportedToBuildEffective(false);
165 performDeviateAdd(sourceNodeStmtCtx, targetNodeStmtCtx);
168 performDeviateReplace(sourceNodeStmtCtx, targetNodeStmtCtx);
171 performDeviateDelete(sourceNodeStmtCtx, targetNodeStmtCtx);
176 public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
177 throw new InferenceException(deviateStmtCtx.getParentContext().getStatementSourceReference(),
178 "Deviation target '%s' not found.", deviationTarget);
183 private static boolean isDeviationSupported(final Mutable<DeviateKind, DeviateStatement,
184 EffectiveStatement<DeviateKind, DeviateStatement>> deviateStmtCtx,
185 final SchemaNodeIdentifier deviationTarget) {
186 final Map<QNameModule, Set<QNameModule>> modulesDeviatedByModules = deviateStmtCtx.getFromNamespace(
187 ModulesDeviatedByModules.class, SupportedModules.SUPPORTED_MODULES);
188 if (modulesDeviatedByModules == null) {
192 final QNameModule currentModule = deviateStmtCtx.getFromNamespace(ModuleCtxToModuleQName.class,
193 deviateStmtCtx.getRoot());
194 final QNameModule targetModule = deviationTarget.getLastComponent().getModule();
196 final Set<QNameModule> deviationModulesSupportedByTargetModule = modulesDeviatedByModules.get(targetModule);
197 if (deviationModulesSupportedByTargetModule != null) {
198 return deviationModulesSupportedByTargetModule.contains(currentModule);
204 private static void performDeviateAdd(final StatementContextBase<?, ?, ?> deviateStmtCtx,
205 final StatementContextBase<?, ?, ?> targetCtx) {
206 for (Mutable<?, ?, ?> originalStmtCtx : deviateStmtCtx.mutableDeclaredSubstatements()) {
207 validateDeviationTarget(originalStmtCtx, targetCtx);
208 addStatement(originalStmtCtx, targetCtx);
212 private static void addStatement(final Mutable<?, ?, ?> stmtCtxToBeAdded,
213 final StatementContextBase<?, ?, ?> targetCtx) {
214 if (StmtContextUtils.isUnknownStatement(stmtCtxToBeAdded)) {
215 targetCtx.addEffectiveSubstatement(stmtCtxToBeAdded.createCopy(targetCtx, CopyType.ORIGINAL));
219 final StatementDefinition stmtToBeAdded = stmtCtxToBeAdded.getPublicDefinition();
221 if (SINGLETON_STATEMENTS.contains(stmtToBeAdded) || YangStmtMapping.DEFAULT.equals(stmtToBeAdded)
222 && YangStmtMapping.LEAF.equals(targetCtx.getPublicDefinition())) {
223 final Iterable<StmtContext<?, ?, ?>> targetCtxSubstatements = Iterables.concat(
224 targetCtx.declaredSubstatements(), targetCtx.effectiveSubstatements());
226 for (final StmtContext<?, ?, ?> targetCtxSubstatement : targetCtxSubstatements) {
227 InferenceException.throwIf(stmtToBeAdded.equals(targetCtxSubstatement.getPublicDefinition()),
228 stmtCtxToBeAdded.getStatementSourceReference(), "Deviation cannot add substatement %s " +
229 "to target node %s because it is already defined in target and can appear only once.",
230 stmtToBeAdded.getStatementName(), targetCtx.getStatementArgument());
234 targetCtx.addEffectiveSubstatement(stmtCtxToBeAdded.createCopy(targetCtx, CopyType.ORIGINAL));
237 private static void performDeviateReplace(final StatementContextBase<?, ?, ?> deviateStmtCtx,
238 final StatementContextBase<?, ?, ?> targetCtx) {
239 for (Mutable<?, ?, ?> originalStmtCtx : deviateStmtCtx.mutableDeclaredSubstatements()) {
240 validateDeviationTarget(originalStmtCtx, targetCtx);
241 replaceStatement(originalStmtCtx, targetCtx);
245 private static void replaceStatement(final Mutable<?, ?, ?> stmtCtxToBeReplaced,
246 final StatementContextBase<?, ?, ?> targetCtx) {
247 final StatementDefinition stmtToBeReplaced = stmtCtxToBeReplaced.getPublicDefinition();
249 if (YangStmtMapping.DEFAULT.equals(stmtToBeReplaced)
250 && YangStmtMapping.LEAF_LIST.equals(targetCtx.getPublicDefinition())) {
251 LOG.error("Deviation cannot replace substatement {} in target leaf-list {} because a leaf-list can " +
252 "have multiple default statements. At line: {}", stmtToBeReplaced.getStatementName(),
253 targetCtx.getStatementArgument(), stmtCtxToBeReplaced.getStatementSourceReference());
257 for (final StmtContext<?, ?, ?> targetCtxSubstatement : targetCtx.effectiveSubstatements()) {
258 if (stmtToBeReplaced.equals(targetCtxSubstatement.getPublicDefinition())) {
259 targetCtx.removeStatementFromEffectiveSubstatements(stmtToBeReplaced);
260 targetCtx.addEffectiveSubstatement(stmtCtxToBeReplaced.createCopy(targetCtx, CopyType.ORIGINAL));
265 for (final Mutable<?, ?, ?> targetCtxSubstatement : targetCtx.mutableDeclaredSubstatements()) {
266 if (stmtToBeReplaced.equals(targetCtxSubstatement.getPublicDefinition())) {
267 targetCtxSubstatement.setIsSupportedToBuildEffective(false);
268 targetCtx.addEffectiveSubstatement(stmtCtxToBeReplaced.createCopy(targetCtx, CopyType.ORIGINAL));
273 throw new InferenceException(stmtCtxToBeReplaced.getStatementSourceReference(), "Deviation cannot " +
274 "replace substatement %s in target node %s because it does not exist in target node.",
275 stmtToBeReplaced.getStatementName(), targetCtx.getStatementArgument());
278 private static void performDeviateDelete(final StatementContextBase<?, ?, ?> deviateStmtCtx,
279 final StatementContextBase<?, ?, ?> targetCtx) {
280 for (Mutable<?, ?, ?> originalStmtCtx : deviateStmtCtx.mutableDeclaredSubstatements()) {
281 validateDeviationTarget(originalStmtCtx, targetCtx);
282 deleteStatement(originalStmtCtx, targetCtx);
286 private static void deleteStatement(final StmtContext<?, ?, ?> stmtCtxToBeDeleted,
287 final StatementContextBase<?, ?, ?> targetCtx) {
288 final StatementDefinition stmtToBeDeleted = stmtCtxToBeDeleted.getPublicDefinition();
289 final String stmtArgument = stmtCtxToBeDeleted.rawStatementArgument();
291 for (final Mutable<?, ?, ?> targetCtxSubstatement : targetCtx.mutableEffectiveSubstatements()) {
292 if (statementsAreEqual(stmtToBeDeleted, stmtArgument, targetCtxSubstatement.getPublicDefinition(),
293 targetCtxSubstatement.rawStatementArgument())) {
294 targetCtx.removeStatementFromEffectiveSubstatements(stmtToBeDeleted, stmtArgument);
299 for (final Mutable<?, ?, ?> targetCtxSubstatement : targetCtx.mutableDeclaredSubstatements()) {
300 if (statementsAreEqual(stmtToBeDeleted, stmtArgument, targetCtxSubstatement.getPublicDefinition(),
301 targetCtxSubstatement.rawStatementArgument())) {
302 targetCtxSubstatement.setIsSupportedToBuildEffective(false);
307 LOG.error("Deviation cannot delete substatement {} with argument '{}' in target node {} because it does " +
308 "not exist in the target node. At line: {}", stmtToBeDeleted.getStatementName(), stmtArgument,
309 targetCtx.getStatementArgument(), stmtCtxToBeDeleted.getStatementSourceReference());
312 private static boolean statementsAreEqual(final StatementDefinition firstStmtDef, final String firstStmtArg,
313 final StatementDefinition secondStmtDef, final String secondStmtArg) {
314 return firstStmtDef.equals(secondStmtDef) && Objects.equals(firstStmtArg, secondStmtArg);
317 private static void validateDeviationTarget(final StmtContext<?, ?, ?> deviateSubStmtCtx,
318 final StmtContext<?, ?, ?> targetCtx) {
319 InferenceException.throwIf(!isSupportedDeviationTarget(deviateSubStmtCtx, targetCtx,
320 targetCtx.getRootVersion()), deviateSubStmtCtx.getStatementSourceReference(),
321 "%s is not a valid deviation target for substatement %s.",
322 targetCtx.getStatementArgument(), deviateSubStmtCtx.getPublicDefinition().getStatementName());
325 private static boolean isSupportedDeviationTarget(final StmtContext<?, ?, ?> deviateSubstatementCtx,
326 final StmtContext<?, ?, ?> deviateTargetCtx, final YangVersion yangVersion) {
327 Set<StatementDefinition> supportedDeviationTargets =
328 YangValidationBundles.SUPPORTED_DEVIATION_TARGETS.get(deviateTargetCtx.getRootVersion(),
329 deviateSubstatementCtx.getPublicDefinition());
331 if (supportedDeviationTargets == null) {
332 supportedDeviationTargets = YangValidationBundles.SUPPORTED_DEVIATION_TARGETS.get(YangVersion.VERSION_1,
333 deviateSubstatementCtx.getPublicDefinition());
336 // if supportedDeviationTargets is null, it means that the deviate substatement is an unknown statement
337 return supportedDeviationTargets == null || supportedDeviationTargets.contains(
338 deviateTargetCtx.getPublicDefinition());
341 protected SubstatementValidator getSubstatementValidatorForDeviate(final DeviateKind deviateKind) {
342 switch (deviateKind) {
344 return DEVIATE_NOT_SUPPORTED_SUBSTATEMENT_VALIDATOR;
346 return DEVIATE_ADD_SUBSTATEMENT_VALIDATOR;
348 return DEVIATE_REPLACE_SUBSTATEMENT_VALIDATOR;
350 return DEVIATE_DELETE_SUBSTATEMENT_VALIDATOR;
352 throw new IllegalStateException(String.format(
353 "Substatement validator for deviate %s has not been defined.", deviateKind));
358 protected SubstatementValidator getSubstatementValidator() {
364 public DeviateKind getValue() {