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 java.util.concurrent.ExecutionException;
12 import com.google.common.base.Optional;
13 import com.google.common.base.Preconditions;
14 import com.google.common.util.concurrent.CheckedFuture;
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
17 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
20 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
21 import org.opendaylight.groupbasedpolicy.renderer.vpp.commands.ConfigCommand;
22 import org.opendaylight.yangtools.yang.binding.DataObject;
23 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
27 public class GbpNetconfTransaction {
29 public static final byte RETRY_COUNT = 5;
30 private static final Logger LOG = LoggerFactory.getLogger(GbpNetconfTransaction.class);
33 * Use {@link ConfigCommand} to put data into netconf transaction and submit. Transaction is restarted if failed
35 * @param mountpoint to access remote device
36 * @param command config command with data, datastore type and iid
37 * @param retryCounter number of attempts
38 * @return true if transaction is successful, false otherwise
40 public synchronized static boolean write(final DataBroker mountpoint, final ConfigCommand command,
42 LOG.trace("Netconf WRITE transaction started. RetryCounter: {}", retryCounter);
43 Preconditions.checkNotNull(mountpoint);
44 final ReadWriteTransaction rwTx = mountpoint.newReadWriteTransaction();
46 command.execute(rwTx);
47 final CheckedFuture<Void, TransactionCommitFailedException> futureTask = rwTx.submit();
49 LOG.trace("Netconf WRITE transaction done. Retry counter: {}", retryCounter);
51 } catch (IllegalStateException e) {
53 if (retryCounter > 0) {
54 LOG.warn("Assuming that netconf write-transaction failed, restarting ...", e.getMessage());
55 return write(mountpoint, command, --retryCounter);
57 LOG.warn("Netconf write-transaction failed. Maximal number of attempts reached", e.getMessage());
60 } catch (InterruptedException | ExecutionException e) {
61 LOG.warn("Exception while writing data ...", e.getMessage());
67 * Write data to remote device. Transaction is restarted if failed
69 * @param mountpoint to access remote device
70 * @param iid data identifier
71 * @param data to write
72 * @param retryCounter number of attempts
73 * @param <T> generic data type. Has to be child of {@link DataObject}
74 * @return true if transaction is successful, false otherwise
76 public synchronized static <T extends DataObject> boolean write(final DataBroker mountpoint,
77 final InstanceIdentifier<T> iid,
80 LOG.trace("Netconf WRITE transaction started. RetryCounter: {}", retryCounter);
81 Preconditions.checkNotNull(mountpoint);
82 final ReadWriteTransaction rwTx = mountpoint.newReadWriteTransaction();
84 rwTx.put(LogicalDatastoreType.CONFIGURATION, iid, data, true);
85 final CheckedFuture<Void, TransactionCommitFailedException> futureTask = rwTx.submit();
87 LOG.trace("Netconf WRITE transaction done. Retry counter: {}", retryCounter);
89 } catch (IllegalStateException e) {
91 if (retryCounter > 0) {
92 LOG.warn("Assuming that netconf write-transaction failed, restarting ...", e.getMessage());
93 return write(mountpoint, iid, data, --retryCounter);
95 LOG.warn("Netconf write-transaction failed. Maximal number of attempts reached", e.getMessage());
98 } catch (InterruptedException | ExecutionException e) {
99 LOG.warn("Exception while writing data ...", e.getMessage());
105 * Read data from remote device. Transaction is restarted if failed.
107 * @param mountpoint to access remote device
108 * @param datastoreType {@link LogicalDatastoreType}
109 * @param iid data identifier
110 * @param retryCounter number of attempts
111 * @param <T> generic data type. Has to be child of {@link DataObject}
112 * @return optional data object if successful, {@link Optional#absent()} if failed
114 public synchronized static <T extends DataObject> Optional<T> read(final DataBroker mountpoint,
115 final LogicalDatastoreType datastoreType,
116 final InstanceIdentifier<T> iid,
118 LOG.trace("Netconf READ transaction started. RetryCounter: {}", retryCounter);
119 Preconditions.checkNotNull(mountpoint);
120 final ReadOnlyTransaction rTx = mountpoint.newReadOnlyTransaction();
123 final CheckedFuture<Optional<T>, ReadFailedException> futureData =
124 rTx.read(datastoreType, iid);
125 data = futureData.get();
126 LOG.trace("Netconf READ transaction done. Data present: {}, Retry counter: {}",
127 data.isPresent(), retryCounter);
129 } catch (IllegalStateException e) {
131 if (retryCounter > 0) {
132 LOG.warn("Assuming that netconf read-transaction failed, restarting ...", e.getMessage());
134 return read(mountpoint, datastoreType, iid, --retryCounter);
136 LOG.warn("Netconf read-transaction failed. Maximal number of attempts reached", e.getMessage());
137 return Optional.absent();
139 } catch (InterruptedException | ExecutionException e) {
140 LOG.warn("Exception while reading data ...", e.getMessage());
141 return Optional.absent();
146 * Remove data from remote device using {@link ConfigCommand}. Transaction is restarted if failed.
148 * @param mountpoint to access remote device
149 * @param command config command with data, datastore type and iid
150 * @param retryCounter number of attempts
151 * @return true if transaction is successful, false otherwise
153 public synchronized static boolean delete(final DataBroker mountpoint, final ConfigCommand command,
155 LOG.trace("Netconf DELETE transaction started. RetryCounter: {}", retryCounter);
156 Preconditions.checkNotNull(mountpoint);
157 final ReadWriteTransaction rwTx = mountpoint.newReadWriteTransaction();
159 command.execute(rwTx);
160 final CheckedFuture<Void, TransactionCommitFailedException> futureTask = rwTx.submit();
162 LOG.trace("Netconf DELETE transaction done. Retry counter: {}", retryCounter);
164 } catch (IllegalStateException e) {
166 if (retryCounter > 0) {
167 LOG.warn("Assuming that netconf delete-transaction failed, restarting ...", e.getMessage());
168 return delete(mountpoint, command, --retryCounter);
170 LOG.warn("Netconf delete-transaction failed. Maximal number of attempts reached", e.getMessage());
173 } catch (InterruptedException | ExecutionException e) {
174 LOG.warn("Exception while removing data ...", e.getMessage());
180 * Remove data from remote device. Transaction is restarted if failed.
182 * @param mountpoint to access remote device
183 * @param iid data identifier
184 * @param retryCounter number of attempts
185 * @param <T> generic data type. Has to be child of {@link DataObject}
186 * @return true if transaction is successful, false otherwise
188 public synchronized static <T extends DataObject> boolean delete(final DataBroker mountpoint,
189 final InstanceIdentifier<T> iid,
191 LOG.trace("Netconf DELETE transaction started. RetryCounter: {}", retryCounter);
192 Preconditions.checkNotNull(mountpoint);
193 final ReadWriteTransaction rwTx = mountpoint.newReadWriteTransaction();
195 rwTx.delete(LogicalDatastoreType.CONFIGURATION, iid);
196 final CheckedFuture<Void, TransactionCommitFailedException> futureTask = rwTx.submit();
198 LOG.trace("Netconf DELETE transaction done. Retry counter: {}", retryCounter);
200 } catch (IllegalStateException e) {
202 if (retryCounter > 0) {
203 LOG.warn("Assuming that netconf delete-transaction failed, restarting ...", e.getMessage());
204 return delete(mountpoint, iid, --retryCounter);
206 LOG.warn("Netconf delete-transaction failed. Maximal number of attempts reached", e.getMessage());
209 } catch (InterruptedException | ExecutionException e) {
210 LOG.warn("Exception while removing data ...", e.getMessage());