Switch CompositeModification to bypass thread-local streams
[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 com.google.common.base.Preconditions;
12 import java.io.IOException;
13 import java.io.ObjectInput;
14 import java.io.ObjectOutput;
15 import java.util.ArrayList;
16 import java.util.Collections;
17 import java.util.List;
18 import org.opendaylight.controller.cluster.datastore.DataStoreVersions;
19 import org.opendaylight.controller.cluster.datastore.messages.VersionedExternalizableMessage;
20 import org.opendaylight.controller.cluster.datastore.node.utils.stream.NormalizedNodeDataInput;
21 import org.opendaylight.controller.cluster.datastore.node.utils.stream.NormalizedNodeDataOutput;
22 import org.opendaylight.controller.cluster.datastore.node.utils.stream.NormalizedNodeInputOutput;
23 import org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction;
24 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
25
26 /**
27  * MutableCompositeModification is just a mutable version of a CompositeModification.
28  */
29 public class MutableCompositeModification extends VersionedExternalizableMessage implements CompositeModification {
30     private static final long serialVersionUID = 1L;
31
32     private final List<Modification> modifications = new ArrayList<>();
33     private List<Modification> immutableModifications = null;
34
35     public MutableCompositeModification() {
36         this(DataStoreVersions.CURRENT_VERSION);
37     }
38
39     public MutableCompositeModification(final short version) {
40         super(version);
41     }
42
43     @Override
44     public void apply(final DOMStoreWriteTransaction transaction) {
45         for (Modification modification : modifications) {
46             modification.apply(transaction);
47         }
48     }
49
50     @Override
51     public void apply(final DataTreeModification transaction) {
52         for (Modification modification : modifications) {
53             modification.apply(transaction);
54         }
55     }
56
57     @Override
58     public byte getType() {
59         return COMPOSITE;
60     }
61
62     /**
63      * Add a new Modification to the list of Modifications represented by this composite.
64      *
65      * @param modification the modification to add.
66      */
67     public void addModification(final Modification modification) {
68         Preconditions.checkNotNull(modification);
69         modifications.add(modification);
70     }
71
72     public void addModifications(final Iterable<Modification> newMods) {
73         for (Modification mod : newMods) {
74             addModification(mod);
75         }
76     }
77
78     @Override
79     public List<Modification> getModifications() {
80         if (immutableModifications == null) {
81             immutableModifications = Collections.unmodifiableList(modifications);
82         }
83
84         return immutableModifications;
85     }
86
87     @Override
88     public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
89         super.readExternal(in);
90
91         int size = in.readInt();
92         if (size > 0) {
93             final NormalizedNodeDataInput input = NormalizedNodeInputOutput.newDataInputWithoutValidation(in);
94             for (int i = 0; i < size; i++) {
95                 byte type = in.readByte();
96                 switch (type) {
97                     case Modification.WRITE:
98                         modifications.add(WriteModification.fromStream(input, getVersion()));
99                         break;
100
101                     case Modification.MERGE:
102                         modifications.add(MergeModification.fromStream(input, getVersion()));
103                         break;
104
105                     case Modification.DELETE:
106                         modifications.add(DeleteModification.fromStream(input, getVersion()));
107                         break;
108                     default:
109                         break;
110                 }
111             }
112         }
113     }
114
115     @Override
116     public void writeExternal(final ObjectOutput out) throws IOException {
117         super.writeExternal(out);
118
119         final int size = modifications.size();
120         out.writeInt(size);
121         if (size > 0) {
122             try (NormalizedNodeDataOutput stream = NormalizedNodeInputOutput.newDataOutput(out)) {
123                 for (Modification mod : modifications) {
124                     out.writeByte(mod.getType());
125                     mod.writeTo(stream);
126                 }
127             }
128         }
129     }
130
131     public static MutableCompositeModification fromSerializable(final Object serializable) {
132         Preconditions.checkArgument(serializable instanceof MutableCompositeModification);
133         return (MutableCompositeModification)serializable;
134     }
135
136     @Override
137     public void writeTo(final NormalizedNodeDataOutput out) throws IOException {
138         throw new UnsupportedOperationException();
139     }
140 }

©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.