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.yangtools.yang.parser.builder.impl;
10 import com.google.common.collect.ImmutableList;
11 import java.util.List;
12 import org.opendaylight.yangtools.yang.model.api.Deviation;
13 import org.opendaylight.yangtools.yang.model.api.Deviation.Deviate;
14 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
15 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
16 import org.opendaylight.yangtools.yang.parser.builder.api.UnknownSchemaNodeBuilder;
17 import org.opendaylight.yangtools.yang.parser.builder.util.AbstractBuilder;
18 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
20 public final class DeviationBuilder extends AbstractBuilder {
21 private DeviationImpl instance;
22 private final String targetPathStr;
23 private SchemaPath targetPath;
24 private Deviate deviate;
25 private String reference;
27 DeviationBuilder(final String moduleName, final int line, final String targetPathStr) {
28 super(moduleName, line);
29 if (!targetPathStr.startsWith("/")) {
30 throw new YangParseException(moduleName, line,
31 "Deviation argument string must be an absolute schema node identifier.");
33 this.targetPathStr = targetPathStr;
34 this.targetPath = BuilderUtils.parseXPathString(targetPathStr);
38 public Deviation build() {
39 if (targetPath == null) {
40 throw new YangParseException(getModuleName(), getLine(), "Unresolved deviation target");
43 if (instance != null) {
47 instance = new DeviationImpl();
48 instance.targetPath = targetPath;
49 instance.deviate = deviate;
50 instance.reference = reference;
53 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
54 unknownNodes.add(b.build());
56 instance.unknownNodes = ImmutableList.copyOf(unknownNodes);
61 public SchemaPath getTargetPath() {
65 public void setTargetPath(final SchemaPath targetPath) {
66 this.targetPath = targetPath;
69 public void setDeviate(final String deviate) {
70 if ("not-supported".equals(deviate)) {
71 this.deviate = Deviate.NOT_SUPPORTED;
72 } else if ("add".equals(deviate)) {
73 this.deviate = Deviate.ADD;
74 } else if ("replace".equals(deviate)) {
75 this.deviate = Deviate.REPLACE;
76 } else if ("delete".equals(deviate)) {
77 this.deviate = Deviate.DELETE;
79 throw new YangParseException(getModuleName(), getLine(), "Unsupported type of 'deviate' statement: " + deviate);
83 public void setReference(final String reference) {
84 this.reference = reference;
88 public String toString() {
89 return "deviation " + targetPathStr;
92 private static final class DeviationImpl implements Deviation {
93 private SchemaPath targetPath;
94 private Deviate deviate;
95 private String reference;
96 private ImmutableList<UnknownSchemaNode> unknownNodes;
98 private DeviationImpl() {
102 public SchemaPath getTargetPath() {
107 public Deviate getDeviate() {
112 public String getReference() {
117 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
122 public int hashCode() {
123 final int prime = 31;
125 result = prime * result + ((targetPath == null) ? 0 : targetPath.hashCode());
126 result = prime * result + ((deviate == null) ? 0 : deviate.hashCode());
127 result = prime * result + ((reference == null) ? 0 : reference.hashCode());
132 public boolean equals(Object obj) {
139 if (getClass() != obj.getClass()) {
142 DeviationImpl other = (DeviationImpl) obj;
143 if (targetPath == null) {
144 if (other.targetPath != null) {
147 } else if (!targetPath.equals(other.targetPath)) {
150 if (deviate == null) {
151 if (other.deviate != null) {
154 } else if (!deviate.equals(other.deviate)) {
157 if (reference == null) {
158 if (other.reference != null) {
161 } else if (!reference.equals(other.reference)) {
168 public String toString() {
169 StringBuilder sb = new StringBuilder(DeviationImpl.class.getSimpleName());
171 sb.append("targetPath=").append(targetPath);
172 sb.append(", deviate=").append(deviate);
173 sb.append(", reference=").append(reference);
175 return sb.toString();