Search in sources :

Example 21 with EqRestriction

use of org.opennms.core.criteria.restrictions.EqRestriction in project opennms by OpenNMS.

the class AlarmDaoIT method testSave.

@Test
@Transactional
public void testSave() {
    OnmsEvent event = new OnmsEvent();
    event.setEventLog("Y");
    event.setEventDisplay("Y");
    event.setEventCreateTime(new Date());
    event.setDistPoller(m_distPollerDao.whoami());
    event.setEventTime(new Date());
    event.setEventSeverity(new Integer(7));
    event.setEventUei("uei://org/opennms/test/EventDaoTest");
    event.setEventSource("test");
    m_eventDao.save(event);
    OnmsNode node = m_nodeDao.findAll().iterator().next();
    OnmsAlarm alarm = new OnmsAlarm();
    alarm.setNode(node);
    alarm.setUei(event.getEventUei());
    alarm.setSeverityId(event.getEventSeverity());
    alarm.setFirstEventTime(event.getEventTime());
    alarm.setLastEvent(event);
    alarm.setCounter(1);
    alarm.setDistPoller(m_distPollerDao.whoami());
    m_alarmDao.save(alarm);
    // It works we're so smart! hehe
    OnmsAlarm newAlarm = m_alarmDao.load(alarm.getId());
    assertEquals("uei://org/opennms/test/EventDaoTest", newAlarm.getUei());
    assertEquals(alarm.getLastEvent().getId(), newAlarm.getLastEvent().getId());
    Collection<OnmsAlarm> alarms;
    Criteria criteria = new Criteria(OnmsAlarm.class);
    criteria.addRestriction(new EqRestriction("node.id", node.getId()));
    alarms = m_alarmDao.findMatching(criteria);
    assertEquals(1, alarms.size());
    newAlarm = alarms.iterator().next();
    assertEquals("uei://org/opennms/test/EventDaoTest", newAlarm.getUei());
    assertEquals(alarm.getLastEvent().getId(), newAlarm.getLastEvent().getId());
}
Also used : OnmsEvent(org.opennms.netmgt.model.OnmsEvent) OnmsNode(org.opennms.netmgt.model.OnmsNode) OnmsAlarm(org.opennms.netmgt.model.OnmsAlarm) EqRestriction(org.opennms.core.criteria.restrictions.EqRestriction) Criteria(org.opennms.core.criteria.Criteria) Date(java.util.Date) Test(org.junit.Test) Transactional(org.springframework.transaction.annotation.Transactional)

Example 22 with EqRestriction

use of org.opennms.core.criteria.restrictions.EqRestriction in project opennms by OpenNMS.

the class NodeLabelDaoImpl method computeLabel.

/**
     * This method determines what label should be associated with a particular
     * node.
     *
     * Algorithm for determining a node's label is as follows: 1) If node has a
     * NetBIOS name associated with it, the NetBIOS name is used as the node's
     * label. 2) If no NetBIOS name available, retrieve all the 'ipinterface'
     * table entries associated with the node with an 'isManaged' field value of
     * 'M' 3) Find the primary interface where "primary" is defined as the
     * managed interface with the smallest IP address (each IP address is
     * converted to an integer value -- the IP address with the smallest integer
     * value wins). 4) IF the primary interface's IP host name is known it
     * becomes the node's label. ELSE IF the node's MIB-II sysName value is
     * known it becomes the node's label ELSE the primary interface's IP address
     * becomes the node's label.
     *
     * NOTE: If for some reason a node has no "managed" interfaces null is
     * returned for the NodeLabel.
     *
     * @param nodeID
     *            Unique identifier of the node to be updated.
     * @param dbConnection
     *            SQL database connection
     * @return NodeLabel Object containing label and source values or null if
     *         node does not have a primary interface.
     * @throws java.sql.SQLException if any.
     */
@Override
public NodeLabel computeLabel(final int nodeID) throws SQLException {
    OnmsNode node = nodeDao.get(nodeID);
    if (node == null) {
        return null;
    }
    // Retrieve NetBIOS name associated with the node
    String netbiosName = node.getNetBiosName();
    if (netbiosName != null) {
        // Truncate sysName if it exceeds max node label length
        if (netbiosName.length() > MAX_NODE_LABEL_LENGTH) {
            netbiosName = netbiosName.substring(0, MAX_NODE_LABEL_LENGTH);
        }
        LOG.debug("NodeLabel.computeLabel: returning NetBIOS name as nodeLabel: {}", netbiosName);
        return new NodeLabelDaoImpl(netbiosName, NodeLabelSource.NETBIOS);
    }
    // OK, if we get this far the node has no NetBIOS name associated with it so,
    // retrieve the primary interface select method property which indicates
    // the method to use for determining which interface on a multi-interface
    // system is to be deemed the primary interface. The primary interface
    // will then determine what the node's label is.
    String method = System.getProperty(PROP_PRIMARY_INTERFACE_SELECT_METHOD);
    if (method == null) {
        method = DEFAULT_SELECT_METHOD;
    }
    if (!method.equals(SELECT_METHOD_MIN) && !method.equals(SELECT_METHOD_MAX)) {
        LOG.warn("Interface selection method is '{}'.  Valid values are 'min' & 'max'.  Will use default value: {}", method, DEFAULT_SELECT_METHOD);
        method = DEFAULT_SELECT_METHOD;
    }
    List<InetAddress> ipv4AddrList = new ArrayList<InetAddress>();
    List<String> ipHostNameList = new ArrayList<String>();
    final org.opennms.core.criteria.Criteria criteria = new org.opennms.core.criteria.Criteria(OnmsIpInterface.class).setAliases(Arrays.asList(new Alias[] { new Alias("node", "node", JoinType.LEFT_JOIN) })).addRestriction(new EqRestriction("node.id", nodeID)).addRestriction(new EqRestriction("isManaged", "M"));
    List<OnmsIpInterface> ints = ipInterfaceDao.findMatching(criteria);
    for (OnmsIpInterface iface : ints) {
        InetAddress inetAddr = iface.getIpAddress();
        ipv4AddrList.add(inetAddr);
        String hostName = iface.getIpHostName();
        if (hostName == null || hostName.equals(inetAddr.toString())) {
            ipHostNameList.add("");
        } else {
            ipHostNameList.add(hostName);
        }
    }
    InetAddress primaryAddr = selectPrimaryAddress(ipv4AddrList, method);
    // and select the primary interface from them.
    if (primaryAddr == null) {
        LOG.debug("NodeLabel.computeLabel: unable to find a primary address for node {}, returning null", nodeID);
        ipv4AddrList.clear();
        ipHostNameList.clear();
        final org.opennms.core.criteria.Criteria crit = new org.opennms.core.criteria.Criteria(OnmsIpInterface.class).setAliases(Arrays.asList(new Alias[] { new Alias("node", "node", JoinType.LEFT_JOIN) })).addRestriction(new EqRestriction("node.id", nodeID)).addRestriction(new NeRestriction("isManaged", "M"));
        List<OnmsIpInterface> sec = ipInterfaceDao.findMatching(crit);
        for (OnmsIpInterface iface : sec) {
            InetAddress inetAddr = iface.getIpAddress();
            ipv4AddrList.add(inetAddr);
            String hostName = iface.getIpHostName();
            if (hostName == null || hostName.equals(inetAddr.toString())) {
                ipHostNameList.add("");
            } else {
                ipHostNameList.add(hostName);
            }
        }
        primaryAddr = selectPrimaryAddress(ipv4AddrList, method);
    }
    if (primaryAddr == null) {
        LOG.warn("Could not find primary interface for node {}, cannot compute nodelabel", nodeID);
        return new NodeLabelDaoImpl("Unknown", NodeLabelSource.UNKNOWN);
    }
    // We now know the IP address of the primary interface so
    // now see if it has a IP host name
    int index = ipv4AddrList.indexOf(primaryAddr);
    String primaryHostName = ipHostNameList.get(index);
    // If length of string is > 0 then the primary interface has a hostname
    if (primaryHostName.length() != 0) {
        // Truncate host name if it exceeds max node label length
        if (primaryHostName.length() > MAX_NODE_LABEL_LENGTH) {
            primaryHostName = primaryHostName.substring(0, MAX_NODE_LABEL_LENGTH);
        }
        return new NodeLabelDaoImpl(primaryHostName, NodeLabelSource.HOSTNAME);
    }
    // If we get this far either the primary interface does not have
    // a host name or the node does not have a primary interface...
    // so we need to use the node's sysName if available...
    // retrieve sysName for the node
    String primarySysName = node.getSysName();
    if (primarySysName != null && primarySysName.length() > 0) {
        // Truncate sysName if it exceeds max node label length
        if (primarySysName.length() > MAX_NODE_LABEL_LENGTH) {
            primarySysName = primarySysName.substring(0, MAX_NODE_LABEL_LENGTH);
        }
        return new NodeLabelDaoImpl(primarySysName, NodeLabelSource.SYSNAME);
    }
    // use the ipAddress as the nodeLabel
    return new NodeLabelDaoImpl(InetAddressUtils.str(primaryAddr), NodeLabelSource.ADDRESS);
}
Also used : OnmsNode(org.opennms.netmgt.model.OnmsNode) ArrayList(java.util.ArrayList) NeRestriction(org.opennms.core.criteria.restrictions.NeRestriction) OnmsIpInterface(org.opennms.netmgt.model.OnmsIpInterface) Alias(org.opennms.core.criteria.Alias) EqRestriction(org.opennms.core.criteria.restrictions.EqRestriction) InetAddress(java.net.InetAddress)

Example 23 with EqRestriction

use of org.opennms.core.criteria.restrictions.EqRestriction in project opennms by OpenNMS.

the class PathOutageManagerDaoImpl method getLabelAndStatus.

/**
     * This method is responsible for determining the
     * node label of a node, and the up/down status
     * and status color
     *
     * @param nodeIDStr a {@link java.lang.String} object.
     * @param conn a {@link java.sql.Connection} object.
     * @return an array of {@link java.lang.String} objects.
     * @throws java.sql.SQLException if any.
     */
@Override
public String[] getLabelAndStatus(String nodeIDStr, Connection conn) {
    String[] result = new String[3];
    result[1] = "Cleared";
    result[2] = "Unmanaged";
    int nodeID = WebSecurityUtils.safeParseInt(nodeIDStr);
    OnmsNode node = nodeDao.get(nodeID);
    if (node == null) {
        // TODO Log that node could not be found in database
        return result;
    }
    result[0] = node.getLabel();
    final org.opennms.core.criteria.Criteria crit = new org.opennms.core.criteria.Criteria(OnmsMonitoredService.class).setAliases(Arrays.asList(new Alias[] { new Alias("ipInterface", "ipInterface", JoinType.INNER_JOIN) })).addRestriction(new EqRestriction("status", "A")).addRestriction(new EqRestriction("ipInterface.node", node));
    // Get all active services on the node
    List<OnmsMonitoredService> services = monitoredServiceDao.findMatching(crit);
    int countManagedSvcs = services.size();
    // Count how many of these services have open outages
    int countOutages = 0;
    for (OnmsMonitoredService service : services) {
        OnmsOutage out = outageDao.currentOutageForService(service);
        if (out != null) {
            countOutages++;
        }
    }
    if (countManagedSvcs == countOutages) {
        result[1] = "Critical";
        result[2] = "All Services Down";
    } else if (countOutages == 0) {
        result[1] = "Normal";
        result[2] = "All Services Up";
    } else {
        result[1] = "Minor";
        result[2] = "Some Services Down";
    }
    return result;
}
Also used : OnmsOutage(org.opennms.netmgt.model.OnmsOutage) OnmsNode(org.opennms.netmgt.model.OnmsNode) Alias(org.opennms.core.criteria.Alias) EqRestriction(org.opennms.core.criteria.restrictions.EqRestriction) OnmsMonitoredService(org.opennms.netmgt.model.OnmsMonitoredService)

Aggregations

EqRestriction (org.opennms.core.criteria.restrictions.EqRestriction)23 Criteria (org.opennms.core.criteria.Criteria)16 Alias (org.opennms.core.criteria.Alias)12 OnmsNode (org.opennms.netmgt.model.OnmsNode)7 OnmsIpInterface (org.opennms.netmgt.model.OnmsIpInterface)5 OnmsOutage (org.opennms.netmgt.model.OnmsOutage)5 ArrayList (java.util.ArrayList)4 Date (java.util.Date)4 NullRestriction (org.opennms.core.criteria.restrictions.NullRestriction)4 Test (org.junit.Test)3 NeRestriction (org.opennms.core.criteria.restrictions.NeRestriction)3 OnmsAlarm (org.opennms.netmgt.model.OnmsAlarm)3 Transactional (org.springframework.transaction.annotation.Transactional)3 InetAddress (java.net.InetAddress)2 Matcher (java.util.regex.Matcher)2 Criteria (org.opennms.features.topology.api.topo.Criteria)2 LldpElement (org.opennms.netmgt.model.LldpElement)2 OnmsLocationMonitor (org.opennms.netmgt.model.OnmsLocationMonitor)2 OnmsMonitoredService (org.opennms.netmgt.model.OnmsMonitoredService)2 LocalDateTime (java.time.LocalDateTime)1