BUG-1381: make users of JavassistUtils.getLock synchronized properly
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / md / sal / binding / impl / AbstractReadWriteTransaction.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.binding.impl;
9
10 import java.util.ArrayList;
11 import java.util.Iterator;
12 import java.util.List;
13 import java.util.Map.Entry;
14 import java.util.concurrent.ExecutionException;
15
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.controller.md.sal.common.impl.util.compat.DataNormalizationException;
18 import org.opendaylight.controller.md.sal.common.impl.util.compat.DataNormalizationOperation;
19 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
20 import org.opendaylight.yangtools.yang.binding.DataObject;
21 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
23 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 import com.google.common.base.Optional;
28
29 public class AbstractReadWriteTransaction extends AbstractWriteTransaction<DOMDataReadWriteTransaction> {
30
31     private static final Logger LOG = LoggerFactory.getLogger(AbstractReadWriteTransaction.class);
32
33     public AbstractReadWriteTransaction(final DOMDataReadWriteTransaction delegate, final BindingToNormalizedNodeCodec codec) {
34         super(delegate, codec);
35     }
36
37     protected final void doPutWithEnsureParents(final LogicalDatastoreType store, final InstanceIdentifier<?> path, final DataObject data) {
38         final Entry<org.opendaylight.yangtools.yang.data.api.InstanceIdentifier, NormalizedNode<?, ?>> normalized = getCodec()
39                 .toNormalizedNode(path, data);
40
41         final org.opendaylight.yangtools.yang.data.api.InstanceIdentifier normalizedPath = normalized.getKey();
42         ensureParentsByMerge(store, normalizedPath, path);
43         LOG.debug("Tx: {} : Putting data {}", getDelegate().getIdentifier(), normalizedPath);
44         doPut(store, path, data);
45     }
46
47     protected final void doMergeWithEnsureParents(final LogicalDatastoreType store, final InstanceIdentifier<?> path, final DataObject data) {
48         final Entry<org.opendaylight.yangtools.yang.data.api.InstanceIdentifier, NormalizedNode<?, ?>> normalized = getCodec()
49                 .toNormalizedNode(path, data);
50
51         final org.opendaylight.yangtools.yang.data.api.InstanceIdentifier normalizedPath = normalized.getKey();
52         ensureParentsByMerge(store, normalizedPath, path);
53         LOG.debug("Tx: {} : Merge data {}", getDelegate().getIdentifier(), normalizedPath);
54         doMerge(store, path, data);
55     }
56
57     private final void ensureParentsByMerge(final LogicalDatastoreType store,
58             final org.opendaylight.yangtools.yang.data.api.InstanceIdentifier normalizedPath,
59             final InstanceIdentifier<?> path) {
60         List<PathArgument> currentArguments = new ArrayList<>();
61         DataNormalizationOperation<?> currentOp = getCodec().getDataNormalizer().getRootOperation();
62         Iterator<PathArgument> iterator = normalizedPath.getPathArguments().iterator();
63         while (iterator.hasNext()) {
64             PathArgument currentArg = iterator.next();
65             try {
66                 currentOp = currentOp.getChild(currentArg);
67             } catch (DataNormalizationException e) {
68                 throw new IllegalArgumentException(String.format("Invalid child encountered in path %s", path), e);
69             }
70             currentArguments.add(currentArg);
71             org.opendaylight.yangtools.yang.data.api.InstanceIdentifier currentPath = org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.create(
72                     currentArguments);
73
74             final Optional<NormalizedNode<?, ?>> d;
75             try {
76                 d = getDelegate().read(store, currentPath).get();
77             } catch (InterruptedException | ExecutionException e) {
78                 LOG.error("Failed to read pre-existing data from store {} path {}", store, currentPath, e);
79                 throw new IllegalStateException("Failed to read pre-existing data", e);
80             }
81
82             if (!d.isPresent() && iterator.hasNext()) {
83                 getDelegate().merge(store, currentPath, currentOp.createDefault(currentArg));
84             }
85         }
86     }
87
88
89 }

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