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.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;
30 * Provides various utility methods for serialization and de-serialization.
32 * @author Thomas Pantelis
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<>();
38 public interface Applier<T> {
39 void apply(T instance, YangInstanceIdentifier path, NormalizedNode<?, ?> node);
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);
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);
60 public static void serializePathAndNode(YangInstanceIdentifier path, NormalizedNode<?, ?> node,
62 Preconditions.checkNotNull(path);
63 Preconditions.checkNotNull(node);
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",
74 public static <T> void deserializePathAndNode(DataInput in, T instance, Applier<T> applier) {
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);
85 private static NormalizedNode<?, ?> tryDeserializeNormalizedNode(DataInput in) throws IOException {
86 boolean present = in.readBoolean();
88 NormalizedNodeDataInput streamReader = streamReader(in);
89 return streamReader.readNormalizedNode();
95 public static NormalizedNode<?, ?> deserializeNormalizedNode(DataInput in) {
97 return tryDeserializeNormalizedNode(in);
98 } catch (IOException e) {
99 throw new IllegalArgumentException("Error deserializing NormalizedNode", e);
103 public static NormalizedNode<?, ?> deserializeNormalizedNode(byte [] bytes) {
105 return tryDeserializeNormalizedNode(new DataInputStream(new ByteArrayInputStream(bytes)));
106 } catch (InvalidNormalizedNodeStreamException e) {
107 // Probably from legacy protobuf serialization - try that.
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);
114 } catch (IOException e) {
115 throw new IllegalArgumentException("Error deserializing NormalizedNode", e);
119 public static void serializeNormalizedNode(NormalizedNode<?, ?> node, DataOutput out) {
121 out.writeBoolean(node != null);
123 NormalizedNodeDataOutput streamWriter = streamWriter(out);
124 streamWriter.writeNormalizedNode(node);
126 } catch (IOException e) {
127 throw new IllegalArgumentException(String.format("Error serializing NormalizedNode %s",
132 public static byte [] serializeNormalizedNode(NormalizedNode<?, ?> node) {
133 ByteArrayOutputStream bos = new ByteArrayOutputStream();
134 serializeNormalizedNode(node, new DataOutputStream(bos));
135 return bos.toByteArray();
138 public static void serializePath(YangInstanceIdentifier path, DataOutput out) {
139 Preconditions.checkNotNull(path);
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);
148 public static YangInstanceIdentifier deserializePath(DataInput in) {
150 NormalizedNodeDataInput streamReader = streamReader(in);
151 return streamReader.readYangInstanceIdentifier();
152 } catch (IOException e) {
153 throw new IllegalArgumentException("Error deserializing path", e);