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.AbstractBuilder;
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 extends AbstractBuilder {
24 private final String targetPathStr;
25 private boolean isBuilt;
26 private final DeviationImpl instance;
28 private SchemaPath targetPath;
29 private String reference;
30 private final List<UnknownSchemaNodeBuilder> addedUnknownNodes = new ArrayList<UnknownSchemaNodeBuilder>();
32 DeviationBuilder(final String moduleName, final int line, final String targetPathStr) {
33 super(moduleName, line);
34 if (!targetPathStr.startsWith("/")) {
35 throw new YangParseException(moduleName, line,
36 "Deviation argument string must be an absolute schema node identifier.");
38 this.targetPathStr = targetPathStr;
39 this.targetPath = ParserListenerUtils.parseAugmentPath(targetPathStr);
40 instance = new DeviationImpl();
44 public Deviation build() {
45 if (targetPath == null) {
46 throw new YangParseException(moduleName, line, "Unresolved deviation target");
50 instance.setTargetPath(targetPath);
51 instance.setReference(reference);
54 List<UnknownSchemaNode> unknownNodes = new ArrayList<UnknownSchemaNode>();
55 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
56 unknownNodes.add(b.build());
58 Collections.sort(unknownNodes, Comparators.SCHEMA_NODE_COMP);
59 instance.setUnknownSchemaNodes(unknownNodes);
67 public SchemaPath getTargetPath() {
71 public void setTargetPath(final SchemaPath targetPath) {
72 this.targetPath = targetPath;
75 public void setDeviate(final String deviate) {
76 if ("not-supported".equals(deviate)) {
77 instance.setDeviate(Deviate.NOT_SUPPORTED);
78 } else if ("add".equals(deviate)) {
79 instance.setDeviate(Deviate.ADD);
80 } else if ("replace".equals(deviate)) {
81 instance.setDeviate(Deviate.REPLACE);
82 } else if ("delete".equals(deviate)) {
83 instance.setDeviate(Deviate.DELETE);
85 throw new YangParseException(moduleName, line, "Unsupported type of 'deviate' statement: " + deviate);
89 public void setReference(final String reference) {
90 this.reference = reference;
94 public String toString() {
95 return "deviation " + targetPathStr;
98 private final class DeviationImpl implements Deviation {
99 private SchemaPath targetPath;
100 private Deviate deviate;
101 private String reference;
102 private List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
104 private DeviationImpl() {
108 public SchemaPath getTargetPath() {
112 private void setTargetPath(final SchemaPath targetPath) {
113 this.targetPath = targetPath;
117 public Deviate getDeviate() {
121 private void setDeviate(final Deviate deviate) {
122 this.deviate = deviate;
126 public String getReference() {
130 private void setReference(final String reference) {
131 this.reference = reference;
135 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
139 private void setUnknownSchemaNodes(List<UnknownSchemaNode> unknownSchemaNodes) {
140 if (unknownSchemaNodes != null) {
141 this.unknownNodes = unknownSchemaNodes;
146 public int hashCode() {
147 final int prime = 31;
149 result = prime * result + ((targetPath == null) ? 0 : targetPath.hashCode());
150 result = prime * result + ((deviate == null) ? 0 : deviate.hashCode());
151 result = prime * result + ((reference == null) ? 0 : reference.hashCode());
156 public boolean equals(Object obj) {
163 if (getClass() != obj.getClass()) {
166 DeviationImpl other = (DeviationImpl) obj;
167 if (targetPath == null) {
168 if (other.targetPath != null) {
171 } else if (!targetPath.equals(other.targetPath)) {
174 if (deviate == null) {
175 if (other.deviate != null) {
178 } else if (!deviate.equals(other.deviate)) {
181 if (reference == null) {
182 if (other.reference != null) {
185 } else if (!reference.equals(other.reference)) {
192 public String toString() {
193 StringBuilder sb = new StringBuilder(DeviationImpl.class.getSimpleName());
195 sb.append("targetPath=" + targetPath);
196 sb.append(", deviate=" + deviate);
197 sb.append(", reference=" + reference);
199 return sb.toString();