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.utils;
10 import com.google.common.base.Preconditions;
11 import java.io.ByteArrayInputStream;
12 import java.io.ByteArrayOutputStream;
13 import java.io.DataInput;
14 import java.io.DataInputStream;
15 import java.io.DataOutput;
16 import java.io.DataOutputStream;
17 import java.io.IOException;
18 import org.opendaylight.controller.cluster.datastore.node.utils.stream.NormalizedNodeDataInput;
19 import org.opendaylight.controller.cluster.datastore.node.utils.stream.NormalizedNodeDataOutput;
20 import org.opendaylight.controller.cluster.datastore.node.utils.stream.NormalizedNodeInputOutput;
21 import org.opendaylight.controller.cluster.datastore.node.utils.stream.NormalizedNodeInputStreamReader;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
23 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
26 * Provides various utility methods for serialization and de-serialization.
28 * @author Thomas Pantelis
30 public final class SerializationUtils {
31 public static final ThreadLocal<NormalizedNodeDataOutput> REUSABLE_WRITER_TL = new ThreadLocal<>();
32 public static final ThreadLocal<NormalizedNodeDataInput> REUSABLE_READER_TL = new ThreadLocal<>();
34 public interface Applier<T> {
35 void apply(T instance, YangInstanceIdentifier path, NormalizedNode<?, ?> node);
38 private static NormalizedNodeDataOutput streamWriter(DataOutput out) throws IOException {
39 NormalizedNodeDataOutput streamWriter = REUSABLE_WRITER_TL.get();
40 if (streamWriter == null) {
41 streamWriter = NormalizedNodeInputOutput.newDataOutput(out);
47 private static NormalizedNodeDataInput streamReader(DataInput in) throws IOException {
48 NormalizedNodeDataInput streamReader = REUSABLE_READER_TL.get();
49 if (streamReader == null) {
50 streamReader = new NormalizedNodeInputStreamReader(in);
56 public static void serializePathAndNode(YangInstanceIdentifier path, NormalizedNode<?, ?> node,
58 Preconditions.checkNotNull(path);
59 Preconditions.checkNotNull(node);
61 NormalizedNodeDataOutput streamWriter = streamWriter(out);
62 streamWriter.writeNormalizedNode(node);
63 streamWriter.writeYangInstanceIdentifier(path);
64 } catch (IOException e) {
65 throw new IllegalArgumentException(String.format("Error serializing path %s and Node %s",
70 public static <T> void deserializePathAndNode(DataInput in, T instance, Applier<T> applier) {
72 NormalizedNodeDataInput streamReader = streamReader(in);
73 NormalizedNode<?, ?> node = streamReader.readNormalizedNode();
74 YangInstanceIdentifier path = streamReader.readYangInstanceIdentifier();
75 applier.apply(instance, path, node);
76 } catch (IOException e) {
77 throw new IllegalArgumentException("Error deserializing path and Node", e);
81 private static NormalizedNode<?, ?> tryDeserializeNormalizedNode(DataInput in) throws IOException {
82 boolean present = in.readBoolean();
84 NormalizedNodeDataInput streamReader = streamReader(in);
85 return streamReader.readNormalizedNode();
91 public static NormalizedNode<?, ?> deserializeNormalizedNode(DataInput in) {
93 return tryDeserializeNormalizedNode(in);
94 } catch (IOException e) {
95 throw new IllegalArgumentException("Error deserializing NormalizedNode", e);
99 public static NormalizedNode<?, ?> deserializeNormalizedNode(byte [] bytes) {
101 return tryDeserializeNormalizedNode(new DataInputStream(new ByteArrayInputStream(bytes)));
102 } catch (IOException e) {
103 throw new IllegalArgumentException("Error deserializing NormalizedNode", e);
107 public static void serializeNormalizedNode(NormalizedNode<?, ?> node, DataOutput out) {
109 out.writeBoolean(node != null);
111 NormalizedNodeDataOutput streamWriter = streamWriter(out);
112 streamWriter.writeNormalizedNode(node);
114 } catch (IOException e) {
115 throw new IllegalArgumentException(String.format("Error serializing NormalizedNode %s",
120 public static byte [] serializeNormalizedNode(NormalizedNode<?, ?> node) {
121 ByteArrayOutputStream bos = new ByteArrayOutputStream();
122 serializeNormalizedNode(node, new DataOutputStream(bos));
123 return bos.toByteArray();
126 public static void serializePath(YangInstanceIdentifier path, DataOutput out) {
127 Preconditions.checkNotNull(path);
129 NormalizedNodeDataOutput streamWriter = streamWriter(out);
130 streamWriter.writeYangInstanceIdentifier(path);
131 } catch (IOException e) {
132 throw new IllegalArgumentException(String.format("Error serializing path %s", path), e);
136 public static YangInstanceIdentifier deserializePath(DataInput in) {
138 NormalizedNodeDataInput streamReader = streamReader(in);
139 return streamReader.readYangInstanceIdentifier();
140 } catch (IOException e) {
141 throw new IllegalArgumentException("Error deserializing path", e);