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