BUG-4626: Introduce NormalizedNodeData{Input,Output}
[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 com.google.protobuf.InvalidProtocolBufferException;
12 import java.io.ByteArrayInputStream;
13 import java.io.ByteArrayOutputStream;
14 import java.io.DataInput;
15 import java.io.DataInputStream;
16 import java.io.DataOutput;
17 import java.io.DataOutputStream;
18 import java.io.IOException;
19 import org.opendaylight.controller.cluster.datastore.node.NormalizedNodeToNodeCodec;
20 import org.opendaylight.controller.cluster.datastore.node.utils.stream.InvalidNormalizedNodeStreamException;
21 import org.opendaylight.controller.cluster.datastore.node.utils.stream.NormalizedNodeDataInput;
22 import org.opendaylight.controller.cluster.datastore.node.utils.stream.NormalizedNodeDataOutput;
23 import org.opendaylight.controller.cluster.datastore.node.utils.stream.NormalizedNodeInputStreamReader;
24 import org.opendaylight.controller.cluster.datastore.node.utils.stream.NormalizedNodeOutputStreamWriter;
25 import org.opendaylight.controller.protobuff.messages.common.NormalizedNodeMessages;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
27 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
28
29 /**
30  * Provides various utility methods for serialization and de-serialization.
31  *
32  * @author Thomas Pantelis
33  */
34 public final class SerializationUtils {
35     public static final ThreadLocal<NormalizedNodeOutputStreamWriter> REUSABLE_WRITER_TL = new ThreadLocal<>();
36     public static final ThreadLocal<NormalizedNodeInputStreamReader> REUSABLE_READER_TL = new ThreadLocal<>();
37
38     public static interface Applier<T> {
39         void apply(T instance, YangInstanceIdentifier path, NormalizedNode<?, ?> node);
40     }
41
42     private static NormalizedNodeDataOutput streamWriter(DataOutput out) throws IOException {
43         NormalizedNodeOutputStreamWriter streamWriter = REUSABLE_WRITER_TL.get();
44         if(streamWriter == null) {
45             streamWriter = new NormalizedNodeOutputStreamWriter(out);
46         }
47
48         return streamWriter;
49     }
50
51     private static NormalizedNodeDataInput streamReader(DataInput in) throws IOException {
52         NormalizedNodeInputStreamReader streamWriter = REUSABLE_READER_TL.get();
53         if(streamWriter == null) {
54             streamWriter = new NormalizedNodeInputStreamReader(in);
55         }
56
57         return streamWriter;
58     }
59
60     public static void serializePathAndNode(YangInstanceIdentifier path, NormalizedNode<?, ?> node,
61             DataOutput out) {
62         Preconditions.checkNotNull(path);
63         Preconditions.checkNotNull(node);
64         try {
65             NormalizedNodeDataOutput streamWriter = streamWriter(out);
66             streamWriter.writeNormalizedNode(node);
67             streamWriter.writeYangInstanceIdentifier(path);
68         } catch (IOException e) {
69             throw new IllegalArgumentException(String.format("Error serializing path %s and Node %s",
70                     path, node), e);
71         }
72     }
73
74     public static <T> void deserializePathAndNode(DataInput in, T instance, Applier<T> applier) {
75         try {
76             NormalizedNodeDataInput streamReader = streamReader(in);
77             NormalizedNode<?, ?> node = streamReader.readNormalizedNode();
78             YangInstanceIdentifier path = streamReader.readYangInstanceIdentifier();
79             applier.apply(instance, path, node);
80         } catch (IOException e) {
81             throw new IllegalArgumentException("Error deserializing path and Node", e);
82         }
83     }
84
85     public static void serializeNormalizedNode(NormalizedNode<?, ?> node, DataOutput out) {
86         try {
87             out.writeBoolean(node != null);
88             if(node != null) {
89                 NormalizedNodeDataOutput streamWriter = streamWriter(out);
90                 streamWriter.writeNormalizedNode(node);
91             }
92         } catch (IOException e) {
93             throw new IllegalArgumentException(String.format("Error serializing NormalizedNode %s",
94                     node), e);
95         }
96     }
97
98     public static NormalizedNode<?, ?> deserializeNormalizedNode(DataInput in) {
99         try {
100             return tryDeserializeNormalizedNode(in);
101         } catch (IOException e) {
102             throw new IllegalArgumentException("Error deserializing NormalizedNode", e);
103         }
104     }
105
106     private static NormalizedNode<?, ?> tryDeserializeNormalizedNode(DataInput in) throws IOException {
107         boolean present = in.readBoolean();
108         if(present) {
109             NormalizedNodeDataInput streamReader = streamReader(in);
110             return streamReader.readNormalizedNode();
111         }
112
113         return null;
114     }
115
116     public static NormalizedNode<?, ?> deserializeNormalizedNode(byte [] bytes) {
117         NormalizedNode<?, ?> node = null;
118         try {
119             node = tryDeserializeNormalizedNode(new DataInputStream(new ByteArrayInputStream(bytes)));
120         } catch(InvalidNormalizedNodeStreamException e) {
121             // Probably from legacy protobuf serialization - try that.
122             try {
123                 NormalizedNodeMessages.Node serializedNode = NormalizedNodeMessages.Node.parseFrom(bytes);
124                 node =  new NormalizedNodeToNodeCodec(null).decode(serializedNode);
125             } catch (InvalidProtocolBufferException e2) {
126                 throw new IllegalArgumentException("Error deserializing NormalizedNode", e);
127             }
128         } catch (IOException e) {
129             throw new IllegalArgumentException("Error deserializing NormalizedNode", e);
130         }
131
132         return node;
133     }
134
135     public static byte [] serializeNormalizedNode(NormalizedNode<?, ?> node) {
136         ByteArrayOutputStream bos = new ByteArrayOutputStream();
137         serializeNormalizedNode(node, new DataOutputStream(bos));
138         return bos.toByteArray();
139     }
140
141     public static void serializePath(YangInstanceIdentifier path, DataOutput out) {
142         Preconditions.checkNotNull(path);
143         try {
144             NormalizedNodeDataOutput streamWriter = streamWriter(out);
145             streamWriter.writeYangInstanceIdentifier(path);
146         } catch (IOException e) {
147             throw new IllegalArgumentException(String.format("Error serializing path %s", path), e);
148         }
149     }
150
151     public static YangInstanceIdentifier deserializePath(DataInput in) {
152         try {
153             NormalizedNodeDataInput streamReader = streamReader(in);
154             return streamReader.readYangInstanceIdentifier();
155         } catch (IOException e) {
156             throw new IllegalArgumentException("Error deserializing path", e);
157         }
158     }
159 }