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());
}
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);
}
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;
}
Aggregations