Mod aaa-12 修复无法获取cpu、内存的bug。

RCA:
SOL:
修改人:dongxiancun
检视人:dongxiancun
This commit is contained in:
dongxiancun 2019-08-27 19:15:10 +08:00
parent f0df2f8df2
commit 86c714344f
4 changed files with 58 additions and 40 deletions

View File

@ -26,9 +26,7 @@ module device-status {
container memory-infos { container memory-infos {
description "内存的状态信息"; description "内存的状态信息";
leaf memory-total { leaf memory-total {
type int16 { type int64 ;
range "0..100";
}
} }
leaf usage-rate { leaf usage-rate {
type int16 { type int16 {

View File

@ -8,6 +8,7 @@
package com.cmcc.cmhi.huawei.impl; package com.cmcc.cmhi.huawei.impl;
import org.opendaylight.controller.md.sal.binding.api.DataBroker; import org.opendaylight.controller.md.sal.binding.api.DataBroker;
import org.opendaylight.controller.md.sal.binding.api.MountPointService;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -16,13 +17,15 @@ public class HuaweiDriverProvider {
private static final Logger LOG = LoggerFactory.getLogger(HuaweiDriverProvider.class); private static final Logger LOG = LoggerFactory.getLogger(HuaweiDriverProvider.class);
private final DataBroker dataBroker; private final DataBroker dataBroker;
private final MountPointService mountService;
private HuaweiDriverRegister register = null; private HuaweiDriverRegister register = null;
private HuaweiNetconfSpeaker netconfSpeaker = null; private HuaweiNetconfSpeaker netconfSpeaker = null;
public HuaweiDriverProvider(final DataBroker dataBroker) { public HuaweiDriverProvider(final DataBroker dataBroker, final MountPointService mountService) {
this.dataBroker = dataBroker; this.dataBroker = dataBroker;
this.mountService = mountService;
this.register = new HuaweiDriverRegister(this.dataBroker); this.register = new HuaweiDriverRegister(this.dataBroker);
this.netconfSpeaker = new HuaweiNetconfSpeaker(this.dataBroker); this.netconfSpeaker = new HuaweiNetconfSpeaker(this.dataBroker, this.mountService);
} }
/** /**

View File

@ -52,12 +52,14 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev15
import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology; import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId; import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId; import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId;
import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology; import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey; import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node; import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder; import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey; import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
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.yangtools.concepts.ListenerRegistration; import org.opendaylight.yangtools.concepts.ListenerRegistration;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -75,7 +77,7 @@ import static org.opendaylight.controller.md.sal.binding.api.DataObjectModificat
public class HuaweiNetconfSpeaker implements DataTreeChangeListener<ConnectorInfo>, public class HuaweiNetconfSpeaker implements DataTreeChangeListener<ConnectorInfo>,
BindingAwareProvider, AutoCloseable { AutoCloseable {
private final ListenerRegistration<HuaweiNetconfSpeaker> configurationReg; private final ListenerRegistration<HuaweiNetconfSpeaker> configurationReg;
private MountPointService mountService; private MountPointService mountService;
@ -93,8 +95,9 @@ public class HuaweiNetconfSpeaker implements DataTreeChangeListener<ConnectorInf
private ThreadFactory timeThreadFactory = new ThreadFactoryBuilder().setNameFormat("huawei-pool-%d").setDaemon(true).build(); private ThreadFactory timeThreadFactory = new ThreadFactoryBuilder().setNameFormat("huawei-pool-%d").setDaemon(true).build();
public HuaweiNetconfSpeaker(DataBroker dataBroker) { public HuaweiNetconfSpeaker(DataBroker dataBroker, MountPointService mountService) {
this.dataBroker = dataBroker; this.dataBroker = dataBroker;
this.mountService = mountService;
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);
@ -131,7 +134,7 @@ public class HuaweiNetconfSpeaker implements DataTreeChangeListener<ConnectorInf
.build(); .build();
final WriteTransaction transaction = dataBroker.newWriteOnlyTransaction(); final WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
transaction.put(LogicalDatastoreType.CONFIGURATION, NetconfIidFactory.netconfNodeIid(nodeId.getValue()), node); transaction.put(LogicalDatastoreType.CONFIGURATION, NetconfIidFactory.netconfNodeIid(nodeId.getValue()), node, true);
Futures.addCallback(transaction.submit(), new FutureCallback<Void>() { Futures.addCallback(transaction.submit(), new FutureCallback<Void>() {
@ -143,7 +146,7 @@ public class HuaweiNetconfSpeaker implements DataTreeChangeListener<ConnectorInf
@Override @Override
public void onSuccess(Void avoid) { public void onSuccess(Void avoid) {
LOG.debug("NetconfNode={} created successfully", netconfNode); LOG.info("NetconfNode={} created successfully", netconfNode);
startTask(connectDevice.getId().getValue()); startTask(connectDevice.getId().getValue());
} }
}); });
@ -172,22 +175,22 @@ public class HuaweiNetconfSpeaker implements DataTreeChangeListener<ConnectorInf
configurationReg.close(); configurationReg.close();
} }
/** // /**
* A method called when the session to MD-SAL is established. It initializes // * A method called when the session to MD-SAL is established. It initializes
* references to MD-SAL services needed throughout the lifetime of the // * references to MD-SAL services needed throughout the lifetime of the
* huawei netconf application and registers its RPC implementation and Data change // * huawei netconf application and registers its RPC implementation and Data change
* Listener with the MD-SAL // * Listener with the MD-SAL
* <p> // * <p>
* The skeleton for this method was generated with the MD-SAL application // * The skeleton for this method was generated with the MD-SAL application
* archetype. // * archetype.
* // *
* @param session Reference to the established MD-SAL session // * @param session Reference to the established MD-SAL session
*/ // */
@Override // @Override
public void onSessionInitiated(BindingAwareBroker.ProviderContext session) { // public void onSessionInitiated(BindingAwareBroker.ProviderContext session) {
LOG.info("HuaweiNetconfSpeaker Session Initiated"); // LOG.info("HuaweiNetconfSpeaker Session Initiated");
this.mountService = session.getSALService(MountPointService.class); // this.mountService = session.getSALService(MountPointService.class);
} // }
@Override @Override
@ -213,7 +216,7 @@ public class HuaweiNetconfSpeaker implements DataTreeChangeListener<ConnectorInf
LOG.info("onDataTreeChanged - Toaster config with path {} was deleted: old Toaster: {}", LOG.info("onDataTreeChanged - Toaster config with path {} was deleted: old Toaster: {}",
change.getRootPath().getRootIdentifier(), rootNode.getDataBefore()); change.getRootPath().getRootIdentifier(), rootNode.getDataBefore());
ConnectorInfo oldEntry = rootNode.getDataBefore(); ConnectorInfo oldEntry = rootNode.getDataBefore();
oldEntry.getRemoteDevices().forEach( d -> { oldEntry.getRemoteDevices().forEach(d -> {
disConnectDevice(d.getId().getValue()); disConnectDevice(d.getId().getValue());
}); });
@ -261,15 +264,19 @@ public class HuaweiNetconfSpeaker implements DataTreeChangeListener<ConnectorInf
ScheduledExecutorService pool = new ScheduledThreadPoolExecutor(2, timeThreadFactory, ScheduledExecutorService pool = new ScheduledThreadPoolExecutor(2, timeThreadFactory,
new ThreadPoolExecutor.AbortPolicy()); new ThreadPoolExecutor.AbortPolicy());
pool.scheduleAtFixedRate(() -> { pool.scheduleAtFixedRate(() -> {
try {
readInfoFromDevice(nodeId); readInfoFromDevice(nodeId);
}, 0, 10000, TimeUnit.MILLISECONDS); } catch (Exception e) {
LOG.error("捕获一个异常:{}", e);
}
}, 180000, 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()) {
threads.get(nodeId).shutdownNow(); threads.get(nodeId).shutdownNow();
} }
} }
LOG.info("put pool in thread map with key {}",nodeId); LOG.info("put pool in thread map with key {}", nodeId);
threads.put(nodeId, pool); threads.put(nodeId, pool);
} }
@ -285,13 +292,15 @@ public class HuaweiNetconfSpeaker implements DataTreeChangeListener<ConnectorInf
} }
private void readInfoFromDevice(String nodeId) { private void readInfoFromDevice(String nodeId) {
LOG.info("开始向华为设备获取cpu信息");
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");
Preconditions.checkArgument(hwNodeOptional.isPresent(), Preconditions.checkArgument(hwNodeOptional.isPresent(),
"Unable to locate mountpoint: %s, not mounted yet or not configured", "Unable to locate mountpoint: %s, not mounted yet or not configured",
nodeId); nodeId);
final MountPoint hwNode = hwNodeOptional.get(); final MountPoint hwNode = hwNodeOptional.get();
LOG.info("Get the DataBroker for the mounted node ");
// Get the DataBroker for the mounted node // Get the DataBroker for the mounted node
final DataBroker hwNodeBroker = hwNode.getService(DataBroker.class).get(); final DataBroker hwNodeBroker = hwNode.getService(DataBroker.class).get();
@ -308,7 +317,7 @@ public class HuaweiNetconfSpeaker implements DataTreeChangeListener<ConnectorInf
InstanceIdentifier<CpuInfos> iid = InstanceIdentifier<CpuInfos> iid =
InstanceIdentifier.create(Devm.class).child(CpuInfos.class); InstanceIdentifier.create(Devm.class).child(CpuInfos.class);
Optional<CpuInfos> cupInfos; Optional<CpuInfos> cupInfos;
LOG.info("开始向华为设备获取cpu信息"); LOG.info("开始向华为设备读取cupInfos");
try { try {
// Read from a transaction is asynchronous, but a simple // Read from a transaction is asynchronous, but a simple
// get/checkedGet makes the call synchronous // get/checkedGet makes the call synchronous
@ -331,9 +340,10 @@ public class HuaweiNetconfSpeaker implements DataTreeChangeListener<ConnectorInf
iid, nodeId); iid, nodeId);
} }
//write cpu information to layer //write cpu information to layer
InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.cmcc.cmhi.adaptation.layer.device.status.rev190809.cpu.status.CpuInfos> cpuIid = NETCONF_TOPO_IID.child(Node.class). InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.cmcc.cmhi.adaptation.layer.device.status.rev190809.cpu.status.CpuInfos> cpuIid = NETCONF_TOPO_IID
augmentation(NodeCpu.class). .child(Node.class, new NodeKey(new NodeId(nodeId)))
child(org.opendaylight.yang.gen.v1.urn.cmcc.cmhi.adaptation.layer.device.status.rev190809.cpu.status.CpuInfos.class); .augmentation(NodeCpu.class)
.child(org.opendaylight.yang.gen.v1.urn.cmcc.cmhi.adaptation.layer.device.status.rev190809.cpu.status.CpuInfos.class);
for (int i = 0; i < ifcList.size(); i++) { for (int i = 0; i < ifcList.size(); i++) {
final WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction(); final WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
writeTransaction.put(LogicalDatastoreType.OPERATIONAL, cpuIid, ifcList.get(i)); writeTransaction.put(LogicalDatastoreType.OPERATIONAL, cpuIid, ifcList.get(i));
@ -369,7 +379,8 @@ public class HuaweiNetconfSpeaker implements DataTreeChangeListener<ConnectorInf
LOG.info("Show memory with serial {},cpu usage is {} ", LOG.info("Show memory with serial {},cpu usage is {} ",
memoryInfo.getKey().getPosition(), memoryInfo.getDoMemoryUsage()); memoryInfo.getKey().getPosition(), memoryInfo.getDoMemoryUsage());
MemoryInfosBuilder builder = new MemoryInfosBuilder(); MemoryInfosBuilder builder = new MemoryInfosBuilder();
org.opendaylight.yang.gen.v1.urn.cmcc.cmhi.adaptation.layer.device.status.rev190809.mem.status.MemoryInfos temp = builder.setMemoryTotal(memoryInfo.getOsMemoryTotal().shortValue()). LOG.info("Huawei Device {} OsMemoryTotal is: {}", nodeId, memoryInfo.getOsMemoryTotal());
org.opendaylight.yang.gen.v1.urn.cmcc.cmhi.adaptation.layer.device.status.rev190809.mem.status.MemoryInfos temp = builder.setMemoryTotal(memoryInfo.getOsMemoryTotal()).
setUsageRate(memoryInfo.getOsMemoryUsage().shortValue()).build(); setUsageRate(memoryInfo.getOsMemoryUsage().shortValue()).build();
memoryList.add(temp); memoryList.add(temp);
@ -380,9 +391,10 @@ public class HuaweiNetconfSpeaker implements DataTreeChangeListener<ConnectorInf
} }
//write memory information to layer //write memory information to layer
InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.cmcc.cmhi.adaptation.layer.device.status.rev190809.mem.status.MemoryInfos> customMemoryIID = NETCONF_TOPO_IID.child(Node.class). InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.cmcc.cmhi.adaptation.layer.device.status.rev190809.mem.status.MemoryInfos> customMemoryIID = NETCONF_TOPO_IID
augmentation(NodeMemory.class). .child(Node.class, new NodeKey(new NodeId(nodeId)))
child(org.opendaylight.yang.gen.v1.urn.cmcc.cmhi.adaptation.layer.device.status.rev190809.mem.status.MemoryInfos.class); .augmentation(NodeMemory.class)
.child(org.opendaylight.yang.gen.v1.urn.cmcc.cmhi.adaptation.layer.device.status.rev190809.mem.status.MemoryInfos.class);
for (int i = 0; i < memoryList.size(); i++) { for (int i = 0; i < memoryList.size(); i++) {
final WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction(); final WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
writeTransaction.put(LogicalDatastoreType.OPERATIONAL, customMemoryIID, memoryList.get(i)); writeTransaction.put(LogicalDatastoreType.OPERATIONAL, customMemoryIID, memoryList.get(i));
@ -433,9 +445,11 @@ public class HuaweiNetconfSpeaker implements DataTreeChangeListener<ConnectorInf
//write interface information to layer //write interface information to layer
InstanceIdentifier<TpInfos> tpinfosIID = NETCONF_TOPO_IID.child(Node.class).
child(TerminationPoint.class).augmentation(TpExt.class).child(TpInfos.class);
for (int i = 0; i < tpInfosList.size(); i++) { for (int i = 0; i < tpInfosList.size(); i++) {
InstanceIdentifier<TpInfos> tpinfosIID = NETCONF_TOPO_IID
.child(Node.class, new NodeKey(new NodeId(nodeId)))
.child(TerminationPoint.class, new TerminationPointKey(new TpId(tpInfosList.get(i).getTpName())))
.augmentation(TpExt.class).child(TpInfos.class);
final WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction(); final WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
writeTransaction.put(LogicalDatastoreType.OPERATIONAL, tpinfosIID, tpInfosList.get(i)); writeTransaction.put(LogicalDatastoreType.OPERATIONAL, tpinfosIID, tpInfosList.get(i));
Futures.addCallback(writeTransaction.submit(), new FutureCallback<Void>() { Futures.addCallback(writeTransaction.submit(), new FutureCallback<Void>() {

View File

@ -14,11 +14,14 @@ and is available at http://www.eclipse.org/legal/epl-v10.html
<reference id="dataBroker" <reference id="dataBroker"
interface="org.opendaylight.controller.md.sal.binding.api.DataBroker" interface="org.opendaylight.controller.md.sal.binding.api.DataBroker"
odl:type="default"/> odl:type="default"/>
<reference id="mountService"
interface="org.opendaylight.controller.md.sal.binding.api.MountPointService" />
<bean id="provider" <bean id="provider"
class="com.cmcc.cmhi.huawei.impl.HuaweiDriverProvider" class="com.cmcc.cmhi.huawei.impl.HuaweiDriverProvider"
init-method="init" destroy-method="close"> init-method="init" destroy-method="close">
<argument ref="dataBroker"/> <argument ref="dataBroker"/>
<argument ref="mountService"/>
</bean> </bean>
</blueprint> </blueprint>