Improve segmented journal actor metrics
[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.base.MoreObjects;
13 import java.io.IOException;
14 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
15 import org.opendaylight.yangtools.yang.data.api.schema.stream.ReusableStreamReceiver;
16 import org.opendaylight.yangtools.yang.data.codec.binfmt.NormalizedNodeDataInput;
17 import org.opendaylight.yangtools.yang.data.codec.binfmt.NormalizedNodeDataOutput;
18
19 /**
20  * An individual modification of a transaction's state. This class and its subclasses are not serializable, but rather
21  * expose {@link #writeTo(NormalizedNodeDataOutput)} and
22  * {@link #readFrom(NormalizedNodeDataInput, ReusableStreamReceiver)} methods for explicit serialization. The reason for
23  * this is that they are usually transmitted in bulk, hence it is advantageous to reuse
24  * a {@link NormalizedNodeDataOutput} instance to achieve better compression.
25  */
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 = requireNonNull(path);
35     }
36
37     public final YangInstanceIdentifier getPath() {
38         return path;
39     }
40
41     @Override
42     public final String toString() {
43         return MoreObjects.toStringHelper(this).add("path", path).toString();
44     }
45
46     abstract byte getType();
47
48     void writeTo(final NormalizedNodeDataOutput out) throws IOException {
49         out.writeByte(getType());
50         out.writeYangInstanceIdentifier(path);
51     }
52
53     static TransactionModification readFrom(final NormalizedNodeDataInput in, final ReusableStreamReceiver writer)
54             throws IOException {
55         final byte type = in.readByte();
56         return switch (type) {
57             case TYPE_DELETE -> new TransactionDelete(in.readYangInstanceIdentifier());
58             case TYPE_MERGE -> new TransactionMerge(in.readYangInstanceIdentifier(), in.readNormalizedNode(writer));
59             case TYPE_WRITE -> new TransactionWrite(in.readYangInstanceIdentifier(), in.readNormalizedNode(writer));
60             default -> throw new IllegalArgumentException("Unhandled type " + type);
61         };
62     }
63 }