Search in sources :

Example 11 with OnmsAcknowledgment

use of org.opennms.netmgt.model.OnmsAcknowledgment in project opennms by OpenNMS.

the class AckdIT method testAcknowledgeAlarm.

/**
 * This tests the acknowledgment of an alarm and any related notifications.
 */
@Test
public void testAcknowledgeAlarm() {
    VerificationObject vo = createAckStructure();
    Assert.assertTrue(vo.m_nodeId > 0);
    Assert.assertTrue(vo.m_alarmId > 0);
    Assert.assertTrue(vo.m_eventID > 0);
    Assert.assertTrue(vo.m_userNotifId > 0);
    OnmsAlarm alarm = m_alarmDao.get(vo.m_alarmId);
    OnmsAcknowledgment ack = new OnmsAcknowledgment(m_alarmDao.get(vo.m_alarmId));
    m_ackDao.save(ack);
    m_ackDao.flush();
    m_ackDao.processAck(ack);
    alarm = m_alarmDao.get(ack.getRefId());
    Assert.assertNotNull(alarm.getAlarmAckUser());
    Assert.assertEquals("admin", alarm.getAlarmAckUser());
    OnmsNotification notif = m_notificationDao.get(vo.m_notifId);
    Assert.assertNotNull(notif);
    Assert.assertEquals("admin", notif.getAnsweredBy());
    Assert.assertTrue(alarm.getAlarmAckTime().before(notif.getRespondTime()));
}
Also used : OnmsAcknowledgment(org.opennms.netmgt.model.OnmsAcknowledgment) OnmsAlarm(org.opennms.netmgt.model.OnmsAlarm) OnmsNotification(org.opennms.netmgt.model.OnmsNotification) Test(org.junit.Test)

Example 12 with OnmsAcknowledgment

use of org.opennms.netmgt.model.OnmsAcknowledgment in project opennms by OpenNMS.

the class JavaMailAckReaderIT method workingWithSimpleTextMessages.

/**
 * tests the ability to create acknowledgments from an email for plain text.  This test
 * creates a message from scratch rather than reading from an inbox.
 */
@Test
public void workingWithSimpleTextMessages() {
    Properties props = new Properties();
    Message msg = new MimeMessage(Session.getDefaultInstance(props));
    try {
        Address[] addrs = new Address[1];
        addrs[0] = new InternetAddress("david@opennms.org");
        msg.addFrom(addrs);
        msg.addRecipient(javax.mail.internet.MimeMessage.RecipientType.TO, addrs[0]);
        msg.setSubject("Re: Notice #1234 JavaMailReaderImplTest Test Message");
        msg.setText("ACK");
    } catch (AddressException e) {
        e.printStackTrace();
    } catch (MessagingException e) {
        e.printStackTrace();
    }
    List<Message> msgs = new ArrayList<Message>(1);
    msgs.add(msg);
    List<OnmsAcknowledgment> acks = m_processor.createAcks(msgs);
    Assert.assertEquals(1, acks.size());
    Assert.assertEquals(AckType.NOTIFICATION, acks.get(0).getAckType());
    Assert.assertEquals("david@opennms.org", acks.get(0).getAckUser());
    Assert.assertEquals(AckAction.ACKNOWLEDGE, acks.get(0).getAckAction());
    Assert.assertEquals(new Integer(1234), acks.get(0).getRefId());
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) OnmsAcknowledgment(org.opennms.netmgt.model.OnmsAcknowledgment) Message(javax.mail.Message) SendmailMessage(org.opennms.netmgt.config.javamail.SendmailMessage) MimeMessage(javax.mail.internet.MimeMessage) Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress) MessagingException(javax.mail.MessagingException) ArrayList(java.util.ArrayList) Properties(java.util.Properties) MimeMessage(javax.mail.internet.MimeMessage) AddressException(javax.mail.internet.AddressException) Test(org.junit.Test)

Example 13 with OnmsAcknowledgment

use of org.opennms.netmgt.model.OnmsAcknowledgment in project opennms by OpenNMS.

the class JavaMailAckReaderIT method workingWithMultiPartMessages.

/**
 * tests the ability to create acknowledgments from an email for a multi-part text.  This test
 * creates a message from scratch rather than reading from an inbox.  This message creation
 * may not actually represent what comes from a mail server.
 */
@Test
public void workingWithMultiPartMessages() throws JavaMailerException, MessagingException {
    List<Message> msgs = new ArrayList<>();
    Properties props = new Properties();
    Message msg = new MimeMessage(Session.getDefaultInstance(props));
    Address[] addrs = new Address[1];
    addrs[0] = new InternetAddress("david@opennms.org");
    msg.addFrom(addrs);
    msg.addRecipient(RecipientType.TO, new InternetAddress("david@opennms.org"));
    msg.setSubject("Re: Notice #1234 JavaMailReaderImplTest Test Message");
    Multipart mpContent = new MimeMultipart();
    BodyPart textBp = new MimeBodyPart();
    BodyPart htmlBp = new MimeBodyPart();
    textBp.setText("ack");
    htmlBp.setContent("<html>\n" + " <head>\n" + "  <title>\n" + "   Acknowledge\n" + "  </title>\n" + " </head>\n" + " <body>\n" + "  <h1>\n" + "   ack\n" + "  </h1>\n" + " </body>\n" + "</html>", "text/html");
    mpContent.addBodyPart(textBp);
    mpContent.addBodyPart(htmlBp);
    msg.setContent(mpContent);
    msgs.add(msg);
    List<OnmsAcknowledgment> acks = m_processor.createAcks(msgs);
    Assert.assertEquals(1, acks.size());
    Assert.assertEquals(AckType.NOTIFICATION, acks.get(0).getAckType());
    Assert.assertEquals("david@opennms.org", acks.get(0).getAckUser());
    Assert.assertEquals(AckAction.ACKNOWLEDGE, acks.get(0).getAckAction());
    Assert.assertEquals(new Integer(1234), acks.get(0).getRefId());
}
Also used : MimeBodyPart(javax.mail.internet.MimeBodyPart) BodyPart(javax.mail.BodyPart) InternetAddress(javax.mail.internet.InternetAddress) MimeMultipart(javax.mail.internet.MimeMultipart) Multipart(javax.mail.Multipart) OnmsAcknowledgment(org.opennms.netmgt.model.OnmsAcknowledgment) Message(javax.mail.Message) SendmailMessage(org.opennms.netmgt.config.javamail.SendmailMessage) MimeMessage(javax.mail.internet.MimeMessage) Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress) ArrayList(java.util.ArrayList) Properties(java.util.Properties) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) MimeBodyPart(javax.mail.internet.MimeBodyPart) Test(org.junit.Test)

Example 14 with OnmsAcknowledgment

use of org.opennms.netmgt.model.OnmsAcknowledgment in project opennms by OpenNMS.

the class AlarmRestService method doUpdateProperties.

@Override
protected Response doUpdateProperties(SecurityContext securityContext, UriInfo uriInfo, OnmsAlarm alarm, MultivaluedMapImpl params) {
    boolean isProcessAck = true;
    final String ackValue = params.getFirst("ack");
    final String escalateValue = params.getFirst("escalate");
    final String clearValue = params.getFirst("clear");
    final String ackUserValue = params.getFirst("ackUser");
    final String ticketIdValue = params.getFirst("ticketId");
    final String ticketStateValue = params.getFirst("ticketState");
    final String ackUser = ackUserValue == null ? securityContext.getUserPrincipal().getName() : ackUserValue;
    SecurityHelper.assertUserEditCredentials(securityContext, ackUser);
    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 if (StringUtils.isNotBlank(ticketIdValue)) {
        isProcessAck = false;
        alarm.setTTicketId(ticketIdValue);
    } else if (EnumUtils.isValidEnum(TroubleTicketState.class, ticketStateValue)) {
        isProcessAck = false;
        alarm.setTTicketState(TroubleTicketState.valueOf(ticketStateValue));
    } else {
        throw getException(Status.BAD_REQUEST, "Must supply one of the 'ack', 'escalate', or 'clear' parameters, set to either 'true' or 'false'.");
    }
    if (isProcessAck) {
        m_ackDao.processAck(acknowledgement);
    } else {
        getDao().saveOrUpdate(alarm);
    }
    return Response.noContent().build();
}
Also used : OnmsAcknowledgment(org.opennms.netmgt.model.OnmsAcknowledgment)

Example 15 with OnmsAcknowledgment

use of org.opennms.netmgt.model.OnmsAcknowledgment 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();
}
Also used : OnmsAcknowledgment(org.opennms.netmgt.model.OnmsAcknowledgment) OnmsAlarm(org.opennms.netmgt.model.OnmsAlarm) OnmsNotification(org.opennms.netmgt.model.OnmsNotification) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

OnmsAcknowledgment (org.opennms.netmgt.model.OnmsAcknowledgment)26 OnmsAlarm (org.opennms.netmgt.model.OnmsAlarm)14 Test (org.junit.Test)11 Transactional (org.springframework.transaction.annotation.Transactional)10 Date (java.util.Date)6 OnmsNotification (org.opennms.netmgt.model.OnmsNotification)6 Message (javax.mail.Message)4 OnmsEvent (org.opennms.netmgt.model.OnmsEvent)4 OnmsNode (org.opennms.netmgt.model.OnmsNode)4 ArrayList (java.util.ArrayList)3 InternetAddress (javax.mail.internet.InternetAddress)3 MimeMessage (javax.mail.internet.MimeMessage)3 Consumes (javax.ws.rs.Consumes)3 SendmailMessage (org.opennms.netmgt.config.javamail.SendmailMessage)3 Properties (java.util.Properties)2 Address (javax.mail.Address)2 MessagingException (javax.mail.MessagingException)2 PUT (javax.ws.rs.PUT)2 JUnitTemporaryDatabase (org.opennms.core.test.db.annotations.JUnitTemporaryDatabase)2 OnmsCriteria (org.opennms.netmgt.model.OnmsCriteria)2