29dd0725245e761a2551a2cf00bab41b6c6e35f8
[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         @SuppressWarnings("checkstyle:RedundantModifier")
21         public Proxy() {
22             // For Externalizable
23         }
24
25         Proxy(final byte[] serialized) {
26             super(serialized);
27         }
28
29         @Override
30         protected ClientIdentifier readIdentifier(final DataInput in) throws IOException {
31             return ClientIdentifier.readFrom(in);
32         }
33
34         @Override
35         protected DisableTrackingPayload createObject(final ClientIdentifier identifier,
36                 final byte[] serialized) {
37             return new DisableTrackingPayload(identifier, serialized);
38         }
39     }
40
41     private static final Logger LOG = LoggerFactory.getLogger(DisableTrackingPayload.class);
42     private static final long serialVersionUID = 1L;
43
44     DisableTrackingPayload(final ClientIdentifier clientId, final byte[] serialized) {
45         super(clientId, serialized);
46     }
47
48     public static DisableTrackingPayload create(final ClientIdentifier clientId,
49             final int initialSerializedBufferCapacity) {
50         final ByteArrayDataOutput out = ByteStreams.newDataOutput(initialSerializedBufferCapacity);
51         try {
52             clientId.writeTo(out);
53         } catch (IOException e) {
54             // This should never happen
55             LOG.error("Failed to serialize {}", clientId, e);
56             throw new RuntimeException("Failed to serialize " + clientId, e);
57         }
58         return new DisableTrackingPayload(clientId, out.toByteArray());
59     }
60
61     @Override
62     protected Proxy externalizableProxy(final byte[] serialized) {
63         return new Proxy(serialized);
64     }
65 }