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 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.AbstractBuilder;
17 import org.opendaylight.yangtools.yang.parser.util.ParserUtils;
18 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
20 import com.google.common.collect.ImmutableList;
22 public final class DeviationBuilder extends AbstractBuilder {
23 private DeviationImpl instance;
24 private final String targetPathStr;
25 private SchemaPath targetPath;
26 private Deviate deviate;
27 private String reference;
29 DeviationBuilder(final String moduleName, final int line, final String targetPathStr) {
30 super(moduleName, line);
31 if (!targetPathStr.startsWith("/")) {
32 throw new YangParseException(moduleName, line,
33 "Deviation argument string must be an absolute schema node identifier.");
35 this.targetPathStr = targetPathStr;
36 this.targetPath = ParserUtils.parseXPathString(targetPathStr);
40 public Deviation build() {
41 if (targetPath == null) {
42 throw new YangParseException(getModuleName(), getLine(), "Unresolved deviation target");
45 if (instance != null) {
49 instance = new DeviationImpl();
50 instance.targetPath = targetPath;
51 instance.deviate = deviate;
52 instance.reference = reference;
55 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
56 unknownNodes.add(b.build());
58 instance.unknownNodes = ImmutableList.copyOf(unknownNodes);
63 public SchemaPath getTargetPath() {
67 public void setTargetPath(final SchemaPath targetPath) {
68 this.targetPath = targetPath;
71 public void setDeviate(final String deviate) {
72 if ("not-supported".equals(deviate)) {
73 this.deviate = Deviate.NOT_SUPPORTED;
74 } else if ("add".equals(deviate)) {
75 this.deviate = Deviate.ADD;
76 } else if ("replace".equals(deviate)) {
77 this.deviate = Deviate.REPLACE;
78 } else if ("delete".equals(deviate)) {
79 this.deviate = Deviate.DELETE;
81 throw new YangParseException(getModuleName(), getLine(), "Unsupported type of 'deviate' statement: " + deviate);
85 public void setReference(final String reference) {
86 this.reference = reference;
90 public String toString() {
91 return "deviation " + targetPathStr;
94 private static final class DeviationImpl implements Deviation {
95 private SchemaPath targetPath;
96 private Deviate deviate;
97 private String reference;
98 private ImmutableList<UnknownSchemaNode> unknownNodes;
100 private DeviationImpl() {
104 public SchemaPath getTargetPath() {
109 public Deviate getDeviate() {
114 public String getReference() {
119 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
124 public int hashCode() {
125 final int prime = 31;
127 result = prime * result + ((targetPath == null) ? 0 : targetPath.hashCode());
128 result = prime * result + ((deviate == null) ? 0 : deviate.hashCode());
129 result = prime * result + ((reference == null) ? 0 : reference.hashCode());
134 public boolean equals(Object obj) {
141 if (getClass() != obj.getClass()) {
144 DeviationImpl other = (DeviationImpl) obj;
145 if (targetPath == null) {
146 if (other.targetPath != null) {
149 } else if (!targetPath.equals(other.targetPath)) {
152 if (deviate == null) {
153 if (other.deviate != null) {
156 } else if (!deviate.equals(other.deviate)) {
159 if (reference == null) {
160 if (other.reference != null) {
163 } else if (!reference.equals(other.reference)) {
170 public String toString() {
171 StringBuilder sb = new StringBuilder(DeviationImpl.class.getSimpleName());
173 sb.append("targetPath=" + targetPath);
174 sb.append(", deviate=" + deviate);
175 sb.append(", reference=" + reference);
177 return sb.toString();