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