e4168d530033c64c5baa85d25b4e805ca51de988
[controller.git] / opendaylight / md-sal / cds-access-api / src / main / java / org / opendaylight / controller / cluster / access / commands / TransactionModification.java
1 /*
2  * Copyright (c) 2016 Cisco 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.access.commands;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.annotations.Beta;
13 import com.google.common.base.MoreObjects;
14 import java.io.IOException;
15 import org.opendaylight.controller.cluster.datastore.node.utils.stream.NormalizedNodeDataInput;
16 import org.opendaylight.controller.cluster.datastore.node.utils.stream.NormalizedNodeDataOutput;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
18 import org.opendaylight.yangtools.yang.data.impl.schema.ReusableImmutableNormalizedNodeStreamWriter;
19
20 /**
21  * An individual modification of a transaction's state. This class and its subclasses are not serializable, but rather
22  * expose {@link #writeTo(NormalizedNodeDataOutput)} and
23  * {@link #readFrom(NormalizedNodeDataInput, ReusableImmutableNormalizedNodeStreamWriter)} methods for explicit
24  * serialization. The reason for this is that they are usually transmitted in bulk, hence it is advantageous to reuse
25  * a {@link NormalizedNodeDataOutput} instance to achieve better compression.
26  *
27  * @author Robert Varga
28  */
29 @Beta
30 public abstract class TransactionModification {
31     static final byte TYPE_DELETE = 1;
32     static final byte TYPE_MERGE = 2;
33     static final byte TYPE_WRITE = 3;
34
35     private final YangInstanceIdentifier path;
36
37     TransactionModification(final YangInstanceIdentifier path) {
38         this.path = requireNonNull(path);
39     }
40
41     public final YangInstanceIdentifier getPath() {
42         return path;
43     }
44
45     @Override
46     public final String toString() {
47         return MoreObjects.toStringHelper(this).add("path", path).toString();
48     }
49
50     abstract byte getType();
51
52     void writeTo(final NormalizedNodeDataOutput out) throws IOException {
53         out.writeByte(getType());
54         out.writeYangInstanceIdentifier(path);
55     }
56
57     static TransactionModification readFrom(final NormalizedNodeDataInput in,
58             final ReusableImmutableNormalizedNodeStreamWriter writer) throws IOException {
59         final byte type = in.readByte();
60         switch (type) {
61             case TYPE_DELETE:
62                 return new TransactionDelete(in.readYangInstanceIdentifier());
63             case TYPE_MERGE:
64                 return new TransactionMerge(in.readYangInstanceIdentifier(), in.readNormalizedNode(writer));
65             case TYPE_WRITE:
66                 return new TransactionWrite(in.readYangInstanceIdentifier(), in.readNormalizedNode(writer));
67             default:
68                 throw new IllegalArgumentException("Unhandled type " + type);
69         }
70     }
71 }