BUG-5280: expand ShardDataTree to cover transaction mechanics
[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 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     public static void toFile(File file, DataTreeModification modification) {
35         try(FileOutputStream outStream = new FileOutputStream(file)) {
36             modification.applyToCursor(new DataTreeModificationOutputCursor(new DataOutputStream(outStream)));
37         } catch(Exception e) {
38             LOG.error("Error writing DataTreeModification to file {}", file, e);
39         }
40     }
41
42     private static class DataTreeModificationOutputCursor extends AbstractDataTreeModificationCursor {
43         private final DataOutputStream output;
44
45         DataTreeModificationOutputCursor(DataOutputStream output) {
46             this.output = output;
47         }
48
49         @Override
50         public void delete(PathArgument child) {
51             try {
52                 output.write("\nDELETE -> ".getBytes());
53                 output.write(current().node(child).toString().getBytes());
54                 output.writeByte('\n');
55             } catch(IOException e) {
56                 Throwables.propagate(e);
57             }
58         }
59
60         @Override
61         public void merge(PathArgument child, NormalizedNode<?, ?> data) {
62             outputPathAndNode("MERGE", child, data);
63         }
64
65         @Override
66         public void write(PathArgument child, NormalizedNode<?, ?> data) {
67             outputPathAndNode("WRITE", child, data);
68         }
69
70         private void outputPathAndNode(String name, PathArgument child, NormalizedNode<?, ?> data) {
71             try {
72                 output.writeByte('\n');
73                 output.write(name.getBytes());
74                 output.write(" -> ".getBytes());
75                 output.write(current().node(child).toString().getBytes());
76                 output.write(": \n".getBytes());
77                 NormalizedNodeXMLOutput.toStream(output, data);
78                 output.writeByte('\n');
79             } catch(IOException | XMLStreamException e) {
80                 Throwables.propagate(e);
81             }
82         }
83     }
84 }