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 static 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 public static void serializeNormalizedNode(NormalizedNode<?, ?> node, DataOutput out) {
87 out.writeBoolean(node != null);
89 NormalizedNodeDataOutput streamWriter = streamWriter(out);
90 streamWriter.writeNormalizedNode(node);
92 } catch (IOException e) {
93 throw new IllegalArgumentException(String.format("Error serializing NormalizedNode %s",
98 public static NormalizedNode<?, ?> deserializeNormalizedNode(DataInput in) {
100 return tryDeserializeNormalizedNode(in);
101 } catch (IOException e) {
102 throw new IllegalArgumentException("Error deserializing NormalizedNode", e);
106 private static NormalizedNode<?, ?> tryDeserializeNormalizedNode(DataInput in) throws IOException {
107 boolean present = in.readBoolean();
109 NormalizedNodeDataInput streamReader = streamReader(in);
110 return streamReader.readNormalizedNode();
116 public static NormalizedNode<?, ?> deserializeNormalizedNode(byte [] bytes) {
117 NormalizedNode<?, ?> node = null;
119 node = tryDeserializeNormalizedNode(new DataInputStream(new ByteArrayInputStream(bytes)));
120 } catch(InvalidNormalizedNodeStreamException e) {
121 // Probably from legacy protobuf serialization - try that.
123 NormalizedNodeMessages.Node serializedNode = NormalizedNodeMessages.Node.parseFrom(bytes);
124 node = new NormalizedNodeToNodeCodec(null).decode(serializedNode);
125 } catch (InvalidProtocolBufferException e2) {
126 throw new IllegalArgumentException("Error deserializing NormalizedNode", e);
128 } catch (IOException e) {
129 throw new IllegalArgumentException("Error deserializing NormalizedNode", e);
135 public static byte [] serializeNormalizedNode(NormalizedNode<?, ?> node) {
136 ByteArrayOutputStream bos = new ByteArrayOutputStream();
137 serializeNormalizedNode(node, new DataOutputStream(bos));
138 return bos.toByteArray();
141 public static void serializePath(YangInstanceIdentifier path, DataOutput out) {
142 Preconditions.checkNotNull(path);
144 NormalizedNodeDataOutput streamWriter = streamWriter(out);
145 streamWriter.writeYangInstanceIdentifier(path);
146 } catch (IOException e) {
147 throw new IllegalArgumentException(String.format("Error serializing path %s", path), e);
151 public static YangInstanceIdentifier deserializePath(DataInput in) {
153 NormalizedNodeDataInput streamReader = streamReader(in);
154 return streamReader.readYangInstanceIdentifier();
155 } catch (IOException e) {
156 throw new IllegalArgumentException("Error deserializing path", e);