7b7b8b2e7a28a56c1ef3209358016bc0c1b96ca1
[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 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;
18
19 import java.util.Optional;
20
21 /**
22  * Purpose: safely create transaction
23  */
24
25 public class NetconfTransactionCreator {
26
27     private final static Logger LOG = LoggerFactory.getLogger(NetconfTransactionCreator.class);
28     private static final long TIMEOUT = 5000L;
29
30     public static Optional<ReadOnlyTransaction> netconfReadOnlyTransaction(DataBroker mountpoint) {
31         int attempt = 0;
32         do {
33             try {
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)) {
39                     attempt++;
40                     LOG.warn("NetconfDocumentedException thrown, retrying ({})...", attempt);
41                     try {
42                         Thread.sleep(TIMEOUT);
43                     } catch (InterruptedException i) {
44                         LOG.error("Thread interrupted while waiting ... {} ", i);
45                     }
46                 } else {
47                     LOG.error("Runtime exception ... {}", e.getMessage(), e);
48                     return Optional.empty();
49                 }
50             }
51         } while (attempt <= 5);
52         LOG.error("Maximum number of attempts reached");
53         return Optional.empty();
54     }
55
56     public static Optional<WriteTransaction> netconfWriteOnlyTransaction(DataBroker mountpoint) {
57         int attempt = 0;
58         do {
59             try {
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)) {
65                     attempt++;
66                     LOG.warn("NetconfDocumentedException thrown, retrying ({})...", attempt);
67                     try {
68                         Thread.sleep(TIMEOUT);
69                     } catch (InterruptedException i) {
70                         LOG.error("Thread interrupted while waiting ... {} ", i);
71                     }
72                 } else {
73                     LOG.error("Runtime exception ... {}", e.getMessage());
74                     return Optional.empty();
75                 }
76             }
77         } while (attempt <= 5);
78         LOG.error("Maximum number of attempts reached");
79         return Optional.empty();
80     }
81
82     public static Optional<ReadWriteTransaction> netconfReadWriteTransaction(DataBroker mountpoint) {
83         int attempt = 0;
84         do {
85             try {
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)) {
91                     attempt++;
92                     LOG.warn("NetconfDocumentedException thrown, retrying ({})...", attempt);
93                     try {
94                         Thread.sleep(TIMEOUT);
95                     } catch (InterruptedException i) {
96                         LOG.error("Thread interrupted while waiting ... {} ", i);
97                     }
98                 } else {
99                     LOG.error("Runtime exception ... {}", e.getMessage());
100                     return Optional.empty();
101                 }
102             }
103         } while (attempt <= 5);
104         LOG.error("Maximum number of attempts reached");
105         return Optional.empty();
106     }
107 }