Improve segmented journal actor metrics
[controller.git] / opendaylight / md-sal / cds-access-api / src / main / java / org / opendaylight / controller / cluster / access / commands / ModifyTransactionRequest.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 akka.actor.ActorRef;
11 import com.google.common.base.MoreObjects.ToStringHelper;
12 import com.google.common.collect.ImmutableList;
13 import java.io.IOException;
14 import java.io.ObjectInput;
15 import java.io.ObjectInputStream;
16 import java.io.ObjectOutput;
17 import java.io.ObjectOutputStream;
18 import java.io.ObjectStreamException;
19 import java.util.ArrayList;
20 import java.util.List;
21 import java.util.Optional;
22 import org.opendaylight.controller.cluster.access.ABIVersion;
23 import org.opendaylight.controller.cluster.access.concepts.SliceableMessage;
24 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
25 import org.opendaylight.yangtools.yang.data.codec.binfmt.NormalizedNodeDataInput;
26 import org.opendaylight.yangtools.yang.data.impl.schema.ReusableImmutableNormalizedNodeStreamWriter;
27
28 /**
29  * A transaction request to apply a particular set of operations on top of the current transaction. This message is
30  * used to also finish a transaction by specifying a {@link PersistenceProtocol}.
31  */
32 public final class ModifyTransactionRequest extends TransactionRequest<ModifyTransactionRequest>
33         implements SliceableMessage {
34     interface SerialForm extends TransactionRequest.SerialForm<ModifyTransactionRequest> {
35
36
37         @Override
38         default ModifyTransactionRequest readExternal(final ObjectInput in, final TransactionIdentifier target,
39                 final long sequence, final ActorRef replyTo) throws IOException {
40
41             final var protocol = Optional.ofNullable(PersistenceProtocol.readFrom(in));
42             final int size = in.readInt();
43             final List<TransactionModification> modifications;
44             if (size != 0) {
45                 modifications = new ArrayList<>(size);
46                 final var nnin = NormalizedNodeDataInput.newDataInput(in);
47                 final var writer = ReusableImmutableNormalizedNodeStreamWriter.create();
48                 for (int i = 0; i < size; ++i) {
49                     modifications.add(TransactionModification.readFrom(nnin, writer));
50                 }
51             } else {
52                 modifications = ImmutableList.of();
53             }
54
55             return new ModifyTransactionRequest(target, sequence, replyTo, modifications, protocol.orElse(null));
56         }
57
58         @Override
59         default void writeExternal(final ObjectOutput out, final ModifyTransactionRequest msg) throws IOException {
60             TransactionRequest.SerialForm.super.writeExternal(out, msg);
61
62             out.writeByte(PersistenceProtocol.byteValue(msg.getPersistenceProtocol().orElse(null)));
63
64             final var modifications = msg.getModifications();
65             out.writeInt(modifications.size());
66             if (!modifications.isEmpty()) {
67                 try (var nnout = msg.getVersion().getStreamVersion().newDataOutput(out)) {
68                     for (var op : modifications) {
69                         op.writeTo(nnout);
70                     }
71                 }
72             }
73         }
74     }
75
76     @java.io.Serial
77     private static final long serialVersionUID = 1L;
78
79     private final List<TransactionModification> modifications;
80     private final PersistenceProtocol protocol;
81
82     private ModifyTransactionRequest(final ModifyTransactionRequest request, final ABIVersion version) {
83         super(request, version);
84         modifications = request.modifications;
85         protocol = request.protocol;
86     }
87
88     ModifyTransactionRequest(final TransactionIdentifier target, final long sequence, final ActorRef replyTo,
89         final List<TransactionModification> modifications, final PersistenceProtocol protocol) {
90         super(target, sequence, replyTo);
91         this.modifications = ImmutableList.copyOf(modifications);
92         this.protocol = protocol;
93     }
94
95     public Optional<PersistenceProtocol> getPersistenceProtocol() {
96         return Optional.ofNullable(protocol);
97     }
98
99     public List<TransactionModification> getModifications() {
100         return modifications;
101     }
102
103     @Override
104     protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
105         return super.addToStringAttributes(toStringHelper).add("modifications", modifications.size())
106                 .add("protocol", protocol);
107     }
108
109     @Override
110     protected SerialForm externalizableProxy(final ABIVersion version) {
111         return new MTR(this);
112     }
113
114     @Override
115     protected ModifyTransactionRequest cloneAsVersion(final ABIVersion version) {
116         return new ModifyTransactionRequest(this, version);
117     }
118
119     @java.io.Serial
120     private void readObject(final ObjectInputStream stream) throws IOException, ClassNotFoundException {
121         throwNSE();
122     }
123
124     @java.io.Serial
125     private void readObjectNoData() throws ObjectStreamException {
126         throwNSE();
127     }
128
129     @java.io.Serial
130     private void writeObject(final ObjectOutputStream stream) throws IOException {
131         throwNSE();
132     }
133 }