Bug 2268: Use streaming for Modification payload
[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.DataInput;
12 import java.io.DataOutput;
13 import java.io.IOException;
14 import org.opendaylight.controller.cluster.datastore.node.utils.stream.NormalizedNodeInputStreamReader;
15 import org.opendaylight.controller.cluster.datastore.node.utils.stream.NormalizedNodeOutputStreamWriter;
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 ThreadLocal<NormalizedNodeOutputStreamWriter> REUSABLE_WRITER_TL = new ThreadLocal<>();
26     public static ThreadLocal<NormalizedNodeInputStreamReader> REUSABLE_READER_TL = new ThreadLocal<>();
27
28     public static interface Applier<T> {
29         void apply(T instance, YangInstanceIdentifier path, NormalizedNode<?, ?> node);
30     }
31
32     private static NormalizedNodeOutputStreamWriter streamWriter(DataOutput out) throws IOException {
33         NormalizedNodeOutputStreamWriter streamWriter = REUSABLE_WRITER_TL.get();
34         if(streamWriter == null) {
35             streamWriter = new NormalizedNodeOutputStreamWriter(out);
36         }
37
38         return streamWriter;
39     }
40
41     private static NormalizedNodeInputStreamReader streamReader(DataInput in) throws IOException {
42         NormalizedNodeInputStreamReader streamWriter = REUSABLE_READER_TL.get();
43         if(streamWriter == null) {
44             streamWriter = new NormalizedNodeInputStreamReader(in);
45         }
46
47         return streamWriter;
48     }
49
50     public static void serializePathAndNode(YangInstanceIdentifier path, NormalizedNode<?, ?> node,
51             DataOutput out) {
52         Preconditions.checkNotNull(path);
53         Preconditions.checkNotNull(node);
54         try {
55             NormalizedNodeOutputStreamWriter streamWriter = streamWriter(out);
56             streamWriter.writeNormalizedNode(node);
57             streamWriter.writeYangInstanceIdentifier(path);
58         } catch (IOException e) {
59             throw new IllegalArgumentException(String.format("Error serializing path %s and Node %s",
60                     path, node), e);
61         }
62     }
63
64     public static <T> void deserializePathAndNode(DataInput in, T instance, Applier<T> applier) {
65         try {
66             NormalizedNodeInputStreamReader streamReader = streamReader(in);
67             NormalizedNode<?, ?> node = streamReader.readNormalizedNode();
68             YangInstanceIdentifier path = streamReader.readYangInstanceIdentifier();
69             applier.apply(instance, path, node);
70         } catch (IOException e) {
71             throw new IllegalArgumentException("Error deserializing path and Node", e);
72         }
73     }
74
75     public static void serializeNormalizedNode(NormalizedNode<?, ?> node, DataOutput out) {
76         try {
77             out.writeBoolean(node != null);
78             if(node != null) {
79                 NormalizedNodeOutputStreamWriter streamWriter = streamWriter(out);
80                 streamWriter.writeNormalizedNode(node);
81             }
82         } catch (IOException e) {
83             throw new IllegalArgumentException(String.format("Error serializing NormalizedNode %s",
84                     node), e);
85         }
86     }
87
88     public static NormalizedNode<?, ?> deserializeNormalizedNode(DataInput in) {
89             try {
90                 boolean present = in.readBoolean();
91                 if(present) {
92                     NormalizedNodeInputStreamReader streamReader = streamReader(in);
93                     return streamReader.readNormalizedNode();
94                 }
95             } catch (IOException e) {
96                 throw new IllegalArgumentException("Error deserializing NormalizedNode", e);
97             }
98
99         return null;
100     }
101
102     public static void serializePath(YangInstanceIdentifier path, DataOutput out) {
103         Preconditions.checkNotNull(path);
104         try {
105             NormalizedNodeOutputStreamWriter streamWriter = streamWriter(out);
106             streamWriter.writeYangInstanceIdentifier(path);
107         } catch (IOException e) {
108             throw new IllegalArgumentException(String.format("Error serializing path %s", path), e);
109         }
110     }
111
112     public static YangInstanceIdentifier deserializePath(DataInput in) {
113         try {
114             NormalizedNodeInputStreamReader streamReader = streamReader(in);
115             return streamReader.readYangInstanceIdentifier();
116         } catch (IOException e) {
117             throw new IllegalArgumentException("Error deserializing path", e);
118         }
119     }
120 }