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 {
26 public static interface Applier<T> {
27 void apply(T instance, YangInstanceIdentifier path, NormalizedNode<?, ?> node);
30 public static void serializePathAndNode(YangInstanceIdentifier path, NormalizedNode<?, ?> node,
32 Preconditions.checkNotNull(path);
33 Preconditions.checkNotNull(node);
35 NormalizedNodeOutputStreamWriter streamWriter = new NormalizedNodeOutputStreamWriter(out);
36 NormalizedNodeWriter.forStreamWriter(streamWriter).write(node);
37 streamWriter.writeYangInstanceIdentifier(path);
38 } catch (IOException e) {
39 throw new IllegalArgumentException(String.format("Error serializing path {} and Node {}",
44 public static <T> void deserializePathAndNode(DataInput in, T instance, Applier<T> applier) {
46 NormalizedNodeInputStreamReader streamReader = new NormalizedNodeInputStreamReader(in);
47 NormalizedNode<?, ?> node = streamReader.readNormalizedNode();
48 YangInstanceIdentifier path = streamReader.readYangInstanceIdentifier();
49 applier.apply(instance, path, node);
50 } catch (IOException e) {
51 throw new IllegalArgumentException("Error deserializing path and Node", e);
55 public static void serializeNormalizedNode(NormalizedNode<?, ?> node, DataOutput out) {
57 out.writeBoolean(node != null);
59 NormalizedNodeOutputStreamWriter streamWriter = new NormalizedNodeOutputStreamWriter(out);
60 NormalizedNodeWriter.forStreamWriter(streamWriter).write(node);
62 } catch (IOException e) {
63 throw new IllegalArgumentException(String.format("Error serializing NormalizedNode {}",
68 public static NormalizedNode<?, ?> deserializeNormalizedNode(DataInput in) {
70 boolean present = in.readBoolean();
72 NormalizedNodeInputStreamReader streamReader = new NormalizedNodeInputStreamReader(in);
73 return streamReader.readNormalizedNode();
75 } catch (IOException e) {
76 throw new IllegalArgumentException("Error deserializing NormalizedNode", e);