use of org.opennms.netmgt.config.snmp.Range in project opennms by OpenNMS.
the class RangeComparatorTest method testCompare.
@Test
public void testCompare() {
List<Range> ranges = new ArrayList<Range>();
Range addMe = new Range();
addMe.setBegin("192.168.101.1");
addMe.setEnd("192.168.101.254");
ranges.add(addMe);
addMe = new Range();
addMe.setBegin("192.168.100.1");
addMe.setEnd("192.168.100.254");
ranges.add(addMe);
addMe = new Range();
addMe.setBegin("10.1.2.1");
addMe.setEnd("10.1.2.100");
ranges.add(addMe);
addMe = new Range();
addMe.setBegin("11.1.2.1");
addMe.setEnd("11.1.2.100");
ranges.add(addMe);
addMe = new Range();
addMe.setBegin("12.1.2.1");
addMe.setEnd("12.1.2.100");
ranges.add(addMe);
Collections.sort(ranges, new RangeComparator());
assertEquals("10.1.2.1", ranges.get(0).getBegin());
assertEquals("10.1.2.100", ranges.get(0).getEnd());
assertEquals("11.1.2.1", ranges.get(1).getBegin());
assertEquals("11.1.2.100", ranges.get(1).getEnd());
assertEquals("12.1.2.1", ranges.get(2).getBegin());
assertEquals("12.1.2.100", ranges.get(2).getEnd());
assertEquals("192.168.100.1", ranges.get(3).getBegin());
assertEquals("192.168.100.254", ranges.get(3).getEnd());
assertEquals("192.168.101.1", ranges.get(4).getBegin());
assertEquals("192.168.101.254", ranges.get(4).getEnd());
/*
<?xml version="1.0"?>
<snmp-config retry="3" timeout="800"
read-community="public" write-community="private">
<definition version="v2c">
<specific>192.168.0.5</specific>
</definition>
<definition read-community="opennmsrules">
<range begin="192.168.100.1" end="192.168.100.254"/>
<range begin="192.168.101.1" end="192.168.101.254"/>
<range begin="192.168.102.1" end="192.168.102.254"/>
<range begin="192.168.103.1" end="192.168.103.254"/>
<range begin="192.168.104.1" end="192.168.104.254"/>
<range begin="192.168.105.1" end="192.168.105.254"/>
<range begin="192.168.106.1" end="192.168.106.254"/>
<range begin="192.168.107.1" end="192.168.107.254"/>
<range begin="192.168.0.1" end="192.168.0.10"/>
</definition>
<definition version="v2c" read-community="splice-test">
<range begin="10.1.2.1" end="10.1.2.100"/>
<range begin="11.1.2.1" end="11.1.2.100"/>
<range begin="12.1.2.1" end="12.1.2.100"/>
<specific>10.1.1.1</specific>
<specific>10.1.1.2</specific>
<specific>10.1.1.3</specific>
<specific>10.1.1.5</specific>
<specific>10.1.1.6</specific>
<specific>10.1.1.10</specific>
</definition>
<definition read-community="splice2-test">
<range begin="10.1.1.11" end="10.1.1.100"/>
<range begin="11.1.2.1" end="11.1.2.100"/>
<range begin="12.1.2.1" end="12.1.2.100"/>
<specific>10.1.1.10</specific>
</definition>
<definition read-community="splice3-test">
<range begin="10.1.1.11" end="10.1.1.100"/>
<range begin="11.1.2.1" end="11.1.2.1"/>
<range begin="12.1.2.1" end="12.1.2.1"/>
<specific>10.1.1.10</specific>
<specific>10.1.1.12</specific>
</definition>
</snmp-config>
*/
}
use of org.opennms.netmgt.config.snmp.Range in project opennms by OpenNMS.
the class MergeableDefinition method mergeMatchingAttributeDef.
/**
* This method is called when a definition is found in the config and
* that has the same attributes as the params in the configureSNMP event and
* the IP specific/range needs to be merged into the definition.
*
* @param eventDefefinition a {@link org.opennms.netmgt.config.MergeableDefinition} object.
*/
protected void mergeMatchingAttributeDef(MergeableDefinition eventDefinition) {
m_configRanges.addAll(eventDefinition.getAddressRanges());
getConfigDef().setRanges(new ArrayList<Range>());
getConfigDef().setSpecifics(new ArrayList<String>());
for (IPAddressRange range : m_configRanges) {
if (range.isSingleton()) {
getConfigDef().addSpecific(range.getBegin().toUserString());
} else {
Range xmlRange = new Range();
xmlRange.setBegin(range.getBegin().toUserString());
xmlRange.setEnd(range.getEnd().toUserString());
getConfigDef().addRange(xmlRange);
}
}
}
use of org.opennms.netmgt.config.snmp.Range in project opennms by OpenNMS.
the class MergeableDefinition method removeRanges.
void removeRanges(MergeableDefinition eventDefinition) {
m_configRanges.removeAll(eventDefinition.getAddressRanges());
getConfigDef().setRanges(new ArrayList<Range>());
getConfigDef().setSpecifics(new ArrayList<String>());
for (IPAddressRange r : m_configRanges) {
if (r.isSingleton()) {
getConfigDef().addSpecific(r.getBegin().toUserString());
} else {
Range xmlRange = new Range();
xmlRange.setBegin(r.getBegin().toUserString());
xmlRange.setEnd(r.getEnd().toUserString());
getConfigDef().addRange(xmlRange);
}
}
}
use of org.opennms.netmgt.config.snmp.Range in project opennms by OpenNMS.
the class MergeableRange method removeSpecificFromRange.
/**
* Changes the current Range by moving the end before the specific and
* creates a new range to the right of the specific ending with the
* current Range's end address.
*
* @return a new Range to be added to Definition
* @param spec a {@link java.lang.String} object.
*/
protected Range removeSpecificFromRange(final String spec) {
if (!coversSpecific(spec))
throw new IllegalArgumentException("Specific: " + spec + ", doesn't affect range: ");
MergeableSpecific specific = new MergeableSpecific(spec);
Range newRange = null;
ByteArrayComparator comparator = new ByteArrayComparator();
try {
if (comparator.compare(specific.getValue(), getFirst().getValue()) == 0) {
getRange().setBegin(InetAddressUtils.incr(specific.getSpecific()));
} else if (comparator.compare(specific.getValue(), getLast().getValue()) == 0) {
getRange().setEnd(InetAddressUtils.decr(specific.getSpecific()));
} else {
newRange = new Range();
newRange.setBegin(InetAddressUtils.incr(specific.getSpecific()));
newRange.setEnd(getRange().getEnd());
getRange().setEnd(InetAddressUtils.decr(specific.getSpecific()));
}
} catch (UnknownHostException e) {
LOG.error("Error converting string to IP address", e);
}
return newRange;
}
use of org.opennms.netmgt.config.snmp.Range in project opennms by OpenNMS.
the class SnmpEventInfo method createDef.
/**
* Creates an SNMP config definition representing the data in this class.
* The defintion will either have one specific IP element or one Range element.
*
* @return a {@link org.opennms.netmgt.config.snmp.Definition} object.
*/
public Definition createDef() {
Definition definition = new Definition();
if (StringUtils.isNotEmpty(getVersion()))
definition.setVersion(getVersion());
if (getRetryCount() != 0)
definition.setRetry(Integer.valueOf(getRetryCount()));
if (getTimeout() != 0)
definition.setTimeout(Integer.valueOf(getTimeout()));
if (getPort() != 0)
definition.setPort(Integer.valueOf(getPort()));
if (getMaxRepetitions() != 0)
definition.setMaxRepetitions(Integer.valueOf(getMaxRepetitions()));
if (getMaxVarsPerPdu() != 0)
definition.setMaxVarsPerPdu(Integer.valueOf(getMaxVarsPerPdu()));
if (getMaxRequestSize() != 0)
definition.setMaxRequestSize(Integer.valueOf(getMaxRequestSize()));
if (StringUtils.isNotEmpty(getProxyHost()))
definition.setProxyHost(getProxyHost());
if (StringUtils.isNotEmpty(getLocation()))
definition.setLocation(getLocation());
// version dependend parameters
if (getVersion() != null && getVersion().equals("v3")) {
if (StringUtils.isNotEmpty(getAuthPassphrase()))
definition.setAuthPassphrase(getAuthPassphrase());
if (StringUtils.isNotEmpty(getAuthProtocol()))
definition.setAuthProtocol(getAuthProtocol());
if (StringUtils.isNotEmpty(getContextEngineId()))
definition.setContextEngineId(getContextEngineId());
if (StringUtils.isNotEmpty(getContextName()))
definition.setContextName(getContextName());
if (StringUtils.isNotEmpty(getEngineId()))
definition.setEngineId(getEngineId());
if (StringUtils.isNotEmpty(getEnterpriseId()))
definition.setEnterpriseId(getEnterpriseId());
if (StringUtils.isNotEmpty(getPrivPassPhrase()))
definition.setPrivacyPassphrase(getPrivPassPhrase());
if (StringUtils.isNotEmpty(getPrivProtocol()))
definition.setPrivacyProtocol(getPrivProtocol());
if (StringUtils.isNotEmpty(getSecurityName()))
definition.setSecurityName(getSecurityName());
if (getSecurityLevel() > 0)
definition.setSecurityLevel(getSecurityLevel());
} else {
//v1, v2c or invalid version
if (getReadCommunityString() != null)
definition.setReadCommunity(getReadCommunityString());
if (getWriteCommunityString() != null)
definition.setWriteCommunity(getWriteCommunityString());
}
if (isSpecific()) {
definition.addSpecific(getFirstIPAddress());
} else {
// first ip address of range must be < than last ip address of range
if (BigInteger.ZERO.compareTo(InetAddressUtils.difference(getFirstIPAddress(), getLastIPAddress())) < 0) {
LOG.error("createDef: Can not create Definition when specified last is < first IP address: {}", this);
throw new IllegalArgumentException("First: " + getFirstIPAddress() + " is greater than: " + getLastIPAddress());
}
Range range = new Range();
range.setBegin(getFirstIPAddress());
range.setEnd(getLastIPAddress());
definition.addRange(range);
}
LOG.debug("createDef: created new Definition from: {}", this);
return definition;
}
Aggregations