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

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