use of org.opennms.netmgt.model.OnmsAlarm in project opennms by OpenNMS.
the class AcknowledgmentRestService method acknowledge.
/**
* <p>acknowledgeAlarm</p>
*
* @param alarmId a {@link java.lang.String} object.
* @param action a {@link java.lang.String} object.
* @return a {@link javax.ws.rs.core.Response} object.
*/
@POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Transactional
public Response acknowledge(@Context final SecurityContext securityContext, MultivaluedMap<String, String> formParams) {
String alarmId = formParams.getFirst("alarmId");
String notifId = formParams.getFirst("notifId");
String action = formParams.getFirst("action");
String ackUser = formParams.getFirst("ackUser");
if (action == null) {
action = "ack";
}
if (ackUser == null) {
ackUser = securityContext.getUserPrincipal().getName();
}
SecurityHelper.assertUserEditCredentials(securityContext, ackUser);
OnmsAcknowledgment ack = null;
if (alarmId == null && notifId == null) {
return getBadRequestResponse("You must supply either an alarmId or notifId");
} else if (alarmId != null && notifId != null) {
return getBadRequestResponse("You cannot supply both an alarmId and a notifId");
} else if (alarmId != null) {
final Integer numericAlarmId = getNumericValue(alarmId);
if (numericAlarmId == null) {
return getBadRequestResponse("The alarmId has to be an integer value");
}
final OnmsAlarm alarm = m_alarmDao.get(numericAlarmId);
if (alarm == null) {
return Response.notModified().build();
}
ack = new OnmsAcknowledgment(alarm, ackUser);
} else if (notifId != null) {
final Integer numericNotifId = getNumericValue(notifId);
if (numericNotifId == null) {
return getBadRequestResponse("The notifId has to be an integer value");
}
final OnmsNotification notification = m_notificationDao.get(numericNotifId);
if (notification == null) {
return Response.notModified().build();
}
ack = new OnmsAcknowledgment(notification, ackUser);
}
if ("ack".equals(action)) {
ack.setAckAction(AckAction.ACKNOWLEDGE);
} else if ("unack".equals(action)) {
ack.setAckAction(AckAction.UNACKNOWLEDGE);
} else if ("clear".equals(action)) {
ack.setAckAction(AckAction.CLEAR);
} else if ("esc".equals(action)) {
ack.setAckAction(AckAction.ESCALATE);
} else {
return getBadRequestResponse("Must supply the action parameter, set to either 'ack, 'unack', 'clear', or 'esc'");
}
m_ackDao.processAck(ack);
return Response.ok(ack).build();
}
use of org.opennms.netmgt.model.OnmsAlarm in project opennms by OpenNMS.
the class AlarmRestService method updateAlarms.
/**
* <p>
* updateAlarms
* </p>
*
* @param formProperties
* a {@link org.opennms.web.rest.support.MultivaluedMapImpl} object.
*/
@PUT
@Transactional
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Response updateAlarms(@Context final SecurityContext securityContext, final MultivaluedMapImpl formProperties) {
writeLock();
try {
final String ackValue = formProperties.getFirst("ack");
formProperties.remove("ack");
final String escalateValue = formProperties.getFirst("escalate");
formProperties.remove("escalate");
final String clearValue = formProperties.getFirst("clear");
formProperties.remove("clear");
final CriteriaBuilder builder = getCriteriaBuilder(formProperties, false);
builder.distinct();
builder.limit(0);
builder.offset(0);
final String ackUser = formProperties.containsKey("ackUser") ? formProperties.getFirst("ackUser") : securityContext.getUserPrincipal().getName();
formProperties.remove("ackUser");
SecurityHelper.assertUserEditCredentials(securityContext, ackUser);
final List<OnmsAlarm> alarms = m_alarmDao.findMatching(builder.toCriteria());
for (final OnmsAlarm alarm : alarms) {
final OnmsAcknowledgment acknowledgement = new OnmsAcknowledgment(alarm, ackUser);
acknowledgement.setAckAction(AckAction.UNSPECIFIED);
if (ackValue != null) {
if (Boolean.parseBoolean(ackValue)) {
acknowledgement.setAckAction(AckAction.ACKNOWLEDGE);
} else {
acknowledgement.setAckAction(AckAction.UNACKNOWLEDGE);
}
} else if (escalateValue != null) {
if (Boolean.parseBoolean(escalateValue)) {
acknowledgement.setAckAction(AckAction.ESCALATE);
}
} else if (clearValue != null) {
if (Boolean.parseBoolean(clearValue)) {
acknowledgement.setAckAction(AckAction.CLEAR);
}
} else {
throw getException(Status.BAD_REQUEST, "Must supply one of the 'ack', 'escalate', or 'clear' parameters, set to either 'true' or 'false'.");
}
m_ackDao.processAck(acknowledgement);
}
return alarms == null || alarms.isEmpty() ? Response.notModified().build() : Response.noContent().build();
} finally {
writeUnlock();
}
}
use of org.opennms.netmgt.model.OnmsAlarm in project opennms by OpenNMS.
the class OssDaoOpenNMSImpl method localUpdateAlarmCacheTransaction.
/**
* method to run in transaction to update from database
*/
private void localUpdateAlarmCacheTransaction() {
Collection<OnmsAlarm> c = _alarmDao.findAll();
// clear previous hashtable
alarmCacheByID.clear();
alarmCacheByUniqueKey.clear();
OnmsAlarm[] alarms = (OnmsAlarm[]) c.toArray(new OnmsAlarm[c.size()]);
// TODO - ISSUE if too many alarms?
for (int i = 0; i < alarms.length; i++) {
OnmsAlarm newalarm = alarms[i];
// retrieve inner contents of alarm node if there is a node associated with the alarm
if (newalarm.getNode() != null) {
newalarm.getNode().getLabel();
}
alarmCacheByID.put(new Integer(newalarm.getId()), newalarm);
// only update alarmCacheByUniqueKey if key is not null or empty
if (!((newalarm.getApplicationDN() == null) || (newalarm.getOssPrimaryKey() == null) || (newalarm.getApplicationDN().equals("")) || (newalarm.getOssPrimaryKey().equals("")))) {
String uniqueKey = newalarm.getApplicationDN() + newalarm.getOssPrimaryKey();
if (alarmCacheByUniqueKey.get(uniqueKey) == null) {
alarmCacheByUniqueKey.put(uniqueKey, newalarm);
} else {
LOG.error("OssDaoOpenNMSImpl().localUpdateAlarmCache(): ERROR - duplicate alarm uniqueKey in database ={} AlarmID:{}", uniqueKey, newalarm.getId());
}
}
}
}
use of org.opennms.netmgt.model.OnmsAlarm in project opennms by OpenNMS.
the class OssDaoOpenNMSImpl method addCurrentAlarmForUniqueKey.
// ************************
// Business Methods
// ************************
/* (non-Javadoc)
* @see org.openoss.opennms.spring.dao.OssDao#addCurrentAlarmForUniqueKey(org.opennms.netmgt.model.OnmsAlarm)
*/
/**
* {@inheritDoc}
*/
@Override
public synchronized OnmsAlarm addCurrentAlarmForUniqueKey(final OnmsAlarm alarm) {
if ((alarm == null) || (alarm.getId() != null))
throw new IllegalArgumentException("OssDaoOpenNMSImpl().addCurrentAlarmForUniqueKey(): Illegal value: alarm==null or alarmID!=null");
if ((alarm.getAlarmType() != 1))
throw new IllegalArgumentException("OssDaoOpenNMSImpl().addCurrentAlarmForUniqueKey(): Illegal value: alarm.getAlarmType() not 'raise' alarm type '1'");
if ((alarm.getApplicationDN() == null) || (alarm.getApplicationDN().equals("")))
throw new IllegalArgumentException("OssDaoOpenNMSImpl().addCurrentAlarmForUniqueKey(): Illegal value: alarm ApplicationDN null or empty ");
if ((alarm.getOssPrimaryKey() == null) || (alarm.getOssPrimaryKey().equals("")))
throw new IllegalArgumentException("OssDaoOpenNMSImpl().addCurrentAlarmForUniqueKey(): Illegal value: alarm OssPrimaryKey null or empty");
OnmsAlarm checkAlarm = getCurrentAlarmForUniqueKey(alarm.getApplicationDN(), alarm.getOssPrimaryKey());
if (checkAlarm != null) {
// if not a unique alarm throw error
throw new IllegalArgumentException("OssDaoOpenNMSImpl().addCurrentAlarmForUniqueKey(): Illegal value: alarm not unique in Current Alarm list: ApplicationDN:" + alarm.getApplicationDN() + " OssPrimaryKey:" + alarm.getOssPrimaryKey());
} else {
try {
// add new alarm then save alarm in local alarm list
LOG.debug("OssDaoOpenNMSImpl().addCurrentAlarmForUniqueKey(): ALARM TO SAVE:{}", alarmToString(alarm));
String uniqueKey = alarm.getApplicationDN() + alarm.getOssPrimaryKey();
// _alarmDao.save(alarm); // - replaced by;
transTemplate.execute(new TransactionCallbackWithoutResult() {
@Override
public void doInTransactionWithoutResult(TransactionStatus status) {
_alarmDao.save(alarm);
}
});
// update local cache
alarmCacheByID.put(new Integer(alarm.getId()), alarm);
alarmCacheByUniqueKey.put(uniqueKey, alarm);
} catch (Throwable ex) {
LOG.error("OssDaoOpenNMSImpl().addCurrentAlarmForUniqueKey():Error creating alarm in database", ex);
return null;
}
// try { // add new alarm then update alarm in local alarm list
// alarm = getCurrentAlarmForUniqueKey(alarm.getApplicationDN() , alarm.getOssPrimaryKey());
// } catch (Throwable ex){
// log.error("OssDaoOpenNMSImpl().addCurrentAlarmForUniqueKey():Error updating alarm in local list:"+ex);
// return null;
// }
}
LOG.debug("OssDaoOpenNMSImpl().addCurrentAlarmForUniqueKey(): ALARM SAVED: {}", alarmToStringBrief(alarm));
try {
LOG.debug("OssDaoOpenNMSImpl().addCurrentAlarmForUniqueKey(): Sending Updated alarm list to QoSD");
sendAlarms();
} catch (Exception e) {
// ignore this exception as Qosd may not be running
LOG.debug("OssDaoOpenNMSImpl().addCurrentAlarmForUniqueKey(): problem sending alarm to QoSD ( QoSD may not be running )", e);
}
return alarm;
}
use of org.opennms.netmgt.model.OnmsAlarm in project opennms by OpenNMS.
the class InsSession method getXMLEvent.
private Event getXMLEvent(final OnmsEvent ev) {
final Integer id = ev.getId();
LOG.info("Working on XML Event for id: {}", id);
LOG.debug("Setting Event id: {}", id);
final Event e = new Event();
e.setDbid(id);
// UEI
final String uei = ev.getEventUei();
if (uei != null) {
LOG.debug("Setting Event uei: {}", uei);
e.setUei(uei);
} else {
LOG.warn("No Event uei found: skipping event....");
return null;
}
// Source
final String source = ev.getEventSource();
if (source != null) {
LOG.debug("Setting Event source: {}", source);
e.setSource(source);
} else {
LOG.info("No Event source found.");
}
// nodeid
final Integer nodeid = ev.getNode().getId();
if (ev.getNode() != null && nodeid != null) {
LOG.debug("Setting Event nodeid: {}", nodeid);
e.setNodeid(nodeid.longValue());
} else {
LOG.info("No Event node found.");
}
// timestamp
final Date time = ev.getEventTime();
if (time != null) {
LOG.debug("Setting event date timestamp to (GMT): {}", time);
e.setTime(time);
} else {
LOG.info("No Event time found.");
}
// host
final String host = ev.getEventHost();
if (host != null) {
LOG.debug("Setting Event Host: {}", host);
e.setHost(host);
} else {
LOG.info("No Event host found.");
}
// interface
final InetAddress ipAddr = ev.getIpAddr();
if (ipAddr != null) {
LOG.debug("Setting Event Interface/ipaddress: {}", ipAddr);
e.setInterfaceAddress(ipAddr);
} else {
LOG.info("No Event ip address found.");
}
// Service Name
if (ev.getServiceType() != null) {
final String serviceName = ev.getServiceType().getName();
LOG.debug("Setting Event Service Name: {}", serviceName);
e.setService(serviceName);
} else {
LOG.info("No Event service name found.");
}
// Description
final String descr = ev.getEventDescr();
if (descr != null) {
LOG.debug("Setting Event Description: {}", descr);
e.setDescr(descr);
} else {
LOG.info("No Event ip address found.");
}
// Log message
final String logmsg = ev.getEventLogMsg();
if (logmsg != null) {
final Logmsg msg = new Logmsg();
LOG.debug("Setting Event Log Message: {}", logmsg);
msg.setContent(logmsg);
e.setLogmsg(msg);
} else {
LOG.info("No Event log Message found.");
}
// severity
final Integer severity = ev.getEventSeverity();
if (severity != null) {
LOG.debug("Setting Event Severity: {}", severity);
e.setSeverity(OnmsSeverity.get(severity).getLabel());
} else {
LOG.info("No Event severity found.");
}
final Integer ifIndex = ev.getIfIndex();
if (ifIndex != null && ifIndex > 0) {
e.setIfIndex(ifIndex);
e.setIfAlias(getIfAlias(nodeid, ifIndex));
} else {
e.setIfIndex(-1);
e.setIfAlias("-1");
}
// operator Instruction
final String operInstruct = ev.getEventOperInstruct();
if (operInstruct != null) {
LOG.debug("Setting Event Operator Instruction: {}", operInstruct);
e.setOperinstruct(operInstruct);
} else {
LOG.info("No Event operator Instruction found.");
}
// parms
final String eventParms = ev.getEventParms();
if (eventParms != null) {
LOG.debug("Setting Event Parms: {}", eventParms);
final List<Parm> parms = EventParameterUtils.decode(eventParms);
if (parms != null)
e.setParmCollection(parms);
} else {
LOG.info("No Event parms found.");
}
final AlarmData ad = new AlarmData();
final OnmsAlarm onmsAlarm = ev.getAlarm();
try {
if (onmsAlarm != null) {
ad.setReductionKey(onmsAlarm.getReductionKey());
ad.setAlarmType(onmsAlarm.getAlarmType());
ad.setClearKey(onmsAlarm.getClearKey());
e.setAlarmData(ad);
}
} catch (final ObjectNotFoundException e1) {
LOG.warn("Correlated alarm data not found.", e1);
}
LOG.info("Returning event with id: {}", id);
return e;
}
Aggregations