BUG-5280: separate request sequence and transmit sequence
[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 com.google.common.base.MoreObjects;
11 import com.google.common.base.Preconditions;
12 import java.io.Serializable;
13 import org.opendaylight.yangtools.concepts.Immutable;
14
15 public abstract class Envelope<T extends Message<?, ?>> implements Immutable, Serializable {
16     private static final long serialVersionUID = 1L;
17
18     private final T message;
19     private final long txSequence;
20     private final long sessionId;
21
22     Envelope(final T message, final long sessionId, final long txSequence) {
23         this.message = Preconditions.checkNotNull(message);
24         this.sessionId = sessionId;
25         this.txSequence = txSequence;
26     }
27
28     /**
29      * Get the enclosed message
30      *
31      * @return enclose message
32      */
33     public T getMessage() {
34         return message;
35     }
36
37     /**
38      * Get the message transmission sequence of this envelope.
39      *
40      * @return Message sequence
41      */
42     public long getTxSequence() {
43         return txSequence;
44     }
45
46     /**
47      * Get the session identifier.
48      *
49      * @return Session identifier
50      */
51     public long getSessionId() {
52         return sessionId;
53     }
54
55     @Override
56     public String toString() {
57         return MoreObjects.toStringHelper(Envelope.class).add("sessionId", Long.toHexString(sessionId))
58                 .add("txSequence", Long.toHexString(txSequence)).add("message", message).toString();
59     }
60
61     final Object writeReplace() {
62         return createProxy();
63     }
64
65     abstract AbstractEnvelopeProxy<T> createProxy();
66 }