Mod aaa-12 修改读取华为设备的信息方式为监听,解决设备重启后控制器不会自动读取信息的问题。

This commit is contained in:
hujun 2019-09-20 17:26:12 +08:00
parent f665802453
commit d0c183eca5
1 changed files with 54 additions and 6 deletions

View File

@ -14,6 +14,7 @@ import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ThreadFactoryBuilder; import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.opendaylight.controller.md.sal.binding.api.*; import org.opendaylight.controller.md.sal.binding.api.*;
import org.opendaylight.controller.md.sal.binding.api.MountPointService.MountPointListener;
import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
import org.opendaylight.controller.md.sal.common.api.data.OptimisticLockFailedException; import org.opendaylight.controller.md.sal.common.api.data.OptimisticLockFailedException;
import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException; import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
@ -65,7 +66,9 @@ import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.
import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint; import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointKey; import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointKey;
import org.opendaylight.yangtools.concepts.ListenerRegistration; import org.opendaylight.yangtools.concepts.ListenerRegistration;
import org.opendaylight.yangtools.yang.binding.DataObject;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.PathArgument;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -81,7 +84,7 @@ import static org.opendaylight.controller.md.sal.binding.api.DataObjectModificat
public class HuaweiNetconfSpeaker implements DataTreeChangeListener<ConnectorInfo>, public class HuaweiNetconfSpeaker implements DataTreeChangeListener<ConnectorInfo>,
AutoCloseable { AutoCloseable, MountPointListener {
private final ListenerRegistration<HuaweiNetconfSpeaker> configurationReg; private final ListenerRegistration<HuaweiNetconfSpeaker> configurationReg;
private MountPointService mountService; private MountPointService mountService;
@ -102,6 +105,9 @@ public class HuaweiNetconfSpeaker implements DataTreeChangeListener<ConnectorInf
public HuaweiNetconfSpeaker(DataBroker dataBroker, MountPointService mountService) { public HuaweiNetconfSpeaker(DataBroker dataBroker, MountPointService mountService) {
this.dataBroker = dataBroker; this.dataBroker = dataBroker;
this.mountService = mountService; this.mountService = mountService;
if(null != this.mountService) {
this.mountService.registerListener(NETCONF_TOPO_IID, this);
}
InstanceIdentifier<ConnectorInfo> connectPath = InstanceIdentifier.create(ConnectorInfo.class); InstanceIdentifier<ConnectorInfo> connectPath = InstanceIdentifier.create(ConnectorInfo.class);
final DataTreeIdentifier<ConnectorInfo> dataTreeIdentifier = new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION, connectPath); final DataTreeIdentifier<ConnectorInfo> dataTreeIdentifier = new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION, connectPath);
configurationReg = dataBroker.registerDataTreeChangeListener(dataTreeIdentifier, this); configurationReg = dataBroker.registerDataTreeChangeListener(dataTreeIdentifier, this);
@ -145,13 +151,13 @@ public class HuaweiNetconfSpeaker implements DataTreeChangeListener<ConnectorInf
@Override @Override
public void onFailure(Throwable throwable) { public void onFailure(Throwable throwable) {
LOG.error("Failed to created NetconfNode={}", netconfNode, throwable); LOG.error("Failed to created NetconfNode={}", netconfNode, throwable);
stopTask(connectDevice.getId().getValue()); // stopTask(connectDevice.getId().getValue());
} }
@Override @Override
public void onSuccess(Void avoid) { public void onSuccess(Void avoid) {
LOG.info("NetconfNode={} created successfully", netconfNode); LOG.info("NetconfNode={} created successfully", netconfNode);
startTask(connectDevice.getId().getValue()); // startTask(connectDevice.getId().getValue());
} }
}); });
} }
@ -273,7 +279,7 @@ public class HuaweiNetconfSpeaker implements DataTreeChangeListener<ConnectorInf
} catch (Exception e) { } catch (Exception e) {
LOG.error("捕获一个异常:{}", e); LOG.error("捕获一个异常:{}", e);
} }
}, 180000, 10000, TimeUnit.MILLISECONDS); }, 10, 10000, TimeUnit.MILLISECONDS);
if (Objects.nonNull(threads.get(nodeId))) { if (Objects.nonNull(threads.get(nodeId))) {
LOG.info("remove exist pool in thread map"); LOG.info("remove exist pool in thread map");
if (!threads.get(nodeId).isShutdown()) { if (!threads.get(nodeId).isShutdown()) {
@ -289,14 +295,14 @@ public class HuaweiNetconfSpeaker implements DataTreeChangeListener<ConnectorInf
if (threads.containsKey(nodeId)) { if (threads.containsKey(nodeId)) {
ScheduledExecutorService pool = threads.get(nodeId); ScheduledExecutorService pool = threads.get(nodeId);
if (Objects.nonNull(pool)) { if (Objects.nonNull(pool)) {
pool.shutdown(); pool.shutdownNow();
} }
threads.remove(nodeId); threads.remove(nodeId);
} }
} }
private void readInfoFromDevice(String nodeId) { private void readInfoFromDevice(String nodeId) {
LOG.info("开始向华为设备获取cpu信息"); LOG.info("开始向华为设备(id:{})获取cpu信息", nodeId);
final Optional<MountPoint> hwNodeOptional = mountService.getMountPoint(NETCONF_TOPO_IID final Optional<MountPoint> hwNodeOptional = mountService.getMountPoint(NETCONF_TOPO_IID
.child(Node.class, new NodeKey(new NodeId(nodeId)))); .child(Node.class, new NodeKey(new NodeId(nodeId))));
LOG.info("开始向华为设备获取cpu信息2"); LOG.info("开始向华为设备获取cpu信息2");
@ -593,6 +599,48 @@ public class HuaweiNetconfSpeaker implements DataTreeChangeListener<ConnectorInf
return addressList; return addressList;
} }
/* (non-Javadoc)
* @see org.opendaylight.controller.md.sal.binding.api.MountPointService.MountPointListener#onMountPointCreated(org.opendaylight.yangtools.yang.binding.InstanceIdentifier)
*/
@Override
public void onMountPointCreated(InstanceIdentifier<?> path) {
LOG.info("onMountPointCreated: path is {}.", path);
String nodeid = getNodeidFromPath(path);
if (null != nodeid) {
startTask(nodeid);
} else {
LOG.error("can't get nodeid from path:{}", path);
}
}
/* (non-Javadoc)
* @see org.opendaylight.controller.md.sal.binding.api.MountPointService.MountPointListener#onMountPointRemoved(org.opendaylight.yangtools.yang.binding.InstanceIdentifier)
*/
@Override
public void onMountPointRemoved(InstanceIdentifier<?> path) {
LOG.info("onMountPointRemoved: path is {}.", path);
String nodeid = getNodeidFromPath(path);
if (null != nodeid) {
stopTask(nodeid);
}
}
private String getNodeidFromPath(InstanceIdentifier<?> path) {
InstanceIdentifier<Node> node = path.firstIdentifierOf(Node.class);
if (null == node) {
return null;
}
NodeId nodeId = node.firstKeyOf(Node.class).getNodeId();
if (null == nodeId) {
return null;
} else {
return nodeId.getValue();
}
}
} }
/** /**