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 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;
20 * Provides various utility methods for serialization and de-serialization.
22 * @author Thomas Pantelis
24 public final class SerializationUtils {
25 public static ThreadLocal<NormalizedNodeOutputStreamWriter> REUSABLE_WRITER_TL = new ThreadLocal<>();
26 public static ThreadLocal<NormalizedNodeInputStreamReader> REUSABLE_READER_TL = new ThreadLocal<>();
28 public static interface Applier<T> {
29 void apply(T instance, YangInstanceIdentifier path, NormalizedNode<?, ?> node);
32 private static NormalizedNodeOutputStreamWriter streamWriter(DataOutput out) throws IOException {
33 NormalizedNodeOutputStreamWriter streamWriter = REUSABLE_WRITER_TL.get();
34 if(streamWriter == null) {
35 streamWriter = new NormalizedNodeOutputStreamWriter(out);
41 private static NormalizedNodeInputStreamReader streamReader(DataInput in) throws IOException {
42 NormalizedNodeInputStreamReader streamWriter = REUSABLE_READER_TL.get();
43 if(streamWriter == null) {
44 streamWriter = new NormalizedNodeInputStreamReader(in);
50 public static void serializePathAndNode(YangInstanceIdentifier path, NormalizedNode<?, ?> node,
52 Preconditions.checkNotNull(path);
53 Preconditions.checkNotNull(node);
55 NormalizedNodeOutputStreamWriter streamWriter = streamWriter(out);
56 streamWriter.writeNormalizedNode(node);
57 streamWriter.writeYangInstanceIdentifier(path);
58 } catch (IOException e) {
59 throw new IllegalArgumentException(String.format("Error serializing path %s and Node %s",
64 public static <T> void deserializePathAndNode(DataInput in, T instance, Applier<T> applier) {
66 NormalizedNodeInputStreamReader streamReader = streamReader(in);
67 NormalizedNode<?, ?> node = streamReader.readNormalizedNode();
68 YangInstanceIdentifier path = streamReader.readYangInstanceIdentifier();
69 applier.apply(instance, path, node);
70 } catch (IOException e) {
71 throw new IllegalArgumentException("Error deserializing path and Node", e);
75 public static void serializeNormalizedNode(NormalizedNode<?, ?> node, DataOutput out) {
77 out.writeBoolean(node != null);
79 NormalizedNodeOutputStreamWriter streamWriter = streamWriter(out);
80 streamWriter.writeNormalizedNode(node);
82 } catch (IOException e) {
83 throw new IllegalArgumentException(String.format("Error serializing NormalizedNode %s",
88 public static NormalizedNode<?, ?> deserializeNormalizedNode(DataInput in) {
90 boolean present = in.readBoolean();
92 NormalizedNodeInputStreamReader streamReader = streamReader(in);
93 return streamReader.readNormalizedNode();
95 } catch (IOException e) {
96 throw new IllegalArgumentException("Error deserializing NormalizedNode", e);
102 public static void serializePath(YangInstanceIdentifier path, DataOutput out) {
103 Preconditions.checkNotNull(path);
105 NormalizedNodeOutputStreamWriter streamWriter = streamWriter(out);
106 streamWriter.writeYangInstanceIdentifier(path);
107 } catch (IOException e) {
108 throw new IllegalArgumentException(String.format("Error serializing path %s", path), e);
112 public static YangInstanceIdentifier deserializePath(DataInput in) {
114 NormalizedNodeInputStreamReader streamReader = streamReader(in);
115 return streamReader.readYangInstanceIdentifier();
116 } catch (IOException e) {
117 throw new IllegalArgumentException("Error deserializing path", e);