2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.groupbasedpolicy.renderer.vpp.util;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import com.google.common.util.concurrent.CheckedFuture;
14 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
15 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
16 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
17 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
18 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
19 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
20 import org.opendaylight.groupbasedpolicy.renderer.vpp.commands.ConfigCommand;
21 import org.opendaylight.yangtools.yang.binding.DataObject;
22 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
26 public class GbpNetconfTransaction {
28 public static final byte RETRY_COUNT = 5;
29 private static final Logger LOG = LoggerFactory.getLogger(GbpNetconfTransaction.class);
32 * Use {@link ConfigCommand} to put data into netconf transaction and submit. Transaction is restarted if failed
34 * @param mountpoint to access remote device
35 * @param command config command with data, datastore type and iid
36 * @param retryCounter number of attempts
37 * @return true if transaction is successful, false otherwise
39 public synchronized static boolean write(final DataBroker mountpoint, final ConfigCommand command,
41 LOG.trace("Netconf WRITE transaction started. RetryCounter: {}", retryCounter);
42 Preconditions.checkNotNull(mountpoint);
43 final ReadWriteTransaction rwTx = mountpoint.newReadWriteTransaction();
45 command.execute(rwTx);
46 final CheckedFuture<Void, TransactionCommitFailedException> futureTask = rwTx.submit();
48 LOG.trace("Netconf WRITE transaction done. Retry counter: {}", retryCounter);
50 } catch (IllegalStateException e) {
52 if (retryCounter > 0) {
53 LOG.warn("Assuming that netconf write-transaction failed, restarting ...", e.getMessage());
54 return write(mountpoint, command, --retryCounter);
56 LOG.warn("Netconf write-transaction failed. Maximal number of attempts reached", e.getMessage());
59 } catch (Exception e) {
60 LOG.warn("Exception while writing data ...", e.getMessage());
66 * Write data to remote device. Transaction is restarted if failed
68 * @param mountpoint to access remote device
69 * @param iid data identifier
70 * @param data to write
71 * @param retryCounter number of attempts
72 * @param <T> generic data type. Has to be child of {@link DataObject}
73 * @return true if transaction is successful, false otherwise
75 public synchronized static <T extends DataObject> boolean write(final DataBroker mountpoint,
76 final InstanceIdentifier<T> iid,
79 LOG.trace("Netconf WRITE transaction started. RetryCounter: {}", retryCounter);
80 Preconditions.checkNotNull(mountpoint);
81 final ReadWriteTransaction rwTx = mountpoint.newReadWriteTransaction();
83 rwTx.put(LogicalDatastoreType.CONFIGURATION, iid, data, true);
84 final CheckedFuture<Void, TransactionCommitFailedException> futureTask = rwTx.submit();
86 LOG.trace("Netconf WRITE transaction done. Retry counter: {}", retryCounter);
88 } catch (IllegalStateException e) {
90 if (retryCounter > 0) {
91 LOG.warn("Assuming that netconf write-transaction failed, restarting ...", e.getMessage());
92 return write(mountpoint, iid, data, --retryCounter);
94 LOG.warn("Netconf write-transaction failed. Maximal number of attempts reached", e.getMessage());
97 } catch (Exception e) {
98 LOG.warn("Exception while writing data ...", e.getMessage());
104 * Read data from remote device. Transaction is restarted if failed.
106 * @param mountpoint to access remote device
107 * @param datastoreType {@link LogicalDatastoreType}
108 * @param iid data identifier
109 * @param retryCounter number of attempts
110 * @param <T> generic data type. Has to be child of {@link DataObject}
111 * @return optional data object if successful, {@link Optional#absent()} if failed
113 public synchronized static <T extends DataObject> Optional<T> read(final DataBroker mountpoint,
114 final LogicalDatastoreType datastoreType,
115 final InstanceIdentifier<T> iid,
117 LOG.trace("Netconf READ transaction started. RetryCounter: {}", retryCounter);
118 Preconditions.checkNotNull(mountpoint);
119 final ReadOnlyTransaction rTx = mountpoint.newReadOnlyTransaction();
122 final CheckedFuture<Optional<T>, ReadFailedException> futureData =
123 rTx.read(datastoreType, iid);
124 data = futureData.get();
125 LOG.trace("Netconf READ transaction done. Data present: {}, Retry counter: {}",
126 data.isPresent(), retryCounter);
128 } catch (IllegalStateException e) {
130 if (retryCounter > 0) {
131 LOG.warn("Assuming that netconf read-transaction failed, restarting ...", e.getMessage());
133 return read(mountpoint, datastoreType, iid, --retryCounter);
135 LOG.warn("Netconf read-transaction failed. Maximal number of attempts reached", e.getMessage());
136 return Optional.absent();
138 } catch (Exception e) {
139 LOG.warn("Exception while reading data ...", e.getMessage());
140 return Optional.absent();
145 * Remove data from remote device using {@link ConfigCommand}. Transaction is restarted if failed.
147 * @param mountpoint to access remote device
148 * @param command config command with data, datastore type and iid
149 * @param retryCounter number of attempts
150 * @return true if transaction is successful, false otherwise
152 public synchronized static boolean delete(final DataBroker mountpoint, final ConfigCommand command,
154 LOG.trace("Netconf DELETE transaction started. RetryCounter: {}", retryCounter);
155 Preconditions.checkNotNull(mountpoint);
156 final ReadWriteTransaction rwTx = mountpoint.newReadWriteTransaction();
158 command.execute(rwTx);
159 final CheckedFuture<Void, TransactionCommitFailedException> futureTask = rwTx.submit();
161 LOG.trace("Netconf DELETE transaction done. Retry counter: {}", retryCounter);
163 } catch (IllegalStateException e) {
165 if (retryCounter > 0) {
166 LOG.warn("Assuming that netconf delete-transaction failed, restarting ...", e.getMessage());
167 return delete(mountpoint, command, --retryCounter);
169 LOG.warn("Netconf delete-transaction failed. Maximal number of attempts reached", e.getMessage());
172 } catch (Exception e) {
173 LOG.warn("Exception while removing data ...", e.getMessage());
179 * Remove data from remote device. Transaction is restarted if failed.
181 * @param mountpoint to access remote device
182 * @param iid data identifier
183 * @param retryCounter number of attempts
184 * @param <T> generic data type. Has to be child of {@link DataObject}
185 * @return true if transaction is successful, false otherwise
187 public synchronized static <T extends DataObject> boolean delete(final DataBroker mountpoint,
188 final InstanceIdentifier<T> iid,
190 LOG.trace("Netconf DELETE transaction started. RetryCounter: {}", retryCounter);
191 Preconditions.checkNotNull(mountpoint);
192 final ReadWriteTransaction rwTx = mountpoint.newReadWriteTransaction();
194 rwTx.delete(LogicalDatastoreType.CONFIGURATION, iid);
195 final CheckedFuture<Void, TransactionCommitFailedException> futureTask = rwTx.submit();
197 LOG.trace("Netconf DELETE transaction done. Retry counter: {}", retryCounter);
199 } catch (IllegalStateException e) {
201 if (retryCounter > 0) {
202 LOG.warn("Assuming that netconf delete-transaction failed, restarting ...", e.getMessage());
203 return delete(mountpoint, iid, --retryCounter);
205 LOG.warn("Netconf delete-transaction failed. Maximal number of attempts reached", e.getMessage());
208 } catch (Exception e) {
209 LOG.warn("Exception while removing data ...", e.getMessage());