Remove pre-Lithium serialization support in sal-distributed-datastore
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / utils / 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.utils;
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.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;
24
25 /**
26  * Provides various utility methods for serialization and de-serialization.
27  *
28  * @author Thomas Pantelis
29  */
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<>();
33
34     public interface Applier<T> {
35         void apply(T instance, YangInstanceIdentifier path, NormalizedNode<?, ?> node);
36     }
37
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);
42         }
43
44         return streamWriter;
45     }
46
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);
51         }
52
53         return streamReader;
54     }
55
56     public static void serializePathAndNode(YangInstanceIdentifier path, NormalizedNode<?, ?> node,
57             DataOutput out) {
58         Preconditions.checkNotNull(path);
59         Preconditions.checkNotNull(node);
60         try {
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",
66                     path, node), e);
67         }
68     }
69
70     public static <T> void deserializePathAndNode(DataInput in, T instance, Applier<T> applier) {
71         try {
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);
78         }
79     }
80
81     private static NormalizedNode<?, ?> tryDeserializeNormalizedNode(DataInput in) throws IOException {
82         boolean present = in.readBoolean();
83         if (present) {
84             NormalizedNodeDataInput streamReader = streamReader(in);
85             return streamReader.readNormalizedNode();
86         }
87
88         return null;
89     }
90
91     public static NormalizedNode<?, ?> deserializeNormalizedNode(DataInput in) {
92         try {
93             return tryDeserializeNormalizedNode(in);
94         } catch (IOException e) {
95             throw new IllegalArgumentException("Error deserializing NormalizedNode", e);
96         }
97     }
98
99     public static NormalizedNode<?, ?> deserializeNormalizedNode(byte [] bytes) {
100         try {
101             return tryDeserializeNormalizedNode(new DataInputStream(new ByteArrayInputStream(bytes)));
102         } catch (IOException e) {
103             throw new IllegalArgumentException("Error deserializing NormalizedNode", e);
104         }
105     }
106
107     public static void serializeNormalizedNode(NormalizedNode<?, ?> node, DataOutput out) {
108         try {
109             out.writeBoolean(node != null);
110             if (node != null) {
111                 NormalizedNodeDataOutput streamWriter = streamWriter(out);
112                 streamWriter.writeNormalizedNode(node);
113             }
114         } catch (IOException e) {
115             throw new IllegalArgumentException(String.format("Error serializing NormalizedNode %s",
116                     node), e);
117         }
118     }
119
120     public static byte [] serializeNormalizedNode(NormalizedNode<?, ?> node) {
121         ByteArrayOutputStream bos = new ByteArrayOutputStream();
122         serializeNormalizedNode(node, new DataOutputStream(bos));
123         return bos.toByteArray();
124     }
125
126     public static void serializePath(YangInstanceIdentifier path, DataOutput out) {
127         Preconditions.checkNotNull(path);
128         try {
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);
133         }
134     }
135
136     public static YangInstanceIdentifier deserializePath(DataInput in) {
137         try {
138             NormalizedNodeDataInput streamReader = streamReader(in);
139             return streamReader.readYangInstanceIdentifier();
140         } catch (IOException e) {
141             throw new IllegalArgumentException("Error deserializing path", e);
142         }
143     }
144 }