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;
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.*;
17 import com.google.common.collect.ImmutableList;
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;
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.");
32 this.targetPathStr = targetPathStr;
33 this.targetPath = ParserUtils.parseXPathString(targetPathStr);
37 public Deviation build() {
38 if (targetPath == null) {
39 throw new YangParseException(moduleName, line, "Unresolved deviation target");
42 if (instance != null) {
46 instance = new DeviationImpl();
47 instance.targetPath = targetPath;
48 instance.deviate = deviate;
49 instance.reference = reference;
52 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
53 unknownNodes.add(b.build());
55 instance.unknownNodes = ImmutableList.copyOf(unknownNodes);
60 public SchemaPath getTargetPath() {
64 public void setTargetPath(final SchemaPath targetPath) {
65 this.targetPath = targetPath;
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;
78 throw new YangParseException(moduleName, line, "Unsupported type of 'deviate' statement: " + deviate);
82 public void setReference(final String reference) {
83 this.reference = reference;
87 public String toString() {
88 return "deviation " + targetPathStr;
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;
97 private DeviationImpl() {
101 public SchemaPath getTargetPath() {
106 public Deviate getDeviate() {
111 public String getReference() {
116 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
121 public int hashCode() {
122 final int prime = 31;
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());
131 public boolean equals(Object obj) {
138 if (getClass() != obj.getClass()) {
141 DeviationImpl other = (DeviationImpl) obj;
142 if (targetPath == null) {
143 if (other.targetPath != null) {
146 } else if (!targetPath.equals(other.targetPath)) {
149 if (deviate == null) {
150 if (other.deviate != null) {
153 } else if (!deviate.equals(other.deviate)) {
156 if (reference == null) {
157 if (other.reference != null) {
160 } else if (!reference.equals(other.reference)) {
167 public String toString() {
168 StringBuilder sb = new StringBuilder(DeviationImpl.class.getSimpleName());
170 sb.append("targetPath=" + targetPath);
171 sb.append(", deviate=" + deviate);
172 sb.append(", reference=" + reference);
174 return sb.toString();