use of org.opennms.netmgt.dao.api.NodeDao in project opennms by OpenNMS.
the class WSManCollectorTest method canSuccesfullyCollectFromGroupWithNoAttributes.
@Test
public void canSuccesfullyCollectFromGroupWithNoAttributes() throws CollectionInitializationException, CollectionException {
OnmsNode node = mock(OnmsNode.class);
NodeDao nodeDao = mock(NodeDao.class);
when(nodeDao.get(0)).thenReturn(node);
WSManConfigDao configDao = mock(WSManConfigDao.class);
when(configDao.getAgentConfig(any())).thenReturn(new Definition());
Collection collection = new Collection();
WSManDataCollectionConfigDao dataCollectionConfigDao = mock(WSManDataCollectionConfigDao.class);
when(dataCollectionConfigDao.getCollectionByName("default")).thenReturn(collection);
WsManCollector collector = new WsManCollector();
collector.setWSManClientFactory(mock(WSManClientFactory.class));
collector.setWSManConfigDao(configDao);
collector.setWSManDataCollectionConfigDao(dataCollectionConfigDao);
collector.setNodeDao(nodeDao);
CollectionAgent agent = mock(CollectionAgent.class);
when(agent.getAddress()).thenReturn(InetAddrUtils.getLocalHostAddress());
when(agent.getStorageResourcePath()).thenReturn(ResourcePath.get());
Map<String, Object> collectionParams = Maps.newHashMap();
collectionParams.put("collection", "default");
collectionParams.putAll(collector.getRuntimeAttributes(agent, collectionParams));
CollectionSet collectionSet = collector.collect(agent, collectionParams);
assertEquals(CollectionStatus.SUCCEEDED, collectionSet.getStatus());
assertEquals(0, CollectionSetUtils.getAttributesByName(collectionSet).size());
}
use of org.opennms.netmgt.dao.api.NodeDao in project opennms by OpenNMS.
the class CollectdTest method setUp.
@Before
public void setUp() throws Exception {
EventIpcManager m_eventIpcManager;
NodeDao m_nodeDao;
MockLogAppender.setupLogging();
Resource threshdResource = new ClassPathResource("/etc/thresholds.xml");
File homeDir = threshdResource.getFile().getParentFile().getParentFile();
System.setProperty("opennms.home", homeDir.getAbsolutePath());
// Test setup
m_eventIpcManager = m_easyMockUtils.createMock(EventIpcManager.class);
EventIpcManagerFactory.setIpcManager(m_eventIpcManager);
m_nodeDao = m_easyMockUtils.createMock(NodeDao.class);
m_ipIfDao = m_easyMockUtils.createMock(IpInterfaceDao.class);
m_scheduler = new MockScheduler();
m_eventIpcManager.addEventListener(isA(EventListener.class));
expectLastCall().anyTimes();
m_eventIpcManager.addEventListener(isA(EventListener.class), isACollection(String.class));
expectLastCall().anyTimes();
m_eventIpcManager.addEventListener(isA(EventListener.class), isA(String.class));
expectLastCall().anyTimes();
m_eventIpcManager.removeEventListener(isA(EventListener.class));
expectLastCall().anyTimes();
// Mock the FilterDao without using EasyMockUtils so that it can be verified separately
m_filterDao = EasyMock.createMock(FilterDao.class);
List<InetAddress> allIps = new ArrayList<InetAddress>();
allIps.add(addr("192.168.1.1"));
allIps.add(addr("192.168.1.2"));
allIps.add(addr("192.168.1.3"));
allIps.add(addr("192.168.1.4"));
allIps.add(addr("192.168.1.5"));
expect(m_filterDao.getActiveIPAddressList("IPADDR IPLIKE *.*.*.*")).andReturn(allIps).anyTimes();
expect(m_filterDao.getActiveIPAddressList("IPADDR IPLIKE 1.1.1.1")).andReturn(new ArrayList<InetAddress>(0)).anyTimes();
EasyMock.replay(m_filterDao);
FilterDaoFactory.setInstance(m_filterDao);
// This call will also ensure that the poll-outages.xml file can parse IPv4
// and IPv6 addresses.
Resource resource = new ClassPathResource("etc/poll-outages.xml");
PollOutagesConfigFactory factory = new PollOutagesConfigFactory(resource);
factory.afterPropertiesSet();
PollOutagesConfigFactory.setInstance(factory);
final MockTransactionTemplate transTemplate = new MockTransactionTemplate();
transTemplate.afterPropertiesSet();
m_collectd = new Collectd();
m_collectd.setEventIpcManager(m_eventIpcManager);
m_collectd.setNodeDao(m_nodeDao);
m_collectd.setIpInterfaceDao(m_ipIfDao);
m_collectd.setFilterDao(m_filterDao);
m_collectd.setScheduler(m_scheduler);
m_collectd.setTransactionTemplate(transTemplate);
m_collectd.setPersisterFactory(new MockPersisterFactory());
m_collectd.setServiceCollectorRegistry(new DefaultServiceCollectorRegistry());
m_collectd.setLocationAwareCollectorClient(CollectorTestUtils.createLocationAwareCollectorClient());
ThresholdingConfigFactory.setInstance(new ThresholdingConfigFactory(ConfigurationTestUtils.getInputStreamForConfigFile("thresholds.xml")));
}
use of org.opennms.netmgt.dao.api.NodeDao in project opennms by OpenNMS.
the class RrdSummaryControllerTest method canGenerateEmptySummary.
/**
* Verifies that an empty summary can be generated so an existing
* node without any resources.
*/
@Test
public void canGenerateEmptySummary() {
// Return a single node when called using the given filter
String rule = "ipaddr iplike 172.20.1.1";
FilterDao filterDao = mock(FilterDao.class);
when(filterDao.getNodeMap(rule)).thenReturn(ImmutableSortedMap.of(1, "node1"));
OnmsNode node = mock(OnmsNode.class);
NodeDao nodeDao = mock(NodeDao.class);
when(nodeDao.load(1)).thenReturn(node);
OnmsResource resource = mock(OnmsResource.class);
ResourceDao resourceDao = mock(ResourceDao.class);
when(resourceDao.getResourceForNode(node)).thenReturn(resource);
// Use our mocks
m_rrdSummaryService.setFilterDao(filterDao);
m_rrdSummaryService.setNodeDao(nodeDao);
m_rrdSummaryService.setResourceDao(resourceDao);
// Building the summary spec.
SummarySpecification summarySpec = new SummarySpecification();
summarySpec.setFilterRule("ipaddr iplike 172.20.1.1");
summarySpec.setStartTime(1472746964);
summarySpec.setEndTime(1473265364);
summarySpec.setAttributeSieve(".*");
// Invoke the controller
HttpServletResponse response = new MockHttpServletResponse();
ModelAndView mv = m_controller.processFormSubmission(response, summarySpec);
Summary summary = (Summary) mv.getModel().get("summary");
// Verify the response
assertEquals(0, summary.getResources().size());
}
use of org.opennms.netmgt.dao.api.NodeDao in project opennms by OpenNMS.
the class DefaultProvisionService method createUndiscoveredNode.
/** {@inheritDoc} */
@Transactional
@Override
public OnmsNode createUndiscoveredNode(final String ipAddress, final String foreignSource, final String locationString) {
final String effectiveForeignSource = foreignSource == null ? FOREIGN_SOURCE_FOR_DISCOVERED_NODES : foreignSource;
final String effectiveLocationName = MonitoringLocationUtils.isDefaultLocationName(locationString) ? null : locationString;
final OnmsNode node = new UpsertTemplate<OnmsNode, NodeDao>(m_transactionManager, m_nodeDao) {
@Override
protected OnmsNode query() {
// Find all of the nodes in the target requisition with the given IP address
return m_nodeDao.findByForeignSourceAndIpAddress(effectiveForeignSource, ipAddress).stream().filter(n -> {
final String existingLocationName = MonitoringLocationUtils.getLocationNameOrNullIfDefault(n);
return Objects.equals(existingLocationName, effectiveLocationName);
}).findFirst().orElse(null);
}
@Override
protected OnmsNode doUpdate(OnmsNode existingNode) {
// we found an existing node so exit by returning null;
return null;
}
@Override
protected OnmsNode doInsert() {
final Date now = new Date();
OnmsMonitoringLocation location = createLocationIfNecessary(locationString);
// Associate the location with the node
final OnmsNode node = new OnmsNode(location);
final String hostname = getHostnameResolver().getHostname(addr(ipAddress), locationString);
if (hostname == null || ipAddress.equals(hostname)) {
node.setLabel(ipAddress);
node.setLabelSource(NodeLabelSource.ADDRESS);
} else {
node.setLabel(hostname);
node.setLabelSource(NodeLabelSource.HOSTNAME);
}
node.setForeignSource(effectiveForeignSource);
node.setType(NodeType.ACTIVE);
node.setLastCapsdPoll(now);
final OnmsIpInterface iface = new OnmsIpInterface(InetAddressUtils.addr(ipAddress), node);
iface.setIsManaged("M");
iface.setIpHostName(hostname);
iface.setIsSnmpPrimary(PrimaryType.NOT_ELIGIBLE);
iface.setIpLastCapsdPoll(now);
m_nodeDao.save(node);
m_nodeDao.flush();
return node;
}
}.execute();
if (node != null) {
if (effectiveForeignSource != null) {
node.setForeignId(node.getNodeId());
createUpdateRequistion(ipAddress, node, effectiveLocationName, effectiveForeignSource);
}
// we do this here rather than in the doInsert method because
// the doInsert may abort
node.visit(new AddEventVisitor(m_eventForwarder));
}
return node;
}
Aggregations