2 * Copyright (c) 2014 Brocade Communications Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.datastore.utils;
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.NormalizedNodeInputStreamReader;
21 import org.opendaylight.controller.cluster.datastore.node.utils.stream.NormalizedNodeOutputStreamWriter;
22 import org.opendaylight.controller.protobuff.messages.common.NormalizedNodeMessages;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
27 * Provides various utility methods for serialization and de-serialization.
29 * @author Thomas Pantelis
31 public final class SerializationUtils {
32 public static ThreadLocal<NormalizedNodeOutputStreamWriter> REUSABLE_WRITER_TL = new ThreadLocal<>();
33 public static ThreadLocal<NormalizedNodeInputStreamReader> REUSABLE_READER_TL = new ThreadLocal<>();
35 public static interface Applier<T> {
36 void apply(T instance, YangInstanceIdentifier path, NormalizedNode<?, ?> node);
39 private static NormalizedNodeOutputStreamWriter streamWriter(DataOutput out) throws IOException {
40 NormalizedNodeOutputStreamWriter streamWriter = REUSABLE_WRITER_TL.get();
41 if(streamWriter == null) {
42 streamWriter = new NormalizedNodeOutputStreamWriter(out);
48 private static NormalizedNodeInputStreamReader streamReader(DataInput in) throws IOException {
49 NormalizedNodeInputStreamReader streamWriter = REUSABLE_READER_TL.get();
50 if(streamWriter == null) {
51 streamWriter = new NormalizedNodeInputStreamReader(in);
57 public static void serializePathAndNode(YangInstanceIdentifier path, NormalizedNode<?, ?> node,
59 Preconditions.checkNotNull(path);
60 Preconditions.checkNotNull(node);
62 NormalizedNodeOutputStreamWriter streamWriter = streamWriter(out);
63 streamWriter.writeNormalizedNode(node);
64 streamWriter.writeYangInstanceIdentifier(path);
65 } catch (IOException e) {
66 throw new IllegalArgumentException(String.format("Error serializing path %s and Node %s",
71 public static <T> void deserializePathAndNode(DataInput in, T instance, Applier<T> applier) {
73 NormalizedNodeInputStreamReader streamReader = streamReader(in);
74 NormalizedNode<?, ?> node = streamReader.readNormalizedNode();
75 YangInstanceIdentifier path = streamReader.readYangInstanceIdentifier();
76 applier.apply(instance, path, node);
77 } catch (IOException e) {
78 throw new IllegalArgumentException("Error deserializing path and Node", e);
82 public static void serializeNormalizedNode(NormalizedNode<?, ?> node, DataOutput out) {
84 out.writeBoolean(node != null);
86 NormalizedNodeOutputStreamWriter streamWriter = streamWriter(out);
87 streamWriter.writeNormalizedNode(node);
89 } catch (IOException e) {
90 throw new IllegalArgumentException(String.format("Error serializing NormalizedNode %s",
95 public static NormalizedNode<?, ?> deserializeNormalizedNode(DataInput in) {
97 boolean present = in.readBoolean();
99 NormalizedNodeInputStreamReader streamReader = streamReader(in);
100 return streamReader.readNormalizedNode();
102 } catch (IOException e) {
103 throw new IllegalArgumentException("Error deserializing NormalizedNode", e);
109 public static NormalizedNode<?, ?> deserializeNormalizedNode(byte [] bytes) {
110 NormalizedNode<?, ?> node = null;
112 node = deserializeNormalizedNode(new DataInputStream(new ByteArrayInputStream(bytes)));
113 } catch(Exception e) {
117 // Must be from legacy protobuf serialization - try that.
119 NormalizedNodeMessages.Node serializedNode = NormalizedNodeMessages.Node.parseFrom(bytes);
120 node = new NormalizedNodeToNodeCodec(null).decode(serializedNode);
121 } catch (InvalidProtocolBufferException e) {
122 throw new IllegalArgumentException("Error deserializing NormalizedNode", e);
129 public static byte [] serializeNormalizedNode(NormalizedNode<?, ?> node) {
130 ByteArrayOutputStream bos = new ByteArrayOutputStream();
131 serializeNormalizedNode(node, new DataOutputStream(bos));
132 return bos.toByteArray();
135 public static void serializePath(YangInstanceIdentifier path, DataOutput out) {
136 Preconditions.checkNotNull(path);
138 NormalizedNodeOutputStreamWriter streamWriter = streamWriter(out);
139 streamWriter.writeYangInstanceIdentifier(path);
140 } catch (IOException e) {
141 throw new IllegalArgumentException(String.format("Error serializing path %s", path), e);
145 public static YangInstanceIdentifier deserializePath(DataInput in) {
147 NormalizedNodeInputStreamReader streamReader = streamReader(in);
148 return streamReader.readYangInstanceIdentifier();
149 } catch (IOException e) {
150 throw new IllegalArgumentException("Error deserializing path", e);