Search in sources :

Example 31 with OnmsIpInterface

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()));
}
Also used : OnmsNode(org.opennms.netmgt.model.OnmsNode) HashMap(java.util.HashMap) CollectionAgentDTO(org.opennms.netmgt.collection.dto.CollectionAgentDTO) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) IpInterfaceDao(org.opennms.netmgt.dao.api.IpInterfaceDao) CollectionSet(org.opennms.netmgt.collection.api.CollectionSet) OnmsIpInterface(org.opennms.netmgt.model.OnmsIpInterface) ServiceCollector(org.opennms.netmgt.collection.api.ServiceCollector) CollectionAgent(org.opennms.netmgt.collection.api.CollectionAgent) DefaultCollectionAgent(org.opennms.netmgt.collection.core.DefaultCollectionAgent) Test(org.junit.Test)

Example 32 with OnmsIpInterface

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()));
}
Also used : OnmsNode(org.opennms.netmgt.model.OnmsNode) OnmsIpInterface(org.opennms.netmgt.model.OnmsIpInterface) HashMap(java.util.HashMap) ServiceCollector(org.opennms.netmgt.collection.api.ServiceCollector) CollectionAgent(org.opennms.netmgt.collection.api.CollectionAgent) DefaultCollectionAgent(org.opennms.netmgt.collection.core.DefaultCollectionAgent) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) IpInterfaceDao(org.opennms.netmgt.dao.api.IpInterfaceDao) CollectionSet(org.opennms.netmgt.collection.api.CollectionSet) Test(org.junit.Test)

Example 33 with OnmsIpInterface

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);
}
Also used : OnmsNode(org.opennms.netmgt.model.OnmsNode) OnmsIpInterface(org.opennms.netmgt.model.OnmsIpInterface)

Example 34 with OnmsIpInterface

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();
}
Also used : OnmsNode(org.opennms.netmgt.model.OnmsNode) OnmsIpInterface(org.opennms.netmgt.model.OnmsIpInterface) Value(org.opennms.netmgt.xml.event.Value) SnmpValue(org.opennms.netmgt.snmp.SnmpValue) Event(org.opennms.netmgt.xml.event.Event) Parm(org.opennms.netmgt.xml.event.Parm) Test(org.junit.Test) DirtiesContext(org.springframework.test.annotation.DirtiesContext)

Example 35 with OnmsIpInterface

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;
}
Also used : CriteriaBuilder(org.opennms.core.criteria.CriteriaBuilder) OnmsIpInterface(org.opennms.netmgt.model.OnmsIpInterface) ArrayList(java.util.ArrayList) SearchResult(org.opennms.features.topology.api.topo.SearchResult) CollapsibleCriteria(org.opennms.features.topology.api.topo.CollapsibleCriteria) Criteria(org.opennms.core.criteria.Criteria) IpLikeHopCriteria(org.opennms.features.topology.app.internal.support.IpLikeHopCriteria) HashSet(java.util.HashSet)

Aggregations

OnmsIpInterface (org.opennms.netmgt.model.OnmsIpInterface)186 OnmsNode (org.opennms.netmgt.model.OnmsNode)117 Test (org.junit.Test)53 OnmsSnmpInterface (org.opennms.netmgt.model.OnmsSnmpInterface)43 InetAddress (java.net.InetAddress)40 OnmsMonitoredService (org.opennms.netmgt.model.OnmsMonitoredService)33 Transactional (org.springframework.transaction.annotation.Transactional)30 ArrayList (java.util.ArrayList)22 OnmsEvent (org.opennms.netmgt.model.OnmsEvent)21 OnmsServiceType (org.opennms.netmgt.model.OnmsServiceType)20 Date (java.util.Date)17 OnmsAlarm (org.opennms.netmgt.model.OnmsAlarm)17 File (java.io.File)16 HashMap (java.util.HashMap)15 Before (org.junit.Before)15 LinkedList (java.util.LinkedList)14 IpInterfaceDao (org.opennms.netmgt.dao.api.IpInterfaceDao)14 NorthboundAlarm (org.opennms.netmgt.alarmd.api.NorthboundAlarm)12 OnmsEventParameter (org.opennms.netmgt.model.OnmsEventParameter)12 OnmsResource (org.opennms.netmgt.model.OnmsResource)11