use of org.opennms.netmgt.config.ami.Range in project opennms by OpenNMS.
the class AmiPeerFactory method optimize.
/**
* Combine specific and range elements so that AMIPeerFactory 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 AMI. Maybe some sort of visitor methodology would work. The basic logic should be fine as it's all IP address manipulation
*
* @throws UnknownHostException
*/
void optimize() throws UnknownHostException {
getWriteLock().lock();
try {
// First pass: Remove empty definition elements
for (final Iterator<Definition> definitionsIterator = m_config.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 : m_config.getDefinitions()) {
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 (final Definition definition : m_config.getDefinitions()) {
// Sort specifics
final TreeMap<InetAddress, String> specificsMap = new TreeMap<InetAddress, String>(new InetAddressComparator());
for (final 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 (final 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;
}
final 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()));
}
} finally {
getWriteLock().unlock();
}
}
use of org.opennms.netmgt.config.ami.Range in project opennms by OpenNMS.
the class AmiPeerFactory method getAgentConfig.
/**
* <p>getAgentConfig</p>
*
* @param agentInetAddress a {@link java.net.InetAddress} object.
* @return a {@link org.opennms.protocols.ami.AmiAgentConfig} object.
*/
public AmiAgentConfig getAgentConfig(final InetAddress agentInetAddress) {
getReadLock().lock();
try {
if (m_config == null)
return new AmiAgentConfig(agentInetAddress);
final AmiAgentConfig agentConfig = new AmiAgentConfig(agentInetAddress);
// Now set the defaults from the m_config
setAmiAgentConfig(agentConfig, new Definition());
// Attempt to locate the node
DEFLOOP: for (final Definition def : m_config.getDefinitions()) {
// check the specifics first
for (String saddr : def.getSpecifics()) {
saddr = saddr.trim();
final InetAddress addr = InetAddressUtils.addr(saddr);
if (addr.equals(agentConfig.getAddress())) {
setAmiAgentConfig(agentConfig, def);
break DEFLOOP;
}
}
// check the ranges
for (final Range rng : def.getRanges()) {
if (InetAddressUtils.isInetAddressInRange(InetAddressUtils.str(agentConfig.getAddress().orElse(null)), rng.getBegin(), rng.getEnd())) {
setAmiAgentConfig(agentConfig, def);
break DEFLOOP;
}
}
// check the matching IP expressions
for (final String ipMatch : def.getIpMatches()) {
if (IPLike.matches(InetAddressUtils.str(agentInetAddress), ipMatch)) {
setAmiAgentConfig(agentConfig, def);
break DEFLOOP;
}
}
}
if (agentConfig == null)
setAmiAgentConfig(agentConfig, new Definition());
return agentConfig;
} finally {
getReadLock().unlock();
}
}
Aggregations