dde7e60a9237c2d1a52112ebb62901d2f86514f2
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / utils / DataTreeModificationOutput.java
1 /*
2  * Copyright (c) 2016 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.cluster.datastore.utils;
9
10 import com.google.common.base.Throwables;
11 import java.io.DataOutputStream;
12 import java.io.File;
13 import java.io.FileOutputStream;
14 import java.io.IOException;
15 import java.nio.charset.StandardCharsets;
16 import javax.xml.stream.XMLStreamException;
17 import org.opendaylight.controller.cluster.datastore.util.AbstractDataTreeModificationCursor;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
19 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
20 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 /**
25  * Utility class to output DataTreeModifications in readable format.
26  *
27  * @author Thomas Pantelis
28  */
29 public final class DataTreeModificationOutput {
30     private static final Logger LOG = LoggerFactory.getLogger(DataTreeModificationOutput.class);
31
32     private DataTreeModificationOutput() {
33     }
34
35     @SuppressWarnings("checkstyle:IllegalCatch")
36     public static void toFile(File file, DataTreeModification modification) {
37         try (FileOutputStream outStream = new FileOutputStream(file)) {
38             modification.applyToCursor(new DataTreeModificationOutputCursor(new DataOutputStream(outStream)));
39         } catch (IOException | RuntimeException e) {
40             LOG.error("Error writing DataTreeModification to file {}", file, e);
41         }
42     }
43
44     private static class DataTreeModificationOutputCursor extends AbstractDataTreeModificationCursor {
45         private final DataOutputStream output;
46
47         DataTreeModificationOutputCursor(DataOutputStream output) {
48             this.output = output;
49         }
50
51         @Override
52         public void delete(PathArgument child) {
53             try {
54                 output.write("\nDELETE -> ".getBytes(StandardCharsets.UTF_8));
55                 output.write(current().node(child).toString().getBytes(StandardCharsets.UTF_8));
56                 output.writeByte('\n');
57             } catch (IOException e) {
58                 Throwables.propagate(e);
59             }
60         }
61
62         @Override
63         public void merge(PathArgument child, NormalizedNode<?, ?> data) {
64             outputPathAndNode("MERGE", child, data);
65         }
66
67         @Override
68         public void write(PathArgument child, NormalizedNode<?, ?> data) {
69             outputPathAndNode("WRITE", child, data);
70         }
71
72         private void outputPathAndNode(String name, PathArgument child, NormalizedNode<?, ?> data) {
73             try {
74                 output.writeByte('\n');
75                 output.write(name.getBytes(StandardCharsets.UTF_8));
76                 output.write(" -> ".getBytes(StandardCharsets.UTF_8));
77                 output.write(current().node(child).toString().getBytes(StandardCharsets.UTF_8));
78                 output.write(": \n".getBytes(StandardCharsets.UTF_8));
79                 NormalizedNodeXMLOutput.toStream(output, data);
80                 output.writeByte('\n');
81             } catch (IOException | XMLStreamException e) {
82                 Throwables.propagate(e);
83             }
84         }
85     }
86 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.