[ios-xe-renderer] Increases coverage for PolicyWriterUtil
[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     private NetconfTransactionCreator() {
33         throw new IllegalAccessError("instance of util class not supported");
34     }
35
36     public static Optional<ReadOnlyTransaction> netconfReadOnlyTransaction(DataBroker mountpoint) {
37         int attempt = 0;
38         do {
39             try {
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)) {
45                     attempt++;
46                     LOG.warn("NetconfDocumentedException thrown, retrying ({})...", attempt);
47                     try {
48                         Thread.sleep(timeout);
49                     } catch (InterruptedException i) {
50                         LOG.error("Thread interrupted while waiting ... {} ", i);
51                     }
52                 } else {
53                     LOG.error("Runtime exception ... {}", e.getMessage(), e);
54                     return Optional.empty();
55                 }
56             }
57         } while (attempt <= 5);
58         LOG.error("Maximum number of attempts reached");
59         return Optional.empty();
60     }
61
62     public static Optional<WriteTransaction> netconfWriteOnlyTransaction(DataBroker mountpoint) {
63         int attempt = 0;
64         do {
65             try {
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)) {
71                     attempt++;
72                     LOG.warn("NetconfDocumentedException thrown, retrying ({})...", attempt);
73                     try {
74                         Thread.sleep(timeout);
75                     } catch (InterruptedException i) {
76                         LOG.error("Thread interrupted while waiting ... {} ", i);
77                     }
78                 } else {
79                     LOG.error("Runtime exception ... {}", e.getMessage());
80                     return Optional.empty();
81                 }
82             }
83         } while (attempt <= 5);
84         LOG.error("Maximum number of attempts reached");
85         return Optional.empty();
86     }
87
88     public static Optional<ReadWriteTransaction> netconfReadWriteTransaction(DataBroker mountpoint) {
89         int attempt = 0;
90         do {
91             try {
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)) {
97                     attempt++;
98                     LOG.warn("NetconfDocumentedException thrown, retrying ({})...", attempt);
99                     try {
100                         Thread.sleep(timeout);
101                     } catch (InterruptedException i) {
102                         LOG.error("Thread interrupted while waiting ... {} ", i);
103                     }
104                 } else {
105                     LOG.error("Runtime exception ... {}", e.getMessage());
106                     return Optional.empty();
107                 }
108             }
109         } while (attempt <= 5);
110         LOG.error("Maximum number of attempts reached");
111         return Optional.empty();
112     }
113
114     @VisibleForTesting
115     public static void setTimeout (long newTimeout) {
116         timeout = newTimeout;
117     }
118 }