46f809b31b5b6c3583de5616d627f32a2576c3ac
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / builder / impl / AugmentationSchemaBuilderImpl.java
1 /*
2  * Copyright (c) 2013 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.builder.impl;
9
10 import com.google.common.collect.ImmutableList;
11 import java.util.Objects;
12 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
13 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
14 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
15 import org.opendaylight.yangtools.yang.model.util.RevisionAwareXPathImpl;
16 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationSchemaBuilder;
17 import org.opendaylight.yangtools.yang.parser.builder.api.Builder;
18 import org.opendaylight.yangtools.yang.parser.builder.api.UnknownSchemaNodeBuilder;
19 import org.opendaylight.yangtools.yang.parser.builder.util.AbstractDocumentedDataNodeContainerBuilder;
20
21 public final class AugmentationSchemaBuilderImpl extends AbstractDocumentedDataNodeContainerBuilder implements AugmentationSchemaBuilder {
22     private final int order;
23     private AugmentationSchemaImpl instance;
24     private String whenCondition;
25
26     private final String augmentTargetStr;
27     private final SchemaPath targetPath;
28
29     private boolean resolved;
30     private boolean unsupportedTarget = false;
31
32     private AugmentationSchemaBuilder copyOf;
33
34     public AugmentationSchemaBuilderImpl(final String moduleName, final int line, final String augmentTargetStr,
35             final SchemaPath targetPath, final int order) {
36         super(moduleName, line, null);
37         this.order = order;
38         this.augmentTargetStr = augmentTargetStr;
39         this.targetPath = targetPath;
40     }
41
42     @Override
43     protected String getStatementName() {
44         return "augment";
45     }
46
47     @Override
48     public SchemaPath getPath() {
49         return targetPath;
50     }
51
52     @Override
53     public SchemaPath getTargetPath() {
54         return targetPath;
55     }
56
57     @Override
58     public AugmentationSchema build() {
59         if (instance != null) {
60             return instance;
61         }
62
63         buildChildren();
64
65         instance = new AugmentationSchemaImpl(targetPath, order,this);
66
67         Builder parent = getParent();
68         if (parent instanceof ModuleBuilder) {
69             ModuleBuilder moduleBuilder = (ModuleBuilder) parent;
70             instance.namespace = moduleBuilder.getNamespace();
71             instance.revision = moduleBuilder.getRevision();
72         }
73
74         if (copyOf != null) {
75             instance.setCopyOf(copyOf.build());
76         }
77
78         RevisionAwareXPath whenStmt;
79         if (whenCondition == null) {
80             whenStmt = null;
81         } else {
82             whenStmt = new RevisionAwareXPathImpl(whenCondition, false);
83         }
84         instance.whenCondition = whenStmt;
85
86         // UNKNOWN NODES
87         for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
88             unknownNodes.add(b.build());
89         }
90         instance.unknownNodes = ImmutableList.copyOf(unknownNodes);
91
92         return instance;
93     }
94
95     @Override
96     public boolean isResolved() {
97         return resolved;
98     }
99
100     @Override
101     public void setResolved(final boolean resolved) {
102         this.resolved = resolved;
103     }
104
105     /**
106      *  Set true if target of augment is unsupported (e.g. node in body of extension).
107      *  In such case, augmentation is skipped and AugmentationSchema is not built.
108      */
109     @Override
110     public void setUnsupportedTarget(boolean unsupportedTarget) {
111         this.unsupportedTarget = unsupportedTarget;
112     }
113
114     /**
115      *  Return true if target of augment is unsupported (e.g. node in body of extension).
116      *  In such case, augmentation is skipped and AugmentationSchema is not built.
117      */
118     @Override
119     public boolean isUnsupportedTarget() {
120         return unsupportedTarget;
121     }
122
123     @Override
124     public String getWhenCondition() {
125         return whenCondition;
126     }
127
128     @Override
129     public void addWhenCondition(final String whenCondition) {
130         this.whenCondition = whenCondition;
131     }
132
133     @Override
134     public String getTargetPathAsString() {
135         return augmentTargetStr;
136     }
137
138     @Override
139     public int getOrder() {
140         return order;
141     }
142
143     @Override
144     public int hashCode() {
145         final int prime = 17;
146         int result = 1;
147         result = prime * result + Objects.hashCode(augmentTargetStr);
148         result = prime * result + Objects.hashCode(whenCondition);
149         result = prime * result + getChildNodeBuilders().hashCode();
150         return result;
151     }
152
153     @Override
154     public boolean equals(final Object obj) {
155         if (this == obj) {
156             return true;
157         }
158         if (obj == null) {
159             return false;
160         }
161         if (getClass() != obj.getClass()) {
162             return false;
163         }
164         AugmentationSchemaBuilderImpl other = (AugmentationSchemaBuilderImpl) obj;
165         if (augmentTargetStr == null) {
166             if (other.augmentTargetStr != null) {
167                 return false;
168             }
169         } else if (!augmentTargetStr.equals(other.augmentTargetStr)) {
170             return false;
171         }
172         if (whenCondition == null) {
173             if (other.whenCondition != null) {
174                 return false;
175             }
176         } else if (!whenCondition.equals(other.whenCondition)) {
177             return false;
178         }
179         if (!getChildNodeBuilders().equals(other.getChildNodeBuilders())) {
180             return false;
181         }
182         return true;
183     }
184
185     @Override
186     public String toString() {
187         return "augment " + augmentTargetStr;
188     }
189
190     public void setCopyOf(final AugmentationSchemaBuilder old) {
191         copyOf = old;
192     }
193 }