7e0de62111bf5e9c0c4d89eb8f8523fa745a5dd6
[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.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;
18
19 /**
20  * Provides various utility methods for serialization and de-serialization.
21  *
22  * @author Thomas Pantelis
23  */
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<>();
27
28     private SerializationUtils() {
29     }
30
31     public interface Applier<T> {
32         void apply(T instance, YangInstanceIdentifier path, NormalizedNode<?, ?> node);
33     }
34
35     private static NormalizedNodeDataOutput streamWriter(DataOutput out) {
36         NormalizedNodeDataOutput streamWriter = REUSABLE_WRITER_TL.get();
37         if (streamWriter == null) {
38             streamWriter = NormalizedNodeInputOutput.newDataOutput(out);
39         }
40
41         return streamWriter;
42     }
43
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);
48         }
49
50         return streamReader;
51     }
52
53     public static void serializePathAndNode(YangInstanceIdentifier path, NormalizedNode<?, ?> node,
54             DataOutput out) {
55         Preconditions.checkNotNull(path);
56         Preconditions.checkNotNull(node);
57         try {
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",
63                     path, node), e);
64         }
65     }
66
67     public static <T> void deserializePathAndNode(DataInput in, T instance, Applier<T> applier) {
68         try {
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);
75         }
76     }
77
78     private static NormalizedNode<?, ?> tryDeserializeNormalizedNode(DataInput in) throws IOException {
79         boolean present = in.readBoolean();
80         if (present) {
81             NormalizedNodeDataInput streamReader = streamReader(in);
82             return streamReader.readNormalizedNode();
83         }
84
85         return null;
86     }
87
88     public static NormalizedNode<?, ?> deserializeNormalizedNode(DataInput in) {
89         try {
90             return tryDeserializeNormalizedNode(in);
91         } catch (IOException e) {
92             throw new IllegalArgumentException("Error deserializing NormalizedNode", e);
93         }
94     }
95
96     public static NormalizedNode<?, ?> deserializeNormalizedNode(byte [] bytes) {
97         try {
98             return tryDeserializeNormalizedNode(new DataInputStream(new ByteArrayInputStream(bytes)));
99         } catch (IOException e) {
100             throw new IllegalArgumentException("Error deserializing NormalizedNode", e);
101         }
102     }
103
104     public static void serializeNormalizedNode(NormalizedNode<?, ?> node, DataOutput out) {
105         try {
106             out.writeBoolean(node != null);
107             if (node != null) {
108                 NormalizedNodeDataOutput streamWriter = streamWriter(out);
109                 streamWriter.writeNormalizedNode(node);
110             }
111         } catch (IOException e) {
112             throw new IllegalArgumentException(String.format("Error serializing NormalizedNode %s",
113                     node), e);
114         }
115     }
116
117     public static void serializePath(YangInstanceIdentifier path, DataOutput out) {
118         Preconditions.checkNotNull(path);
119         try {
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);
124         }
125     }
126
127     public static YangInstanceIdentifier deserializePath(DataInput in) {
128         try {
129             NormalizedNodeDataInput streamReader = streamReader(in);
130             return streamReader.readYangInstanceIdentifier();
131         } catch (IOException e) {
132             throw new IllegalArgumentException("Error deserializing path", e);
133         }
134     }
135 }