Fixed a bug to block the creation of a static host on an ISL port, removed the code...
[controller.git] / opendaylight / sal / yang-prototype / code-generator / yang-model-parser-impl / src / main / java / org / opendaylight / controller / yang / parser / builder / impl / ExtensionBuilder.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.yang.parser.builder.impl;
9
10 import java.util.ArrayList;
11 import java.util.Collections;
12 import java.util.List;
13
14 import org.opendaylight.controller.yang.common.QName;
15 import org.opendaylight.controller.yang.model.api.ExtensionDefinition;
16 import org.opendaylight.controller.yang.model.api.SchemaPath;
17 import org.opendaylight.controller.yang.model.api.Status;
18 import org.opendaylight.controller.yang.model.api.UnknownSchemaNode;
19 import org.opendaylight.controller.yang.parser.builder.api.AbstractSchemaNodeBuilder;
20 import org.opendaylight.controller.yang.parser.util.Comparators;
21
22 public final class ExtensionBuilder extends AbstractSchemaNodeBuilder {
23     private boolean isBuilt;
24     private final ExtensionDefinitionImpl instance;
25
26     ExtensionBuilder(final int line, final QName qname) {
27         super(line, qname);
28         instance = new ExtensionDefinitionImpl(qname);
29     }
30
31     @Override
32     public ExtensionDefinition build() {
33         if (!isBuilt) {
34             instance.setPath(schemaPath);
35             instance.setDescription(description);
36             instance.setReference(reference);
37             instance.setStatus(status);
38
39             // UNKNOWN NODES
40             if (unknownNodes == null) {
41                 unknownNodes = new ArrayList<UnknownSchemaNode>();
42                 for (UnknownSchemaNodeBuilder un : addedUnknownNodes) {
43                     unknownNodes.add(un.build());
44                 }
45                 Collections.sort(unknownNodes, Comparators.SCHEMA_NODE_COMP);
46             }
47             instance.setUnknownSchemaNodes(unknownNodes);
48
49             isBuilt = true;
50         }
51
52         return instance;
53     }
54
55     public void setYinElement(boolean yin) {
56         instance.setYinElement(yin);
57     }
58
59     public void setArgument(String argument) {
60         instance.setArgument(argument);
61     }
62
63     private final class ExtensionDefinitionImpl implements ExtensionDefinition {
64         private final QName qname;
65         private String argument;
66         private SchemaPath schemaPath;
67         private String description;
68         private String reference;
69         private Status status = Status.CURRENT;
70         private List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
71         private boolean yin;
72
73         private ExtensionDefinitionImpl(QName qname) {
74             this.qname = qname;
75         }
76
77         @Override
78         public QName getQName() {
79             return qname;
80         }
81
82         @Override
83         public SchemaPath getPath() {
84             return schemaPath;
85         }
86
87         private void setPath(SchemaPath schemaPath) {
88             this.schemaPath = schemaPath;
89         }
90
91         @Override
92         public String getDescription() {
93             return description;
94         }
95
96         private void setDescription(String description) {
97             this.description = description;
98         }
99
100         @Override
101         public String getReference() {
102             return reference;
103         }
104
105         private void setReference(String reference) {
106             this.reference = reference;
107         }
108
109         @Override
110         public Status getStatus() {
111             return status;
112         }
113
114         private void setStatus(Status status) {
115             if (status != null) {
116                 this.status = status;
117             }
118         }
119
120         @Override
121         public List<UnknownSchemaNode> getUnknownSchemaNodes() {
122             return unknownNodes;
123         }
124
125         private void setUnknownSchemaNodes(List<UnknownSchemaNode> unknownNodes) {
126             if (unknownNodes != null) {
127                 this.unknownNodes = unknownNodes;
128             }
129         }
130
131         @Override
132         public String getArgument() {
133             return argument;
134         }
135
136         private void setArgument(String argument) {
137             this.argument = argument;
138         }
139
140         @Override
141         public boolean isYinElement() {
142             return yin;
143         }
144
145         private void setYinElement(boolean yin) {
146             this.yin = yin;
147         }
148
149         @Override
150         public int hashCode() {
151             final int prime = 31;
152             int result = 1;
153             result = prime * result + ((qname == null) ? 0 : qname.hashCode());
154             result = prime * result + ((schemaPath == null) ? 0 : schemaPath.hashCode());
155             return result;
156         }
157
158         @Override
159         public boolean equals(Object obj) {
160             if (this == obj) {
161                 return true;
162             }
163             if (obj == null) {
164                 return false;
165             }
166             if (getClass() != obj.getClass()) {
167                 return false;
168             }
169             ExtensionDefinitionImpl other = (ExtensionDefinitionImpl) obj;
170             if (qname == null) {
171                 if (other.qname != null) {
172                     return false;
173                 }
174             } else if (!qname.equals(other.qname)) {
175                 return false;
176             }
177             if (schemaPath == null) {
178                 if (other.schemaPath != null) {
179                     return false;
180                 }
181             } else if (!schemaPath.equals(other.schemaPath)) {
182                 return false;
183             }
184             return true;
185         }
186
187         @Override
188         public String toString() {
189             StringBuilder sb = new StringBuilder(ExtensionDefinitionImpl.class.getSimpleName());
190             sb.append("[");
191             sb.append("argument=" + argument);
192             sb.append(", qname=" + qname);
193             sb.append(", schemaPath=" + schemaPath);
194             sb.append(", extensionSchemaNodes=" + unknownNodes);
195             sb.append(", yin=" + yin);
196             sb.append("]");
197             return sb.toString();
198         }
199     }
200
201 }