44be74b4248b0d25ee40d484d17ff8c96e82d6a5
[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.concurrent.ExecutionException;
14
15 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
16 import org.opendaylight.controller.md.sal.common.impl.util.compat.DataNormalizationException;
17 import org.opendaylight.controller.md.sal.common.impl.util.compat.DataNormalizationOperation;
18 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
19 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
21 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 import com.google.common.base.Optional;
26
27 public class AbstractReadWriteTransaction extends AbstractWriteTransaction<DOMDataReadWriteTransaction> {
28
29     private static final Logger LOG = LoggerFactory.getLogger(AbstractReadWriteTransaction.class);
30
31     public AbstractReadWriteTransaction(final DOMDataReadWriteTransaction delegate, final BindingToNormalizedNodeCodec codec) {
32         super(delegate, codec);
33     }
34
35     @Override
36     protected final void ensureParentsByMerge(final LogicalDatastoreType store,
37             final org.opendaylight.yangtools.yang.data.api.InstanceIdentifier normalizedPath,
38             final InstanceIdentifier<?> path) {
39         List<PathArgument> currentArguments = new ArrayList<>();
40         DataNormalizationOperation<?> currentOp = getCodec().getDataNormalizer().getRootOperation();
41         Iterator<PathArgument> iterator = normalizedPath.getPathArguments().iterator();
42         while (iterator.hasNext()) {
43             PathArgument currentArg = iterator.next();
44             try {
45                 currentOp = currentOp.getChild(currentArg);
46             } catch (DataNormalizationException e) {
47                 throw new IllegalArgumentException(String.format("Invalid child encountered in path %s", path), e);
48             }
49             currentArguments.add(currentArg);
50             org.opendaylight.yangtools.yang.data.api.InstanceIdentifier currentPath = org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.create(
51                     currentArguments);
52
53             final Optional<NormalizedNode<?, ?>> d;
54             try {
55                 d = getDelegate().read(store, currentPath).get();
56             } catch (InterruptedException | ExecutionException e) {
57                 LOG.error("Failed to read pre-existing data from store {} path {}", store, currentPath, e);
58                 throw new IllegalStateException("Failed to read pre-existing data", e);
59             }
60
61             if (!d.isPresent() && iterator.hasNext()) {
62                 getDelegate().merge(store, currentPath, currentOp.createDefault(currentArg));
63             }
64         }
65     }
66
67
68 }

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