2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.access.commands;
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 import org.opendaylight.yangtools.yang.data.impl.schema.ReusableImmutableNormalizedNodeStreamWriter;
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, ReusableImmutableNormalizedNodeStreamWriter)} methods for explicit
23 * serialization. The reason for this is that they are usually transmitted in bulk, hence it is advantageous to reuse
24 * a {@link NormalizedNodeDataOutput} instance to achieve better compression.
26 * @author Robert Varga
29 public abstract class TransactionModification {
30 static final byte TYPE_DELETE = 1;
31 static final byte TYPE_MERGE = 2;
32 static final byte TYPE_WRITE = 3;
34 private final YangInstanceIdentifier path;
36 TransactionModification(final YangInstanceIdentifier path) {
37 this.path = Preconditions.checkNotNull(path);
40 public final YangInstanceIdentifier getPath() {
45 public final String toString() {
46 return MoreObjects.toStringHelper(this).add("path", path).toString();
49 abstract byte getType();
51 void writeTo(final NormalizedNodeDataOutput out) throws IOException {
52 out.writeByte(getType());
53 out.writeYangInstanceIdentifier(path);
56 static TransactionModification readFrom(final NormalizedNodeDataInput in,
57 final ReusableImmutableNormalizedNodeStreamWriter writer) throws IOException {
58 final byte type = in.readByte();
61 return new TransactionDelete(in.readYangInstanceIdentifier());
63 return new TransactionMerge(in.readYangInstanceIdentifier(), in.readNormalizedNode(writer));
65 return new TransactionWrite(in.readYangInstanceIdentifier(), in.readNormalizedNode(writer));
67 throw new IllegalArgumentException("Unhandled type " + type);