04bc63c5a5e73506dc9835ae4abf36915372bcee
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / messages / DeleteData.java
1 /*
2  * Copyright (c) 2014 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
9 package org.opendaylight.controller.cluster.datastore.messages;
10
11 import java.io.Externalizable;
12 import java.io.IOException;
13 import java.io.ObjectInput;
14 import java.io.ObjectOutput;
15 import org.opendaylight.controller.cluster.datastore.DataStoreVersions;
16 import org.opendaylight.controller.cluster.datastore.util.InstanceIdentifierUtils;
17 import org.opendaylight.controller.cluster.datastore.utils.SerializationUtils;
18 import org.opendaylight.controller.protobuff.messages.transaction.ShardTransactionMessages;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
20
21 public class DeleteData implements VersionedSerializableMessage, Externalizable {
22     private static final long serialVersionUID = 1L;
23
24     public static final Class<DeleteData> SERIALIZABLE_CLASS = DeleteData.class;
25
26     private YangInstanceIdentifier path;
27     private short version;
28
29     public DeleteData() {
30     }
31
32     public DeleteData(final YangInstanceIdentifier path) {
33         this.path = path;
34     }
35
36     public YangInstanceIdentifier getPath() {
37         return path;
38     }
39
40     public short getVersion() {
41         return version;
42     }
43
44     @Override
45     public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
46         version = in.readShort(); // Read the version - don't need to do anything with it now
47         path = SerializationUtils.deserializePath(in);
48     }
49
50     @Override
51     public void writeExternal(ObjectOutput out) throws IOException {
52         out.writeShort(version);
53         SerializationUtils.serializePath(path, out);
54     }
55
56     @Override
57     public Object toSerializable(short toVersion) {
58         if(toVersion >= DataStoreVersions.LITHIUM_VERSION) {
59             version = toVersion;
60             return this;
61         } else {
62             // To base or R1 Helium version
63             return ShardTransactionMessages.DeleteData.newBuilder().setInstanceIdentifierPathArguments(
64                     InstanceIdentifierUtils.toSerializable(path)).build();
65         }
66     }
67
68     public static DeleteData fromSerializable(final Object serializable) {
69         if(serializable instanceof DeleteData) {
70             return (DeleteData) serializable;
71         } else {
72             // From base or R1 Helium version
73             ShardTransactionMessages.DeleteData o = (ShardTransactionMessages.DeleteData) serializable;
74             return new DeleteData(InstanceIdentifierUtils.fromSerializable(o.getInstanceIdentifierPathArguments()));
75         }
76     }
77
78     public static boolean isSerializedType(Object message) {
79         return SERIALIZABLE_CLASS.isInstance(message) ||
80                message instanceof ShardTransactionMessages.DeleteData;
81     }
82 }