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