Fixed a bug to block the creation of a static host on an ISL port, removed the code...
[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         @Override
150         public List<UnknownSchemaNode> getUnknownSchemaNodes() {
151             return unknownNodes;
152         }
153
154         private void setUnknownSchemaNodes(List<UnknownSchemaNode> unknownSchemaNodes) {
155             if (unknownSchemaNodes != null) {
156                 this.unknownNodes = unknownSchemaNodes;
157             }
158         }
159
160         @Override
161         public int hashCode() {
162             final int prime = 31;
163             int result = 1;
164             result = prime * result
165                     + ((targetPath == null) ? 0 : targetPath.hashCode());
166             result = prime * result
167                     + ((deviate == null) ? 0 : deviate.hashCode());
168             result = prime * result
169                     + ((reference == null) ? 0 : reference.hashCode());
170             return result;
171         }
172
173         @Override
174         public boolean equals(Object obj) {
175             if (this == obj) {
176                 return true;
177             }
178             if (obj == null) {
179                 return false;
180             }
181             if (getClass() != obj.getClass()) {
182                 return false;
183             }
184             DeviationImpl other = (DeviationImpl) obj;
185             if (targetPath == null) {
186                 if (other.targetPath != null) {
187                     return false;
188                 }
189             } else if (!targetPath.equals(other.targetPath)) {
190                 return false;
191             }
192             if (deviate == null) {
193                 if (other.deviate != null) {
194                     return false;
195                 }
196             } else if (!deviate.equals(other.deviate)) {
197                 return false;
198             }
199             if (reference == null) {
200                 if (other.reference != null) {
201                     return false;
202                 }
203             } else if (!reference.equals(other.reference)) {
204                 return false;
205             }
206             return true;
207         }
208
209         @Override
210         public String toString() {
211             StringBuilder sb = new StringBuilder(
212                     DeviationImpl.class.getSimpleName());
213             sb.append("[");
214             sb.append("targetPath=" + targetPath);
215             sb.append(", deviate=" + deviate);
216             sb.append(", reference=" + reference);
217             sb.append("]");
218             return sb.toString();
219         }
220     }
221
222 }