BUG-4556: Introduce StmtContext.getSchemaPath()
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / reactor / RootStatementContext.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 com.google.common.base.Optional;
11 import java.util.Collection;
12 import org.opendaylight.yangtools.yang.common.QNameModule;
13 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
14 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
15 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
16 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.NamespaceStorageNode;
17 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.Registry;
18 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.StorageNodeType;
19 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
20 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
21
22 /**
23  * root statement class for a Yang source
24  */
25 public class RootStatementContext<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>> extends
26         StatementContextBase<A, D, E> {
27
28     private final SourceSpecificContext sourceContext;
29     private final A argument;
30
31     RootStatementContext(final ContextBuilder<A, D, E> builder, final SourceSpecificContext sourceContext) throws SourceException {
32         super(builder);
33         this.sourceContext = sourceContext;
34         this.argument = builder.getDefinition().parseArgumentValue(this, builder.getRawArgument());
35     }
36
37     RootStatementContext(final RootStatementContext<A, D, E> original, final QNameModule newQNameModule, final TypeOfCopy typeOfCopy)
38             throws SourceException {
39         super(original);
40
41         sourceContext = original.sourceContext;
42         this.argument = original.argument;
43
44         copyDeclaredStmts(original, newQNameModule, typeOfCopy);
45
46         copyEffectiveStmts(original, newQNameModule, typeOfCopy);
47
48     }
49
50     /**
51      * copies declared statements from original to this' substatements
52      *
53      * @param typeOfCopy
54      *            determines whether copy is used by augmentation or uses
55      * @throws SourceException
56      */
57     private void copyDeclaredStmts(final RootStatementContext<A, D, E> original, final QNameModule newQNameModule,
58             final TypeOfCopy typeOfCopy) throws SourceException {
59         Collection<? extends StmtContext<?, ?, ?>> originalDeclaredSubstatements = original.declaredSubstatements();
60         for (StmtContext<?, ?, ?> stmtContext : originalDeclaredSubstatements) {
61             this.addEffectiveSubstatement(stmtContext.createCopy(newQNameModule, this, typeOfCopy));
62         }
63     }
64
65     /**
66      * copies effective statements from original to this' substatements
67      *
68      * @param typeOfCopy
69      *            determines whether copy is used by augmentation or uses
70      * @throws SourceException
71      */
72     private void copyEffectiveStmts(final RootStatementContext<A, D, E> original, final QNameModule newQNameModule,
73             final TypeOfCopy typeOfCopy) throws SourceException {
74         Collection<? extends StmtContext<?, ?, ?>> originalEffectiveSubstatements = original.effectiveSubstatements();
75         for (StmtContext<?, ?, ?> stmtContext : originalEffectiveSubstatements) {
76             this.addEffectiveSubstatement(stmtContext.createCopy(newQNameModule, this, typeOfCopy));
77         }
78     }
79
80     /**
81      * @return null as root cannot have parent
82      */
83     @Override
84     public StatementContextBase<?, ?, ?> getParentContext() {
85         return null;
86     }
87
88     /**
89      * @return namespace storage of source context
90      */
91     @Override
92     public NamespaceStorageNode getParentNamespaceStorage() {
93         return sourceContext;
94     }
95
96     /**
97      * @return registry of source context
98      */
99     @Override
100     public Registry getBehaviourRegistry() {
101         return sourceContext;
102     }
103
104     @Override
105     public StorageNodeType getStorageNodeType() {
106         return StorageNodeType.ROOT_STATEMENT_LOCAL;
107     }
108     /**
109      * @return this as its own root
110      */
111     @Override
112     public RootStatementContext<?, ?, ?> getRoot() {
113         return this;
114     }
115
116     SourceSpecificContext getSourceContext() {
117         return sourceContext;
118     }
119
120     @Override
121     public A getStatementArgument() {
122         return argument;
123     }
124
125     /**
126      * @return copy of this considering {@link TypeOfCopy} (augment, uses)
127      *
128      * @throws SourceException instance of SourceException
129      */
130     @Override
131     public StatementContextBase<?, ?, ?> createCopy(final StatementContextBase<?, ?, ?> newParent, final TypeOfCopy typeOfCopy)
132             throws SourceException {
133         return createCopy(null, newParent, typeOfCopy);
134     }
135
136     /**
137      * @return copy of this considering {@link TypeOfCopy} (augment, uses)
138      *
139      * @throws SourceException instance of SourceException
140      */
141     @Override
142     public StatementContextBase<A, D, E> createCopy(final QNameModule newQNameModule,
143             final StatementContextBase<?, ?, ?> newParent, final TypeOfCopy typeOfCopy) throws SourceException {
144         RootStatementContext<A, D, E> copy = new RootStatementContext<>(this, newQNameModule, typeOfCopy);
145
146         copy.addAllToCopyHistory(this.getCopyHistory());
147         copy.addToCopyHistory(typeOfCopy);
148
149         if(this.getOriginalCtx() != null) {
150             copy.setOriginalCtx(this.getOriginalCtx());
151         } else {
152             copy.setOriginalCtx(this);
153         }
154         definition().onStatementAdded(copy);
155         return copy;
156     }
157
158     @Override
159     public Optional<SchemaPath> getSchemaPath() {
160         return Optional.of(SchemaPath.ROOT);
161     }
162
163     /**
164      * @return true
165      */
166     @Override
167     public boolean isRootContext() {
168         return true;
169     }
170 }