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

©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.