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