2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.yang.parser.builder.impl;
10 import java.util.ArrayList;
11 import java.util.Collections;
12 import java.util.List;
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;
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;
29 private SchemaPath targetPath;
30 private String reference;
31 private final List<UnknownSchemaNodeBuilder> addedUnknownNodes = new ArrayList<UnknownSchemaNodeBuilder>();
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.");
38 this.targetPath = ParserListenerUtils.parseAugmentPath(targetPathStr);
39 instance = new DeviationImpl();
43 public Deviation build() {
44 if(targetPath == null) {
45 throw new YangParseException(line, "Unresolved deviation target");
49 instance.setTargetPath(targetPath);
50 instance.setReference(reference);
53 List<UnknownSchemaNode> unknownNodes = new ArrayList<UnknownSchemaNode>();
54 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
55 unknownNodes.add(b.build());
57 Collections.sort(unknownNodes, Comparators.SCHEMA_NODE_COMP);
58 instance.setUnknownSchemaNodes(unknownNodes);
67 public int getLine() {
72 public Builder getParent() {
77 public void setParent(final Builder parent) {
82 public void addUnknownSchemaNode(UnknownSchemaNodeBuilder unknownNode) {
83 addedUnknownNodes.add(unknownNode);
86 public SchemaPath getTargetPath() {
90 public void setTargetPath(final SchemaPath targetPath) {
91 this.targetPath = targetPath;
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);
104 throw new YangParseException(line,
105 "Unsupported type of 'deviate' statement: " + deviate);
109 public void setReference(final String reference) {
110 this.reference = reference;
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();
119 private DeviationImpl() {
123 public SchemaPath getTargetPath() {
127 private void setTargetPath(final SchemaPath targetPath) {
128 this.targetPath = targetPath;
132 public Deviate getDeviate() {
136 private void setDeviate(final Deviate deviate) {
137 this.deviate = deviate;
141 public String getReference() {
145 private void setReference(final String reference) {
146 this.reference = reference;
149 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
153 private void setUnknownSchemaNodes(List<UnknownSchemaNode> unknownSchemaNodes) {
154 if (unknownSchemaNodes != null) {
155 this.unknownNodes = unknownSchemaNodes;
160 public int hashCode() {
161 final int prime = 31;
163 result = prime * result
164 + ((targetPath == null) ? 0 : targetPath.hashCode());
165 result = prime * result
166 + ((deviate == null) ? 0 : deviate.hashCode());
167 result = prime * result
168 + ((reference == null) ? 0 : reference.hashCode());
173 public boolean equals(Object obj) {
180 if (getClass() != obj.getClass()) {
183 DeviationImpl other = (DeviationImpl) obj;
184 if (targetPath == null) {
185 if (other.targetPath != null) {
188 } else if (!targetPath.equals(other.targetPath)) {
191 if (deviate == null) {
192 if (other.deviate != null) {
195 } else if (!deviate.equals(other.deviate)) {
198 if (reference == null) {
199 if (other.reference != null) {
202 } else if (!reference.equals(other.reference)) {
209 public String toString() {
210 StringBuilder sb = new StringBuilder(
211 DeviationImpl.class.getSimpleName());
213 sb.append("targetPath=" + targetPath);
214 sb.append(", deviate=" + deviate);
215 sb.append(", reference=" + reference);
217 return sb.toString();