318df05d92fce4bd55409007b362d1960b48eefd
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / persisted / DisableTrackingPayload.java
1 /*
2  * Copyright (c) 2019 PANTHEON.tech, 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.datastore.persisted;
9
10 import com.google.common.io.ByteArrayDataOutput;
11 import com.google.common.io.ByteStreams;
12 import java.io.DataInput;
13 import java.io.IOException;
14 import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
17
18 public final class DisableTrackingPayload extends AbstractIdentifiablePayload<ClientIdentifier> {
19     private static final class Proxy extends AbstractProxy<ClientIdentifier> {
20         @java.io.Serial
21         private static final long serialVersionUID = -5490519942445085251L;
22
23         @SuppressWarnings("checkstyle:RedundantModifier")
24         public Proxy() {
25             // For Externalizable
26         }
27
28         Proxy(final byte[] serialized) {
29             super(serialized);
30         }
31
32         @Override
33         protected ClientIdentifier readIdentifier(final DataInput in) throws IOException {
34             return ClientIdentifier.readFrom(in);
35         }
36
37         @Override
38         protected DisableTrackingPayload createObject(final ClientIdentifier identifier,
39                 final byte[] serialized) {
40             return new DisableTrackingPayload(identifier, serialized);
41         }
42     }
43
44     private static final Logger LOG = LoggerFactory.getLogger(DisableTrackingPayload.class);
45     private static final long serialVersionUID = 1L;
46     private static final int PROXY_SIZE = externalizableProxySize(Proxy::new);
47
48     DisableTrackingPayload(final ClientIdentifier clientId, final byte[] serialized) {
49         super(clientId, serialized);
50     }
51
52     public static DisableTrackingPayload create(final ClientIdentifier clientId,
53             final int initialSerializedBufferCapacity) {
54         final ByteArrayDataOutput out = ByteStreams.newDataOutput(initialSerializedBufferCapacity);
55         try {
56             clientId.writeTo(out);
57         } catch (IOException e) {
58             // This should never happen
59             LOG.error("Failed to serialize {}", clientId, e);
60             throw new IllegalStateException("Failed to serialize " + clientId, e);
61         }
62         return new DisableTrackingPayload(clientId, out.toByteArray());
63     }
64
65     @Override
66     protected Proxy externalizableProxy(final byte[] serialized) {
67         return new Proxy(serialized);
68     }
69
70     @Override
71     protected int externalizableProxySize() {
72         return PROXY_SIZE;
73     }
74 }