Updated model yang file to use recommended practices found on wiki, then updated...
[packetcable.git] / packetcable-policy-server / src / main / java / org / opendaylight / controller / packetcable / provider / MdsalUtils.java
1 /*
2  * Copyright (c) 2015 Inocybe 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.packetcable.provider;
10
11 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
12 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
13 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
14 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
15 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 import com.google.common.util.concurrent.CheckedFuture;
20
21 public class MdsalUtils {
22     private static final Logger LOG = LoggerFactory.getLogger(MdsalUtils.class);
23     private DataBroker databroker = null;
24
25     /**
26      * Class constructor setting the data broker.
27      *
28      * @param dataBroker the {@link org.opendaylight.controller.md.sal.binding.api.DataBroker}
29      */
30     public MdsalUtils(DataBroker dataBroker) {
31         this.databroker = dataBroker;
32     }
33
34     /**
35      * Executes delete as a blocking transaction.
36      *
37      * @param store {@link LogicalDatastoreType} which should be modified
38      * @param path {@link InstanceIdentifier} to read from
39      * @param <D> the data object type
40      * @return the result of the request
41      */
42     public <D extends org.opendaylight.yangtools.yang.binding.DataObject> boolean delete(
43             final LogicalDatastoreType store, final InstanceIdentifier<D> path)  {
44         boolean result = false;
45         final WriteTransaction transaction = databroker.newWriteOnlyTransaction();
46         transaction.delete(store, path);
47         CheckedFuture<Void, TransactionCommitFailedException> future = transaction.submit();
48         try {
49             future.checkedGet();
50             result = true;
51         } catch (TransactionCommitFailedException e) {
52             LOG.warn("Failed to delete {} ", path, e);
53         }
54         return result;
55     }
56
57     /**
58      * Executes merge as a blocking transaction.
59      *
60      * @param logicalDatastoreType {@link LogicalDatastoreType} which should be modified
61      * @param path {@link InstanceIdentifier} for path to read
62      * @param <D> the data object type
63      * @return the result of the request
64      */
65     public <D extends org.opendaylight.yangtools.yang.binding.DataObject> boolean merge(
66             final LogicalDatastoreType logicalDatastoreType, final InstanceIdentifier<D> path, D data)  {
67         boolean result = false;
68         final WriteTransaction transaction = databroker.newWriteOnlyTransaction();
69         transaction.merge(logicalDatastoreType, path, data, true);
70         CheckedFuture<Void, TransactionCommitFailedException> future = transaction.submit();
71         try {
72             future.checkedGet();
73             result = true;
74         } catch (TransactionCommitFailedException e) {
75             LOG.warn("Failed to merge {} ", path, e);
76         }
77         return result;
78     }
79 }