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.base.Preconditions;
11 import com.google.common.collect.ImmutableSet;
12 import java.util.ArrayList;
13 import java.util.Collection;
15 import javax.annotation.Nonnull;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.common.QNameModule;
18 import org.opendaylight.yangtools.yang.common.YangVersion;
19 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
20 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
21 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
22 import org.opendaylight.yangtools.yang.model.api.stmt.AugmentStatement;
23 import org.opendaylight.yangtools.yang.model.api.stmt.DescriptionStatement;
24 import org.opendaylight.yangtools.yang.model.api.stmt.IfFeatureStatement;
25 import org.opendaylight.yangtools.yang.model.api.stmt.ReferenceStatement;
26 import org.opendaylight.yangtools.yang.model.api.stmt.RefineStatement;
27 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
28 import org.opendaylight.yangtools.yang.model.api.stmt.StatusStatement;
29 import org.opendaylight.yangtools.yang.model.api.stmt.UsesStatement;
30 import org.opendaylight.yangtools.yang.model.api.stmt.WhenStatement;
31 import org.opendaylight.yangtools.yang.parser.spi.GroupingNamespace;
32 import org.opendaylight.yangtools.yang.parser.spi.SubstatementValidator;
33 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractDeclaredStatement;
34 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
35 import org.opendaylight.yangtools.yang.parser.spi.meta.CopyType;
36 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
37 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
38 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceAction;
39 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.Prerequisite;
40 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
41 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
42 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
43 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
44 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleCtxToModuleQName;
45 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
46 import org.opendaylight.yangtools.yang.parser.spi.validation.ValidationBundlesNamespace;
47 import org.opendaylight.yangtools.yang.parser.spi.validation.ValidationBundlesNamespace.ValidationBundleType;
48 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
49 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.UsesEffectiveStatementImpl;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
54 public class UsesStatementImpl extends AbstractDeclaredStatement<QName> implements UsesStatement {
55 private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(YangStmtMapping
57 .addAny(YangStmtMapping.AUGMENT)
58 .addOptional(YangStmtMapping.DESCRIPTION)
59 .addAny(YangStmtMapping.IF_FEATURE)
60 .addAny(YangStmtMapping.REFINE)
61 .addOptional(YangStmtMapping.REFERENCE)
62 .addOptional(YangStmtMapping.STATUS)
63 .addOptional(YangStmtMapping.WHEN)
66 private static final Logger LOG = LoggerFactory.getLogger(UsesStatementImpl.class);
68 protected UsesStatementImpl(final StmtContext<QName, UsesStatement, ?> context) {
72 public static class Definition extends
73 AbstractStatementSupport<QName, UsesStatement, EffectiveStatement<QName, UsesStatement>> {
76 super(YangStmtMapping.USES);
80 public QName parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
81 return Utils.qNameFromArgument(ctx, value);
85 public void onFullDefinitionDeclared(
86 final StmtContext.Mutable<QName, UsesStatement, EffectiveStatement<QName, UsesStatement>> usesNode) {
87 if (!StmtContextUtils.areFeaturesSupported(usesNode)) {
90 super.onFullDefinitionDeclared(usesNode);
92 if (StmtContextUtils.isInExtensionBody(usesNode)) {
96 final ModelActionBuilder usesAction = usesNode.newInferenceAction(ModelProcessingPhase.EFFECTIVE_MODEL);
97 final QName groupingName = usesNode.getStatementArgument();
99 final Prerequisite<StmtContext<?, ?, ?>> sourceGroupingPre = usesAction.requiresCtx(usesNode,
100 GroupingNamespace.class, groupingName, ModelProcessingPhase.EFFECTIVE_MODEL);
101 final Prerequisite<? extends StmtContext.Mutable<?, ?, ?>> targetNodePre = usesAction.mutatesEffectiveCtx(
102 usesNode.getParentContext());
104 usesAction.apply(new InferenceAction() {
107 public void apply() {
108 final StatementContextBase<?, ?, ?> targetNodeStmtCtx = (StatementContextBase<?, ?, ?>) targetNodePre.get();
109 final StatementContextBase<?, ?, ?> sourceGrpStmtCtx = (StatementContextBase<?, ?, ?>) sourceGroupingPre.get();
112 copyFromSourceToTarget(sourceGrpStmtCtx, targetNodeStmtCtx, usesNode);
113 resolveUsesNode(usesNode, targetNodeStmtCtx);
114 } catch (final SourceException e) {
115 LOG.warn(e.getMessage(), e);
121 public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
122 InferenceException.throwIf(failed.contains(sourceGroupingPre),
123 usesNode.getStatementSourceReference(), "Grouping '%s' was not resolved.", groupingName);
124 throw new InferenceException("Unknown error occurred.", usesNode.getStatementSourceReference());
130 public UsesStatement createDeclared(final StmtContext<QName, UsesStatement, ?> ctx) {
131 return new UsesStatementImpl(ctx);
135 public EffectiveStatement<QName, UsesStatement> createEffective(
136 final StmtContext<QName, UsesStatement, EffectiveStatement<QName, UsesStatement>> ctx) {
137 return new UsesEffectiveStatementImpl(ctx);
141 protected SubstatementValidator getSubstatementValidator() {
142 return SUBSTATEMENT_VALIDATOR;
149 public QName getName() {
154 public WhenStatement getWhenStatement() {
155 return firstDeclared(WhenStatement.class);
160 public Collection<? extends IfFeatureStatement> getIfFeatures() {
161 return allDeclared(IfFeatureStatement.class);
165 public StatusStatement getStatus() {
166 return firstDeclared(StatusStatement.class);
170 public DescriptionStatement getDescription() {
171 return firstDeclared(DescriptionStatement.class);
175 public ReferenceStatement getReference() {
176 return firstDeclared(ReferenceStatement.class);
181 public Collection<? extends AugmentStatement> getAugments() {
182 return allDeclared(AugmentStatement.class);
187 public Collection<? extends RefineStatement> getRefines() {
188 return allDeclared(RefineStatement.class);
192 * @param sourceGrpStmtCtx
193 * source grouping statement context
198 * @throws SourceException
199 * instance of SourceException
201 private static void copyFromSourceToTarget(final StatementContextBase<?, ?, ?> sourceGrpStmtCtx,
202 final StatementContextBase<?, ?, ?> targetCtx,
203 final StmtContext.Mutable<QName, UsesStatement, EffectiveStatement<QName, UsesStatement>> usesNode) {
204 final Collection<StatementContextBase<?, ?, ?>> declared = sourceGrpStmtCtx.declaredSubstatements();
205 final Collection<StatementContextBase<?, ?, ?>> effective = sourceGrpStmtCtx.effectiveSubstatements();
206 final Collection<StatementContextBase<?, ?, ?>> buffer = new ArrayList<>(declared.size() + effective.size());
207 final QNameModule newQNameModule = getNewQNameModule(targetCtx, sourceGrpStmtCtx);
209 for (final StatementContextBase<?, ?, ?> original : declared) {
210 if (StmtContextUtils.areFeaturesSupported(original)) {
211 copyStatement(original, targetCtx, newQNameModule, buffer);
215 for (final StatementContextBase<?, ?, ?> original : effective) {
216 copyStatement(original, targetCtx, newQNameModule, buffer);
219 targetCtx.addEffectiveSubstatements(buffer);
220 usesNode.addAsEffectOfStatement(buffer);
223 private static void copyStatement(final StatementContextBase<?, ?, ?> original,
224 final StatementContextBase<?, ?, ?> targetCtx, final QNameModule targetModule,
225 final Collection<StatementContextBase<?, ?, ?>> buffer) {
226 if (needToCopyByUses(original)) {
227 final StatementContextBase<?, ?, ?> copy = original.createCopy(targetModule, targetCtx,
228 CopyType.ADDED_BY_USES);
230 } else if (isReusedByUsesOnTop(original)) {
231 buffer.add(original);
235 private static final Set<YangStmtMapping> TOP_REUSED_DEF_SET = ImmutableSet.of(
236 YangStmtMapping.TYPE,
237 YangStmtMapping.TYPEDEF);
239 private static boolean isReusedByUsesOnTop(final StmtContext<?, ?, ?> stmtContext) {
240 return TOP_REUSED_DEF_SET.contains(stmtContext.getPublicDefinition());
243 private static final Set<YangStmtMapping> NOCOPY_FROM_GROUPING_SET = ImmutableSet.of(
244 YangStmtMapping.DESCRIPTION,
245 YangStmtMapping.REFERENCE,
246 YangStmtMapping.STATUS);
247 private static final Set<YangStmtMapping> REUSED_DEF_SET = ImmutableSet.of(
248 YangStmtMapping.TYPE,
249 YangStmtMapping.TYPEDEF,
250 YangStmtMapping.USES);
252 public static boolean needToCopyByUses(final StmtContext<?, ?, ?> stmtContext) {
253 final StatementDefinition def = stmtContext.getPublicDefinition();
254 if (REUSED_DEF_SET.contains(def)) {
255 LOG.debug("Will reuse {} statement {}", def, stmtContext);
258 if (NOCOPY_FROM_GROUPING_SET.contains(def)) {
259 return !YangStmtMapping.GROUPING.equals(stmtContext.getParentContext().getPublicDefinition());
262 LOG.debug("Will copy {} statement {}", def, stmtContext);
266 public static void resolveUsesNode(
267 final Mutable<QName, UsesStatement, EffectiveStatement<QName, UsesStatement>> usesNode,
268 final StatementContextBase<?, ?, ?> targetNodeStmtCtx) {
269 for (final StatementContextBase<?, ?, ?> subStmtCtx : usesNode.declaredSubstatements()) {
270 if (StmtContextUtils.producesDeclared(subStmtCtx, RefineStatement.class)
271 && areFeaturesSupported(subStmtCtx)) {
272 performRefine(subStmtCtx, targetNodeStmtCtx);
277 private static boolean areFeaturesSupported(final StatementContextBase<?, ?, ?> subStmtCtx) {
279 * In case of Yang 1.1, checks whether features are supported.
281 return !YangVersion.VERSION_1_1.equals(subStmtCtx.getRootVersion()) || StmtContextUtils
282 .areFeaturesSupported(subStmtCtx);
285 private static void performRefine(final StatementContextBase<?, ?, ?> refineCtx,
286 final StatementContextBase<?, ?, ?> usesParentCtx) {
288 final Object refineArgument = refineCtx.getStatementArgument();
289 Preconditions.checkArgument(refineArgument instanceof SchemaNodeIdentifier,
290 "Invalid refine argument %s. It must be instance of SchemaNodeIdentifier. At %s", refineArgument,
291 refineCtx.getStatementSourceReference());
293 final SchemaNodeIdentifier refineTargetNodeIdentifier = (SchemaNodeIdentifier) refineArgument;
294 final StatementContextBase<?, ?, ?> refineTargetNodeCtx = Utils.findNode(usesParentCtx,
295 refineTargetNodeIdentifier);
296 Preconditions.checkArgument(refineTargetNodeCtx != null, "Refine target node %s not found. At %s",
297 refineTargetNodeIdentifier, refineCtx.getStatementSourceReference());
298 if (StmtContextUtils.isUnknownStatement(refineTargetNodeCtx)) {
300 "Refine node '{}' in uses '{}' has target node unknown statement '{}'. Refine has been skipped. At line: {}",
301 refineCtx.getStatementArgument(), refineCtx.getParentContext().getStatementArgument(),
302 refineTargetNodeCtx.getStatementArgument(), refineCtx.getStatementSourceReference());
303 refineCtx.addAsEffectOfStatement(refineTargetNodeCtx);
307 addOrReplaceNodes(refineCtx, refineTargetNodeCtx);
308 refineCtx.addAsEffectOfStatement(refineTargetNodeCtx);
311 private static void addOrReplaceNodes(final StatementContextBase<?, ?, ?> refineCtx,
312 final StatementContextBase<?, ?, ?> refineTargetNodeCtx) {
313 for (final StatementContextBase<?, ?, ?> refineSubstatementCtx : refineCtx.declaredSubstatements()) {
314 if (isSupportedRefineSubstatement(refineSubstatementCtx)) {
315 addOrReplaceNode(refineSubstatementCtx, refineTargetNodeCtx);
320 private static void addOrReplaceNode(final StatementContextBase<?, ?, ?> refineSubstatementCtx,
321 final StatementContextBase<?, ?, ?> refineTargetNodeCtx) {
323 final StatementDefinition refineSubstatementDef = refineSubstatementCtx.getPublicDefinition();
325 SourceException.throwIf(!isSupportedRefineTarget(refineSubstatementCtx, refineTargetNodeCtx),
326 refineSubstatementCtx.getStatementSourceReference(),
327 "Error in module '%s' in the refine of uses '%s': can not perform refine of '%s' for the target '%s'.",
328 refineSubstatementCtx.getRoot().getStatementArgument(), refineSubstatementCtx.getParentContext()
329 .getStatementArgument(), refineSubstatementCtx.getPublicDefinition(), refineTargetNodeCtx
330 .getPublicDefinition());
332 if (isAllowedToAddByRefine(refineSubstatementDef)) {
333 refineTargetNodeCtx.addEffectiveSubstatement(refineSubstatementCtx);
335 refineTargetNodeCtx.removeStatementFromEffectiveSubstatements(refineSubstatementDef);
336 refineTargetNodeCtx.addEffectiveSubstatement(refineSubstatementCtx);
340 private static final Set<YangStmtMapping> ALLOWED_TO_ADD_BY_REFINE_DEF_SET = ImmutableSet.of(YangStmtMapping.MUST);
342 private static boolean isAllowedToAddByRefine(final StatementDefinition publicDefinition) {
343 return ALLOWED_TO_ADD_BY_REFINE_DEF_SET.contains(publicDefinition);
346 private static boolean isSupportedRefineSubstatement(final StatementContextBase<?, ?, ?> refineSubstatementCtx) {
347 final Collection<?> supportedRefineSubstatements = refineSubstatementCtx.getFromNamespace(
348 ValidationBundlesNamespace.class, ValidationBundleType.SUPPORTED_REFINE_SUBSTATEMENTS);
350 return supportedRefineSubstatements == null || supportedRefineSubstatements.isEmpty()
351 || supportedRefineSubstatements.contains(refineSubstatementCtx.getPublicDefinition())
352 || StmtContextUtils.isUnknownStatement(refineSubstatementCtx);
355 private static boolean isSupportedRefineTarget(final StatementContextBase<?, ?, ?> refineSubstatementCtx,
356 final StatementContextBase<?, ?, ?> refineTargetNodeCtx) {
358 final Collection<?> supportedRefineTargets = YangValidationBundles.SUPPORTED_REFINE_TARGETS
359 .get(refineSubstatementCtx.getPublicDefinition());
361 return supportedRefineTargets == null || supportedRefineTargets.isEmpty()
362 || supportedRefineTargets.contains(refineTargetNodeCtx.getPublicDefinition());
366 private static QNameModule getNewQNameModule(final StatementContextBase<?, ?, ?> targetCtx,
367 final StmtContext<?, ?, ?> stmtContext) {
368 if (targetCtx.isRootContext()) {
369 return targetCtx.getFromNamespace(ModuleCtxToModuleQName.class, targetCtx);
371 if (targetCtx.getPublicDefinition() == YangStmtMapping.AUGMENT) {
372 return targetCtx.getFromNamespace(ModuleCtxToModuleQName.class, targetCtx.getRoot());
375 final Object targetStmtArgument = targetCtx.getStatementArgument();
376 final Object sourceStmtArgument = stmtContext.getStatementArgument();
377 if (targetStmtArgument instanceof QName && sourceStmtArgument instanceof QName) {
378 return ((QName) targetStmtArgument).getModule();