Bug 2265: Use streaming for DeleteData message
[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 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
19
20 /**
21  * Provides various utility methods for serialization and de-serialization.
22  *
23  * @author Thomas Pantelis
24  */
25 public final class SerializationUtils {
26
27     public static interface Applier<T> {
28         void apply(T instance, YangInstanceIdentifier path, NormalizedNode<?, ?> node);
29     }
30
31     public static void serializePathAndNode(YangInstanceIdentifier path, NormalizedNode<?, ?> node,
32             DataOutput out) {
33         Preconditions.checkNotNull(path);
34         Preconditions.checkNotNull(node);
35         try {
36             NormalizedNodeOutputStreamWriter streamWriter = new NormalizedNodeOutputStreamWriter(out);
37             NormalizedNodeWriter.forStreamWriter(streamWriter).write(node);
38             streamWriter.writeYangInstanceIdentifier(path);
39         } catch (IOException e) {
40             throw new IllegalArgumentException(String.format("Error serializing path {} and Node {}",
41                     path, node), e);
42         }
43     }
44
45     public static <T> void deserializePathAndNode(DataInput in, T instance, Applier<T> applier) {
46         try {
47             NormalizedNodeInputStreamReader streamReader = new NormalizedNodeInputStreamReader(in);
48             NormalizedNode<?, ?> node = streamReader.readNormalizedNode();
49             YangInstanceIdentifier path = streamReader.readYangInstanceIdentifier();
50             applier.apply(instance, path, node);
51         } catch (IOException e) {
52             throw new IllegalArgumentException("Error deserializing path and Node", e);
53         }
54     }
55
56     public static void serializeNormalizedNode(NormalizedNode<?, ?> node, DataOutput out) {
57         try {
58             out.writeBoolean(node != null);
59             if(node != null) {
60                 NormalizedNodeOutputStreamWriter streamWriter = new NormalizedNodeOutputStreamWriter(out);
61                 NormalizedNodeWriter.forStreamWriter(streamWriter).write(node);
62             }
63         } catch (IOException e) {
64             throw new IllegalArgumentException(String.format("Error serializing NormalizedNode {}",
65                     node), e);
66         }
67     }
68
69     public static NormalizedNode<?, ?> deserializeNormalizedNode(DataInput in) {
70             try {
71                 boolean present = in.readBoolean();
72                 if(present) {
73                     NormalizedNodeInputStreamReader streamReader = new NormalizedNodeInputStreamReader(in);
74                     return streamReader.readNormalizedNode();
75                 }
76             } catch (IOException e) {
77                 throw new IllegalArgumentException("Error deserializing NormalizedNode", e);
78             }
79
80         return null;
81     }
82
83     public static void serializePath(YangInstanceIdentifier path, DataOutput out) {
84         Preconditions.checkNotNull(path);
85         try {
86             NormalizedNodeOutputStreamWriter streamWriter = new NormalizedNodeOutputStreamWriter(out);
87             streamWriter.writeYangInstanceIdentifier(path);
88         } catch (IOException e) {
89             throw new IllegalArgumentException(String.format("Error serializing path {}", path), e);
90         }
91     }
92
93     public static YangInstanceIdentifier deserializePath(DataInput in) {
94         try {
95             NormalizedNodeInputStreamReader streamReader = new NormalizedNodeInputStreamReader(in);
96             return streamReader.readYangInstanceIdentifier();
97         } catch (IOException e) {
98             throw new IllegalArgumentException("Error deserializing path", e);
99         }
100     }
101 }