fc0497280cc59212f4feed17f1b7460c6a215af6
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / main / java / org / opendaylight / controller / cluster / datastore / node / utils / stream / SerializationUtils.java
1 /*
2  * Copyright (c) 2014 Brocade Communications 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 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.yangtools.yang.data.api.YangInstanceIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
20
21 /**
22  * Provides various utility methods for serialization and de-serialization.
23  *
24  * @author Thomas Pantelis
25  */
26 public final class SerializationUtils {
27     public static final ThreadLocal<NormalizedNodeDataOutput> REUSABLE_WRITER_TL = new ThreadLocal<>();
28     public static final ThreadLocal<NormalizedNodeDataInput> REUSABLE_READER_TL = new ThreadLocal<>();
29
30     public interface Applier<T> {
31         void apply(T instance, YangInstanceIdentifier path, NormalizedNode<?, ?> node);
32     }
33
34     private static NormalizedNodeDataOutput streamWriter(DataOutput out) throws IOException {
35         NormalizedNodeDataOutput streamWriter = REUSABLE_WRITER_TL.get();
36         if (streamWriter == null) {
37             streamWriter = NormalizedNodeInputOutput.newDataOutput(out);
38         }
39
40         return streamWriter;
41     }
42
43     private static NormalizedNodeDataInput streamReader(DataInput in) throws IOException {
44         NormalizedNodeDataInput streamReader = REUSABLE_READER_TL.get();
45         if (streamReader == null) {
46             streamReader = NormalizedNodeInputOutput.newDataInput(in);
47         }
48
49         return streamReader;
50     }
51
52     public static void serializePathAndNode(YangInstanceIdentifier path, NormalizedNode<?, ?> node,
53             DataOutput out) {
54         Preconditions.checkNotNull(path);
55         Preconditions.checkNotNull(node);
56         try {
57             NormalizedNodeDataOutput streamWriter = streamWriter(out);
58             streamWriter.writeNormalizedNode(node);
59             streamWriter.writeYangInstanceIdentifier(path);
60         } catch (IOException e) {
61             throw new IllegalArgumentException(String.format("Error serializing path %s and Node %s",
62                     path, node), e);
63         }
64     }
65
66     public static <T> void deserializePathAndNode(DataInput in, T instance, Applier<T> applier) {
67         try {
68             NormalizedNodeDataInput streamReader = streamReader(in);
69             NormalizedNode<?, ?> node = streamReader.readNormalizedNode();
70             YangInstanceIdentifier path = streamReader.readYangInstanceIdentifier();
71             applier.apply(instance, path, node);
72         } catch (IOException e) {
73             throw new IllegalArgumentException("Error deserializing path and Node", e);
74         }
75     }
76
77     private static NormalizedNode<?, ?> tryDeserializeNormalizedNode(DataInput in) throws IOException {
78         boolean present = in.readBoolean();
79         if (present) {
80             NormalizedNodeDataInput streamReader = streamReader(in);
81             return streamReader.readNormalizedNode();
82         }
83
84         return null;
85     }
86
87     public static NormalizedNode<?, ?> deserializeNormalizedNode(DataInput in) {
88         try {
89             return tryDeserializeNormalizedNode(in);
90         } catch (IOException e) {
91             throw new IllegalArgumentException("Error deserializing NormalizedNode", e);
92         }
93     }
94
95     public static NormalizedNode<?, ?> deserializeNormalizedNode(byte [] bytes) {
96         try {
97             return tryDeserializeNormalizedNode(new DataInputStream(new ByteArrayInputStream(bytes)));
98         } catch (IOException e) {
99             throw new IllegalArgumentException("Error deserializing NormalizedNode", e);
100         }
101     }
102
103     public static void serializeNormalizedNode(NormalizedNode<?, ?> node, DataOutput out) {
104         try {
105             out.writeBoolean(node != null);
106             if (node != null) {
107                 NormalizedNodeDataOutput streamWriter = streamWriter(out);
108                 streamWriter.writeNormalizedNode(node);
109             }
110         } catch (IOException e) {
111             throw new IllegalArgumentException(String.format("Error serializing NormalizedNode %s",
112                     node), e);
113         }
114     }
115
116     public static byte [] serializeNormalizedNode(NormalizedNode<?, ?> node) {
117         ByteArrayOutputStream bos = new ByteArrayOutputStream();
118         serializeNormalizedNode(node, new DataOutputStream(bos));
119         return bos.toByteArray();
120     }
121
122     public static void serializePath(YangInstanceIdentifier path, DataOutput out) {
123         Preconditions.checkNotNull(path);
124         try {
125             NormalizedNodeDataOutput streamWriter = streamWriter(out);
126             streamWriter.writeYangInstanceIdentifier(path);
127         } catch (IOException e) {
128             throw new IllegalArgumentException(String.format("Error serializing path %s", path), e);
129         }
130     }
131
132     public static YangInstanceIdentifier deserializePath(DataInput in) {
133         try {
134             NormalizedNodeDataInput streamReader = streamReader(in);
135             return streamReader.readYangInstanceIdentifier();
136         } catch (IOException e) {
137             throw new IllegalArgumentException("Error deserializing path", e);
138         }
139     }
140 }