Bump upstreams for Silicon
[lispflowmapping.git] / mappingservice / neutron / src / main / java / org / opendaylight / lispflowmapping / neutron / intenthandler / util / VppNetconfTrasaction.java
1 /*
2  * Copyright (c) 2017 Cisco Systems, Inc.  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 package org.opendaylight.lispflowmapping.neutron.intenthandler.util;
9
10 import static java.util.Objects.requireNonNull;
11
12 import java.util.Optional;
13 import java.util.concurrent.ExecutionException;
14 import org.opendaylight.mdsal.binding.api.DataBroker;
15 import org.opendaylight.mdsal.binding.api.ReadTransaction;
16 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
17 import org.opendaylight.yangtools.yang.binding.DataObject;
18 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 /**
23  * Created by Shakib Ahmed on 1/12/17.
24  */
25 public final class VppNetconfTrasaction {
26     private static final Logger LOG = LoggerFactory.getLogger(VppNetconfTrasaction.class);
27
28     public static final byte RETRY_COUNT = 5;
29
30     private VppNetconfTrasaction() {
31     }
32
33     public static synchronized <T extends DataObject> Optional<T> read(DataBroker dataBroker,
34                                                                        LogicalDatastoreType datastoreType,
35                                                                        InstanceIdentifier<T> instanceIdentifier) {
36         LOG.trace("Started Netconf transaction on VPP Node");
37         requireNonNull(dataBroker);
38
39         Optional<T> returnData;
40
41         int retryCounter = RETRY_COUNT;
42
43         while (retryCounter > 0) {
44             ReadTransaction readTransaction = dataBroker.newReadOnlyTransaction();
45             try {
46                 returnData = readTransaction(instanceIdentifier, datastoreType, readTransaction);
47                 LOG.trace("Netconf READ transaction SUCCESSFUL. Data present: {}", returnData.isPresent());
48                 readTransaction.close();
49                 return returnData;
50             } catch (IllegalStateException e) {
51
52                 LOG.warn("Assuming that netconf read-transaction failed, retrying. Retry Count: " + retryCounter,
53                             e.getMessage());
54                 readTransaction.close();
55             } catch (InterruptedException | ExecutionException e) {
56                 LOG.warn("Exception while reading data. Retry Aborted.", e.getMessage());
57                 readTransaction.close();
58                 break;
59             }
60             retryCounter--;
61         }
62         return Optional.empty();
63     }
64
65     private static <T extends DataObject> Optional<T> readTransaction(InstanceIdentifier<T> instanceIdentifier,
66                                                                       LogicalDatastoreType datastoreType,
67                                                                       ReadTransaction readTransaction)
68             throws IllegalStateException, InterruptedException, ExecutionException {
69         return readTransaction.read(datastoreType, instanceIdentifier).get();
70     }
71 }