Merge "Bug 716: Errors on controller shutdown"
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / md / sal / dom / store / impl / tree / data / NormalizedNodeContainerModificationStrategy.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.md.sal.dom.store.impl.tree.data;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11
12 import java.util.Map;
13
14 import org.opendaylight.controller.md.sal.dom.store.impl.tree.DataPreconditionFailedException;
15 import org.opendaylight.controller.md.sal.dom.store.impl.tree.ModificationType;
16 import org.opendaylight.controller.md.sal.dom.store.impl.tree.StoreUtils;
17 import org.opendaylight.controller.md.sal.dom.store.impl.tree.data.DataNodeContainerModificationStrategy.ListEntryModificationStrategy;
18 import org.opendaylight.controller.md.sal.dom.store.impl.tree.data.ValueNodeModificationStrategy.LeafSetEntryModificationStrategy;
19 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifierWithPredicates;
22 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeWithValue;
23 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
24 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
28 import org.opendaylight.yangtools.yang.data.api.schema.OrderedLeafSetNode;
29 import org.opendaylight.yangtools.yang.data.api.schema.OrderedMapNode;
30 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
31 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
32 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableChoiceNodeBuilder;
33 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetNodeBuilder;
34 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapNodeBuilder;
35 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableOrderedLeafSetNodeBuilder;
36 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableOrderedMapNodeBuilder;
37 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
38 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
39 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
40 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
41 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
42
43 import com.google.common.base.Optional;
44 import com.google.common.collect.ImmutableMap;
45 import com.google.common.primitives.UnsignedLong;
46
47 abstract class NormalizedNodeContainerModificationStrategy extends SchemaAwareApplyOperation {
48
49     private final Class<? extends NormalizedNode<?, ?>> nodeClass;
50
51     protected NormalizedNodeContainerModificationStrategy(final Class<? extends NormalizedNode<?, ?>> nodeClass) {
52         this.nodeClass = nodeClass;
53     }
54
55     @Override
56     public void verifyStructure(final NodeModification modification) throws IllegalArgumentException {
57         if (modification.getModificationType() == ModificationType.WRITE) {
58
59         }
60         for (NodeModification childModification : modification.getModifications()) {
61             resolveChildOperation(childModification.getIdentifier()).verifyStructure(childModification);
62         }
63     }
64
65     @Override
66     protected void checkWriteApplicable(final InstanceIdentifier path, final NodeModification modification,
67             final Optional<StoreMetadataNode> current) throws DataPreconditionFailedException {
68         // FIXME: Implement proper write check for replacement of node container
69         //        prerequisite is to have transaction chain available for clients
70         //        otherwise this will break chained writes to same node.
71     }
72
73     @SuppressWarnings("rawtypes")
74     @Override
75     protected void verifyWrittenStructure(final NormalizedNode<?, ?> writtenValue) {
76         checkArgument(nodeClass.isInstance(writtenValue), "Node should must be of type %s", nodeClass);
77         checkArgument(writtenValue instanceof NormalizedNodeContainer);
78
79         NormalizedNodeContainer container = (NormalizedNodeContainer) writtenValue;
80         for (Object child : container.getValue()) {
81             checkArgument(child instanceof NormalizedNode);
82
83             /*
84              * FIXME: fail-fast semantics:
85              *
86              * We can validate the data structure here, aborting the commit
87              * before it ever progresses to being committed.
88              */
89         }
90     }
91
92     @Override
93     protected StoreMetadataNode applyWrite(final NodeModification modification,
94             final Optional<StoreMetadataNode> currentMeta, final UnsignedLong subtreeVersion) {
95
96         NormalizedNode<?, ?> newValue = modification.getWrittenValue();
97
98         final UnsignedLong nodeVersion;
99         if (currentMeta.isPresent()) {
100             nodeVersion = StoreUtils.increase(currentMeta.get().getNodeVersion());
101         } else {
102             nodeVersion = subtreeVersion;
103         }
104
105         final StoreMetadataNode newValueMeta = StoreMetadataNode.createRecursively(newValue, nodeVersion);
106         if (!modification.hasAdditionalModifications()) {
107             return newValueMeta;
108         }
109
110         @SuppressWarnings("rawtypes")
111         NormalizedNodeContainerBuilder dataBuilder = createBuilder(newValue);
112         StoreNodeCompositeBuilder builder = StoreNodeCompositeBuilder.create(nodeVersion, dataBuilder) //
113                 .setSubtreeVersion(subtreeVersion);
114
115         return mutateChildren(modification.getModifications(), newValueMeta, builder, nodeVersion);
116     }
117
118     @Override
119     protected StoreMetadataNode applyMerge(final NodeModification modification, final StoreMetadataNode currentMeta,
120             final UnsignedLong subtreeVersion) {
121         // For Node Containers - merge is same as subtree change - we only replace children.
122         return applySubtreeChange(modification, currentMeta, subtreeVersion);
123     }
124
125     @Override
126     public StoreMetadataNode applySubtreeChange(final NodeModification modification,
127             final StoreMetadataNode currentMeta, final UnsignedLong subtreeVersion) {
128         // Bump subtree version to its new target
129         final UnsignedLong updatedSubtreeVersion = StoreUtils.increase(currentMeta.getSubtreeVersion());
130
131         @SuppressWarnings("rawtypes")
132         NormalizedNodeContainerBuilder dataBuilder = createBuilder(currentMeta.getData());
133         StoreNodeCompositeBuilder builder = StoreNodeCompositeBuilder.create(dataBuilder, currentMeta)
134                 .setIdentifier(modification.getIdentifier())
135                 .setSubtreeVersion(updatedSubtreeVersion);
136
137         return mutateChildren(modification.getModifications(), currentMeta, builder, updatedSubtreeVersion);
138     }
139
140     private StoreMetadataNode mutateChildren(final Iterable<NodeModification> modifications, final StoreMetadataNode meta,
141             final StoreNodeCompositeBuilder builder, final UnsignedLong nodeVersion) {
142
143         for (NodeModification mod : modifications) {
144             final PathArgument id = mod.getIdentifier();
145             final Optional<StoreMetadataNode> cm = meta.getChild(id);
146
147             Optional<StoreMetadataNode> result = resolveChildOperation(id).apply(mod, cm, nodeVersion);
148             if (result.isPresent()) {
149                 builder.add(result.get());
150             } else {
151                 builder.remove(id);
152             }
153         }
154
155         return builder.build();
156     }
157
158     @Override
159     protected void checkSubtreeModificationApplicable(final InstanceIdentifier path,final NodeModification modification,
160             final Optional<StoreMetadataNode> current) throws DataPreconditionFailedException {
161         checkDataPrecondition(path, current.isPresent(), "Node was deleted by other transaction.");
162         checkChildPreconditions(path,modification,current);
163
164     }
165
166     private void checkChildPreconditions(final InstanceIdentifier path, final NodeModification modification, final Optional<StoreMetadataNode> current) throws DataPreconditionFailedException {
167         StoreMetadataNode currentMeta = current.get();
168         for (NodeModification childMod : modification.getModifications()) {
169             PathArgument childId = childMod.getIdentifier();
170             Optional<StoreMetadataNode> childMeta = currentMeta.getChild(childId);
171             InstanceIdentifier childPath = StoreUtils.append(path, childId);
172             resolveChildOperation(childId).checkApplicable(childPath,childMod, childMeta);
173         }
174     }
175
176     @Override
177     protected void checkMergeApplicable(final InstanceIdentifier path, final NodeModification modification,
178             final Optional<StoreMetadataNode> current) throws DataPreconditionFailedException {
179         if(current.isPresent()) {
180             checkChildPreconditions(path,modification,current);
181         }
182     }
183
184     @SuppressWarnings("rawtypes")
185     protected abstract NormalizedNodeContainerBuilder createBuilder(NormalizedNode<?, ?> original);
186
187     public static class ChoiceModificationStrategy extends NormalizedNodeContainerModificationStrategy {
188
189         private final Map<PathArgument, ModificationApplyOperation> childNodes;
190
191         public ChoiceModificationStrategy(final ChoiceNode schemaNode) {
192             super(org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode.class);
193             ImmutableMap.Builder<PathArgument, ModificationApplyOperation> child = ImmutableMap.builder();
194
195             for (ChoiceCaseNode caze : schemaNode.getCases()) {
196                 for (DataSchemaNode cazeChild : caze.getChildNodes()) {
197                     SchemaAwareApplyOperation childNode = from(cazeChild);
198                     child.put(new NodeIdentifier(cazeChild.getQName()), childNode);
199                 }
200             }
201             childNodes = child.build();
202         }
203
204         @Override
205         public Optional<ModificationApplyOperation> getChild(final PathArgument child) {
206             return Optional.fromNullable(childNodes.get(child));
207         }
208
209         @Override
210         @SuppressWarnings("rawtypes")
211         protected DataContainerNodeBuilder createBuilder(final NormalizedNode<?, ?> original) {
212             checkArgument(original instanceof org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode);
213             return ImmutableChoiceNodeBuilder.create((org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode) original);
214         }
215     }
216
217     public static class OrderedLeafSetModificationStrategy extends NormalizedNodeContainerModificationStrategy {
218
219         private final Optional<ModificationApplyOperation> entryStrategy;
220
221         @SuppressWarnings({ "unchecked", "rawtypes" })
222         protected OrderedLeafSetModificationStrategy(final LeafListSchemaNode schema) {
223             super((Class) LeafSetNode.class);
224             entryStrategy = Optional.<ModificationApplyOperation> of(new LeafSetEntryModificationStrategy(schema));
225         }
226
227         @SuppressWarnings("rawtypes")
228         @Override
229         protected NormalizedNodeContainerBuilder createBuilder(final NormalizedNode<?, ?> original) {
230             checkArgument(original instanceof OrderedLeafSetNode<?>);
231             return ImmutableOrderedLeafSetNodeBuilder.create((OrderedLeafSetNode<?>) original);
232         }
233
234         @Override
235         public Optional<ModificationApplyOperation> getChild(final PathArgument identifier) {
236             if (identifier instanceof NodeWithValue) {
237                 return entryStrategy;
238             }
239             return Optional.absent();
240         }
241     }
242
243     public static class OrderedMapModificationStrategy extends NormalizedNodeContainerModificationStrategy {
244
245         private final Optional<ModificationApplyOperation> entryStrategy;
246
247         protected OrderedMapModificationStrategy(final ListSchemaNode schema) {
248             super(OrderedMapNode.class);
249             entryStrategy = Optional.<ModificationApplyOperation> of(new ListEntryModificationStrategy(schema));
250         }
251
252         @SuppressWarnings("rawtypes")
253         @Override
254         protected NormalizedNodeContainerBuilder createBuilder(final NormalizedNode<?, ?> original) {
255             checkArgument(original instanceof OrderedMapNode);
256             return ImmutableOrderedMapNodeBuilder.create((OrderedMapNode) original);
257         }
258
259         @Override
260         public Optional<ModificationApplyOperation> getChild(final PathArgument identifier) {
261             if (identifier instanceof NodeIdentifierWithPredicates) {
262                 return entryStrategy;
263             }
264             return Optional.absent();
265         }
266
267         @Override
268         public String toString() {
269             return "OrderedMapModificationStrategy [entry=" + entryStrategy + "]";
270         }
271     }
272
273     public static class UnorderedLeafSetModificationStrategy extends NormalizedNodeContainerModificationStrategy {
274
275         private final Optional<ModificationApplyOperation> entryStrategy;
276
277         @SuppressWarnings({ "unchecked", "rawtypes" })
278         protected UnorderedLeafSetModificationStrategy(final LeafListSchemaNode schema) {
279             super((Class) LeafSetNode.class);
280             entryStrategy = Optional.<ModificationApplyOperation> of(new LeafSetEntryModificationStrategy(schema));
281         }
282
283         @SuppressWarnings("rawtypes")
284         @Override
285         protected NormalizedNodeContainerBuilder createBuilder(final NormalizedNode<?, ?> original) {
286             checkArgument(original instanceof LeafSetNode<?>);
287             return ImmutableLeafSetNodeBuilder.create((LeafSetNode<?>) original);
288         }
289
290         @Override
291         public Optional<ModificationApplyOperation> getChild(final PathArgument identifier) {
292             if (identifier instanceof NodeWithValue) {
293                 return entryStrategy;
294             }
295             return Optional.absent();
296         }
297     }
298
299     public static class UnorderedMapModificationStrategy extends NormalizedNodeContainerModificationStrategy {
300
301         private final Optional<ModificationApplyOperation> entryStrategy;
302
303         protected UnorderedMapModificationStrategy(final ListSchemaNode schema) {
304             super(MapNode.class);
305             entryStrategy = Optional.<ModificationApplyOperation> of(new ListEntryModificationStrategy(schema));
306         }
307
308         @SuppressWarnings("rawtypes")
309         @Override
310         protected NormalizedNodeContainerBuilder createBuilder(final NormalizedNode<?, ?> original) {
311             checkArgument(original instanceof MapNode);
312             return ImmutableMapNodeBuilder.create((MapNode) original);
313         }
314
315         @Override
316         public Optional<ModificationApplyOperation> getChild(final PathArgument identifier) {
317             if (identifier instanceof NodeIdentifierWithPredicates) {
318                 return entryStrategy;
319             }
320             return Optional.absent();
321         }
322
323         @Override
324         public String toString() {
325             return "UnorderedMapModificationStrategy [entry=" + entryStrategy + "]";
326         }
327     }
328 }