2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.groupbasedpolicy.renderer.vpp.util;
11 import java.util.AbstractMap;
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;
22 import javax.annotation.Nonnull;
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;
34 import com.google.common.base.Optional;
35 import com.google.common.base.Preconditions;
36 import com.google.common.util.concurrent.SettableFuture;
38 public class MountedDataBrokerProvider {
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
47 public MountedDataBrokerProvider(@Nonnull MountPointService mountService, @Nonnull DataBroker dataBroker) {
48 this.mountService = Preconditions.checkNotNull(mountService);
49 this.dataBroker = Preconditions.checkNotNull(dataBroker);
52 public Optional<DataBroker> resolveDataBrokerForMountPoint(@Nonnull InstanceIdentifier<Node> iidToMountPoint) {
54 if (VbdNetconfTransaction.NODE_DATA_BROKER_MAP.get(iidToMountPoint) != null) {
55 return Optional.of(VbdNetconfTransaction.NODE_DATA_BROKER_MAP.get(iidToMountPoint).getKey());
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);
71 LOG.warn("Mount point does not exist for {}", iidToMountPoint);
72 return Optional.absent();
75 LOG.warn("Failed while connecting to node, Iid: {}", iidToMountPoint);
76 return Optional.absent();
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();
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 = () -> {
98 final Optional<MountPoint> optionalMountpoint = mountService.getMountPoint(iid);
99 if (optionalMountpoint.isPresent()) {
100 return optionalMountpoint;
102 LOG.warn("Mountpoint {} is not registered yet", iid);
103 Thread.sleep(DURATION);
104 } catch (InterruptedException e) {
105 LOG.warn("Thread interrupted to ", e);
108 } while (attempt <= 3);
109 return Optional.absent();
111 return executorService.submit(task);
114 public void deleteDataBrokerForMountPoint(InstanceIdentifier<Node> mountPointIid) {
115 VbdNetconfTransaction.NODE_DATA_BROKER_MAP.remove(mountPointIid);