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;
18 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
21 * Provides various utility methods for serialization and de-serialization.
23 * @author Thomas Pantelis
25 public final class SerializationUtils {
27 public static interface Applier<T> {
28 void apply(T instance, YangInstanceIdentifier path, NormalizedNode<?, ?> node);
31 public static void serializePathAndNode(YangInstanceIdentifier path, NormalizedNode<?, ?> node,
33 Preconditions.checkNotNull(path);
34 Preconditions.checkNotNull(node);
36 NormalizedNodeOutputStreamWriter streamWriter = new NormalizedNodeOutputStreamWriter(out);
37 NormalizedNodeWriter.forStreamWriter(streamWriter).write(node);
38 streamWriter.writeYangInstanceIdentifier(path);
39 } catch (IOException e) {
40 throw new IllegalArgumentException(String.format("Error serializing path {} and Node {}",
45 public static <T> void deserializePathAndNode(DataInput in, T instance, Applier<T> applier) {
47 NormalizedNodeInputStreamReader streamReader = new NormalizedNodeInputStreamReader(in);
48 NormalizedNode<?, ?> node = streamReader.readNormalizedNode();
49 YangInstanceIdentifier path = streamReader.readYangInstanceIdentifier();
50 applier.apply(instance, path, node);
51 } catch (IOException e) {
52 throw new IllegalArgumentException("Error deserializing path and Node", e);
56 public static void serializeNormalizedNode(NormalizedNode<?, ?> node, DataOutput out) {
58 out.writeBoolean(node != null);
60 NormalizedNodeOutputStreamWriter streamWriter = new NormalizedNodeOutputStreamWriter(out);
61 NormalizedNodeWriter.forStreamWriter(streamWriter).write(node);
63 } catch (IOException e) {
64 throw new IllegalArgumentException(String.format("Error serializing NormalizedNode {}",
69 public static NormalizedNode<?, ?> deserializeNormalizedNode(DataInput in) {
71 boolean present = in.readBoolean();
73 NormalizedNodeInputStreamReader streamReader = new NormalizedNodeInputStreamReader(in);
74 return streamReader.readNormalizedNode();
76 } catch (IOException e) {
77 throw new IllegalArgumentException("Error deserializing NormalizedNode", e);
83 public static void serializePath(YangInstanceIdentifier path, DataOutput out) {
84 Preconditions.checkNotNull(path);
86 NormalizedNodeOutputStreamWriter streamWriter = new NormalizedNodeOutputStreamWriter(out);
87 streamWriter.writeYangInstanceIdentifier(path);
88 } catch (IOException e) {
89 throw new IllegalArgumentException(String.format("Error serializing path {}", path), e);
93 public static YangInstanceIdentifier deserializePath(DataInput in) {
95 NormalizedNodeInputStreamReader streamReader = new NormalizedNodeInputStreamReader(in);
96 return streamReader.readYangInstanceIdentifier();
97 } catch (IOException e) {
98 throw new IllegalArgumentException("Error deserializing path", e);