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