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 private NetconfTransactionCreator() {
33 throw new IllegalAccessError("instance of util class not supported");
36 public static Optional<ReadOnlyTransaction> netconfReadOnlyTransaction(DataBroker mountpoint) {
40 return Optional.ofNullable(mountpoint.newReadOnlyTransaction());
41 } catch (RuntimeException e) {
42 final Optional<Throwable> optionalCause = Optional.ofNullable(e.getCause());
43 final Optional<Class> optionalCauseClass = optionalCause.map(Throwable::getClass);
44 if (optionalCauseClass.isPresent() && optionalCauseClass.get().equals(NetconfDocumentedException.class)) {
46 LOG.warn("NetconfDocumentedException thrown, retrying ({})...", attempt);
48 Thread.sleep(timeout);
49 } catch (InterruptedException i) {
50 LOG.error("Thread interrupted while waiting ... {} ", i);
53 LOG.error("Runtime exception ... {}", e.getMessage(), e);
54 return Optional.empty();
57 } while (attempt <= 5);
58 LOG.error("Maximum number of attempts reached");
59 return Optional.empty();
62 public static Optional<WriteTransaction> netconfWriteOnlyTransaction(DataBroker mountpoint) {
66 return Optional.of(mountpoint.newWriteOnlyTransaction());
67 } catch (RuntimeException e) {
68 final Optional<Throwable> optionalCause = Optional.ofNullable(e.getCause());
69 final Optional<Class> optionalCauseClass = optionalCause.map(Throwable::getClass);
70 if (optionalCauseClass.isPresent() && optionalCauseClass.get().equals(NetconfDocumentedException.class)) {
72 LOG.warn("NetconfDocumentedException thrown, retrying ({})...", attempt);
74 Thread.sleep(timeout);
75 } catch (InterruptedException i) {
76 LOG.error("Thread interrupted while waiting ... {} ", i);
79 LOG.error("Runtime exception ... {}", e.getMessage());
80 return Optional.empty();
83 } while (attempt <= 5);
84 LOG.error("Maximum number of attempts reached");
85 return Optional.empty();
88 public static Optional<ReadWriteTransaction> netconfReadWriteTransaction(DataBroker mountpoint) {
92 return Optional.of(mountpoint.newReadWriteTransaction());
93 } catch (RuntimeException e) {
94 final Optional<Throwable> optionalCause = Optional.ofNullable(e.getCause());
95 final Optional<Class> optionalCauseClass = optionalCause.map(Throwable::getClass);
96 if (optionalCauseClass.isPresent() && optionalCauseClass.get().equals(NetconfDocumentedException.class)) {
98 LOG.warn("NetconfDocumentedException thrown, retrying ({})...", attempt);
100 Thread.sleep(timeout);
101 } catch (InterruptedException i) {
102 LOG.error("Thread interrupted while waiting ... {} ", i);
105 LOG.error("Runtime exception ... {}", e.getMessage());
106 return Optional.empty();
109 } while (attempt <= 5);
110 LOG.error("Maximum number of attempts reached");
111 return Optional.empty();
115 public static void setTimeout (long newTimeout) {
116 timeout = newTimeout;