56bd6886d94a2ca31d0fbc19ac23403967dd089c
[groupbasedpolicy.git] / renderers / ios-xe / src / main / java / org / opendaylight / groupbasedpolicy / renderer / ios_xe_provider / impl / writer / NetconfTransactionCreator.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.groupbasedpolicy.renderer.ios_xe_provider.impl.writer;
10
11 import java.util.Optional;
12
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;
20
21 import com.google.common.annotations.VisibleForTesting;
22
23 /**
24  * Purpose: safely create transaction
25  */
26
27 public class NetconfTransactionCreator {
28
29     private final static Logger LOG = LoggerFactory.getLogger(NetconfTransactionCreator.class);
30     private static long timeout = 5000L;
31
32     public static Optional<ReadOnlyTransaction> netconfReadOnlyTransaction(DataBroker mountpoint) {
33         int attempt = 0;
34         do {
35             try {
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)) {
41                     attempt++;
42                     LOG.warn("NetconfDocumentedException thrown, retrying ({})...", attempt);
43                     try {
44                         Thread.sleep(timeout);
45                     } catch (InterruptedException i) {
46                         LOG.error("Thread interrupted while waiting ... {} ", i);
47                     }
48                 } else {
49                     LOG.error("Runtime exception ... {}", e.getMessage(), e);
50                     return Optional.empty();
51                 }
52             }
53         } while (attempt <= 5);
54         LOG.error("Maximum number of attempts reached");
55         return Optional.empty();
56     }
57
58     public static Optional<WriteTransaction> netconfWriteOnlyTransaction(DataBroker mountpoint) {
59         int attempt = 0;
60         do {
61             try {
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)) {
67                     attempt++;
68                     LOG.warn("NetconfDocumentedException thrown, retrying ({})...", attempt);
69                     try {
70                         Thread.sleep(timeout);
71                     } catch (InterruptedException i) {
72                         LOG.error("Thread interrupted while waiting ... {} ", i);
73                     }
74                 } else {
75                     LOG.error("Runtime exception ... {}", e.getMessage());
76                     return Optional.empty();
77                 }
78             }
79         } while (attempt <= 5);
80         LOG.error("Maximum number of attempts reached");
81         return Optional.empty();
82     }
83
84     public static Optional<ReadWriteTransaction> netconfReadWriteTransaction(DataBroker mountpoint) {
85         int attempt = 0;
86         do {
87             try {
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)) {
93                     attempt++;
94                     LOG.warn("NetconfDocumentedException thrown, retrying ({})...", attempt);
95                     try {
96                         Thread.sleep(timeout);
97                     } catch (InterruptedException i) {
98                         LOG.error("Thread interrupted while waiting ... {} ", i);
99                     }
100                 } else {
101                     LOG.error("Runtime exception ... {}", e.getMessage());
102                     return Optional.empty();
103                 }
104             }
105         } while (attempt <= 5);
106         LOG.error("Maximum number of attempts reached");
107         return Optional.empty();
108     }
109
110     @VisibleForTesting
111     public static void setTimeout (long newTimeout) {
112         timeout = newTimeout;
113     }
114 }