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;
150 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
154 private void setUnknownSchemaNodes(List<UnknownSchemaNode> unknownSchemaNodes) {
155 if (unknownSchemaNodes != null) {
156 this.unknownNodes = unknownSchemaNodes;
161 public int hashCode() {
162 final int prime = 31;
164 result = prime * result
165 + ((targetPath == null) ? 0 : targetPath.hashCode());
166 result = prime * result
167 + ((deviate == null) ? 0 : deviate.hashCode());
168 result = prime * result
169 + ((reference == null) ? 0 : reference.hashCode());
174 public boolean equals(Object obj) {
181 if (getClass() != obj.getClass()) {
184 DeviationImpl other = (DeviationImpl) obj;
185 if (targetPath == null) {
186 if (other.targetPath != null) {
189 } else if (!targetPath.equals(other.targetPath)) {
192 if (deviate == null) {
193 if (other.deviate != null) {
196 } else if (!deviate.equals(other.deviate)) {
199 if (reference == null) {
200 if (other.reference != null) {
203 } else if (!reference.equals(other.reference)) {
210 public String toString() {
211 StringBuilder sb = new StringBuilder(
212 DeviationImpl.class.getSimpleName());
214 sb.append("targetPath=" + targetPath);
215 sb.append(", deviate=" + deviate);
216 sb.append(", reference=" + reference);
218 return sb.toString();