aee5587a365ffbb2b34ecf5428c7457ead0c8ee1
[controller.git] / opendaylight / sal / yang-prototype / code-generator / yang-model-parser-impl / src / main / java / org / opendaylight / controller / yang / parser / builder / impl / DeviationBuilder.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.controller.yang.parser.builder.impl;
9
10 import java.util.ArrayList;
11 import java.util.Collections;
12 import java.util.List;
13
14 import org.opendaylight.controller.yang.model.api.Deviation;
15 import org.opendaylight.controller.yang.model.api.Deviation.Deviate;
16 import org.opendaylight.controller.yang.model.api.SchemaPath;
17 import org.opendaylight.controller.yang.model.api.UnknownSchemaNode;
18 import org.opendaylight.controller.yang.parser.builder.api.Builder;
19 import org.opendaylight.controller.yang.parser.util.Comparators;
20 import org.opendaylight.controller.yang.parser.util.ParserListenerUtils;
21 import org.opendaylight.controller.yang.parser.util.YangParseException;
22
23 public final class DeviationBuilder implements Builder {
24     private final int line;
25     private Builder parent;
26     private boolean isBuilt;
27     private final DeviationImpl instance;
28
29     private SchemaPath targetPath;
30     private String reference;
31     private final List<UnknownSchemaNodeBuilder> addedUnknownNodes = new ArrayList<UnknownSchemaNodeBuilder>();
32
33     DeviationBuilder(final int line, final String targetPathStr) {
34         if(!targetPathStr.startsWith("/")) {
35             throw new YangParseException(line, "Deviation argument string must be an absolute schema node identifier.");
36         }
37         this.line = line;
38         this.targetPath = ParserListenerUtils.parseAugmentPath(targetPathStr);
39         instance = new DeviationImpl();
40     }
41
42     @Override
43     public Deviation build() {
44         if(targetPath == null) {
45             throw new YangParseException(line, "Unresolved deviation target");
46         }
47
48         if(!isBuilt) {
49             instance.setTargetPath(targetPath);
50             instance.setReference(reference);
51
52             // UNKNOWN NODES
53             List<UnknownSchemaNode> unknownNodes = new ArrayList<UnknownSchemaNode>();
54             for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
55                 unknownNodes.add(b.build());
56             }
57             Collections.sort(unknownNodes, Comparators.SCHEMA_NODE_COMP);
58             instance.setUnknownSchemaNodes(unknownNodes);
59
60             isBuilt = true;
61         }
62
63         return instance;
64     }
65
66     @Override
67     public int getLine() {
68         return line;
69     }
70
71     @Override
72     public Builder getParent() {
73         return parent;
74     }
75
76     @Override
77     public void setParent(final Builder parent) {
78         this.parent = parent;
79     }
80
81     @Override
82     public void addUnknownSchemaNode(UnknownSchemaNodeBuilder unknownNode) {
83         addedUnknownNodes.add(unknownNode);
84     }
85
86     public SchemaPath getTargetPath() {
87         return targetPath;
88     }
89
90     public void setTargetPath(final SchemaPath targetPath) {
91         this.targetPath = targetPath;
92     }
93
94     public void setDeviate(final String deviate) {
95         if ("not-supported".equals(deviate)) {
96             instance.setDeviate(Deviate.NOT_SUPPORTED);
97         } else if ("add".equals(deviate)) {
98             instance.setDeviate(Deviate.ADD);
99         } else if ("replace".equals(deviate)) {
100             instance.setDeviate(Deviate.REPLACE);
101         } else if ("delete".equals(deviate)) {
102             instance.setDeviate(Deviate.DELETE);
103         } else {
104             throw new YangParseException(line,
105                     "Unsupported type of 'deviate' statement: " + deviate);
106         }
107     }
108
109     public void setReference(final String reference) {
110         this.reference = reference;
111     }
112
113     private final class DeviationImpl implements Deviation {
114         private SchemaPath targetPath;
115         private Deviate deviate;
116         private String reference;
117         private List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
118
119         private DeviationImpl() {
120         }
121
122         @Override
123         public SchemaPath getTargetPath() {
124             return targetPath;
125         }
126
127         private void setTargetPath(final SchemaPath targetPath) {
128             this.targetPath = targetPath;
129         }
130
131         @Override
132         public Deviate getDeviate() {
133             return deviate;
134         }
135
136         private void setDeviate(final Deviate deviate) {
137             this.deviate = deviate;
138         }
139
140         @Override
141         public String getReference() {
142             return reference;
143         }
144
145         private void setReference(final String reference) {
146             this.reference = reference;
147         }
148
149         public List<UnknownSchemaNode> getUnknownSchemaNodes() {
150             return unknownNodes;
151         }
152
153         private void setUnknownSchemaNodes(List<UnknownSchemaNode> unknownSchemaNodes) {
154             if (unknownSchemaNodes != null) {
155                 this.unknownNodes = unknownSchemaNodes;
156             }
157         }
158
159         @Override
160         public int hashCode() {
161             final int prime = 31;
162             int result = 1;
163             result = prime * result
164                     + ((targetPath == null) ? 0 : targetPath.hashCode());
165             result = prime * result
166                     + ((deviate == null) ? 0 : deviate.hashCode());
167             result = prime * result
168                     + ((reference == null) ? 0 : reference.hashCode());
169             return result;
170         }
171
172         @Override
173         public boolean equals(Object obj) {
174             if (this == obj) {
175                 return true;
176             }
177             if (obj == null) {
178                 return false;
179             }
180             if (getClass() != obj.getClass()) {
181                 return false;
182             }
183             DeviationImpl other = (DeviationImpl) obj;
184             if (targetPath == null) {
185                 if (other.targetPath != null) {
186                     return false;
187                 }
188             } else if (!targetPath.equals(other.targetPath)) {
189                 return false;
190             }
191             if (deviate == null) {
192                 if (other.deviate != null) {
193                     return false;
194                 }
195             } else if (!deviate.equals(other.deviate)) {
196                 return false;
197             }
198             if (reference == null) {
199                 if (other.reference != null) {
200                     return false;
201                 }
202             } else if (!reference.equals(other.reference)) {
203                 return false;
204             }
205             return true;
206         }
207
208         @Override
209         public String toString() {
210             StringBuilder sb = new StringBuilder(
211                     DeviationImpl.class.getSimpleName());
212             sb.append("[");
213             sb.append("targetPath=" + targetPath);
214             sb.append(", deviate=" + deviate);
215             sb.append(", reference=" + reference);
216             sb.append("]");
217             return sb.toString();
218         }
219     }
220
221 }