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.ImmutableSet;
11 import java.util.ArrayList;
12 import java.util.Collection;
14 import javax.annotation.Nonnull;
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.common.QNameModule;
17 import org.opendaylight.yangtools.yang.common.YangVersion;
18 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
19 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
20 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
21 import org.opendaylight.yangtools.yang.model.api.stmt.AugmentStatement;
22 import org.opendaylight.yangtools.yang.model.api.stmt.DescriptionStatement;
23 import org.opendaylight.yangtools.yang.model.api.stmt.IfFeatureStatement;
24 import org.opendaylight.yangtools.yang.model.api.stmt.ReferenceStatement;
25 import org.opendaylight.yangtools.yang.model.api.stmt.RefineStatement;
26 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
27 import org.opendaylight.yangtools.yang.model.api.stmt.StatusStatement;
28 import org.opendaylight.yangtools.yang.model.api.stmt.UsesStatement;
29 import org.opendaylight.yangtools.yang.model.api.stmt.WhenStatement;
30 import org.opendaylight.yangtools.yang.parser.spi.GroupingNamespace;
31 import org.opendaylight.yangtools.yang.parser.spi.SubstatementValidator;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractDeclaredStatement;
33 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
34 import org.opendaylight.yangtools.yang.parser.spi.meta.CopyType;
35 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
36 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
37 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceAction;
38 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceContext;
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(final InferenceContext ctx) {
108 final StatementContextBase<?, ?, ?> targetNodeStmtCtx = (StatementContextBase<?, ?, ?>) targetNodePre.resolve(ctx);
109 final StatementContextBase<?, ?, ?> sourceGrpStmtCtx = (StatementContextBase<?, ?, ?>) sourceGroupingPre.resolve(ctx);
112 copyFromSourceToTarget(sourceGrpStmtCtx, targetNodeStmtCtx, usesNode);
113 resolveUsesNode(usesNode, targetNodeStmtCtx);
114 StmtContextUtils.validateIfFeatureAndWhenOnListKeys(usesNode);
115 } catch (final SourceException e) {
116 LOG.warn(e.getMessage(), e);
122 public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
123 InferenceException.throwIf(failed.contains(sourceGroupingPre),
124 usesNode.getStatementSourceReference(), "Grouping '%s' was not resolved.", groupingName);
125 throw new InferenceException("Unknown error occurred.", usesNode.getStatementSourceReference());
131 public UsesStatement createDeclared(final StmtContext<QName, UsesStatement, ?> ctx) {
132 return new UsesStatementImpl(ctx);
136 public EffectiveStatement<QName, UsesStatement> createEffective(
137 final StmtContext<QName, UsesStatement, EffectiveStatement<QName, UsesStatement>> ctx) {
138 return new UsesEffectiveStatementImpl(ctx);
142 protected SubstatementValidator getSubstatementValidator() {
143 return SUBSTATEMENT_VALIDATOR;
150 public QName getName() {
155 public WhenStatement getWhenStatement() {
156 return firstDeclared(WhenStatement.class);
161 public Collection<? extends IfFeatureStatement> getIfFeatures() {
162 return allDeclared(IfFeatureStatement.class);
166 public StatusStatement getStatus() {
167 return firstDeclared(StatusStatement.class);
171 public DescriptionStatement getDescription() {
172 return firstDeclared(DescriptionStatement.class);
176 public ReferenceStatement getReference() {
177 return firstDeclared(ReferenceStatement.class);
182 public Collection<? extends AugmentStatement> getAugments() {
183 return allDeclared(AugmentStatement.class);
188 public Collection<? extends RefineStatement> getRefines() {
189 return allDeclared(RefineStatement.class);
193 * @param sourceGrpStmtCtx
194 * source grouping statement context
199 * @throws SourceException
200 * instance of SourceException
202 private static void copyFromSourceToTarget(final StmtContext<?, ?, ?> sourceGrpStmtCtx,
203 final StatementContextBase<?, ?, ?> targetCtx,
204 final StmtContext.Mutable<QName, UsesStatement, EffectiveStatement<QName, UsesStatement>> usesNode) {
205 final Collection<StatementContextBase<?, ?, ?>> declared = sourceGrpStmtCtx.declaredSubstatements();
206 final Collection<StatementContextBase<?, ?, ?>> effective = sourceGrpStmtCtx.effectiveSubstatements();
207 final Collection<StatementContextBase<?, ?, ?>> buffer = new ArrayList<>(declared.size() + effective.size());
208 final QNameModule newQNameModule = getNewQNameModule(targetCtx, sourceGrpStmtCtx);
210 for (final StatementContextBase<?, ?, ?> original : declared) {
211 if (StmtContextUtils.areFeaturesSupported(original)) {
212 copyStatement(original, targetCtx, newQNameModule, buffer);
216 for (final StatementContextBase<?, ?, ?> original : effective) {
217 copyStatement(original, targetCtx, newQNameModule, buffer);
220 targetCtx.addEffectiveSubstatements(buffer);
221 usesNode.addAsEffectOfStatement(buffer);
224 private static void copyStatement(final StatementContextBase<?, ?, ?> original,
225 final StatementContextBase<?, ?, ?> targetCtx, final QNameModule targetModule,
226 final Collection<StatementContextBase<?, ?, ?>> buffer) {
227 if (needToCopyByUses(original)) {
228 final StatementContextBase<?, ?, ?> copy = original.createCopy(targetModule, targetCtx,
229 CopyType.ADDED_BY_USES);
231 } else if (isReusedByUsesOnTop(original)) {
232 buffer.add(original);
236 private static final Set<YangStmtMapping> TOP_REUSED_DEF_SET = ImmutableSet.of(
237 YangStmtMapping.TYPE,
238 YangStmtMapping.TYPEDEF);
240 private static boolean isReusedByUsesOnTop(final StmtContext<?, ?, ?> stmtContext) {
241 return TOP_REUSED_DEF_SET.contains(stmtContext.getPublicDefinition());
244 private static final Set<YangStmtMapping> NOCOPY_FROM_GROUPING_SET = ImmutableSet.of(
245 YangStmtMapping.DESCRIPTION,
246 YangStmtMapping.REFERENCE,
247 YangStmtMapping.STATUS);
248 private static final Set<YangStmtMapping> REUSED_DEF_SET = ImmutableSet.of(
249 YangStmtMapping.TYPE,
250 YangStmtMapping.TYPEDEF,
251 YangStmtMapping.USES);
253 public static boolean needToCopyByUses(final StmtContext<?, ?, ?> stmtContext) {
254 final StatementDefinition def = stmtContext.getPublicDefinition();
255 if (REUSED_DEF_SET.contains(def)) {
256 LOG.debug("Will reuse {} statement {}", def, stmtContext);
259 if (NOCOPY_FROM_GROUPING_SET.contains(def)) {
260 return !YangStmtMapping.GROUPING.equals(stmtContext.getParentContext().getPublicDefinition());
263 LOG.debug("Will copy {} statement {}", def, stmtContext);
267 public static void resolveUsesNode(
268 final Mutable<QName, UsesStatement, EffectiveStatement<QName, UsesStatement>> usesNode,
269 final StatementContextBase<?, ?, ?> targetNodeStmtCtx) {
270 for (final StatementContextBase<?, ?, ?> subStmtCtx : usesNode.declaredSubstatements()) {
271 if (StmtContextUtils.producesDeclared(subStmtCtx, RefineStatement.class)
272 && areFeaturesSupported(subStmtCtx)) {
273 performRefine(subStmtCtx, targetNodeStmtCtx);
278 private static boolean areFeaturesSupported(final Mutable<?, ?, ?> subStmtCtx) {
280 * In case of Yang 1.1, checks whether features are supported.
282 return !YangVersion.VERSION_1_1.equals(subStmtCtx.getRootVersion()) || StmtContextUtils
283 .areFeaturesSupported(subStmtCtx);
286 private static void performRefine(final StatementContextBase<?, ?, ?> refineCtx,
287 final StatementContextBase<?, ?, ?> usesParentCtx) {
289 final Object refineArgument = refineCtx.getStatementArgument();
290 InferenceException.throwIf(!(refineArgument instanceof SchemaNodeIdentifier),
291 refineCtx.getStatementSourceReference(),
292 "Invalid refine argument %s. It must be instance of SchemaNodeIdentifier.", refineArgument);
294 final SchemaNodeIdentifier refineTargetNodeIdentifier = (SchemaNodeIdentifier) refineArgument;
295 final StatementContextBase<?, ?, ?> refineTargetNodeCtx = Utils.findNode(usesParentCtx,
296 refineTargetNodeIdentifier);
298 InferenceException.throwIfNull(refineTargetNodeCtx, refineCtx.getStatementSourceReference(),
299 "Refine target node %s not found.", refineTargetNodeIdentifier);
301 if (StmtContextUtils.isUnknownStatement(refineTargetNodeCtx)) {
303 "Refine node '{}' in uses '{}' has target node unknown statement '{}'. Refine has been skipped. At line: {}",
304 refineCtx.getStatementArgument(), refineCtx.getParentContext().getStatementArgument(),
305 refineTargetNodeCtx.getStatementArgument(), refineCtx.getStatementSourceReference());
306 refineCtx.addAsEffectOfStatement(refineTargetNodeCtx);
310 addOrReplaceNodes(refineCtx, refineTargetNodeCtx);
311 refineCtx.addAsEffectOfStatement(refineTargetNodeCtx);
314 private static void addOrReplaceNodes(final StatementContextBase<?, ?, ?> refineCtx,
315 final StatementContextBase<?, ?, ?> refineTargetNodeCtx) {
316 for (final StatementContextBase<?, ?, ?> refineSubstatementCtx : refineCtx.declaredSubstatements()) {
317 if (isSupportedRefineSubstatement(refineSubstatementCtx)) {
318 addOrReplaceNode(refineSubstatementCtx, refineTargetNodeCtx);
323 private static void addOrReplaceNode(final StatementContextBase<?, ?, ?> refineSubstatementCtx,
324 final StatementContextBase<?, ?, ?> refineTargetNodeCtx) {
326 final StatementDefinition refineSubstatementDef = refineSubstatementCtx.getPublicDefinition();
328 SourceException.throwIf(!isSupportedRefineTarget(refineSubstatementCtx, refineTargetNodeCtx),
329 refineSubstatementCtx.getStatementSourceReference(),
330 "Error in module '%s' in the refine of uses '%s': can not perform refine of '%s' for the target '%s'.",
331 refineSubstatementCtx.getRoot().getStatementArgument(), refineSubstatementCtx.getParentContext()
332 .getStatementArgument(), refineSubstatementCtx.getPublicDefinition(), refineTargetNodeCtx
333 .getPublicDefinition());
335 if (isAllowedToAddByRefine(refineSubstatementDef)) {
336 refineTargetNodeCtx.addEffectiveSubstatement(refineSubstatementCtx);
338 refineTargetNodeCtx.removeStatementFromEffectiveSubstatements(refineSubstatementDef);
339 refineTargetNodeCtx.addEffectiveSubstatement(refineSubstatementCtx);
343 private static final Set<YangStmtMapping> ALLOWED_TO_ADD_BY_REFINE_DEF_SET = ImmutableSet.of(YangStmtMapping.MUST);
345 private static boolean isAllowedToAddByRefine(final StatementDefinition publicDefinition) {
346 return ALLOWED_TO_ADD_BY_REFINE_DEF_SET.contains(publicDefinition);
349 private static boolean isSupportedRefineSubstatement(final StmtContext<?, ?, ?> refineSubstatementCtx) {
350 final Collection<?> supportedRefineSubstatements = refineSubstatementCtx.getFromNamespace(
351 ValidationBundlesNamespace.class, ValidationBundleType.SUPPORTED_REFINE_SUBSTATEMENTS);
353 return supportedRefineSubstatements == null || supportedRefineSubstatements.isEmpty()
354 || supportedRefineSubstatements.contains(refineSubstatementCtx.getPublicDefinition())
355 || StmtContextUtils.isUnknownStatement(refineSubstatementCtx);
358 private static boolean isSupportedRefineTarget(final StmtContext<?, ?, ?> refineSubstatementCtx,
359 final StmtContext<?, ?, ?> refineTargetNodeCtx) {
360 final Collection<?> supportedRefineTargets = YangValidationBundles.SUPPORTED_REFINE_TARGETS
361 .get(refineSubstatementCtx.getPublicDefinition());
363 return supportedRefineTargets == null || supportedRefineTargets.isEmpty()
364 || supportedRefineTargets.contains(refineTargetNodeCtx.getPublicDefinition());
368 private static QNameModule getNewQNameModule(final StmtContext<?, ?, ?> targetCtx,
369 final StmtContext<?, ?, ?> stmtContext) {
370 if (targetCtx.isRootContext()) {
371 return targetCtx.getFromNamespace(ModuleCtxToModuleQName.class, targetCtx);
373 if (targetCtx.getPublicDefinition() == YangStmtMapping.AUGMENT) {
374 return Utils.getRootModuleQName(targetCtx);
377 final Object targetStmtArgument = targetCtx.getStatementArgument();
378 final Object sourceStmtArgument = stmtContext.getStatementArgument();
379 if (targetStmtArgument instanceof QName && sourceStmtArgument instanceof QName) {
380 return ((QName) targetStmtArgument).getModule();