Bug 9048: Vpp node reconnect bug in oxygen
[groupbasedpolicy.git] / renderers / vpp / src / main / java / org / opendaylight / groupbasedpolicy / renderer / vpp / util / MountedDataBrokerProvider.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.vpp.util;
10
11 import java.util.AbstractMap;
12 import java.util.Map;
13 import java.util.concurrent.Callable;
14 import java.util.concurrent.ExecutionException;
15 import java.util.concurrent.ExecutorService;
16 import java.util.concurrent.Executors;
17 import java.util.concurrent.Future;
18 import java.util.concurrent.TimeUnit;
19 import java.util.concurrent.TimeoutException;
20 import java.util.concurrent.locks.ReentrantLock;
21
22 import javax.annotation.Nonnull;
23
24 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
25 import org.opendaylight.controller.md.sal.binding.api.MountPoint;
26 import org.opendaylight.controller.md.sal.binding.api.MountPointService;
27 import org.opendaylight.vbd.impl.transaction.VbdNetconfTransaction;
28 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
29 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 import com.google.common.base.Optional;
35 import com.google.common.base.Preconditions;
36 import com.google.common.util.concurrent.SettableFuture;
37
38 public class MountedDataBrokerProvider {
39
40     private static final Logger LOG = LoggerFactory.getLogger(MountedDataBrokerProvider.class);
41     private static final short DURATION = 3000;
42     private final MountPointService mountService;
43     private final DataBroker dataBroker;
44     @SuppressWarnings("FieldCanBeLocal")
45     private final byte NODE_CONNECTION_TIMER = 60; // seconds
46
47     public MountedDataBrokerProvider(@Nonnull MountPointService mountService, @Nonnull DataBroker dataBroker) {
48         this.mountService = Preconditions.checkNotNull(mountService);
49         this.dataBroker = Preconditions.checkNotNull(dataBroker);
50     }
51
52     public Optional<DataBroker> resolveDataBrokerForMountPoint(@Nonnull InstanceIdentifier<Node> iidToMountPoint) {
53         try {
54             if (VbdNetconfTransaction.NODE_DATA_BROKER_MAP.get(iidToMountPoint) != null) {
55                 return Optional.of(VbdNetconfTransaction.NODE_DATA_BROKER_MAP.get(iidToMountPoint).getKey());
56             }
57             final SettableFuture<Boolean> futureNodeStatus = SettableFuture.create();
58             final NodeKey nodeKey = iidToMountPoint.firstKeyOf(Node.class);
59             new GbpVppNetconfConnectionProbe(nodeKey, futureNodeStatus, dataBroker);
60             if (futureNodeStatus.get(NODE_CONNECTION_TIMER, TimeUnit.SECONDS)) {
61                 LOG.debug("Node connected, mountpoint with iid {} available", iidToMountPoint);
62                 Future<Optional<MountPoint>> mountPointfuture = getMountpointFromSal(iidToMountPoint);
63                 Optional<MountPoint> potentialMountPoint = mountPointfuture.get();
64                 if (potentialMountPoint.isPresent()) {
65                     final Optional<DataBroker> dataBrokerOpt = potentialMountPoint.get().getService(DataBroker.class);
66                     VbdNetconfTransaction.NODE_DATA_BROKER_MAP.put(iidToMountPoint,
67                             new AbstractMap.SimpleEntry(dataBrokerOpt.get(), new ReentrantLock()));
68                     LOG.info("Lock created for {}", iidToMountPoint);
69                     return dataBrokerOpt;
70                 } else {
71                     LOG.warn("Mount point does not exist for {}", iidToMountPoint);
72                     return Optional.absent();
73                 }
74             } else {
75                 LOG.warn("Failed while connecting to node, Iid: {}", iidToMountPoint);
76                 return Optional.absent();
77             }
78         } catch (TimeoutException e) {
79             LOG.warn("Mountpoint not obtained within {} seconds. Iid: {}", NODE_CONNECTION_TIMER, iidToMountPoint, e);
80             return Optional.absent();
81         } catch (ExecutionException | InterruptedException e) {
82             LOG.warn("Error while getting mountpoint. Iid: {}", iidToMountPoint, e);
83             return Optional.absent();
84         }
85     }
86
87     // TODO bug 7699
88     // This works as a workaround for mountpoint registration in cluster. If application is registered on different
89     // node as netconf service, it obtains mountpoint registered by SlaveSalFacade (instead of MasterSalFacade). However
90     // this service registers mountpoint a moment later then connectionStatus is set to "Connected". If NodeManager hits
91     // state where device is connected but mountpoint is not yet available, try to get it again in a while
92     private Future<Optional<MountPoint>> getMountpointFromSal(final InstanceIdentifier<Node> iid) {
93         final ExecutorService executorService = Executors.newSingleThreadExecutor();
94         final Callable<Optional<MountPoint>> task = () -> {
95             byte attempt = 0;
96             do {
97                 try {
98                     final Optional<MountPoint> optionalMountpoint = mountService.getMountPoint(iid);
99                     if (optionalMountpoint.isPresent()) {
100                         return optionalMountpoint;
101                     }
102                     LOG.warn("Mountpoint {} is not registered yet", iid);
103                     Thread.sleep(DURATION);
104                 } catch (InterruptedException e) {
105                     LOG.warn("Thread interrupted to ", e);
106                 }
107                 attempt++;
108             } while (attempt <= 3);
109             return Optional.absent();
110         };
111         return executorService.submit(task);
112     }
113
114     public void deleteDataBrokerForMountPoint(InstanceIdentifier<Node> mountPointIid) {
115         VbdNetconfTransaction.NODE_DATA_BROKER_MAP.remove(mountPointIid);
116     }
117 }