2d6ed5cba7889aec5d9a46442d9eaf088758f532
[mdsal.git] / binding / yang-binding / src / main / java / org / opendaylight / yangtools / yang / binding / BindingStreamEventWriter.java
1 /*
2  * Copyright (c) 2014 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.yangtools.yang.binding;
9
10 import java.io.Closeable;
11 import java.io.Flushable;
12 import java.io.IOException;
13
14
15 /**
16  * Event Stream Writer for Binding Representation.
17  *
18  * <h3>Emmiting Event Stream</h3>
19  *
20  * <ul>
21  * <li><code>container</code> - Container node representation, start event is
22  * emitted using {@link #startContainerNode(Class, int)} and node end event is
23  * emitted using {@link #endNode()}. Container node is implementing
24  * {@link DataObject} interface.
25  *
26  * <li><code>list</code> - YANG list statement has two representation in event
27  * stream - unkeyed list and map. Unkeyed list is YANG list which did not
28  * specify key.
29  *
30  * <ul>
31  * <li><code>Map</code> - Map start event is emitted using
32  * {@link #startMapNode(Class, int)} and is ended using {@link #endNode()}. Each map
33  * entry start is emitted using {@link #startMapEntryNode(Identifier, int)} with Map of keys
34  * and finished using {@link #endNode()}.</li>
35  * <li><code>UnkeyedList</code> - Unkeyed list represent list without keys,
36  * unkeyed list start is emitted using {@link #startUnkeyedList(Class, int)} list
37  * end is emitted using {@link #endNode()}. Each list item is emitted using
38  * {@link #startUnkeyedListItem(int)} and ended using {@link #endNode()}.</li>
39  * </ul></li>
40  * <li><code>leaf</code> - Leaf node event is emitted using
41  * {@link #leafNode(String, Object)}. {@link #endNode()} MUST be not emitted for
42  * leaf node.</li>
43  * <li><code>leaf-list</code> - Leaf list start is emitted using
44  * {@link #startLeafSet(String, int)}. Leaf list end is emitted using
45  * {@link #endNode()}. Leaf list entries are emitted using
46  * {@link #leafSetEntryNode(Object)}.
47  * <li><code>anyxml - Anyxml node event is emitted using
48  * {@link #leafNode(String, Object)}. {@link #endNode()} MUST be not emitted
49  * for anyxml node.</code></li>
50  * <li><code>choice</code> Choice node event is emitted by
51  * {@link #startChoiceNode(Class, int)} event and must be immediately followed by
52  * {@link #startCase(Class, int)} event. Choice node is finished by emitting an
53  * {@link #endNode()} event.</li>
54  * <li>
55  * <code>case</code> - Case node may be emitted only inside choice node by
56  * invoking {@link #startCase(Class, int)}. Case node is finished be emitting an
57  * {@link #endNode()} event.</li>
58  * <li>
59  * <code>augment</code> - Represents augmentation, augmentation node is started
60  * by invoking {@link #startAugmentationNode(Class)} and
61  * finished by invoking {@link #endNode()}.</li>
62  * </ul>
63  *
64  * <h3>Implementation notes</h3> This interface is not intended to be implemented by users of generated Binding DTOs
65  * but to be used by utilities, which needs to emit NormalizedNode model from Binding DTOs.
66  *
67  * <p>
68  * This interface is intended as API definition of facade for real Event / Stream Writer, without explicitly requiring
69  * stream writer and related interfaces to be imported by all generated Binding DTOs.
70  *
71  * <p>
72  * Existence of this interface in base Java Binding package is required to support runtime generation of users of this
73  * interface in OSGI and OSGI-like environment, since this package is only package which is imported by all generated
74  * Binding DTOs and wired in OSGI.
75  */
76 public interface BindingStreamEventWriter extends Closeable, Flushable {
77
78     /**
79      * Methods in this interface allow users to hint the underlying implementation about the sizing of container-like
80      * constructors (leafLists, containers, etc.). These hints may be taken into account by a particular implementation
81      * to improve performance, but clients are not required to provide hints. This constant should be used by clients
82      * who either do not have the sizing information, or do not wish to divulge it (for whatever reasons).
83      * Implementations are free to ignore these hints completely, but if they do use them, they are expected to be
84      * resilient in face of missing and mismatched hints, which is to say the user can specify startLeafSet(..., 1)
85      * and then call leafNode() 15 times.
86      *
87      * <p>
88      * The acceptable hint values are non-negative integers and this constant, all other values will result, based on
89      * implementation preference, in the hint being completely ignored or IllegalArgumentException being thrown.
90      */
91     int UNKNOWN_SIZE = -1;
92
93     /**
94      * Emits a leaf node event with supplied value.
95      *
96      * @param localName
97      *            name of node as defined in schema, namespace and revision are
98      *            derived from parent node.
99      * @param value
100      *            Value of leaf node.
101      * @throws IllegalArgumentException
102      *             If emitted leaf node has invalid value in current context or
103      *             was emitted multiple times.
104      * @throws IllegalStateException
105      *             If node was emitted inside <code>map</code>,
106      *             <code>choice</code> <code>unkeyed list</code> node.
107      * @throws IOException if an underlying IO error occurs
108      */
109     void leafNode(String localName, Object value) throws IOException;
110
111     /**
112      * Emits a start of leaf set (leaf-list). Emits start of leaf set, during writing leaf set event, only
113      * {@link #leafSetEntryNode(Object)} calls are valid. Leaf set event is finished by calling {@link #endNode()}.
114      *
115      * @param localName
116      *            name of node as defined in schema, namespace and revision are
117      *            derived from parent node.
118      * @param childSizeHint
119      *            Non-negative count of expected direct child nodes or
120      *            {@link #UNKNOWN_SIZE} if count is unknown. This is only hint
121      *            and should not fail writing of child events, if there are more
122      *            events than count.
123      * @throws IllegalArgumentException
124      *             If emitted leaf node is invalid in current context or was
125      *             emitted multiple times.
126      * @throws IllegalStateException
127      *             If node was emitted inside <code>map</code>,
128      *             <code>choice</code> <code>unkeyed list</code> node.
129      * @throws IOException if an underlying IO error occurs
130      */
131     void startLeafSet(String localName, int childSizeHint) throws IOException;
132
133     /**
134      * Emits a start of leaf set (leaf-list).
135      *
136      * <p>
137      * Emits start of leaf set, during writing leaf set event, only
138      * {@link #leafSetEntryNode(Object)} calls are valid. Leaf set event is
139      * finished by calling {@link #endNode()}.
140      *
141      * @param localName
142      *            name of node as defined in schema, namespace and revision are
143      *            derived from parent node.
144      * @param childSizeHint
145      *            Non-negative count of expected direct child nodes or
146      *            {@link #UNKNOWN_SIZE} if count is unknown. This is only hint
147      *            and should not fail writing of child events, if there are more
148      *            events than count.
149      * @throws IllegalArgumentException
150      *             If emitted leaf node is invalid in current context or was
151      *             emitted multiple times.
152      * @throws IllegalStateException
153      *             If node was emitted inside <code>map</code>,
154      *             <code>choice</code> <code>unkeyed list</code> node.
155      * @throws IOException if an underlying IO error occurs
156      */
157     void startOrderedLeafSet(String localName, int childSizeHint) throws IOException;
158
159     /**
160      * Emits a leaf set entry node.
161      *
162      * @param value
163      *            Value of leaf set entry node.
164      * @throws IllegalArgumentException
165      *             If emitted leaf node has invalid value.
166      * @throws IllegalStateException
167      *             If node was emitted outside <code>leaf set</code> node.
168      * @throws IOException if an underlying IO error occurs
169      */
170     void leafSetEntryNode(Object value) throws IOException;
171
172     /**
173      * Emits start of new container. End of container event is emitted by invoking {@link #endNode()}.
174      *
175      * <p>
176      * Valid sub-events are:
177      * <ul>
178      * <li>{@link #leafNode(String, Object)}</li>
179      * <li>{@link #startContainerNode(Class, int)}</li>
180      * <li>{@link #startChoiceNode(Class, int)}</li>
181      * <li>{@link #startLeafSet(String, int)}</li>
182      * <li>{@link #startMapNode(Class, int)}</li>
183      * <li>{@link #startUnkeyedList(Class, int)}</li>
184      * <li>{@link #startAugmentationNode(Class)}</li>
185      * </ul>
186      *
187      * @param container
188      *            name of node as defined in schema, namespace and revision are
189      *            derived from parent node.
190      * @param childSizeHint
191      *            Non-negative count of expected direct child nodes or
192      *            {@link #UNKNOWN_SIZE} if count is unknown. This is only hint
193      *            and should not fail writing of child events, if there are more
194      *            events than count.
195      * @throws IllegalArgumentException
196      *             If emitted node is invalid in current context or was emitted
197      *             multiple times.
198      * @throws IllegalStateException
199      *             If node was emitted inside <code>map</code>,
200      *             <code>choice</code> <code>unkeyed list</code> node.
201      * @throws IOException if an underlying IO error occurs
202      */
203     void startContainerNode(Class<? extends DataObject> container, int childSizeHint) throws IOException;
204
205     /**
206      * Emits start of unkeyed list node event.
207      *
208      * <p>
209      * End of unkeyed list event is emitted by invoking {@link #endNode()}.
210      * Valid subevents is only {@link #startUnkeyedListItem(int)}. All other
211      * methods will throw {@link IllegalArgumentException}.
212      *
213      * @param localName
214      *            name of node as defined in schema, namespace and revision are
215      *            derived from parent node.
216      * @param childSizeHint
217      *            Non-negative count of expected direct child nodes or
218      *            {@link #UNKNOWN_SIZE} if count is unknown. This is only hint
219      *            and should not fail writing of child events, if there are more
220      *            events than count.
221      * @throws IllegalArgumentException
222      *             If emitted node is invalid in current context or was emitted
223      *             multiple times.
224      * @throws IllegalStateException
225      *             If node was emitted inside <code>map</code>,
226      *             <code>choice</code> <code>unkeyed list</code> node.
227      * @throws IOException if an underlying IO error occurs
228      */
229     void startUnkeyedList(Class<? extends DataObject> localName, int childSizeHint) throws IOException;
230
231     /**
232      * Emits start of new unkeyed list item.
233      *
234      * <p>
235      * Unkeyed list item event is finished by invoking {@link #endNode()}. Valid
236      * sub-events are:
237      * <ul>
238      * <li>{@link #leafNode(String, Object)}</li>
239      * <li>{@link #startContainerNode(Class, int)}</li>
240      * <li>{@link #startChoiceNode(Class, int)}</li>
241      * <li>{@link #startLeafSet(String, int)}</li>
242      * <li>{@link #startMapNode(Class, int)}</li>
243      * <li>{@link #startUnkeyedList(Class, int)}</li>
244      * <li>{@link #startAugmentationNode(Class)}</li>
245      * </ul>
246      *
247      * @param childSizeHint
248      *            Non-negative count of expected direct child nodes or
249      *            {@link #UNKNOWN_SIZE} if count is unknown. This is only hint
250      *            and should not fail writing of child events, if there are more
251      *            events than count.
252      * @throws IllegalStateException
253      *             If node was emitted outside <code>unkeyed list</code> node.
254      * @throws IOException if an underlying IO error occurs
255      */
256     void startUnkeyedListItem(int childSizeHint) throws IOException;
257
258     /**
259      * Emits start of unordered map node event.
260      *
261      * <p>
262      * End of map node event is emitted by invoking {@link #endNode()}. Valid
263      * subevent is only {@link #startMapEntryNode(Identifier, int)}. All other methods will
264      * throw {@link IllegalArgumentException}.
265      *
266      * @param mapEntryType
267      *            Class of list item, which has defined key.
268      * @param childSizeHint
269      *            Non-negative count of expected direct child nodes or
270      *            {@link #UNKNOWN_SIZE} if count is unknown. This is only hint
271      *            and should not fail writing of child events, if there are more
272      *            events than count.
273      * @throws IllegalArgumentException
274      * @throws IllegalStateException
275      *             If node was emitted inside <code>map</code>,
276      *             <code>choice</code> <code>unkeyed list</code> node.
277      * @throws IOException if an underlying IO error occurs
278      */
279     <T extends DataObject & Identifiable<?>> void startMapNode(Class<T> mapEntryType, int childSizeHint)
280             throws IOException;
281
282     /**
283      * Emits start of ordered map node event.
284      *
285      * <p>
286      * End of map node event is emitted by invoking {@link #endNode()}. Valid
287      * subevent is only {@link #startMapEntryNode(Identifier, int)}. All other methods will
288      * throw {@link IllegalArgumentException}.
289      *
290      * @param mapEntryType
291      *            Class of list item, which has defined key.
292      * @param childSizeHint
293      *            Non-negative count of expected direct child nodes or
294      *            {@link #UNKNOWN_SIZE} if count is unknown. This is only hint
295      *            and should not fail writing of child events, if there are more
296      *            events than count.
297      * @throws IllegalArgumentException
298      * @throws IllegalStateException
299      *             If node was emitted inside <code>map</code>,
300      *             <code>choice</code> <code>unkeyed list</code> node.
301      * @throws IOException if an underlying IO error occurs
302      */
303     <T extends DataObject & Identifiable<?>> void startOrderedMapNode(Class<T> mapEntryType, int childSizeHint)
304             throws IOException;
305
306     /**
307      * Emits start of map entry.
308      *
309      * <p>
310      * End of map entry event is emitted by invoking {@link #endNode()}.
311      *
312      * <p>
313      * Valid sub-events are:
314      * <ul>
315      * <li>{@link #leafNode(String, Object)}</li>
316      * <li>{@link #startContainerNode(Class, int)}</li>
317      * <li>{@link #startChoiceNode(Class, int)}</li>
318      * <li>{@link #startLeafSet(String, int)}</li>
319      * <li>{@link #startMapNode(Class, int)}</li>
320      * <li>{@link #startUnkeyedList(Class, int)}</li>
321      * <li>{@link #startAugmentationNode(Class)}</li>
322      * </ul>
323      *
324      * @param keyValues
325      *            Key of map entry node
326      * @param childSizeHint
327      *            Non-negative count of expected direct child nodes or
328      *            {@link #UNKNOWN_SIZE} if count is unknown. This is only hint
329      *            and should not fail writing of child events, if there are more
330      *            events than count.
331      * @throws IllegalArgumentException
332      *             If key contains incorrect value.
333      * @throws IllegalStateException
334      *             If node was emitted outside <code>map entry</code> node.
335      * @throws IOException if an underlying IO error occurs
336      */
337     void startMapEntryNode(Identifier<?> keyValues, int childSizeHint) throws IOException;
338
339     /**
340      * Emits start of choice node.
341      *
342      * <p>
343      * Valid sub-event in {@link #startCase(Class, int)}, which selects case
344      * which should be written.
345      *
346      * @param choice
347      *            Choice class.
348      * @param childSizeHint
349      *            Non-negative count of expected direct child nodes or
350      *            {@link #UNKNOWN_SIZE} if count is unknown. This is only hint
351      *            and should not fail writing of child events, if there are more
352      *            events than count.
353      * @throws IllegalStateException
354      *             If node was emitted inside <code>map</code>, <code>choice</code>,
355      *             <code>unkeyed list</code> node.
356      * @throws IOException if an underlying IO error occurs
357      */
358     void startChoiceNode(Class<? extends DataContainer> choice, int childSizeHint) throws IOException;
359
360     /**
361      * Starts a case node.
362      *
363      * <p>
364      * Valid sub-events are:
365      * <ul>
366      * <li>{@link #leafNode(String, Object)}</li>
367      * <li>{@link #startContainerNode(Class, int)}</li>
368      * <li>{@link #startChoiceNode(Class, int)}</li>
369      * <li>{@link #startLeafSet(String, int)}</li>
370      * <li>{@link #startMapNode(Class, int)}</li>
371      * <li>{@link #startUnkeyedList(Class, int)}</li>
372      * <li>{@link #startAugmentationNode(Class)}</li>
373      * </ul>
374      *
375      * @param caze Case class
376      * @throws IOException if an underlying IO error occurs
377      */
378     void startCase(Class<? extends DataObject> caze, int childSizeHint) throws IOException;
379
380     /**
381      * Emits start of augmentation node.
382      *
383      * <p>
384      * End of augmentation event is emitted by invoking {@link #endNode()}.
385      *
386      * <p>
387      * Valid sub-events are:
388      *
389      * <ul>
390      * <li>{@link #leafNode(String, Object)}</li>
391      * <li>{@link #startContainerNode(Class, int)}</li>
392      * <li>{@link #startChoiceNode(Class, int)}</li>
393      * <li>{@link #startLeafSet(String, int)}</li>
394      * <li>{@link #startMapNode(Class, int)}</li>
395      * <li>{@link #startUnkeyedList(Class, int)}</li>
396      * </ul>
397      *
398      * <p>
399      * Note this is only method, which does not require childSizeHint, since
400      * maximum value is always size of <code>possibleChildren</code>.
401      *
402      * @param augmentationType augmentation class
403      * @throws IllegalArgumentException
404      *             If augmentation is invalid in current context.
405      * @throws IOException if an underlying IO error occurs
406      */
407     void startAugmentationNode(Class<? extends Augmentation<?>> augmentationType) throws IOException;
408
409     /**
410      * Emits anyxml node event.
411      *
412      * @param name node name
413      * @param value node value
414      * @throws IllegalStateException
415      *             If node was emitted inside <code>map</code>,
416      *             <code>choice</code> <code>unkeyed list</code> node.
417      * @throws IOException if an underlying IO error occurs
418      */
419     void anyxmlNode(String name, Object value) throws IOException;
420
421     /**
422      * Emits end event for node.
423      *
424      * @throws IllegalStateException If there is no open node.
425      * @throws IOException if an underlying IO error occurs
426      */
427     void endNode() throws IOException;
428
429     @Override
430     void flush() throws IOException;
431
432     @Override
433     void close() throws IOException;
434 }