Fix warnings/javadocs 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 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.NormalizedNodeInputOutput;
24 import org.opendaylight.controller.cluster.datastore.node.utils.stream.NormalizedNodeInputStreamReader;
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<NormalizedNodeDataOutput> REUSABLE_WRITER_TL = new ThreadLocal<>();
36     public static final ThreadLocal<NormalizedNodeDataInput> REUSABLE_READER_TL = new ThreadLocal<>();
37
38     public interface Applier<T> {
39         void apply(T instance, YangInstanceIdentifier path, NormalizedNode<?, ?> node);
40     }
41
42     private static NormalizedNodeDataOutput streamWriter(DataOutput out) throws IOException {
43         NormalizedNodeDataOutput streamWriter = REUSABLE_WRITER_TL.get();
44         if (streamWriter == null) {
45             streamWriter = NormalizedNodeInputOutput.newDataOutput(out);
46         }
47
48         return streamWriter;
49     }
50
51     private static NormalizedNodeDataInput streamReader(DataInput in) throws IOException {
52         NormalizedNodeDataInput streamReader = REUSABLE_READER_TL.get();
53         if (streamReader == null) {
54             streamReader = new NormalizedNodeInputStreamReader(in);
55         }
56
57         return streamReader;
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     private static NormalizedNode<?, ?> tryDeserializeNormalizedNode(DataInput in) throws IOException {
86         boolean present = in.readBoolean();
87         if (present) {
88             NormalizedNodeDataInput streamReader = streamReader(in);
89             return streamReader.readNormalizedNode();
90         }
91
92         return null;
93     }
94
95     public static NormalizedNode<?, ?> deserializeNormalizedNode(DataInput in) {
96         try {
97             return tryDeserializeNormalizedNode(in);
98         } catch (IOException e) {
99             throw new IllegalArgumentException("Error deserializing NormalizedNode", e);
100         }
101     }
102
103     public static NormalizedNode<?, ?> deserializeNormalizedNode(byte [] bytes) {
104         try {
105             return tryDeserializeNormalizedNode(new DataInputStream(new ByteArrayInputStream(bytes)));
106         } catch (InvalidNormalizedNodeStreamException e) {
107             // Probably from legacy protobuf serialization - try that.
108             try {
109                 NormalizedNodeMessages.Node serializedNode = NormalizedNodeMessages.Node.parseFrom(bytes);
110                 return new NormalizedNodeToNodeCodec(null).decode(serializedNode);
111             } catch (InvalidProtocolBufferException e2) {
112                 throw new IllegalArgumentException("Error deserializing NormalizedNode", e);
113             }
114         } catch (IOException e) {
115             throw new IllegalArgumentException("Error deserializing NormalizedNode", e);
116         }
117     }
118
119     public static void serializeNormalizedNode(NormalizedNode<?, ?> node, DataOutput out) {
120         try {
121             out.writeBoolean(node != null);
122             if (node != null) {
123                 NormalizedNodeDataOutput streamWriter = streamWriter(out);
124                 streamWriter.writeNormalizedNode(node);
125             }
126         } catch (IOException e) {
127             throw new IllegalArgumentException(String.format("Error serializing NormalizedNode %s",
128                     node), e);
129         }
130     }
131
132     public static byte [] serializeNormalizedNode(NormalizedNode<?, ?> node) {
133         ByteArrayOutputStream bos = new ByteArrayOutputStream();
134         serializeNormalizedNode(node, new DataOutputStream(bos));
135         return bos.toByteArray();
136     }
137
138     public static void serializePath(YangInstanceIdentifier path, DataOutput out) {
139         Preconditions.checkNotNull(path);
140         try {
141             NormalizedNodeDataOutput streamWriter = streamWriter(out);
142             streamWriter.writeYangInstanceIdentifier(path);
143         } catch (IOException e) {
144             throw new IllegalArgumentException(String.format("Error serializing path %s", path), e);
145         }
146     }
147
148     public static YangInstanceIdentifier deserializePath(DataInput in) {
149         try {
150             NormalizedNodeDataInput streamReader = streamReader(in);
151             return streamReader.readYangInstanceIdentifier();
152         } catch (IOException e) {
153             throw new IllegalArgumentException("Error deserializing path", e);
154         }
155     }
156 }