Merge "BUG 2412 - restconf @GET getModule(uri) method migration"
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / modification / MutableCompositeModification.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.modification;
10
11 import java.io.IOException;
12 import java.io.ObjectInput;
13 import java.io.ObjectOutput;
14 import java.util.ArrayList;
15 import java.util.List;
16 import org.opendaylight.controller.cluster.datastore.DataStoreVersions;
17 import org.opendaylight.controller.cluster.datastore.node.utils.stream.NormalizedNodeInputStreamReader;
18 import org.opendaylight.controller.cluster.datastore.node.utils.stream.NormalizedNodeOutputStreamWriter;
19 import org.opendaylight.controller.cluster.datastore.utils.SerializationUtils;
20 import org.opendaylight.controller.protobuff.messages.persistent.PersistentMessages;
21 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
22
23 /**
24  * MutableCompositeModification is just a mutable version of a
25  * CompositeModification {@link org.opendaylight.controller.cluster.datastore.modification.MutableCompositeModification#addModification(Modification)}
26  */
27 public class MutableCompositeModification implements CompositeModification {
28     private static final long serialVersionUID = 1L;
29
30     private final List<Modification> modifications = new ArrayList<>();
31     private short version;
32
33     public MutableCompositeModification() {
34         this(DataStoreVersions.CURRENT_VERSION);
35     }
36
37     public MutableCompositeModification(short version) {
38         this.version = version;
39     }
40
41     @Override
42     public void apply(DOMStoreWriteTransaction transaction) {
43         for (Modification modification : modifications) {
44             modification.apply(transaction);
45         }
46     }
47
48     @Override
49     public byte getType() {
50         return COMPOSITE;
51     }
52
53     public short getVersion() {
54         return version;
55     }
56
57     public void setVersion(short version) {
58         this.version = version;
59     }
60
61     /**
62      * Add a new Modification to the list of Modifications represented by this
63      * composite
64      *
65      * @param modification
66      */
67     public void addModification(Modification modification) {
68         modifications.add(modification);
69     }
70
71     @Override
72     public List<Modification> getModifications() {
73         return modifications;
74     }
75
76     @Override
77     public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
78         version = in.readShort();
79
80         int size = in.readInt();
81
82         if(size > 1) {
83             SerializationUtils.REUSABLE_READER_TL.set(new NormalizedNodeInputStreamReader(in));
84         }
85
86         try {
87             for(int i = 0; i < size; i++) {
88                 byte type = in.readByte();
89                 switch(type) {
90                 case Modification.WRITE:
91                     modifications.add(WriteModification.fromStream(in, version));
92                     break;
93
94                 case Modification.MERGE:
95                     modifications.add(MergeModification.fromStream(in, version));
96                     break;
97
98                 case Modification.DELETE:
99                     modifications.add(DeleteModification.fromStream(in, version));
100                     break;
101                 }
102             }
103         } finally {
104             SerializationUtils.REUSABLE_READER_TL.remove();
105         }
106     }
107
108     @Override
109     public void writeExternal(ObjectOutput out) throws IOException {
110         out.writeShort(version);
111
112         out.writeInt(modifications.size());
113
114         if(modifications.size() > 1) {
115             SerializationUtils.REUSABLE_WRITER_TL.set(new NormalizedNodeOutputStreamWriter(out));
116         }
117
118         try {
119             for(Modification mod: modifications) {
120                 out.writeByte(mod.getType());
121                 mod.writeExternal(out);
122             }
123         } finally {
124             SerializationUtils.REUSABLE_WRITER_TL.remove();
125         }
126     }
127
128     @Override
129     @Deprecated
130     public Object toSerializable() {
131         PersistentMessages.CompositeModification.Builder builder =
132                 PersistentMessages.CompositeModification.newBuilder();
133
134         builder.setTimeStamp(System.nanoTime());
135
136         for (Modification m : modifications) {
137             builder.addModification((PersistentMessages.Modification) m.toSerializable());
138         }
139
140         return builder.build();
141     }
142
143     public static MutableCompositeModification fromSerializable(Object serializable) {
144         if(serializable instanceof MutableCompositeModification) {
145             return (MutableCompositeModification)serializable;
146         } else {
147             return fromLegacySerializable(serializable);
148         }
149     }
150
151     private static MutableCompositeModification fromLegacySerializable(Object serializable) {
152         PersistentMessages.CompositeModification o = (PersistentMessages.CompositeModification) serializable;
153         MutableCompositeModification compositeModification = new MutableCompositeModification();
154
155         for(PersistentMessages.Modification m : o.getModificationList()){
156             if(m.getType().equals(DeleteModification.class.toString())){
157                 compositeModification.addModification(DeleteModification.fromSerializable(m));
158             } else if(m.getType().equals(WriteModification.class.toString())){
159                 compositeModification.addModification(WriteModification.fromSerializable(m));
160             } else if(m.getType().equals(MergeModification.class.toString())){
161                 compositeModification.addModification(MergeModification.fromSerializable(m));
162             }
163         }
164
165         return compositeModification;
166     }
167 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.