47082698f98b622dac1f1496f170711194b0ca12
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / main / java / org / opendaylight / controller / cluster / datastore / node / utils / stream / AbstractLithiumDataOutput.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.controller.cluster.datastore.node.utils.stream;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static com.google.common.base.Preconditions.checkState;
12 import static com.google.common.base.Verify.verifyNotNull;
13 import static java.util.Objects.requireNonNull;
14
15 import com.google.common.annotations.VisibleForTesting;
16 import com.google.common.collect.ImmutableMap;
17 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
18 import java.io.DataOutput;
19 import java.io.IOException;
20 import java.io.StringWriter;
21 import java.math.BigDecimal;
22 import java.math.BigInteger;
23 import java.nio.charset.StandardCharsets;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Map.Entry;
28 import java.util.Optional;
29 import java.util.Set;
30 import javax.xml.transform.TransformerException;
31 import javax.xml.transform.TransformerFactory;
32 import javax.xml.transform.dom.DOMSource;
33 import javax.xml.transform.stream.StreamResult;
34 import org.eclipse.jdt.annotation.NonNull;
35 import org.opendaylight.yangtools.yang.common.Empty;
36 import org.opendaylight.yangtools.yang.common.QName;
37 import org.opendaylight.yangtools.yang.common.QNameModule;
38 import org.opendaylight.yangtools.yang.common.Revision;
39 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
40 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
41 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
42 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
43 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
44 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48 /**
49  * "original" type mapping. Baseline is Lithium but it really was introduced in Oxygen, where {@code type empty} was
50  * remapped from null.
51  *
52  * <p>
53  * {@code uint8}, {@code uint16}, {@code uint32} use java.lang types with widening, hence their value types overlap with
54  * mapping of {@code int16}, {@code int32} and {@code int64}, making that difference indiscernible without YANG schema
55  * knowledge.
56  */
57 abstract class AbstractLithiumDataOutput extends AbstractNormalizedNodeDataOutput {
58     private static final Logger LOG = LoggerFactory.getLogger(AbstractLithiumDataOutput.class);
59     private static final TransformerFactory TF = TransformerFactory.newInstance();
60     private static final ImmutableMap<Class<?>, Byte> KNOWN_TYPES = ImmutableMap.<Class<?>, Byte>builder()
61             .put(String.class, ValueTypes.STRING_TYPE)
62             .put(Byte.class, ValueTypes.BYTE_TYPE)
63             .put(Integer.class, ValueTypes.INT_TYPE)
64             .put(Long.class, ValueTypes.LONG_TYPE)
65             .put(Boolean.class, ValueTypes.BOOL_TYPE)
66             .put(QName.class, ValueTypes.QNAME_TYPE)
67             .put(Short.class, ValueTypes.SHORT_TYPE)
68             .put(BigInteger.class, ValueTypes.BIG_INTEGER_TYPE)
69             .put(BigDecimal.class, ValueTypes.BIG_DECIMAL_TYPE)
70             .put(byte[].class, ValueTypes.BINARY_TYPE)
71             .put(Empty.class, ValueTypes.EMPTY_TYPE)
72             .build();
73
74     private final Map<String, Integer> stringCodeMap = new HashMap<>();
75
76     private QName lastLeafSetQName;
77     private boolean inSimple;
78
79     AbstractLithiumDataOutput(final DataOutput output) {
80         super(output);
81     }
82
83     @Override
84     public final void startLeafNode(final NodeIdentifier name) throws IOException {
85         LOG.trace("Starting a new leaf node");
86         startNode(name, LithiumNode.LEAF_NODE);
87         inSimple = true;
88     }
89
90     @Override
91     public final void startLeafSet(final NodeIdentifier name, final int childSizeHint) throws IOException {
92         LOG.trace("Starting a new leaf set");
93         commonStartLeafSet(name, LithiumNode.LEAF_SET);
94     }
95
96     @Override
97     public final void startOrderedLeafSet(final NodeIdentifier name, final int childSizeHint) throws IOException {
98         LOG.trace("Starting a new ordered leaf set");
99         commonStartLeafSet(name, LithiumNode.ORDERED_LEAF_SET);
100     }
101
102     private void commonStartLeafSet(final NodeIdentifier name, final byte nodeType) throws IOException {
103         startNode(name, nodeType);
104         lastLeafSetQName = name.getNodeType();
105     }
106
107     @Override
108     public final void startLeafSetEntryNode(final NodeWithValue<?> name) throws IOException {
109         LOG.trace("Starting a new leaf set entry node");
110
111         output.writeByte(LithiumNode.LEAF_SET_ENTRY_NODE);
112
113         // lastLeafSetQName is set if the parent LeafSetNode was previously written. Otherwise this is a
114         // stand alone LeafSetEntryNode so write out it's name here.
115         if (lastLeafSetQName == null) {
116             writeQNameInternal(name.getNodeType());
117         }
118         inSimple = true;
119     }
120
121     @Override
122     public final void startContainerNode(final NodeIdentifier name, final int childSizeHint) throws IOException {
123         LOG.trace("Starting a new container node");
124         startNode(name, LithiumNode.CONTAINER_NODE);
125     }
126
127     @Override
128     public final void startYangModeledAnyXmlNode(final NodeIdentifier name, final int childSizeHint)
129             throws IOException {
130         LOG.trace("Starting a new yang modeled anyXml node");
131         startNode(name, LithiumNode.YANG_MODELED_ANY_XML_NODE);
132     }
133
134     @Override
135     public final void startUnkeyedList(final NodeIdentifier name, final int childSizeHint) throws IOException {
136         LOG.trace("Starting a new unkeyed list");
137         startNode(name, LithiumNode.UNKEYED_LIST);
138     }
139
140     @Override
141     public final void startUnkeyedListItem(final NodeIdentifier name, final int childSizeHint) throws IOException {
142         LOG.trace("Starting a new unkeyed list item");
143         startNode(name, LithiumNode.UNKEYED_LIST_ITEM);
144     }
145
146     @Override
147     public final void startMapNode(final NodeIdentifier name, final int childSizeHint) throws IOException {
148         LOG.trace("Starting a new map node");
149         startNode(name, LithiumNode.MAP_NODE);
150     }
151
152     @Override
153     public final void startMapEntryNode(final NodeIdentifierWithPredicates identifier, final int childSizeHint)
154             throws IOException {
155         LOG.trace("Starting a new map entry node");
156         startNode(identifier, LithiumNode.MAP_ENTRY_NODE);
157         writeKeyValueMap(identifier.entrySet());
158     }
159
160     @Override
161     public final void startOrderedMapNode(final NodeIdentifier name, final int childSizeHint) throws IOException {
162         LOG.trace("Starting a new ordered map node");
163         startNode(name, LithiumNode.ORDERED_MAP_NODE);
164     }
165
166     @Override
167     public final void startChoiceNode(final NodeIdentifier name, final int childSizeHint) throws IOException {
168         LOG.trace("Starting a new choice node");
169         startNode(name, LithiumNode.CHOICE_NODE);
170     }
171
172     @Override
173     public final void startAugmentationNode(final AugmentationIdentifier identifier) throws IOException {
174         requireNonNull(identifier, "Node identifier should not be null");
175         LOG.trace("Starting a new augmentation node");
176
177         output.writeByte(LithiumNode.AUGMENTATION_NODE);
178         writeAugmentationIdentifier(identifier);
179     }
180
181     @Override
182     public final void startAnyxmlNode(final NodeIdentifier name) throws IOException {
183         LOG.trace("Starting any xml node");
184         startNode(name, LithiumNode.ANY_XML_NODE);
185         inSimple = true;
186     }
187
188     @Override
189     public final void scalarValue(final Object value) throws IOException {
190         writeObject(value);
191     }
192
193     @Override
194     public final void domSourceValue(final DOMSource value) throws IOException {
195         final StringWriter writer = new StringWriter();
196         try {
197             TF.newTransformer().transform(value, new StreamResult(writer));
198         } catch (TransformerException e) {
199             throw new IOException("Error writing anyXml", e);
200         }
201         writeObject(writer.toString());
202     }
203
204     @Override
205     public final void endNode() throws IOException {
206         LOG.trace("Ending the node");
207         if (!inSimple) {
208             lastLeafSetQName = null;
209             output.writeByte(LithiumNode.END_NODE);
210         }
211         inSimple = false;
212     }
213
214     @Override
215     @SuppressFBWarnings(value = "BC_UNCONFIRMED_CAST",
216             justification = "The casts in the switch clauses are indirectly confirmed via the determination of 'type'.")
217     final void writePathArgumentInternal(final PathArgument pathArgument) throws IOException {
218         final byte type = LithiumPathArgument.getSerializablePathArgumentType(pathArgument);
219         output.writeByte(type);
220
221         switch (type) {
222             case LithiumPathArgument.NODE_IDENTIFIER:
223                 NodeIdentifier nodeIdentifier = (NodeIdentifier) pathArgument;
224                 writeQNameInternal(nodeIdentifier.getNodeType());
225                 break;
226             case LithiumPathArgument.NODE_IDENTIFIER_WITH_PREDICATES:
227                 NodeIdentifierWithPredicates nodeIdentifierWithPredicates =
228                     (NodeIdentifierWithPredicates) pathArgument;
229                 writeQNameInternal(nodeIdentifierWithPredicates.getNodeType());
230                 writeKeyValueMap(nodeIdentifierWithPredicates.entrySet());
231                 break;
232             case LithiumPathArgument.NODE_IDENTIFIER_WITH_VALUE:
233                 NodeWithValue<?> nodeWithValue = (NodeWithValue<?>) pathArgument;
234                 writeQNameInternal(nodeWithValue.getNodeType());
235                 writeObject(nodeWithValue.getValue());
236                 break;
237             case LithiumPathArgument.AUGMENTATION_IDENTIFIER:
238                 // No Qname in augmentation identifier
239                 writeAugmentationIdentifier((AugmentationIdentifier) pathArgument);
240                 break;
241             default:
242                 throw new IllegalStateException("Unknown node identifier type is found : "
243                         + pathArgument.getClass().toString());
244         }
245     }
246
247     @Override
248     final void writeYangInstanceIdentifierInternal(final YangInstanceIdentifier identifier) throws IOException {
249         List<PathArgument> pathArguments = identifier.getPathArguments();
250         output.writeInt(pathArguments.size());
251
252         for (PathArgument pathArgument : pathArguments) {
253             writePathArgumentInternal(pathArgument);
254         }
255     }
256
257     final void defaultWriteAugmentationIdentifier(final @NonNull AugmentationIdentifier aid) throws IOException {
258         final Set<QName> qnames = aid.getPossibleChildNames();
259         // Write each child's qname separately, if list is empty send count as 0
260         if (!qnames.isEmpty()) {
261             output.writeInt(qnames.size());
262             for (QName qname : qnames) {
263                 writeQNameInternal(qname);
264             }
265         } else {
266             LOG.debug("augmentation node does not have any child");
267             output.writeInt(0);
268         }
269     }
270
271     final void defaultWriteQName(final QName qname) throws IOException {
272         writeString(qname.getLocalName());
273         writeModule(qname.getModule());
274     }
275
276     final void defaultWriteModule(final QNameModule module) throws IOException {
277         writeString(module.getNamespace().toString());
278         final Optional<Revision> revision = module.getRevision();
279         if (revision.isPresent()) {
280             writeString(revision.get().toString());
281         } else {
282             writeByte(LithiumTokens.IS_NULL_VALUE);
283         }
284     }
285
286     abstract void writeModule(QNameModule module) throws IOException;
287
288     abstract void writeAugmentationIdentifier(@NonNull AugmentationIdentifier aid) throws IOException;
289
290     private void startNode(final PathArgument arg, final byte nodeType) throws IOException {
291         requireNonNull(arg, "Node identifier should not be null");
292         checkState(!inSimple, "Attempted to start a child in a simple node");
293
294         // First write the type of node
295         output.writeByte(nodeType);
296         // Write Start Tag
297         writeQNameInternal(arg.getNodeType());
298     }
299
300     private void writeObjSet(final Set<?> set) throws IOException {
301         output.writeInt(set.size());
302         for (Object o : set) {
303             checkArgument(o instanceof String, "Expected value type to be String but was %s (%s)", o.getClass(), o);
304             writeString((String) o);
305         }
306     }
307
308     private void writeObject(final Object value) throws IOException {
309         byte type = getSerializableType(value);
310         // Write object type first
311         output.writeByte(type);
312
313         switch (type) {
314             case ValueTypes.BOOL_TYPE:
315                 output.writeBoolean((Boolean) value);
316                 break;
317             case ValueTypes.QNAME_TYPE:
318                 writeQNameInternal((QName) value);
319                 break;
320             case ValueTypes.INT_TYPE:
321                 output.writeInt((Integer) value);
322                 break;
323             case ValueTypes.BYTE_TYPE:
324                 output.writeByte((Byte) value);
325                 break;
326             case ValueTypes.LONG_TYPE:
327                 output.writeLong((Long) value);
328                 break;
329             case ValueTypes.SHORT_TYPE:
330                 output.writeShort((Short) value);
331                 break;
332             case ValueTypes.BITS_TYPE:
333                 writeObjSet((Set<?>) value);
334                 break;
335             case ValueTypes.BINARY_TYPE:
336                 byte[] bytes = (byte[]) value;
337                 output.writeInt(bytes.length);
338                 output.write(bytes);
339                 break;
340             case ValueTypes.YANG_IDENTIFIER_TYPE:
341                 writeYangInstanceIdentifierInternal((YangInstanceIdentifier) value);
342                 break;
343             case ValueTypes.EMPTY_TYPE:
344                 break;
345             case ValueTypes.STRING_BYTES_TYPE:
346                 final byte[] valueBytes = value.toString().getBytes(StandardCharsets.UTF_8);
347                 output.writeInt(valueBytes.length);
348                 output.write(valueBytes);
349                 break;
350             default:
351                 output.writeUTF(value.toString());
352                 break;
353         }
354     }
355
356     private void writeKeyValueMap(final Set<Entry<QName, Object>> entrySet) throws IOException {
357         if (!entrySet.isEmpty()) {
358             output.writeInt(entrySet.size());
359             for (Entry<QName, Object> entry : entrySet) {
360                 writeQNameInternal(entry.getKey());
361                 writeObject(entry.getValue());
362             }
363         } else {
364             output.writeInt(0);
365         }
366     }
367
368     private void writeString(final @NonNull String string) throws IOException {
369         final Integer value = stringCodeMap.get(verifyNotNull(string));
370         if (value == null) {
371             stringCodeMap.put(string, stringCodeMap.size());
372             writeByte(LithiumTokens.IS_STRING_VALUE);
373             writeUTF(string);
374         } else {
375             writeByte(LithiumTokens.IS_CODE_VALUE);
376             writeInt(value);
377         }
378     }
379
380     @VisibleForTesting
381     static final byte getSerializableType(final Object node) {
382         final Byte type = KNOWN_TYPES.get(requireNonNull(node).getClass());
383         if (type != null) {
384             if (type == ValueTypes.STRING_TYPE
385                     && ((String) node).length() >= ValueTypes.STRING_BYTES_LENGTH_THRESHOLD) {
386                 return ValueTypes.STRING_BYTES_TYPE;
387             }
388             return type;
389         }
390
391         if (node instanceof Set) {
392             return ValueTypes.BITS_TYPE;
393         }
394
395         if (node instanceof YangInstanceIdentifier) {
396             return ValueTypes.YANG_IDENTIFIER_TYPE;
397         }
398
399         throw new IllegalArgumentException("Unknown value type " + node.getClass().getSimpleName());
400     }
401 }