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