use of org.opennms.netmgt.config.snmpAsset.adapter.MibObj in project opennms by OpenNMS.
the class SnmpAssetProvisioningAdapter method fetchSnmpAssetString.
private static String fetchSnmpAssetString(final LocationAwareSnmpClient locationAwareSnmpClient, final SnmpAgentConfig agentConfig, final String location, final List<MibObj> mibObjs, final String formatString) {
final List<String> aliases = new ArrayList<String>();
final List<SnmpObjId> objs = new ArrayList<SnmpObjId>();
for (final MibObj mibobj : mibObjs) {
aliases.add(mibobj.getAlias());
objs.add(SnmpObjId.get(mibobj.getOid()));
}
// Fetch the values from the SNMP agent
final CompletableFuture<List<SnmpValue>> future = locationAwareSnmpClient.get(agentConfig, objs).withLocation(location).execute();
List<SnmpValue> values;
try {
values = future.get();
} catch (InterruptedException | ExecutionException e) {
// Propagate
throw new RuntimeException(e);
}
if (values.size() == aliases.size()) {
final Properties substitutions = new Properties();
boolean foundAValue = false;
for (int i = 0; i < values.size(); i++) {
// If the value is a NO_SUCH_OBJECT or NO_SUCH_INSTANCE error, then skip it
if (values.get(i) == null || values.get(i).isError()) {
// No value for this OID
continue;
}
foundAValue = true;
// Use trapd's SyntaxToEvent parser so that we format base64
// and MAC address values appropriately
Parm parm = SyntaxToEvent.processSyntax(aliases.get(i), values.get(i));
substitutions.setProperty(aliases.get(i), parm.getValue().getContent());
}
if (!foundAValue) {
LOG.debug("fetchSnmpAssetString: Failed to fetch any SNMP values for system {}", agentConfig);
throw new MissingFormatArgumentException("fetchSnmpAssetString: Failed to fetch any SNMP values for system " + agentConfig.toString());
} else {
LOG.debug("fetchSnmpAssetString: Fetched asset properties from SNMP agent:\n {}", formatPropertiesAsString(substitutions));
}
if (objs.size() != substitutions.size()) {
LOG.warn("fetchSnmpAssetString: Unexpected number of properties returned from SNMP GET:\n {}", formatPropertiesAsString(substitutions));
}
return PropertiesUtils.substitute(formatString, substitutions);
} else {
LOG.warn("fetchSnmpAssetString: Invalid number of SNMP parameters returned: {} != {}", aliases.size(), values.size());
throw new MissingFormatArgumentException("fetchSnmpAssetString: Invalid number of SNMP parameters returned: " + values.size() + " != " + aliases.size());
}
}
Aggregations