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.yangtools.yang.data.api.YangInstanceIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
25 * Provides various utility methods for serialization and de-serialization.
27 * @author Thomas Pantelis
28 * @deprecated use {@link org.opendaylight.controller.cluster.datastore.node.utils.stream.SerializationUtils} instead
31 public final class SerializationUtils {
32 public static final ThreadLocal<NormalizedNodeDataOutput> REUSABLE_WRITER_TL = new ThreadLocal<>();
33 public static final ThreadLocal<NormalizedNodeDataInput> REUSABLE_READER_TL = new ThreadLocal<>();
35 public interface Applier<T> {
36 void apply(T instance, YangInstanceIdentifier path, NormalizedNode<?, ?> node);
39 private static NormalizedNodeDataOutput streamWriter(DataOutput out) throws IOException {
40 NormalizedNodeDataOutput streamWriter = REUSABLE_WRITER_TL.get();
41 if (streamWriter == null) {
42 streamWriter = NormalizedNodeInputOutput.newDataOutput(out);
48 private static NormalizedNodeDataInput streamReader(DataInput in) throws IOException {
49 NormalizedNodeDataInput streamReader = REUSABLE_READER_TL.get();
50 if (streamReader == null) {
51 streamReader = NormalizedNodeInputOutput.newDataInput(in);
57 public static void serializePathAndNode(YangInstanceIdentifier path, NormalizedNode<?, ?> node,
59 Preconditions.checkNotNull(path);
60 Preconditions.checkNotNull(node);
62 NormalizedNodeDataOutput streamWriter = streamWriter(out);
63 streamWriter.writeNormalizedNode(node);
64 streamWriter.writeYangInstanceIdentifier(path);
65 } catch (IOException e) {
66 throw new IllegalArgumentException(String.format("Error serializing path %s and Node %s",
71 public static <T> void deserializePathAndNode(DataInput in, T instance, Applier<T> applier) {
73 NormalizedNodeDataInput streamReader = streamReader(in);
74 NormalizedNode<?, ?> node = streamReader.readNormalizedNode();
75 YangInstanceIdentifier path = streamReader.readYangInstanceIdentifier();
76 applier.apply(instance, path, node);
77 } catch (IOException e) {
78 throw new IllegalArgumentException("Error deserializing path and Node", e);
82 private static NormalizedNode<?, ?> tryDeserializeNormalizedNode(DataInput in) throws IOException {
83 boolean present = in.readBoolean();
85 NormalizedNodeDataInput streamReader = streamReader(in);
86 return streamReader.readNormalizedNode();
92 public static NormalizedNode<?, ?> deserializeNormalizedNode(DataInput in) {
94 return tryDeserializeNormalizedNode(in);
95 } catch (IOException e) {
96 throw new IllegalArgumentException("Error deserializing NormalizedNode", e);
100 public static NormalizedNode<?, ?> deserializeNormalizedNode(byte [] bytes) {
102 return tryDeserializeNormalizedNode(new DataInputStream(new ByteArrayInputStream(bytes)));
103 } catch (IOException e) {
104 throw new IllegalArgumentException("Error deserializing NormalizedNode", e);
108 public static void serializeNormalizedNode(NormalizedNode<?, ?> node, DataOutput out) {
110 out.writeBoolean(node != null);
112 NormalizedNodeDataOutput streamWriter = streamWriter(out);
113 streamWriter.writeNormalizedNode(node);
115 } catch (IOException e) {
116 throw new IllegalArgumentException(String.format("Error serializing NormalizedNode %s",
121 public static byte [] serializeNormalizedNode(NormalizedNode<?, ?> node) {
122 ByteArrayOutputStream bos = new ByteArrayOutputStream();
123 serializeNormalizedNode(node, new DataOutputStream(bos));
124 return bos.toByteArray();
127 public static void serializePath(YangInstanceIdentifier path, DataOutput out) {
128 Preconditions.checkNotNull(path);
130 NormalizedNodeDataOutput streamWriter = streamWriter(out);
131 streamWriter.writeYangInstanceIdentifier(path);
132 } catch (IOException e) {
133 throw new IllegalArgumentException(String.format("Error serializing path %s", path), e);
137 public static YangInstanceIdentifier deserializePath(DataInput in) {
139 NormalizedNodeDataInput streamReader = streamReader(in);
140 return streamReader.readYangInstanceIdentifier();
141 } catch (IOException e) {
142 throw new IllegalArgumentException("Error deserializing path", e);