Improve segmented journal actor metrics
[controller.git] / opendaylight / md-sal / cds-access-api / src / main / java / org / opendaylight / controller / cluster / access / commands / AbstractReadPathTransactionRequest.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies, s.r.o. 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 akka.actor.ActorRef;
13 import com.google.common.base.MoreObjects.ToStringHelper;
14 import java.io.IOException;
15 import java.io.ObjectInput;
16 import java.io.ObjectOutput;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.opendaylight.controller.cluster.access.ABIVersion;
19 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
21 import org.opendaylight.yangtools.yang.data.codec.binfmt.NormalizedNodeDataInput;
22
23 /**
24  * Abstract base class for {@link TransactionRequest}s accessing data as visible in the isolated context of a particular
25  * transaction. The path of the data being accessed is returned via {@link #getPath()}.
26  *
27  * <p>
28  * This class is visible outside of this package for the purpose of allowing common instanceof checks
29  * and simplified codepaths.
30  *
31  * @param <T> Message type
32  */
33 public abstract class AbstractReadPathTransactionRequest<T extends AbstractReadPathTransactionRequest<T>>
34         extends AbstractReadTransactionRequest<T> {
35     interface SerialForm<T extends AbstractReadPathTransactionRequest<T>>
36             extends AbstractReadTransactionRequest.SerialForm<T> {
37
38         @Override
39         default T readExternal(final ObjectInput in, final TransactionIdentifier target, final long sequence,
40                 final ActorRef replyTo, final boolean snapshotOnly) throws IOException {
41             return readExternal(in, target, sequence, replyTo, snapshotOnly,
42                 NormalizedNodeDataInput.newDataInput(in).readYangInstanceIdentifier());
43         }
44
45         @NonNull T readExternal(@NonNull ObjectInput in, @NonNull TransactionIdentifier target, long sequence,
46             @NonNull ActorRef replyTo, boolean snapshotOnly, @NonNull YangInstanceIdentifier path) throws IOException;
47
48         @Override
49         default void writeExternal(final ObjectOutput out, final T msg) throws IOException {
50             AbstractReadTransactionRequest.SerialForm.super.writeExternal(out, msg);
51             try (var nnout = msg.getVersion().getStreamVersion().newDataOutput(out)) {
52                 nnout.writeYangInstanceIdentifier(msg.getPath());
53             }
54         }
55     }
56
57     @java.io.Serial
58     private static final long serialVersionUID = 1L;
59
60     private final @NonNull YangInstanceIdentifier path;
61
62     AbstractReadPathTransactionRequest(final TransactionIdentifier identifier, final long sequence,
63         final ActorRef replyTo, final YangInstanceIdentifier path, final boolean snapshotOnly) {
64         super(identifier, sequence, replyTo, snapshotOnly);
65         this.path = requireNonNull(path);
66     }
67
68     AbstractReadPathTransactionRequest(final T request, final ABIVersion version) {
69         super(request, version);
70         this.path = request.getPath();
71     }
72
73     public final @NonNull YangInstanceIdentifier getPath() {
74         return path;
75     }
76
77     @Override
78     protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
79         return super.addToStringAttributes(toStringHelper).add("path", path);
80     }
81
82     @Override
83     protected abstract SerialForm<T> externalizableProxy(ABIVersion version);
84 }