2 * Copyright (c) 2014 Brocade Communications 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 com.google.common.base.Preconditions;
11 import java.io.ByteArrayInputStream;
12 import java.io.DataInput;
13 import java.io.DataInputStream;
14 import java.io.DataOutput;
15 import java.io.IOException;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
17 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
20 * Provides various utility methods for serialization and de-serialization.
22 * @author Thomas Pantelis
24 public final class SerializationUtils {
25 public static final ThreadLocal<NormalizedNodeDataOutput> REUSABLE_WRITER_TL = new ThreadLocal<>();
26 public static final ThreadLocal<NormalizedNodeDataInput> REUSABLE_READER_TL = new ThreadLocal<>();
28 private SerializationUtils() {
31 public interface Applier<T> {
32 void apply(T instance, YangInstanceIdentifier path, NormalizedNode<?, ?> node);
35 private static NormalizedNodeDataOutput streamWriter(DataOutput out) {
36 NormalizedNodeDataOutput streamWriter = REUSABLE_WRITER_TL.get();
37 if (streamWriter == null) {
38 streamWriter = NormalizedNodeInputOutput.newDataOutput(out);
44 private static NormalizedNodeDataInput streamReader(DataInput in) throws IOException {
45 NormalizedNodeDataInput streamReader = REUSABLE_READER_TL.get();
46 if (streamReader == null) {
47 streamReader = NormalizedNodeInputOutput.newDataInput(in);
53 public static void serializePathAndNode(YangInstanceIdentifier path, NormalizedNode<?, ?> node,
55 Preconditions.checkNotNull(path);
56 Preconditions.checkNotNull(node);
58 NormalizedNodeDataOutput streamWriter = streamWriter(out);
59 streamWriter.writeNormalizedNode(node);
60 streamWriter.writeYangInstanceIdentifier(path);
61 } catch (IOException e) {
62 throw new IllegalArgumentException(String.format("Error serializing path %s and Node %s",
67 public static <T> void deserializePathAndNode(DataInput in, T instance, Applier<T> applier) {
69 NormalizedNodeDataInput streamReader = streamReader(in);
70 NormalizedNode<?, ?> node = streamReader.readNormalizedNode();
71 YangInstanceIdentifier path = streamReader.readYangInstanceIdentifier();
72 applier.apply(instance, path, node);
73 } catch (IOException e) {
74 throw new IllegalArgumentException("Error deserializing path and Node", e);
78 private static NormalizedNode<?, ?> tryDeserializeNormalizedNode(DataInput in) throws IOException {
79 boolean present = in.readBoolean();
81 NormalizedNodeDataInput streamReader = streamReader(in);
82 return streamReader.readNormalizedNode();
88 public static NormalizedNode<?, ?> deserializeNormalizedNode(DataInput in) {
90 return tryDeserializeNormalizedNode(in);
91 } catch (IOException e) {
92 throw new IllegalArgumentException("Error deserializing NormalizedNode", e);
96 public static NormalizedNode<?, ?> deserializeNormalizedNode(byte [] bytes) {
98 return tryDeserializeNormalizedNode(new DataInputStream(new ByteArrayInputStream(bytes)));
99 } catch (IOException e) {
100 throw new IllegalArgumentException("Error deserializing NormalizedNode", e);
104 public static void serializeNormalizedNode(NormalizedNode<?, ?> node, DataOutput out) {
106 out.writeBoolean(node != null);
108 NormalizedNodeDataOutput streamWriter = streamWriter(out);
109 streamWriter.writeNormalizedNode(node);
111 } catch (IOException e) {
112 throw new IllegalArgumentException(String.format("Error serializing NormalizedNode %s",
117 public static void serializePath(YangInstanceIdentifier path, DataOutput out) {
118 Preconditions.checkNotNull(path);
120 NormalizedNodeDataOutput streamWriter = streamWriter(out);
121 streamWriter.writeYangInstanceIdentifier(path);
122 } catch (IOException e) {
123 throw new IllegalArgumentException(String.format("Error serializing path %s", path), e);
127 public static YangInstanceIdentifier deserializePath(DataInput in) {
129 NormalizedNodeDataInput streamReader = streamReader(in);
130 return streamReader.readYangInstanceIdentifier();
131 } catch (IOException e) {
132 throw new IllegalArgumentException("Error deserializing path", e);