2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.cluster.raft.protobuff.client.messages;
11 import com.google.common.annotations.VisibleForTesting;
12 import com.google.common.base.Preconditions;
13 import com.google.protobuf.ByteString;
14 import com.google.protobuf.InvalidProtocolBufferException;
15 import java.io.IOException;
16 import java.io.Serializable;
17 import java.lang.ref.SoftReference;
18 import org.opendaylight.controller.protobuff.messages.persistent.PersistentMessages;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
23 public class CompositeModificationByteStringPayload extends Payload implements
25 private static final long serialVersionUID = 1L;
27 private ByteString byteString;
28 private SoftReference<PersistentMessages.CompositeModification> modificationReference;
29 private static final Logger LOG = LoggerFactory.getLogger(CompositeModificationByteStringPayload.class);
31 public CompositeModificationByteStringPayload(){
34 public CompositeModificationByteStringPayload(Object modification){
35 this(((PersistentMessages.CompositeModification) modification).toByteString());
36 this.modificationReference = new SoftReference<>((PersistentMessages.CompositeModification) modification);
39 private CompositeModificationByteStringPayload(ByteString byteString){
40 this.byteString = Preconditions.checkNotNull(byteString, "byteString should not be null");
43 public Object getModification(){
44 return getModificationInternal();
47 private PersistentMessages.CompositeModification getModificationInternal(){
48 if(this.modificationReference != null && this.modificationReference.get() != null){
49 return this.modificationReference.get();
52 PersistentMessages.CompositeModification compositeModification = PersistentMessages.CompositeModification.parseFrom(this.byteString);
53 this.modificationReference = new SoftReference<>(compositeModification);
54 return compositeModification;
55 } catch (InvalidProtocolBufferException e) {
56 LOG.error("Unexpected exception occurred when parsing byteString to CompositeModification", e);
64 return byteString.size();
67 private void writeObject(java.io.ObjectOutputStream stream)
69 byteString.writeTo(stream);
72 private void readObject(java.io.ObjectInputStream stream)
73 throws IOException, ClassNotFoundException {
74 byteString = ByteString.readFrom(stream);
78 public void clearModificationReference(){
79 if(this.modificationReference != null) {
80 this.modificationReference.clear();