27bd70f2c6d37bb4cbe86a05b17833789bfc5284
[yangtools.git] / yang / yang-parser-reactor / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / reactor / StatementContextWriter.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.reactor;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static com.google.common.base.Preconditions.checkState;
12 import static com.google.common.base.Verify.verify;
13 import static com.google.common.base.Verify.verifyNotNull;
14 import static java.util.Objects.requireNonNull;
15
16 import java.util.Optional;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.model.api.meta.StatementSource;
19 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
20 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
21 import org.opendaylight.yangtools.yang.parser.spi.source.StatementWriter;
22
23 final class StatementContextWriter implements StatementWriter {
24     private final ModelProcessingPhase phase;
25     private final SourceSpecificContext ctx;
26
27     private AbstractResumedStatement<?, ?, ?> current;
28
29     StatementContextWriter(final SourceSpecificContext ctx, final ModelProcessingPhase phase) {
30         this.ctx = requireNonNull(ctx);
31         this.phase = requireNonNull(phase);
32     }
33
34     @Override
35     public Optional<? extends ResumedStatement> resumeStatement(final int childId) {
36         final Optional<AbstractResumedStatement<?, ?, ?>> existing = ctx.lookupDeclaredChild(current, childId);
37         existing.ifPresent(this::resumeStatement);
38         return existing;
39     }
40
41     private void resumeStatement(final AbstractResumedStatement<?, ?, ?> child) {
42         if (child.isFullyDefined()) {
43             child.walkChildren(phase);
44             child.endDeclared(phase);
45         } else {
46             current = child;
47         }
48     }
49
50     @Override
51     public void storeStatement(final int expectedChildren, final boolean fullyDefined) {
52         checkState(current != null);
53         checkArgument(expectedChildren >= 0);
54         current.resizeSubstatements(expectedChildren);
55
56         if (fullyDefined) {
57             current.setFullyDefined();
58         }
59     }
60
61     @Override
62     public void startStatement(final int childId, final QName name, final String argument,
63             final StatementSourceReference ref) {
64         final Optional<AbstractResumedStatement<?, ?, ?>> existing = ctx.lookupDeclaredChild(current, childId);
65         current = existing.isPresent() ? existing.get()
66                 :  verifyNotNull(ctx.createDeclaredChild(current, childId, name, argument, ref));
67     }
68
69     @Override
70     public void endStatement(final StatementSourceReference ref) {
71         checkState(current != null);
72         current.endDeclared(phase);
73         exitStatement();
74     }
75
76     @Override
77     public ModelProcessingPhase getPhase() {
78         return phase;
79     }
80
81     private void exitStatement() {
82         // TODO: AbstractResumedStatement should only ever have AbstractResumedStatement parents, which would:
83         //       - remove the StatementSource check
84         //       - allow endDeclared() to be moved to AbstractResumedStatement
85         //       - remove the need for verify()
86         StatementContextBase<?, ?, ?> parentContext = current.getParentContext();
87         while (parentContext != null && StatementSource.CONTEXT == parentContext.getStatementSource()) {
88             parentContext.endDeclared(phase);
89             parentContext = parentContext.getParentContext();
90         }
91         if (parentContext != null) {
92             verify(parentContext instanceof AbstractResumedStatement, "Unexpected parent context %s", parentContext);
93             current = (AbstractResumedStatement<?, ?, ?>) parentContext;
94         } else {
95             current = null;
96         }
97     }
98 }