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