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 / UnionTypeBuilder.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.model.api.SchemaPath;
15 import org.opendaylight.controller.yang.model.api.Status;
16 import org.opendaylight.controller.yang.model.api.TypeDefinition;
17 import org.opendaylight.controller.yang.model.api.UnknownSchemaNode;
18 import org.opendaylight.controller.yang.model.api.type.LengthConstraint;
19 import org.opendaylight.controller.yang.model.api.type.PatternConstraint;
20 import org.opendaylight.controller.yang.model.api.type.RangeConstraint;
21 import org.opendaylight.controller.yang.model.util.UnionType;
22 import org.opendaylight.controller.yang.parser.builder.api.AbstractTypeAwareBuilder;
23 import org.opendaylight.controller.yang.parser.builder.api.TypeDefinitionBuilder;
24 import org.opendaylight.controller.yang.parser.util.YangParseException;
25
26 /**
27  * Builder for YANG union type. User can add type to this union as
28  * TypeDefinition object (resolved type) or in form of TypeDefinitionBuilder.
29  * When build is called, types in builder form will be built and add to resolved
30  * types.
31  */
32 public final class UnionTypeBuilder extends AbstractTypeAwareBuilder implements TypeDefinitionBuilder {
33     private final static String NAME = "union";
34
35     private final List<TypeDefinition<?>> types;
36     private final List<TypeDefinitionBuilder> typedefs;
37     private UnionType instance;
38     private boolean isBuilt;
39
40     private SchemaPath path;
41
42     public UnionTypeBuilder(final int line) {
43         super(line, null);
44         types = new ArrayList<TypeDefinition<?>>();
45         typedefs = new ArrayList<TypeDefinitionBuilder>();
46     }
47
48
49     public List<TypeDefinition<?>> getTypes() {
50         return types;
51     }
52
53     @Override
54     public TypeDefinition<?> getType() {
55         return null;
56     }
57
58     public List<TypeDefinitionBuilder> getTypedefs() {
59         return Collections.unmodifiableList(typedefs);
60     }
61
62     @Override
63     public TypeDefinitionBuilder getTypedef() {
64         return null;
65     }
66
67     @Override
68     public void setType(final TypeDefinition<?> type) {
69         types.add(type);
70     }
71
72     @Override
73     public void setTypedef(final TypeDefinitionBuilder tdb) {
74         typedefs.add(tdb);
75     }
76
77     @Override
78     public UnionType build() {
79         if (!isBuilt) {
80             instance = new UnionType(path, types);
81             for (TypeDefinitionBuilder tdb : typedefs) {
82                 types.add(tdb.build());
83             }
84             isBuilt = true;
85         }
86         return instance;
87     }
88
89     @Override
90     public void setPath(final SchemaPath schemaPath) {
91         this.path = schemaPath;
92     }
93
94     @Override
95     public void setDescription(final String description) {
96         throw new YangParseException(line, "Can not set description to " + NAME);
97     }
98
99     @Override
100     public void setReference(final String reference) {
101         throw new YangParseException(line, "Can not set reference to " + NAME);
102     }
103
104     @Override
105     public void setStatus(final Status status) {
106         throw new YangParseException(line, "Can not set status to " + NAME);
107     }
108
109     @Override
110     public boolean isAddedByUses() {
111         return false;
112     }
113
114     @Override
115     public void setAddedByUses(final boolean addedByUses) {
116         throw new YangParseException(line, "Union type can not be added by uses.");
117     }
118
119     @Override
120     public List<UnknownSchemaNode> getUnknownNodes() {
121         return Collections.emptyList();
122     }
123
124     @Override
125     public void addUnknownSchemaNode(final UnknownSchemaNodeBuilder unknownNode) {
126         // not yet supported
127     }
128
129     @Override
130     public SchemaPath getPath() {
131         return path;
132     }
133
134     @Override
135     public String getDescription() {
136         return null;
137     }
138
139     @Override
140     public String getReference() {
141         return null;
142     }
143
144     @Override
145     public Status getStatus() {
146         return null;
147     }
148
149     @Override
150     public List<RangeConstraint> getRanges() {
151         return Collections.emptyList();
152     }
153
154     @Override
155     public void setRanges(List<RangeConstraint> ranges) {
156         throw new YangParseException(line, "Can not set ranges to " + NAME);
157     }
158
159     @Override
160     public List<LengthConstraint> getLengths() {
161         return Collections.emptyList();
162     }
163
164     @Override
165     public void setLengths(List<LengthConstraint> lengths) {
166         throw new YangParseException(line, "Can not set lengths to " + NAME);
167     }
168
169     @Override
170     public List<PatternConstraint> getPatterns() {
171         return Collections.emptyList();
172     }
173
174     @Override
175     public void setPatterns(List<PatternConstraint> patterns) {
176         throw new YangParseException(line, "Can not set patterns to " + NAME);
177     }
178
179     @Override
180     public Integer getFractionDigits() {
181         return null;
182     }
183
184     @Override
185     public void setFractionDigits(Integer fractionDigits) {
186         throw new YangParseException(line, "Can not set fraction digits to " + NAME);
187     }
188
189     @Override
190     public List<UnknownSchemaNodeBuilder> getUnknownNodeBuilders() {
191         return Collections.emptyList();
192     }
193
194     @Override
195     public Object getDefaultValue() {
196         return null;
197     }
198
199     @Override
200     public void setDefaultValue(Object defaultValue) {
201         throw new YangParseException(line, "Can not set default value to " + NAME);
202     }
203
204     @Override
205     public String getUnits() {
206         return null;
207     }
208
209     @Override
210     public void setUnits(String units) {
211         throw new YangParseException(line, "Can not set units to " + NAME);
212     }
213
214     @Override
215     public String toString() {
216         final StringBuilder result = new StringBuilder(UnionTypeBuilder.class.getSimpleName() + "[");
217         result.append(", types=" + types);
218         result.append(", typedefs=" + typedefs);
219         result.append("]");
220         return result.toString();
221     }
222
223 }