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.ios_xe_provider.impl.writer;
11 import java.util.Optional;
13 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
14 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
15 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
16 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
17 import org.opendaylight.netconf.api.NetconfDocumentedException;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
21 import com.google.common.annotations.VisibleForTesting;
24 * Purpose: safely create transaction
27 public class NetconfTransactionCreator {
29 private final static Logger LOG = LoggerFactory.getLogger(NetconfTransactionCreator.class);
30 private static long timeout = 5000L;
32 public static Optional<ReadOnlyTransaction> netconfReadOnlyTransaction(DataBroker mountpoint) {
36 return Optional.ofNullable(mountpoint.newReadOnlyTransaction());
37 } catch (RuntimeException e) {
38 final Optional<Throwable> optionalCause = Optional.ofNullable(e.getCause());
39 final Optional<Class> optionalCauseClass = optionalCause.map(Throwable::getClass);
40 if (optionalCauseClass.isPresent() && optionalCauseClass.get().equals(NetconfDocumentedException.class)) {
42 LOG.warn("NetconfDocumentedException thrown, retrying ({})...", attempt);
44 Thread.sleep(timeout);
45 } catch (InterruptedException i) {
46 LOG.error("Thread interrupted while waiting ... {} ", i);
49 LOG.error("Runtime exception ... {}", e.getMessage(), e);
50 return Optional.empty();
53 } while (attempt <= 5);
54 LOG.error("Maximum number of attempts reached");
55 return Optional.empty();
58 public static Optional<WriteTransaction> netconfWriteOnlyTransaction(DataBroker mountpoint) {
62 return Optional.of(mountpoint.newWriteOnlyTransaction());
63 } catch (RuntimeException e) {
64 final Optional<Throwable> optionalCause = Optional.ofNullable(e.getCause());
65 final Optional<Class> optionalCauseClass = optionalCause.map(Throwable::getClass);
66 if (optionalCauseClass.isPresent() && optionalCauseClass.get().equals(NetconfDocumentedException.class)) {
68 LOG.warn("NetconfDocumentedException thrown, retrying ({})...", attempt);
70 Thread.sleep(timeout);
71 } catch (InterruptedException i) {
72 LOG.error("Thread interrupted while waiting ... {} ", i);
75 LOG.error("Runtime exception ... {}", e.getMessage());
76 return Optional.empty();
79 } while (attempt <= 5);
80 LOG.error("Maximum number of attempts reached");
81 return Optional.empty();
84 public static Optional<ReadWriteTransaction> netconfReadWriteTransaction(DataBroker mountpoint) {
88 return Optional.of(mountpoint.newReadWriteTransaction());
89 } catch (RuntimeException e) {
90 final Optional<Throwable> optionalCause = Optional.ofNullable(e.getCause());
91 final Optional<Class> optionalCauseClass = optionalCause.map(Throwable::getClass);
92 if (optionalCauseClass.isPresent() && optionalCauseClass.get().equals(NetconfDocumentedException.class)) {
94 LOG.warn("NetconfDocumentedException thrown, retrying ({})...", attempt);
96 Thread.sleep(timeout);
97 } catch (InterruptedException i) {
98 LOG.error("Thread interrupted while waiting ... {} ", i);
101 LOG.error("Runtime exception ... {}", e.getMessage());
102 return Optional.empty();
105 } while (attempt <= 5);
106 LOG.error("Maximum number of attempts reached");
107 return Optional.empty();
111 public static void setTimeout (long newTimeout) {
112 timeout = newTimeout;