2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.datastore.node.utils.stream;
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;
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;
27 import java.util.Map.Entry;
28 import java.util.Optional;
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;
49 * "original" type mapping. Baseline is Lithium but it really was introduced in Oxygen, where {@code type empty} was
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
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)
74 private final Map<String, Integer> stringCodeMap = new HashMap<>();
76 private QName lastLeafSetQName;
77 private boolean inSimple;
79 AbstractLithiumDataOutput(final DataOutput output) {
84 public final void startLeafNode(final NodeIdentifier name) throws IOException {
85 LOG.trace("Starting a new leaf node");
86 startNode(name, NodeTypes.LEAF_NODE);
91 public final void startLeafSet(final NodeIdentifier name, final int childSizeHint) throws IOException {
92 LOG.trace("Starting a new leaf set");
93 commonStartLeafSet(name, NodeTypes.LEAF_SET);
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, NodeTypes.ORDERED_LEAF_SET);
102 private void commonStartLeafSet(final NodeIdentifier name, final byte nodeType) throws IOException {
103 startNode(name, nodeType);
104 lastLeafSetQName = name.getNodeType();
108 public final void startLeafSetEntryNode(final NodeWithValue<?> name) throws IOException {
109 LOG.trace("Starting a new leaf set entry node");
111 output.writeByte(NodeTypes.LEAF_SET_ENTRY_NODE);
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());
122 public final void startContainerNode(final NodeIdentifier name, final int childSizeHint) throws IOException {
123 LOG.trace("Starting a new container node");
124 startNode(name, NodeTypes.CONTAINER_NODE);
128 public final void startYangModeledAnyXmlNode(final NodeIdentifier name, final int childSizeHint)
130 LOG.trace("Starting a new yang modeled anyXml node");
131 startNode(name, NodeTypes.YANG_MODELED_ANY_XML_NODE);
135 public final void startUnkeyedList(final NodeIdentifier name, final int childSizeHint) throws IOException {
136 LOG.trace("Starting a new unkeyed list");
137 startNode(name, NodeTypes.UNKEYED_LIST);
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, NodeTypes.UNKEYED_LIST_ITEM);
147 public final void startMapNode(final NodeIdentifier name, final int childSizeHint) throws IOException {
148 LOG.trace("Starting a new map node");
149 startNode(name, NodeTypes.MAP_NODE);
153 public final void startMapEntryNode(final NodeIdentifierWithPredicates identifier, final int childSizeHint)
155 LOG.trace("Starting a new map entry node");
156 startNode(identifier, NodeTypes.MAP_ENTRY_NODE);
157 writeKeyValueMap(identifier.entrySet());
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, NodeTypes.ORDERED_MAP_NODE);
167 public final void startChoiceNode(final NodeIdentifier name, final int childSizeHint) throws IOException {
168 LOG.trace("Starting a new choice node");
169 startNode(name, NodeTypes.CHOICE_NODE);
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");
177 output.writeByte(NodeTypes.AUGMENTATION_NODE);
178 writeAugmentationIdentifier(identifier);
182 public final void startAnyxmlNode(final NodeIdentifier name) throws IOException {
183 LOG.trace("Starting any xml node");
184 startNode(name, NodeTypes.ANY_XML_NODE);
189 public final void scalarValue(final Object value) throws IOException {
194 public final void domSourceValue(final DOMSource value) throws IOException {
195 final StringWriter writer = new StringWriter();
197 TF.newTransformer().transform(value, new StreamResult(writer));
198 } catch (TransformerException e) {
199 throw new IOException("Error writing anyXml", e);
201 writeObject(writer.toString());
205 public final void endNode() throws IOException {
206 LOG.trace("Ending the node");
208 lastLeafSetQName = null;
209 output.writeByte(NodeTypes.END_NODE);
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 = PathArgumentTypes.getSerializablePathArgumentType(pathArgument);
219 output.writeByte(type);
222 case PathArgumentTypes.NODE_IDENTIFIER:
223 NodeIdentifier nodeIdentifier = (NodeIdentifier) pathArgument;
224 writeQNameInternal(nodeIdentifier.getNodeType());
226 case PathArgumentTypes.NODE_IDENTIFIER_WITH_PREDICATES:
227 NodeIdentifierWithPredicates nodeIdentifierWithPredicates =
228 (NodeIdentifierWithPredicates) pathArgument;
229 writeQNameInternal(nodeIdentifierWithPredicates.getNodeType());
230 writeKeyValueMap(nodeIdentifierWithPredicates.entrySet());
232 case PathArgumentTypes.NODE_IDENTIFIER_WITH_VALUE:
233 NodeWithValue<?> nodeWithValue = (NodeWithValue<?>) pathArgument;
234 writeQNameInternal(nodeWithValue.getNodeType());
235 writeObject(nodeWithValue.getValue());
237 case PathArgumentTypes.AUGMENTATION_IDENTIFIER:
238 // No Qname in augmentation identifier
239 writeAugmentationIdentifier((AugmentationIdentifier) pathArgument);
242 throw new IllegalStateException("Unknown node identifier type is found : "
243 + pathArgument.getClass().toString());
248 final void writeYangInstanceIdentifierInternal(final YangInstanceIdentifier identifier) throws IOException {
249 List<PathArgument> pathArguments = identifier.getPathArguments();
250 output.writeInt(pathArguments.size());
252 for (PathArgument pathArgument : pathArguments) {
253 writePathArgumentInternal(pathArgument);
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);
266 LOG.debug("augmentation node does not have any child");
271 final void defaultWriteQName(final QName qname) throws IOException {
272 writeString(qname.getLocalName());
273 writeModule(qname.getModule());
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());
282 writeByte(TokenTypes.IS_NULL_VALUE);
286 abstract void writeModule(QNameModule module) throws IOException;
288 abstract void writeAugmentationIdentifier(@NonNull AugmentationIdentifier aid) throws IOException;
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");
294 // First write the type of node
295 output.writeByte(nodeType);
297 writeQNameInternal(arg.getNodeType());
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);
308 private void writeObject(final Object value) throws IOException {
309 byte type = getSerializableType(value);
310 // Write object type first
311 output.writeByte(type);
314 case ValueTypes.BOOL_TYPE:
315 output.writeBoolean((Boolean) value);
317 case ValueTypes.QNAME_TYPE:
318 writeQNameInternal((QName) value);
320 case ValueTypes.INT_TYPE:
321 output.writeInt((Integer) value);
323 case ValueTypes.BYTE_TYPE:
324 output.writeByte((Byte) value);
326 case ValueTypes.LONG_TYPE:
327 output.writeLong((Long) value);
329 case ValueTypes.SHORT_TYPE:
330 output.writeShort((Short) value);
332 case ValueTypes.BITS_TYPE:
333 writeObjSet((Set<?>) value);
335 case ValueTypes.BINARY_TYPE:
336 byte[] bytes = (byte[]) value;
337 output.writeInt(bytes.length);
340 case ValueTypes.YANG_IDENTIFIER_TYPE:
341 writeYangInstanceIdentifierInternal((YangInstanceIdentifier) value);
343 case ValueTypes.EMPTY_TYPE:
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);
351 output.writeUTF(value.toString());
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());
368 private void writeString(final @NonNull String string) throws IOException {
369 final Integer value = stringCodeMap.get(verifyNotNull(string));
371 stringCodeMap.put(string, stringCodeMap.size());
372 writeByte(TokenTypes.IS_STRING_VALUE);
375 writeByte(TokenTypes.IS_CODE_VALUE);
381 static final byte getSerializableType(final Object node) {
382 final Byte type = KNOWN_TYPES.get(requireNonNull(node).getClass());
384 if (type == ValueTypes.STRING_TYPE
385 && ((String) node).length() >= ValueTypes.STRING_BYTES_LENGTH_THRESHOLD) {
386 return ValueTypes.STRING_BYTES_TYPE;
391 if (node instanceof Set) {
392 return ValueTypes.BITS_TYPE;
395 if (node instanceof YangInstanceIdentifier) {
396 return ValueTypes.YANG_IDENTIFIER_TYPE;
399 throw new IllegalArgumentException("Unknown value type " + node.getClass().getSimpleName());