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