Merge changes I3e404877,Ida2a5c32,I9e6ce426,I6a4b90f6,I79717533
[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     public static interface Applier<T> {
27         void apply(T instance, YangInstanceIdentifier path, NormalizedNode<?, ?> node);
28     }
29
30     public static void serializePathAndNode(YangInstanceIdentifier path, NormalizedNode<?, ?> node,
31             DataOutput out) {
32         Preconditions.checkNotNull(path);
33         Preconditions.checkNotNull(node);
34         try {
35             NormalizedNodeOutputStreamWriter streamWriter = new NormalizedNodeOutputStreamWriter(out);
36             NormalizedNodeWriter.forStreamWriter(streamWriter).write(node);
37             streamWriter.writeYangInstanceIdentifier(path);
38         } catch (IOException e) {
39             throw new IllegalArgumentException(String.format("Error serializing path {} and Node {}",
40                     path, node), e);
41         }
42     }
43
44     public static <T> void deserializePathAndNode(DataInput in, T instance, Applier<T> applier) {
45         try {
46             NormalizedNodeInputStreamReader streamReader = new NormalizedNodeInputStreamReader(in);
47             NormalizedNode<?, ?> node = streamReader.readNormalizedNode();
48             YangInstanceIdentifier path = streamReader.readYangInstanceIdentifier();
49             applier.apply(instance, path, node);
50         } catch (IOException e) {
51             throw new IllegalArgumentException("Error deserializing path and Node", e);
52         }
53     }
54
55     public static void serializeNormalizedNode(NormalizedNode<?, ?> node, DataOutput out) {
56         try {
57             out.writeBoolean(node != null);
58             if(node != null) {
59                 NormalizedNodeOutputStreamWriter streamWriter = new NormalizedNodeOutputStreamWriter(out);
60                 NormalizedNodeWriter.forStreamWriter(streamWriter).write(node);
61             }
62         } catch (IOException e) {
63             throw new IllegalArgumentException(String.format("Error serializing NormalizedNode {}",
64                     node), e);
65         }
66     }
67
68     public static NormalizedNode<?, ?> deserializeNormalizedNode(DataInput in) {
69             try {
70                 boolean present = in.readBoolean();
71                 if(present) {
72                     NormalizedNodeInputStreamReader streamReader = new NormalizedNodeInputStreamReader(in);
73                     return streamReader.readNormalizedNode();
74                 }
75             } catch (IOException e) {
76                 throw new IllegalArgumentException("Error deserializing NormalizedNode", e);
77             }
78
79         return null;
80     }
81 }