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 java.util.Collection;
11 import org.opendaylight.yangtools.yang.common.QName;
12 import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping;
13 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
14 import org.opendaylight.yangtools.yang.model.api.stmt.AugmentStatement;
15 import org.opendaylight.yangtools.yang.model.api.stmt.DescriptionStatement;
16 import org.opendaylight.yangtools.yang.model.api.stmt.IfFeatureStatement;
17 import org.opendaylight.yangtools.yang.model.api.stmt.ReferenceStatement;
18 import org.opendaylight.yangtools.yang.model.api.stmt.RefineStatement;
19 import org.opendaylight.yangtools.yang.model.api.stmt.StatusStatement;
20 import org.opendaylight.yangtools.yang.model.api.stmt.UsesStatement;
21 import org.opendaylight.yangtools.yang.model.api.stmt.WhenStatement;
22 import org.opendaylight.yangtools.yang.parser.spi.GroupingNamespace;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractDeclaredStatement;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceAction;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.Prerequisite;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
32 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
33 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
34 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.UsesEffectiveStatementImpl;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
39 public class UsesStatementImpl extends AbstractDeclaredStatement<QName> implements UsesStatement {
41 private static final Logger LOG = LoggerFactory.getLogger(UsesStatementImpl.class);
43 protected UsesStatementImpl(final StmtContext<QName, UsesStatement, ?> context) {
47 public static class Definition extends
48 AbstractStatementSupport<QName, UsesStatement, EffectiveStatement<QName, UsesStatement>> {
51 super(Rfc6020Mapping.USES);
55 public QName parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
56 return Utils.qNameFromArgument(ctx, value);
60 public void onFullDefinitionDeclared(
61 final StmtContext.Mutable<QName, UsesStatement, EffectiveStatement<QName, UsesStatement>> usesNode)
62 throws SourceException {
64 if(StmtContextUtils.isInExtensionBody(usesNode)) {
68 ModelActionBuilder usesAction = usesNode.newInferenceAction(ModelProcessingPhase.EFFECTIVE_MODEL);
69 final QName groupingName = usesNode.getStatementArgument();
71 final Prerequisite<StmtContext<?, ?, ?>> sourceGroupingPre = usesAction.requiresCtx(usesNode,
72 GroupingNamespace.class, groupingName, ModelProcessingPhase.EFFECTIVE_MODEL);
73 final Prerequisite<? extends StmtContext.Mutable<?, ?, ?>> targetNodePre = usesAction.mutatesEffectiveCtx(
74 usesNode.getParentContext());
76 usesAction.apply(new InferenceAction() {
79 public void apply() throws InferenceException {
80 StatementContextBase<?, ?, ?> targetNodeStmtCtx = (StatementContextBase<?, ?, ?>) targetNodePre.get();
81 StatementContextBase<?, ?, ?> sourceGrpStmtCtx = (StatementContextBase<?, ?, ?>) sourceGroupingPre.get();
84 GroupingUtils.copyFromSourceToTarget(sourceGrpStmtCtx, targetNodeStmtCtx, usesNode);
85 GroupingUtils.resolveUsesNode(usesNode, targetNodeStmtCtx);
86 } catch (SourceException e) {
87 LOG.warn(e.getMessage(), e);
93 public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) throws InferenceException {
94 if (failed.contains(sourceGroupingPre)) {
95 throw new InferenceException("Grouping " + groupingName + " was not resolved.", usesNode
96 .getStatementSourceReference());
98 throw new InferenceException("Unknown error occurred.", usesNode.getStatementSourceReference());
104 public UsesStatement createDeclared(final StmtContext<QName, UsesStatement, ?> ctx) {
105 return new UsesStatementImpl(ctx);
109 public EffectiveStatement<QName, UsesStatement> createEffective(
110 final StmtContext<QName, UsesStatement, EffectiveStatement<QName, UsesStatement>> ctx) {
111 return new UsesEffectiveStatementImpl(ctx);
117 public QName getName() {
122 public WhenStatement getWhenStatement() {
123 return firstDeclared(WhenStatement.class);
127 public Collection<? extends IfFeatureStatement> getIfFeatures() {
128 return allDeclared(IfFeatureStatement.class);
132 public StatusStatement getStatus() {
133 return firstDeclared(StatusStatement.class);
137 public DescriptionStatement getDescription() {
138 return firstDeclared(DescriptionStatement.class);
142 public ReferenceStatement getReference() {
143 return firstDeclared(ReferenceStatement.class);
147 public Collection<? extends AugmentStatement> getAugments() {
148 return allDeclared(AugmentStatement.class);
152 public Collection<? extends RefineStatement> getRefines() {
153 return allDeclared(RefineStatement.class);