44cf7171c7f902828b6d0c569a64e93431b5e537
[controller.git] / opendaylight / md-sal / cds-access-api / src / main / java / org / opendaylight / controller / cluster / access / concepts / Envelope.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.concepts;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.base.MoreObjects;
13 import java.io.Serial;
14 import java.io.Serializable;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.opendaylight.yangtools.concepts.Immutable;
17
18 public abstract class Envelope<T extends Message<?, ?>> implements Immutable, Serializable {
19     @Serial
20     private static final long serialVersionUID = 1L;
21
22     private final @NonNull T message;
23     private final long txSequence;
24     private final long sessionId;
25
26     Envelope(final T message, final long sessionId, final long txSequence) {
27         this.message = requireNonNull(message);
28         this.sessionId = sessionId;
29         this.txSequence = txSequence;
30     }
31
32     /**
33      * Get the enclosed message.
34      *
35      * @return enclose message
36      */
37     public @NonNull T getMessage() {
38         return message;
39     }
40
41     /**
42      * Get the message transmission sequence of this envelope.
43      *
44      * @return Message sequence
45      */
46     public long getTxSequence() {
47         return txSequence;
48     }
49
50     /**
51      * Get the session identifier.
52      *
53      * @return Session identifier
54      */
55     public long getSessionId() {
56         return sessionId;
57     }
58
59     @Override
60     public String toString() {
61         return MoreObjects.toStringHelper(Envelope.class).add("sessionId", Long.toHexString(sessionId))
62                 .add("txSequence", Long.toHexString(txSequence)).add("message", message).toString();
63     }
64
65     @Serial
66     final Object writeReplace() {
67         return createProxy();
68     }
69
70     abstract AbstractEnvelopeProxy<T> createProxy();
71 }