More cds-access-api cleanup
[controller.git] / opendaylight / md-sal / cds-access-api / src / main / java / org / opendaylight / controller / cluster / access / concepts / AbstractEnvelopeProxy.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 java.io.Externalizable;
11 import java.io.IOException;
12 import java.io.ObjectInput;
13 import java.io.ObjectOutput;
14 import java.io.Serial;
15 import org.opendaylight.yangtools.concepts.WritableObjects;
16
17 abstract class AbstractEnvelopeProxy<T extends Message<?, ?>> implements Externalizable {
18     @Serial
19     private static final long serialVersionUID = 1L;
20
21     private T message;
22     private long sessionId;
23     private long txSequence;
24
25     AbstractEnvelopeProxy() {
26         // for Externalizable
27     }
28
29     AbstractEnvelopeProxy(final Envelope<T> envelope) {
30         message = envelope.getMessage();
31         txSequence = envelope.getTxSequence();
32         sessionId = envelope.getSessionId();
33     }
34
35     @Override
36     public void writeExternal(final ObjectOutput out) throws IOException {
37         WritableObjects.writeLongs(out, sessionId, txSequence);
38         out.writeObject(message);
39     }
40
41     @SuppressWarnings("unchecked")
42     @Override
43     public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
44         final byte header = WritableObjects.readLongHeader(in);
45         sessionId = WritableObjects.readFirstLong(in, header);
46         txSequence = WritableObjects.readSecondLong(in, header);
47         message = (T) in.readObject();
48     }
49
50     @SuppressWarnings("checkstyle:hiddenField")
51     abstract Envelope<T> createEnvelope(T wrappedNessage, long sessionId, long txSequence);
52
53     @Serial
54     final Object readResolve() {
55         return createEnvelope(message, sessionId, txSequence);
56     }
57 }