Add Neutron host-id to RLOC mapping
[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 com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import com.google.common.util.concurrent.CheckedFuture;
13
14 import java.util.concurrent.ExecutionException;
15
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
20 import org.opendaylight.yangtools.yang.binding.DataObject;
21 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 /**
26  * Created by Shakib Ahmed on 1/12/17.
27  */
28 public class VppNetconfTrasaction {
29     private static final Logger LOG = LoggerFactory.getLogger(VppNetconfTrasaction.class);
30
31     public static final byte RETRY_COUNT = 5;
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         Preconditions.checkNotNull(dataBroker);
38
39         Optional<T> returnData;
40
41         int retryCounter = RETRY_COUNT;
42
43         while (retryCounter > 0) {
44             ReadOnlyTransaction 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.absent();
63     }
64
65     private static <T extends DataObject> Optional<T> readTransaction(InstanceIdentifier<T> instanceIdentifier,
66                                                                       LogicalDatastoreType datastoreType,
67                                                                       ReadOnlyTransaction readTransaction)
68             throws IllegalStateException, InterruptedException, ExecutionException {
69
70         CheckedFuture<Optional<T>, ReadFailedException> futureData =
71                 readTransaction.read(datastoreType, instanceIdentifier);
72
73         return futureData.get();
74     }
75 }