use of org.opennms.netmgt.config.wmi.agent.Definition in project opennms by OpenNMS.
the class WmiPeerFactory method getAgentConfig.
/**
* <p>getAgentConfig</p>
*
* @param agentInetAddress a {@link java.net.InetAddress} object.
* @return a {@link org.opennms.protocols.wmi.WmiAgentConfig} object.
*/
public synchronized WmiAgentConfig getAgentConfig(InetAddress agentInetAddress) {
if (getConfig() == null) {
return new WmiAgentConfig(agentInetAddress);
}
WmiAgentConfig agentConfig = new WmiAgentConfig(agentInetAddress);
//Now set the defaults from the m_config
setWmiAgentConfig(agentConfig, new Definition());
// Attempt to locate the node
//
Iterator<Definition> edef = getConfig().getDefinitions().iterator();
DEFLOOP: while (edef.hasNext()) {
Definition def = edef.next();
// check the specifics first
for (String saddr : def.getSpecifics()) {
InetAddress addr = InetAddressUtils.addr(saddr);
if (addr.equals(agentConfig.getAddress())) {
setWmiAgentConfig(agentConfig, def);
break DEFLOOP;
}
}
// check the ranges
for (Range rng : def.getRanges()) {
if (InetAddressUtils.isInetAddressInRange(InetAddressUtils.str(agentConfig.getAddress()), rng.getBegin(), rng.getEnd())) {
setWmiAgentConfig(agentConfig, def);
break DEFLOOP;
}
}
//
for (String ipMatch : def.getIpMatches()) {
if (IPLike.matches(InetAddressUtils.str(agentInetAddress), ipMatch)) {
setWmiAgentConfig(agentConfig, def);
break DEFLOOP;
}
}
}
if (agentConfig == null) {
Definition def = new Definition();
setWmiAgentConfig(agentConfig, def);
}
return agentConfig;
}
use of org.opennms.netmgt.config.wmi.agent.Definition in project opennms by OpenNMS.
the class WmiPeerFactory method optimize.
/**
* Combine specific and range elements so that WMIPeerFactory has to spend
* less time iterating all these elements.
* TODO This really should be pulled up into PeerFactory somehow, but I'm not sure how (given that "Definition" is different for both
* SNMP and WMI. Maybe some sort of visitor methodology would work. The basic logic should be fine as it's all IP address manipulation
*
* Calls should be preceded by a getWriteLock().lock() and wrapped in a try / finally block with getWriteLock().unlock().
* @throws UnknownHostException
*/
public void optimize() throws UnknownHostException {
// First pass: Remove empty definition elements
for (Iterator<Definition> definitionsIterator = getConfig().getDefinitions().iterator(); definitionsIterator.hasNext(); ) {
final Definition definition = definitionsIterator.next();
if (definition.getSpecifics().size() == 0 && definition.getRanges().size() == 0) {
LOG.debug("optimize: Removing empty definition element");
definitionsIterator.remove();
}
}
// Second pass: Replace single IP range elements with specific elements
for (Definition definition : getConfig().getDefinitions()) {
synchronized (definition) {
for (Iterator<Range> rangesIterator = definition.getRanges().iterator(); rangesIterator.hasNext(); ) {
Range range = rangesIterator.next();
if (range.getBegin().equals(range.getEnd())) {
definition.addSpecific(range.getBegin());
rangesIterator.remove();
}
}
}
}
// readability and then combine them into fewer elements where possible
for (Iterator<Definition> defIterator = getConfig().getDefinitions().iterator(); defIterator.hasNext(); ) {
Definition definition = defIterator.next();
// Sort specifics
final TreeMap<InetAddress, String> specificsMap = new TreeMap<InetAddress, String>(new InetAddressComparator());
for (String specific : definition.getSpecifics()) {
specificsMap.put(InetAddressUtils.getInetAddress(specific), specific.trim());
}
// Sort ranges
final TreeMap<InetAddress, Range> rangesMap = new TreeMap<InetAddress, Range>(new InetAddressComparator());
for (Range range : definition.getRanges()) {
rangesMap.put(InetAddressUtils.getInetAddress(range.getBegin()), range);
}
// Combine consecutive specifics into ranges
InetAddress priorSpecific = null;
Range addedRange = null;
for (final InetAddress specific : specificsMap.keySet()) {
if (priorSpecific == null) {
priorSpecific = specific;
continue;
}
if (BigInteger.ONE.equals(InetAddressUtils.difference(specific, priorSpecific)) && InetAddressUtils.inSameScope(specific, priorSpecific)) {
if (addedRange == null) {
addedRange = new Range();
addedRange.setBegin(InetAddressUtils.toIpAddrString(priorSpecific));
rangesMap.put(priorSpecific, addedRange);
specificsMap.remove(priorSpecific);
}
addedRange.setEnd(InetAddressUtils.toIpAddrString(specific));
specificsMap.remove(specific);
} else {
addedRange = null;
}
priorSpecific = specific;
}
// Move specifics to ranges
for (final InetAddress specific : new ArrayList<InetAddress>(specificsMap.keySet())) {
for (final InetAddress begin : new ArrayList<InetAddress>(rangesMap.keySet())) {
if (!InetAddressUtils.inSameScope(begin, specific)) {
continue;
}
if (InetAddressUtils.toInteger(begin).subtract(BigInteger.ONE).compareTo(InetAddressUtils.toInteger(specific)) > 0) {
continue;
}
Range range = rangesMap.get(begin);
final InetAddress end = InetAddressUtils.getInetAddress(range.getEnd());
if (InetAddressUtils.toInteger(end).add(BigInteger.ONE).compareTo(InetAddressUtils.toInteger(specific)) < 0) {
continue;
}
if (InetAddressUtils.toInteger(specific).compareTo(InetAddressUtils.toInteger(begin)) >= 0 && InetAddressUtils.toInteger(specific).compareTo(InetAddressUtils.toInteger(end)) <= 0) {
specificsMap.remove(specific);
break;
}
if (InetAddressUtils.toInteger(begin).subtract(BigInteger.ONE).equals(InetAddressUtils.toInteger(specific))) {
rangesMap.remove(begin);
rangesMap.put(specific, range);
range.setBegin(InetAddressUtils.toIpAddrString(specific));
specificsMap.remove(specific);
break;
}
if (InetAddressUtils.toInteger(end).add(BigInteger.ONE).equals(InetAddressUtils.toInteger(specific))) {
range.setEnd(InetAddressUtils.toIpAddrString(specific));
specificsMap.remove(specific);
break;
}
}
}
// Combine consecutive ranges
Range priorRange = null;
InetAddress priorBegin = null;
InetAddress priorEnd = null;
for (final Iterator<InetAddress> rangesIterator = rangesMap.keySet().iterator(); rangesIterator.hasNext(); ) {
final InetAddress beginAddress = rangesIterator.next();
final Range range = rangesMap.get(beginAddress);
final InetAddress endAddress = InetAddressUtils.getInetAddress(range.getEnd());
if (priorRange != null) {
if (InetAddressUtils.inSameScope(beginAddress, priorEnd) && InetAddressUtils.difference(beginAddress, priorEnd).compareTo(BigInteger.ONE) <= 0) {
priorBegin = new InetAddressComparator().compare(priorBegin, beginAddress) < 0 ? priorBegin : beginAddress;
priorRange.setBegin(InetAddressUtils.toIpAddrString(priorBegin));
priorEnd = new InetAddressComparator().compare(priorEnd, endAddress) > 0 ? priorEnd : endAddress;
priorRange.setEnd(InetAddressUtils.toIpAddrString(priorEnd));
rangesIterator.remove();
continue;
}
}
priorRange = range;
priorBegin = beginAddress;
priorEnd = endAddress;
}
// Update changes made to sorted maps
definition.setSpecifics(new ArrayList<>(specificsMap.values()));
definition.setRanges(new ArrayList<>(rangesMap.values()));
}
}
Aggregations