Fix CS warnings in sal-clustering-commons and enable enforcement
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / main / java / org / opendaylight / controller / cluster / datastore / node / utils / stream / AbstractNormalizedNodeDataOutput.java
1 /*
2  * Copyright (c) 2015 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.cluster.datastore.node.utils.stream;
9
10 import com.google.common.base.Preconditions;
11 import java.io.DataOutput;
12 import java.io.IOException;
13 import java.io.OutputStream;
14 import java.io.StringWriter;
15 import java.nio.charset.StandardCharsets;
16 import java.util.Collection;
17 import java.util.Map;
18 import java.util.Set;
19 import javax.xml.transform.TransformerException;
20 import javax.xml.transform.TransformerFactory;
21 import javax.xml.transform.TransformerFactoryConfigurationError;
22 import javax.xml.transform.dom.DOMSource;
23 import javax.xml.transform.stream.StreamResult;
24 import org.opendaylight.yangtools.yang.common.QName;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
31 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
33 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 abstract class AbstractNormalizedNodeDataOutput implements NormalizedNodeDataOutput, NormalizedNodeStreamWriter {
38     private static final Logger LOG = LoggerFactory.getLogger(AbstractNormalizedNodeDataOutput.class);
39
40     private final DataOutput output;
41
42     private NormalizedNodeWriter normalizedNodeWriter;
43     private boolean headerWritten;
44     private QName lastLeafSetQName;
45
46     AbstractNormalizedNodeDataOutput(final DataOutput output) {
47         this.output = Preconditions.checkNotNull(output);
48     }
49
50     private void ensureHeaderWritten() throws IOException {
51         if (!headerWritten) {
52             output.writeByte(TokenTypes.SIGNATURE_MARKER);
53             output.writeShort(streamVersion());
54             headerWritten = true;
55         }
56     }
57
58     protected abstract short streamVersion();
59
60     protected abstract void writeQName(QName qname) throws IOException;
61
62     protected abstract void writeString(String string) throws IOException;
63
64     @Override
65     public final void write(final int value) throws IOException {
66         ensureHeaderWritten();
67         output.write(value);
68     }
69
70     @Override
71     public final void write(final byte[] bytes) throws IOException {
72         ensureHeaderWritten();
73         output.write(bytes);
74     }
75
76     @Override
77     public final void write(final byte[] bytes, final int off, final int len) throws IOException {
78         ensureHeaderWritten();
79         output.write(bytes, off, len);
80     }
81
82     @Override
83     public final void writeBoolean(final boolean value) throws IOException {
84         ensureHeaderWritten();
85         output.writeBoolean(value);
86     }
87
88     @Override
89     public final void writeByte(final int value) throws IOException {
90         ensureHeaderWritten();
91         output.writeByte(value);
92     }
93
94     @Override
95     public final void writeShort(final int value) throws IOException {
96         ensureHeaderWritten();
97         output.writeShort(value);
98     }
99
100     @Override
101     public final void writeChar(final int value) throws IOException {
102         ensureHeaderWritten();
103         output.writeChar(value);
104     }
105
106     @Override
107     public final void writeInt(final int value) throws IOException {
108         ensureHeaderWritten();
109         output.writeInt(value);
110     }
111
112     @Override
113     public final void writeLong(final long value) throws IOException {
114         ensureHeaderWritten();
115         output.writeLong(value);
116     }
117
118     @Override
119     public final void writeFloat(final float value) throws IOException {
120         ensureHeaderWritten();
121         output.writeFloat(value);
122     }
123
124     @Override
125     public final void writeDouble(final double value) throws IOException {
126         ensureHeaderWritten();
127         output.writeDouble(value);
128     }
129
130     @Override
131     public final void writeBytes(final String str) throws IOException {
132         ensureHeaderWritten();
133         output.writeBytes(str);
134     }
135
136     @Override
137     public final void writeChars(final String str) throws IOException {
138         ensureHeaderWritten();
139         output.writeChars(str);
140     }
141
142     @Override
143     public final void writeUTF(final String str) throws IOException {
144         ensureHeaderWritten();
145         output.writeUTF(str);
146     }
147
148     private NormalizedNodeWriter normalizedNodeWriter() {
149         if (normalizedNodeWriter == null) {
150             normalizedNodeWriter = NormalizedNodeWriter.forStreamWriter(this);
151         }
152
153         return normalizedNodeWriter;
154     }
155
156     @Override
157     public void writeNormalizedNode(final NormalizedNode<?, ?> node) throws IOException {
158         ensureHeaderWritten();
159         normalizedNodeWriter().write(node);
160     }
161
162     @Override
163     public void leafNode(final NodeIdentifier name, final Object value) throws IOException, IllegalArgumentException {
164         Preconditions.checkNotNull(name, "Node identifier should not be null");
165         LOG.debug("Writing a new leaf node");
166         startNode(name.getNodeType(), NodeTypes.LEAF_NODE);
167
168         writeObject(value);
169     }
170
171     @Override
172     public void startLeafSet(final NodeIdentifier name, final int childSizeHint)
173
174             throws IOException, IllegalArgumentException {
175         Preconditions.checkNotNull(name, "Node identifier should not be null");
176         LOG.debug("Starting a new leaf set");
177
178         lastLeafSetQName = name.getNodeType();
179         startNode(name.getNodeType(), NodeTypes.LEAF_SET);
180     }
181
182     @Override
183     public void startOrderedLeafSet(final NodeIdentifier name, final int childSizeHint)
184             throws IOException, IllegalArgumentException {
185         Preconditions.checkNotNull(name, "Node identifier should not be null");
186         LOG.debug("Starting a new ordered leaf set");
187
188         lastLeafSetQName = name.getNodeType();
189         startNode(name.getNodeType(), NodeTypes.ORDERED_LEAF_SET);
190     }
191
192     @Override
193     public void leafSetEntryNode(final QName name, final Object value) throws IOException, IllegalArgumentException {
194         LOG.debug("Writing a new leaf set entry node");
195
196         output.writeByte(NodeTypes.LEAF_SET_ENTRY_NODE);
197
198         // lastLeafSetQName is set if the parent LeafSetNode was previously written. Otherwise this is a
199         // stand alone LeafSetEntryNode so write out it's name here.
200         if (lastLeafSetQName == null) {
201             writeQName(name);
202         }
203
204         writeObject(value);
205     }
206
207     @Override
208     public void startContainerNode(final NodeIdentifier name, final int childSizeHint)
209             throws IOException, IllegalArgumentException {
210         Preconditions.checkNotNull(name, "Node identifier should not be null");
211
212         LOG.debug("Starting a new container node");
213
214         startNode(name.getNodeType(), NodeTypes.CONTAINER_NODE);
215     }
216
217     @Override
218     public void startYangModeledAnyXmlNode(final NodeIdentifier name, final int childSizeHint)
219             throws IOException, IllegalArgumentException {
220         Preconditions.checkNotNull(name, "Node identifier should not be null");
221
222         LOG.debug("Starting a new yang modeled anyXml node");
223
224         startNode(name.getNodeType(), NodeTypes.YANG_MODELED_ANY_XML_NODE);
225     }
226
227     @Override
228     public void startUnkeyedList(final NodeIdentifier name, final int childSizeHint)
229             throws IOException, IllegalArgumentException {
230         Preconditions.checkNotNull(name, "Node identifier should not be null");
231         LOG.debug("Starting a new unkeyed list");
232
233         startNode(name.getNodeType(), NodeTypes.UNKEYED_LIST);
234     }
235
236     @Override
237     public void startUnkeyedListItem(final NodeIdentifier name, final int childSizeHint)
238             throws IOException, IllegalStateException {
239         Preconditions.checkNotNull(name, "Node identifier should not be null");
240         LOG.debug("Starting a new unkeyed list item");
241
242         startNode(name.getNodeType(), NodeTypes.UNKEYED_LIST_ITEM);
243     }
244
245     @Override
246     public void startMapNode(final NodeIdentifier name, final int childSizeHint)
247             throws IOException, IllegalArgumentException {
248         Preconditions.checkNotNull(name, "Node identifier should not be null");
249         LOG.debug("Starting a new map node");
250
251         startNode(name.getNodeType(), NodeTypes.MAP_NODE);
252     }
253
254     @Override
255     public void startMapEntryNode(final NodeIdentifierWithPredicates identifier, final int childSizeHint)
256             throws IOException, IllegalArgumentException {
257         Preconditions.checkNotNull(identifier, "Node identifier should not be null");
258         LOG.debug("Starting a new map entry node");
259         startNode(identifier.getNodeType(), NodeTypes.MAP_ENTRY_NODE);
260
261         writeKeyValueMap(identifier.getKeyValues());
262
263     }
264
265     @Override
266     public void startOrderedMapNode(final NodeIdentifier name, final int childSizeHint)
267             throws IOException, IllegalArgumentException {
268         Preconditions.checkNotNull(name, "Node identifier should not be null");
269         LOG.debug("Starting a new ordered map node");
270
271         startNode(name.getNodeType(), NodeTypes.ORDERED_MAP_NODE);
272     }
273
274     @Override
275     public void startChoiceNode(final NodeIdentifier name, final int childSizeHint)
276             throws IOException, IllegalArgumentException {
277         Preconditions.checkNotNull(name, "Node identifier should not be null");
278         LOG.debug("Starting a new choice node");
279
280         startNode(name.getNodeType(), NodeTypes.CHOICE_NODE);
281     }
282
283     @Override
284     public void startAugmentationNode(final AugmentationIdentifier identifier)
285             throws IOException, IllegalArgumentException {
286         Preconditions.checkNotNull(identifier, "Node identifier should not be null");
287         LOG.debug("Starting a new augmentation node");
288
289         output.writeByte(NodeTypes.AUGMENTATION_NODE);
290         writeQNameSet(identifier.getPossibleChildNames());
291     }
292
293     @Override
294     public void anyxmlNode(final NodeIdentifier name, final Object value) throws IOException, IllegalArgumentException {
295         Preconditions.checkNotNull(name, "Node identifier should not be null");
296         LOG.debug("Writing any xml node");
297
298         startNode(name.getNodeType(), NodeTypes.ANY_XML_NODE);
299
300         try {
301             StreamResult xmlOutput = new StreamResult(new StringWriter());
302             TransformerFactory.newInstance().newTransformer().transform((DOMSource)value, xmlOutput);
303             writeObject(xmlOutput.getWriter().toString());
304         } catch (TransformerException | TransformerFactoryConfigurationError e) {
305             throw new IOException("Error writing anyXml", e);
306         }
307     }
308
309     @Override
310     public void endNode() throws IOException, IllegalStateException {
311         LOG.debug("Ending the node");
312
313         output.writeByte(NodeTypes.END_NODE);
314     }
315
316     @Override
317     public void close() throws IOException {
318         flush();
319     }
320
321     @Override
322     public void flush() throws IOException {
323         if (output instanceof OutputStream) {
324             ((OutputStream)output).flush();
325         }
326     }
327
328     private void startNode(final QName qname, final byte nodeType) throws IOException {
329         Preconditions.checkNotNull(qname, "QName of node identifier should not be null.");
330
331         ensureHeaderWritten();
332
333         // First write the type of node
334         output.writeByte(nodeType);
335         // Write Start Tag
336         writeQName(qname);
337     }
338
339     private void writeObjSet(final Set<?> set) throws IOException {
340         output.writeInt(set.size());
341         for (Object o : set) {
342             Preconditions.checkArgument(o instanceof String, "Expected value type to be String but was %s (%s)",
343                 o.getClass(), o);
344
345             writeString((String) o);
346         }
347     }
348
349     @Override
350     public void writeYangInstanceIdentifier(final YangInstanceIdentifier identifier) throws IOException {
351         ensureHeaderWritten();
352         writeYangInstanceIdentifierInternal(identifier);
353     }
354
355     private void writeYangInstanceIdentifierInternal(final YangInstanceIdentifier identifier) throws IOException {
356         Collection<PathArgument> pathArguments = identifier.getPathArguments();
357         output.writeInt(pathArguments.size());
358
359         for (PathArgument pathArgument : pathArguments) {
360             writePathArgument(pathArgument);
361         }
362     }
363
364     @Override
365     public void writePathArgument(final PathArgument pathArgument) throws IOException {
366
367         byte type = PathArgumentTypes.getSerializablePathArgumentType(pathArgument);
368
369         output.writeByte(type);
370
371         switch (type) {
372             case PathArgumentTypes.NODE_IDENTIFIER:
373
374                 NodeIdentifier nodeIdentifier = (NodeIdentifier) pathArgument;
375
376                 writeQName(nodeIdentifier.getNodeType());
377                 break;
378
379             case PathArgumentTypes.NODE_IDENTIFIER_WITH_PREDICATES:
380
381                 NodeIdentifierWithPredicates nodeIdentifierWithPredicates =
382                     (NodeIdentifierWithPredicates) pathArgument;
383                 writeQName(nodeIdentifierWithPredicates.getNodeType());
384
385                 writeKeyValueMap(nodeIdentifierWithPredicates.getKeyValues());
386                 break;
387
388             case PathArgumentTypes.NODE_IDENTIFIER_WITH_VALUE :
389
390                 NodeWithValue<?> nodeWithValue = (NodeWithValue<?>) pathArgument;
391
392                 writeQName(nodeWithValue.getNodeType());
393                 writeObject(nodeWithValue.getValue());
394                 break;
395
396             case PathArgumentTypes.AUGMENTATION_IDENTIFIER :
397
398                 AugmentationIdentifier augmentationIdentifier = (AugmentationIdentifier) pathArgument;
399
400                 // No Qname in augmentation identifier
401                 writeQNameSet(augmentationIdentifier.getPossibleChildNames());
402                 break;
403             default :
404                 throw new IllegalStateException("Unknown node identifier type is found : "
405                         + pathArgument.getClass().toString() );
406         }
407     }
408
409     private void writeKeyValueMap(final Map<QName, Object> keyValueMap) throws IOException {
410         if (keyValueMap != null && !keyValueMap.isEmpty()) {
411             output.writeInt(keyValueMap.size());
412
413             for (QName qname : keyValueMap.keySet()) {
414                 writeQName(qname);
415                 writeObject(keyValueMap.get(qname));
416             }
417         } else {
418             output.writeInt(0);
419         }
420     }
421
422     private void writeQNameSet(final Set<QName> children) throws IOException {
423         // Write each child's qname separately, if list is empty send count as 0
424         if (children != null && !children.isEmpty()) {
425             output.writeInt(children.size());
426             for (QName qname : children) {
427                 writeQName(qname);
428             }
429         } else {
430             LOG.debug("augmentation node does not have any child");
431             output.writeInt(0);
432         }
433     }
434
435     private void writeObject(final Object value) throws IOException {
436
437         byte type = ValueTypes.getSerializableType(value);
438         // Write object type first
439         output.writeByte(type);
440
441         switch (type) {
442             case ValueTypes.BOOL_TYPE:
443                 output.writeBoolean((Boolean) value);
444                 break;
445             case ValueTypes.QNAME_TYPE:
446                 writeQName((QName) value);
447                 break;
448             case ValueTypes.INT_TYPE:
449                 output.writeInt((Integer) value);
450                 break;
451             case ValueTypes.BYTE_TYPE:
452                 output.writeByte((Byte) value);
453                 break;
454             case ValueTypes.LONG_TYPE:
455                 output.writeLong((Long) value);
456                 break;
457             case ValueTypes.SHORT_TYPE:
458                 output.writeShort((Short) value);
459                 break;
460             case ValueTypes.BITS_TYPE:
461                 writeObjSet((Set<?>) value);
462                 break;
463             case ValueTypes.BINARY_TYPE:
464                 byte[] bytes = (byte[]) value;
465                 output.writeInt(bytes.length);
466                 output.write(bytes);
467                 break;
468             case ValueTypes.YANG_IDENTIFIER_TYPE:
469                 writeYangInstanceIdentifierInternal((YangInstanceIdentifier) value);
470                 break;
471             case ValueTypes.NULL_TYPE :
472                 break;
473             case ValueTypes.STRING_BYTES_TYPE:
474                 final byte[] valueBytes = value.toString().getBytes(StandardCharsets.UTF_8);
475                 output.writeInt(valueBytes.length);
476                 output.write(valueBytes);
477                 break;
478             default:
479                 output.writeUTF(value.toString());
480                 break;
481         }
482     }
483 }