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 org.opendaylight.controller.md.sal.binding.api.DataBroker;
12 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
13 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
14 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
15 import org.opendaylight.netconf.api.NetconfDocumentedException;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
19 import java.util.Optional;
22 * Purpose: safely create transaction
25 public class NetconfTransactionCreator {
27 private final static Logger LOG = LoggerFactory.getLogger(NetconfTransactionCreator.class);
28 private static final long TIMEOUT = 5000L;
30 public static Optional<ReadOnlyTransaction> netconfReadOnlyTransaction(DataBroker mountpoint) {
34 return Optional.ofNullable(mountpoint.newReadOnlyTransaction());
35 } catch (RuntimeException e) {
36 final Optional<Throwable> optionalCause = Optional.ofNullable(e.getCause());
37 final Optional<Class> optionalCauseClass = optionalCause.map(Throwable::getClass);
38 if (optionalCauseClass.isPresent() && optionalCauseClass.get().equals(NetconfDocumentedException.class)) {
40 LOG.warn("NetconfDocumentedException thrown, retrying ({})...", attempt);
42 Thread.sleep(TIMEOUT);
43 } catch (InterruptedException i) {
44 LOG.error("Thread interrupted while waiting ... {} ", i);
47 LOG.error("Runtime exception ... {}", e.getMessage(), e);
48 return Optional.empty();
51 } while (attempt <= 5);
52 LOG.error("Maximum number of attempts reached");
53 return Optional.empty();
56 public static Optional<WriteTransaction> netconfWriteOnlyTransaction(DataBroker mountpoint) {
60 return Optional.of(mountpoint.newWriteOnlyTransaction());
61 } catch (RuntimeException e) {
62 final Optional<Throwable> optionalCause = Optional.ofNullable(e.getCause());
63 final Optional<Class> optionalCauseClass = optionalCause.map(Throwable::getClass);
64 if (optionalCauseClass.isPresent() && optionalCauseClass.get().equals(NetconfDocumentedException.class)) {
66 LOG.warn("NetconfDocumentedException thrown, retrying ({})...", attempt);
68 Thread.sleep(TIMEOUT);
69 } catch (InterruptedException i) {
70 LOG.error("Thread interrupted while waiting ... {} ", i);
73 LOG.error("Runtime exception ... {}", e.getMessage());
74 return Optional.empty();
77 } while (attempt <= 5);
78 LOG.error("Maximum number of attempts reached");
79 return Optional.empty();
82 public static Optional<ReadWriteTransaction> netconfReadWriteTransaction(DataBroker mountpoint) {
86 return Optional.of(mountpoint.newReadWriteTransaction());
87 } catch (RuntimeException e) {
88 final Optional<Throwable> optionalCause = Optional.ofNullable(e.getCause());
89 final Optional<Class> optionalCauseClass = optionalCause.map(Throwable::getClass);
90 if (optionalCauseClass.isPresent() && optionalCauseClass.get().equals(NetconfDocumentedException.class)) {
92 LOG.warn("NetconfDocumentedException thrown, retrying ({})...", attempt);
94 Thread.sleep(TIMEOUT);
95 } catch (InterruptedException i) {
96 LOG.error("Thread interrupted while waiting ... {} ", i);
99 LOG.error("Runtime exception ... {}", e.getMessage());
100 return Optional.empty();
103 } while (attempt <= 5);
104 LOG.error("Maximum number of attempts reached");
105 return Optional.empty();