3d5d45e1b3310af20e7a053d110bee6aebaedce4
[controller.git] /
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>This class is visible outside of this package for the purpose of allowing common instanceof checks and simplified
28  * codepaths.
29  *
30  * @param <T> Message type
31  */
32 public abstract class AbstractReadPathTransactionRequest<T extends AbstractReadPathTransactionRequest<T>>
33         extends AbstractReadTransactionRequest<T> {
34     interface SerialForm<T extends AbstractReadPathTransactionRequest<T>>
35             extends AbstractReadTransactionRequest.SerialForm<T> {
36
37         @Override
38         default T readExternal(final ObjectInput in, final TransactionIdentifier target, final long sequence,
39                 final ActorRef replyTo, final boolean snapshotOnly) throws IOException {
40             return readExternal(in, target, sequence, replyTo, snapshotOnly,
41                 NormalizedNodeDataInput.newDataInput(in).readYangInstanceIdentifier());
42         }
43
44         @NonNull T readExternal(@NonNull ObjectInput in, @NonNull TransactionIdentifier target, long sequence,
45             @NonNull ActorRef replyTo, boolean snapshotOnly, @NonNull YangInstanceIdentifier path) throws IOException;
46
47         @Override
48         default void writeExternal(final ObjectOutput out, final T msg) throws IOException {
49             AbstractReadTransactionRequest.SerialForm.super.writeExternal(out, msg);
50             try (var nnout = msg.getVersion().getStreamVersion().newDataOutput(out)) {
51                 nnout.writeYangInstanceIdentifier(msg.getPath());
52             }
53         }
54     }
55
56     @java.io.Serial
57     private static final long serialVersionUID = 1L;
58
59     private final @NonNull YangInstanceIdentifier path;
60
61     AbstractReadPathTransactionRequest(final TransactionIdentifier identifier, final long sequence,
62         final ActorRef replyTo, final YangInstanceIdentifier path, final boolean snapshotOnly) {
63         super(identifier, sequence, replyTo, snapshotOnly);
64         this.path = requireNonNull(path);
65     }
66
67     AbstractReadPathTransactionRequest(final T request, final ABIVersion version) {
68         super(request, version);
69         this.path = request.getPath();
70     }
71
72     public final @NonNull YangInstanceIdentifier getPath() {
73         return path;
74     }
75
76     @Override
77     protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
78         return super.addToStringAttributes(toStringHelper).add("path", path);
79     }
80
81     @Override
82     protected abstract SerialForm<T> externalizableProxy(ABIVersion version);
83 }