e1058c9f98a841ce11e97e3cec0baf0e43c58257
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / UsesStatementImpl.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020;
9
10 import static org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase.FULL_DECLARATION;
11
12 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.UsesEffectiveStatementImpl;
13 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
14 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
15 import java.util.Collection;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping;
18 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
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.meta.StatementSource;
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.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.meta.AbstractDeclaredStatement;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
33 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
34 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
35 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceAction;
36 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.Prerequisite;
37 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
38
39 public class UsesStatementImpl extends AbstractDeclaredStatement<QName>
40         implements UsesStatement {
41
42     protected UsesStatementImpl(StmtContext<QName, UsesStatement, ?> context) {
43         super(context);
44     }
45
46     public static class Definition
47             extends
48             AbstractStatementSupport<QName, UsesStatement, EffectiveStatement<QName, UsesStatement>> {
49
50         public Definition() {
51             super(Rfc6020Mapping.USES);
52         }
53
54         @Override
55         public QName parseArgumentValue(StmtContext<?, ?, ?> ctx, String value) {
56             return Utils.qNameFromArgument(ctx, value);
57         }
58
59         @Override
60         public void onFullDefinitionDeclared(final StmtContext.Mutable<QName,UsesStatement,EffectiveStatement<QName,UsesStatement>> usesNode) throws InferenceException ,SourceException {
61
62             ModelActionBuilder usesAction = usesNode.newInferenceAction(FULL_DECLARATION);
63             final QName groupingName = usesNode.getStatementArgument();
64
65             final Prerequisite<StmtContext<?, ?, ?>> sourceGroupingPre = usesAction.requiresCtx(usesNode, GroupingNamespace.class, groupingName, FULL_DECLARATION);
66             final Prerequisite< ?extends StmtContext.Mutable<?,?,?>> targetNodePre = usesAction.mutatesCtx(usesNode.getParentContext(), FULL_DECLARATION);
67
68             usesAction.apply(new InferenceAction() {
69
70                 @Override
71                 public void apply() throws InferenceException {
72                     StatementContextBase<?, ?, ?> targetNodeStmtCtx =  (StatementContextBase<?, ?, ?>) targetNodePre.get();
73                     StatementContextBase<?, ?, ?> sourceGrpStmtCtx = (StatementContextBase<?, ?, ?>) sourceGroupingPre.get();
74
75                     try {
76                         GroupingUtils.copyFromSourceToTarget(sourceGrpStmtCtx,targetNodeStmtCtx);
77                         GroupingUtils.resolveUsesNode(usesNode, targetNodeStmtCtx);
78                     } catch (SourceException e) {
79                         // TODO Auto-generated catch block
80                         e.printStackTrace();
81                     }
82                 }
83
84                 @Override
85                 public void prerequisiteFailed(Collection<? extends Prerequisite<?>> failed) throws InferenceException {
86                     if(failed.contains(sourceGroupingPre)) {
87                         throw new InferenceException("Grouping " + groupingName + " was not resoled.", usesNode.getStatementSourceReference());
88                     }
89                     throw new InferenceException("Unknown error occured.", usesNode.getStatementSourceReference());
90                 }
91
92             });
93
94         }
95
96         @Override
97         public UsesStatement createDeclared(
98                 StmtContext<QName, UsesStatement, ?> ctx) {
99             return new UsesStatementImpl(ctx);
100         }
101
102         @Override
103         public EffectiveStatement<QName, UsesStatement> createEffective(
104                 StmtContext<QName, UsesStatement, EffectiveStatement<QName, UsesStatement>> ctx) {
105             return new UsesEffectiveStatementImpl(ctx);
106         }
107
108     }
109
110     @Override
111     public QName getName() {
112         return argument();
113     }
114
115     @Override
116     public WhenStatement getWhenStatement() {
117         return firstDeclared(WhenStatement.class);
118     }
119
120     @Override
121     public Collection<? extends IfFeatureStatement> getIfFeatures() {
122         return allDeclared(IfFeatureStatement.class);
123     }
124
125     @Override
126     public StatusStatement getStatus() {
127         return firstDeclared(StatusStatement.class);
128     }
129
130     @Override
131     public DescriptionStatement getDescription() {
132         return firstDeclared(DescriptionStatement.class);
133     }
134
135     @Override
136     public ReferenceStatement getReference() {
137         return firstDeclared(ReferenceStatement.class);
138     }
139
140     @Override
141     public Collection<? extends AugmentStatement> getAugments() {
142         return allDeclared(AugmentStatement.class);
143     }
144
145     @Override
146     public Collection<? extends RefineStatement> getRefines() {
147         return allDeclared(RefineStatement.class);
148     }
149
150     @Override
151     public QName argument() {
152         // TODO Auto-generated method stub
153         return null;
154     }
155
156     @Override
157     public String rawArgument() {
158         // TODO Auto-generated method stub
159         return null;
160     }
161
162     @Override
163     public Collection<? extends DeclaredStatement<?>> declaredSubstatements() {
164         // TODO Auto-generated method stub
165         return null;
166     }
167
168     @Override
169     public StatementDefinition statementDefinition() {
170         // TODO Auto-generated method stub
171         return null;
172     }
173
174     @Override
175     public StatementSource getStatementSource() {
176         // TODO Auto-generated method stub
177         return null;
178     }
179
180 }