use of org.opennms.netmgt.model.OnmsIpInterface in project opennms by OpenNMS.
the class CollectorComplianceTest method canCollectUsingMinionWorkflow.
@Test
public void canCollectUsingMinionWorkflow() throws CollectionInitializationException, CollectionException {
Assume.assumeTrue(runsOnMinion);
// create the agent
OnmsNode node = mock(OnmsNode.class);
when(node.getId()).thenReturn(1);
OnmsIpInterface iface = mock(OnmsIpInterface.class);
when(iface.getNode()).thenReturn(node);
when(iface.getIpAddress()).thenReturn(InetAddrUtils.getLocalHostAddress());
IpInterfaceDao ifaceDao = mock(IpInterfaceDao.class);
when(ifaceDao.load(1)).thenReturn(iface);
PlatformTransactionManager transMgr = mock(PlatformTransactionManager.class);
final CollectionAgent agent = DefaultCollectionAgent.create(1, ifaceDao, transMgr);
// init() should execute without any exceptions
final ServiceCollector opennmsCollector = getCollector();
initialize(opennmsCollector);
// getEffectiveLocation() should return the original location
final String targetLocation = "!" + MonitoringLocationDao.DEFAULT_MONITORING_LOCATION_ID;
assertEquals("Location cannot be altered.", targetLocation, opennmsCollector.getEffectiveLocation(targetLocation));
// getRuntimeAttributes() should return a valid map
final Map<String, Object> requiredParams = getRequiredParameters();
final Map<String, Object> runtimeAttrs = opennmsCollector.getRuntimeAttributes(agent, Collections.unmodifiableMap(requiredParams));
// marshalParameters() should marshal all parameters to strings
final Map<String, Object> allParms = new HashMap<>();
allParms.putAll(requiredParams);
allParms.putAll(runtimeAttrs);
final Map<String, String> marshaledParms = opennmsCollector.marshalParameters(Collections.unmodifiableMap(allParms));
beforeMinion();
// create a separate instance of the collector
final ServiceCollector minionCollector = getNewCollector();
// unmarshalParameters() should unmarshal all parameters from strings
final Map<String, Object> unmarshaledParms = minionCollector.unmarshalParameters(Collections.unmodifiableMap(marshaledParms));
// collect() should return a valid collection set
final CollectionAgentDTO agentDTO = new CollectionAgentDTO(agent);
final CollectionSet collectionSet = minionCollector.collect(agentDTO, Collections.unmodifiableMap(unmarshaledParms));
assertEquals(CollectionStatus.SUCCEEDED, collectionSet.getStatus());
afterMinion();
// the collection set should be marshalable
JaxbUtils.marshal(collectionSet);
// getRrdRepository() should return a valid repository
assertNotNull(opennmsCollector.getRrdRepository(getCollectionName()));
}
use of org.opennms.netmgt.model.OnmsIpInterface in project opennms by OpenNMS.
the class CollectorComplianceTest method canCollectUsingOpenNMSWorkflow.
@Test
public void canCollectUsingOpenNMSWorkflow() throws CollectionInitializationException, CollectionException {
// create the agent
OnmsNode node = mock(OnmsNode.class);
OnmsIpInterface iface = mock(OnmsIpInterface.class);
when(iface.getNode()).thenReturn(node);
when(iface.getIpAddress()).thenReturn(InetAddrUtils.getLocalHostAddress());
IpInterfaceDao ifaceDao = mock(IpInterfaceDao.class);
when(ifaceDao.load(1)).thenReturn(iface);
PlatformTransactionManager transMgr = mock(PlatformTransactionManager.class);
final CollectionAgent agent = createAgent(1, ifaceDao, transMgr);
// init() should execute without any exceptions
final ServiceCollector opennmsCollector = getCollector();
initialize(opennmsCollector);
// getEffectiveLocation() should execute without any exceptions
// in this context there are no requirements on its return value
final String targetLocation = "!" + MonitoringLocationDao.DEFAULT_MONITORING_LOCATION_ID;
opennmsCollector.getEffectiveLocation(targetLocation);
// getRuntimeAttributes() should return a valid map
final Map<String, Object> requiredParams = getRequiredParameters();
final Map<String, Object> runtimeAttrs = opennmsCollector.getRuntimeAttributes(agent, Collections.unmodifiableMap(requiredParams));
// collect() should return a valid collection set
final Map<String, Object> allParms = new HashMap<>();
allParms.putAll(requiredParams);
allParms.putAll(runtimeAttrs);
final CollectionSet collectionSet = opennmsCollector.collect(agent, Collections.unmodifiableMap(allParms));
assertEquals(CollectionStatus.SUCCEEDED, collectionSet.getStatus());
// getRrdRepository() should return a valid repository
assertNotNull(opennmsCollector.getRrdRepository(getCollectionName()));
}
use of org.opennms.netmgt.model.OnmsIpInterface in project opennms by OpenNMS.
the class DefaultCollectionAgentFactory method createCollectionAgentAndOverrideLocation.
@Override
public CollectionAgent createCollectionAgentAndOverrideLocation(String nodeCriteria, InetAddress ipAddr, String location) {
final OnmsNode node = nodeDao.get(nodeCriteria);
if (node == null) {
throw new IllegalArgumentException(String.format("No node found with lookup criteria: %s", nodeCriteria));
}
final OnmsIpInterface ipInterface = ipInterfaceDao.findByNodeIdAndIpAddress(node.getId(), InetAddrUtils.str(ipAddr));
if (ipInterface == null) {
throw new IllegalArgumentException(String.format("No interface found with IP %s on node %s", InetAddrUtils.str(ipAddr), nodeCriteria));
}
return DefaultCollectionAgent.create(ipInterface.getId(), ipInterfaceDao, transMgr, location);
}
use of org.opennms.netmgt.model.OnmsIpInterface in project opennms by OpenNMS.
the class TrapHandlerITCase method testInterfaceReparentedModifiesIpMgr.
@Test
@DirtiesContext
public void testInterfaceReparentedModifiesIpMgr() throws Exception {
final OnmsNode node = new OnmsNode(m_dbPopulator.getMonitoringLocationDao().getDefaultLocation(), "test123");
final OnmsIpInterface iface = new OnmsIpInterface();
iface.setIpAddress(m_ip);
iface.setIsSnmpPrimary(PrimaryType.PRIMARY);
node.addIpInterface(iface);
int nodeId = m_dbPopulator.getNodeDao().save(node);
anticipateEvent("uei.opennms.org/default/trap", m_ip, nodeId);
Event event = anticipateEvent(EventConstants.INTERFACE_REPARENTED_EVENT_UEI, m_ip, nodeId);
event.addParm(new Parm() {
{
this.setParmName(EventConstants.PARM_OLD_NODEID);
this.setValue(new Value(Integer.toString(m_nodeId)));
}
});
event.addParm(new Parm() {
{
this.setParmName(EventConstants.PARM_NEW_NODEID);
this.setValue(new Value(Integer.toString(nodeId)));
}
});
m_eventMgr.sendNow(event);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// do nothing
}
sendTrap("v1", null, 6, 1);
finishUp();
}
use of org.opennms.netmgt.model.OnmsIpInterface in project opennms by OpenNMS.
the class IpLikeSearchProvider method query.
/**
* This method processes the <SearchQuery> that the user has typed and returns a <SearchResult> list
* of matching IP addresses as well as the query string itself, which is collapsible, to act
* as a subnet container.
*/
@Override
public List<SearchResult> query(SearchQuery searchQuery, GraphContainer graphContainer) {
LOG.info("SearchProvider->query: called with search query: '{}'", searchQuery);
List<SearchResult> results = new ArrayList<>();
String queryString = searchQuery.getQueryString();
if (!isIpLikeQuery(queryString)) {
LOG.debug("SearchProvider->query: query not IPLIKE compatible.");
return results;
}
CriteriaBuilder bldr = new CriteriaBuilder(OnmsIpInterface.class);
bldr.iplike("ipAddr", queryString).orderBy("ipAddress", true);
Criteria dbQueryCriteria = bldr.toCriteria();
List<OnmsIpInterface> ips;
// since it seems to do something very similar in its matches method.
try {
ips = ipInterfaceProvider.findMatching(dbQueryCriteria);
if (ips.size() == 0) {
return results;
} else {
if (isIpLikeQuery(queryString)) {
LOG.debug("SearchProvider->query: adding IPLIKE search spec '{}' to the search results.", queryString);
SearchResult searchResult = new SearchResult(getSearchProviderNamespace(), queryString, queryString, queryString, SearchResult.COLLAPSIBLE, !SearchResult.COLLAPSED);
if (!results.contains(searchResult)) {
results.add(searchResult);
}
}
}
Set<String> ipAddrs = new HashSet<>();
LOG.info("SearchProvider->query: creating IP address set.");
for (OnmsIpInterface ip : ips) {
String hostAddress = ip.getIpAddress().getHostAddress();
LOG.debug("SearchProvider->query: adding '{}' to set of IPs.", hostAddress);
ipAddrs.add(hostAddress);
}
LOG.info("SearchProvider->query: building search result from set of IPs.");
IPLOOP: for (String ip : ipAddrs) {
if (findCriterion(ip, graphContainer) != null) {
continue IPLOOP;
} else {
SearchResult searchResult = createSearchResult(ip, queryString);
if (!results.contains(searchResult)) {
results.add(searchResult);
}
}
}
LOG.info("SearchProvider->query: found: '{}' IP interfaces.", ips.size());
} catch (Exception e) {
LOG.error("SearchProvider-query: caught exception during iplike query: {}", e);
}
LOG.info("SearchProvider->query: built search result with {} results.", results.size());
return results;
}
Aggregations