private List<OnmsAlarm> createDownAlarm() {
    List<OnmsAlarm> alarms = new ArrayList<OnmsAlarm>();

    OnmsAlarm alarm1 = new OnmsAlarm();
    alarm1.setNode(m_node1);
    alarm1.setIfIndex(10101);
    alarm1.setUei("uei.opennms.org/internal/topology/linkDown");
    alarms.add(alarm1);

    return alarms;
  }
示例#2
0
 private OnmsAlarm buildAlarm(final OnmsEvent event) {
   final OnmsAlarm alarm = new OnmsAlarm();
   alarm.setDistPoller(getDistPollerDao().load("localhost"));
   alarm.setUei(event.getEventUei());
   alarm.setAlarmType(1);
   alarm.setNode(m_node1);
   alarm.setDescription("This is a test alarm");
   alarm.setLogMsg("this is a test alarm log message");
   alarm.setCounter(1);
   alarm.setIpAddr(InetAddressUtils.getInetAddress("192.168.1.1"));
   alarm.setSeverity(OnmsSeverity.NORMAL);
   alarm.setFirstEventTime(event.getEventTime());
   alarm.setLastEvent(event);
   return alarm;
 }
  @Test
  @Transactional
  public void testActions() {
    OnmsEvent event = new OnmsEvent();
    event.setEventLog("Y");
    event.setEventDisplay("Y");
    event.setEventCreateTime(new Date());
    event.setDistPoller(m_distPollerDao.load("localhost"));
    event.setEventTime(new Date());
    event.setEventSeverity(Integer.valueOf(6));
    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.setSeverity(OnmsSeverity.get(event.getEventSeverity()));
    alarm.setSeverityId(event.getEventSeverity());
    alarm.setFirstEventTime(event.getEventTime());
    alarm.setLastEvent(event);
    alarm.setCounter(1);
    alarm.setDistPoller(m_distPollerDao.load("localhost"));

    m_alarmDao.save(alarm);

    OnmsAlarm newAlarm = m_alarmDao.load(alarm.getId());
    assertEquals("uei://org/opennms/test/EventDaoTest", newAlarm.getUei());
    assertEquals(alarm.getLastEvent().getId(), newAlarm.getLastEvent().getId());

    assertEquals(OnmsSeverity.MAJOR, newAlarm.getSeverity());

    newAlarm.escalate("admin");
    assertEquals(OnmsSeverity.CRITICAL, newAlarm.getSeverity());

    newAlarm.clear("admin");
    assertEquals(OnmsSeverity.CLEARED, newAlarm.getSeverity());

    newAlarm.unacknowledge("admin");
    assertNull(newAlarm.getAckUser());
    assertNull(newAlarm.getAlarmAckTime());
  }
  @Test
  @Transactional
  public void testSave() {
    OnmsEvent event = new OnmsEvent();
    event.setEventLog("Y");
    event.setEventDisplay("Y");
    event.setEventCreateTime(new Date());
    event.setDistPoller(m_distPollerDao.load("localhost"));
    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.load("localhost"));

    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());
  }
  @Test
  @Transactional
  public void testAlarmSummary_AlarmWithNoEvent() {
    OnmsNode node = m_nodeDao.findAll().iterator().next();

    OnmsAlarm alarm = new OnmsAlarm();
    alarm.setNode(node);
    alarm.setUei("uei://org/opennms/test/badAlarmTest");
    alarm.setSeverityId(new Integer(7));
    alarm.setCounter(1);
    alarm.setDistPoller(m_distPollerDao.load("localhost"));

    m_alarmDao.save(alarm);

    List<AlarmSummary> summary = m_alarmDao.getNodeAlarmSummaries();
    Assert.assertNotNull(summary);
    Assert.assertEquals(1, summary.size());
    AlarmSummary sum = summary.get(0);
    Assert.assertEquals(node.getLabel(), sum.getNodeLabel());
    Assert.assertEquals(alarm.getSeverity().getId(), sum.getMaxSeverity().getId());
    Assert.assertEquals("N/A", sum.getFuzzyTimeDown());
  }
  @Test
  @Transactional
  @Ignore
  public void testWithoutDistPoller() {
    OnmsEvent event = new OnmsEvent();
    event.setEventLog("Y");
    event.setEventDisplay("Y");
    event.setEventCreateTime(new Date());
    event.setDistPoller(m_distPollerDao.load("localhost"));
    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);

    ThrowableAnticipator ta = new ThrowableAnticipator();
    ta.anticipate(
        new DataIntegrityViolationException(
            "not-null property references a null or transient value: org.opennms.netmgt.model.OnmsAlarm.distPoller; nested exception is org.hibernate.PropertyValueException: not-null property references a null or transient value: org.opennms.netmgt.model.OnmsAlarm.distPoller"));

    try {
      m_alarmDao.save(alarm);
    } catch (Throwable t) {
      ta.throwableReceived(t);
    }

    ta.verifyAnticipated();
  }
  @Test
  @Transactional
  public void testAlarmSummary() {
    OnmsEvent event = new OnmsEvent();
    event.setEventLog("Y");
    event.setEventDisplay("Y");
    event.setEventCreateTime(new Date());
    event.setDistPoller(m_distPollerDao.load("localhost"));
    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.load("localhost"));

    m_alarmDao.save(alarm);

    List<AlarmSummary> summary = m_alarmDao.getNodeAlarmSummaries();
    Assert.assertNotNull(summary);
    Assert.assertEquals(1, summary.size());
    AlarmSummary sum = summary.get(0);
    Assert.assertEquals(node.getLabel(), sum.getNodeLabel());
    Assert.assertEquals(alarm.getSeverity().getId(), sum.getMaxSeverity().getId());
    Assert.assertNotSame("N/A", sum.getFuzzyTimeDown());
  }
  /**
   * This method maps an OSS/J AlarmValue to OpenNMS alarm
   *
   * @param onmsAlarm OnmsAlarm object to be populated
   * @param alarmValue OSS/J AlarmValue data to use to populate OnmsAlarm
   * @param almUpdateBehaviour - determines how to treat the node name of the new alarm must be of
   *     value; <code>USE_TYPE_INSTANCE</code> - populate nodeID with node having same asset type
   *     and instance data as alarm or <code>SPECIFY_OUTSTATION</code> - populate nodeID with node
   *     having same nodeLabel as defaultUpdateNodeLabel
   * @param defaultUpdateNodeLabel name of node to be updated if
   *     almUpdateBehaviour==SPECIFY_OUTSTATION
   * @return the OnmsAlarm populated with OSS/J NotifyNewAlarmEvent data
   * @throws java.lang.IllegalArgumentException if any.
   * @throws javax.oss.UnsupportedAttributeException if any.
   */
  public OnmsAlarm populateOnmsAlarmFromOssjAlarm(
      OnmsAlarm onmsAlarm,
      AlarmValue alarmValue,
      Integer almUpdateBehaviour,
      String defaultUpdateNodeLabel)
      throws IllegalArgumentException, UnsupportedAttributeException {
    ThreadCategory log = getLog();
    String logheader =
        "\t\t" + this.getClass().getSimpleName() + "populateOnmsAlarmFromOssjAlarm():";

    try {
      String ossPrimaryKey = alarmValue.getAlarmKey().getAlarmPrimaryKey();
      String applicationDN = alarmValue.getAlarmKey().getApplicationDN();
      if (log.isDebugEnabled())
        log.debug(
            logheader
                + " - AlarmPrimaryKey: "
                + ossPrimaryKey
                + " ApplictionDN: "
                + applicationDN
                + " alarmRaisedTime: "
                + alarmValue.getAlarmRaisedTime());
      if ((applicationDN == null)
          || (applicationDN.equals(""))
          || (ossPrimaryKey == null)
          || (ossPrimaryKey.equals(""))) {
        log.error(logheader + " ApplicatioDN or PrimaryKey not set");
      } else {
        if (log.isDebugEnabled())
          log.debug(
              logheader + ": trying to find existing alarm using getCurrentAlarmForUniqueKey");

        onmsAlarm = ossDao.getCurrentAlarmForUniqueKey(applicationDN, ossPrimaryKey);
        if (onmsAlarm != null) { // already an alarm with this unique id - log error
          log.error(logheader + " Alarm Already exists with this Unique ID");
        } else {
          onmsAlarm = new OnmsAlarm();

          onmsAlarm.setUei(ossjAlarmTypeToUei(alarmValue.getAlarmType()));
          onmsAlarm.setX733AlarmType(
              (alarmValue.getAlarmType() == null) ? "" : alarmValue.getAlarmType());
          onmsAlarm.setX733ProbableCause(alarmValue.getProbableCause());

          onmsAlarm.setTTicketState(null); // needed?
          onmsAlarm.setTTicketId(""); // needed?
          onmsAlarm.setQosAlarmState("");
          onmsAlarm.setSuppressedUser(""); // needed?
          onmsAlarm.setSuppressedUntil(new Date()); // needed?
          onmsAlarm.setSuppressedTime(new Date()); // needed?

          OnmsSeverity onmsseverity;
          try {
            onmsseverity = ossjSeveritytoOnmsSeverity(alarmValue.getPerceivedSeverity());
          } catch (IllegalArgumentException iae) {
            log.error(
                logheader + " problem setting severity used default:'WARNING'. Exception:" + iae);
            onmsseverity = OnmsSeverity.WARNING;
          }
          onmsAlarm.setSeverity(onmsseverity);

          OnmsServiceType service = new OnmsServiceType();
          service.setId(new Integer(-1));
          onmsAlarm.setServiceType(new OnmsServiceType()); // needed?

          onmsAlarm.setReductionKey(
              ":managedObjectInstance:"
                  + alarmValue.getManagedObjectInstance()
                  + ":managedObjectType:"
                  + alarmValue.getManagedObjectClass()
                  + ":ossPrimaryKey:-"
                  + ossPrimaryKey
                  + ":applicationDN:-"
                  + applicationDN); // must be unique because of alarm_reductionkey_idx

          onmsAlarm.setOssPrimaryKey(ossPrimaryKey);
          onmsAlarm.setOperInstruct(alarmValue.getProposedRepairActions());

          // defaultvalue if search fails - will update node with ID 1
          OnmsNode node = new OnmsNode(); // TODO remove ossDao.makeExtendedOnmsNode();
          node.setId(new Integer(1)); // node id cannot be null
          onmsAlarm.setNode(node); //

          if (almUpdateBehaviour == null) {
            log.error(
                logheader
                    + ": This receiver's alarmUpdateBehaviour is not set: defaulting to update nodeID:1");
          } else {
            if (log.isDebugEnabled())
              log.debug(
                  logheader
                      + " alarmUpdateBehaviour:"
                      + almUpdateBehaviour
                      + " "
                      + getAlarmUpdateBehaviourForInt(almUpdateBehaviour));

            if (almUpdateBehaviour.equals(SPECIFY_OUTSTATION)) {
              // this will look for first match of node label to callingAer.getName()
              // and set node id to this value.

              if (log.isDebugEnabled())
                log.debug(
                    logheader
                        + " SPECIFY_OUTSTATION looking for node with nodelabel:"
                        + defaultUpdateNodeLabel);
              try {
                // TODO temp remove ?
                try {
                  node = ossDao.findNodeByLabel(defaultUpdateNodeLabel);
                } catch (Throwable ex) {
                  log.error(
                      logheader
                          + " alarmUpdateBehaviour.equals(USE_TYPE_INSTANCE) Problem looking up Node "
                          + ex);
                }

                if (node != null) {
                  if (log.isDebugEnabled())
                    log.debug(
                        logheader
                            + " alarmUpdateBehaviour.equals(SPECIFY_OUTSTATION):"
                            + "NODE FOUND for this name:"
                            + defaultUpdateNodeLabel
                            + " setting node id to NodeLabel:"
                            + node.getLabel()
                            + " NodeID:"
                            + node.getId());
                  onmsAlarm.setNode(
                      node); // maps into FIRST instance of node with the same managedObjectInstance
                  // and managedObjectType
                } else {
                  log.error(
                      logheader
                          + " alarmUpdateBehaviour.equals(SPECIFY_OUTSTATION):"
                          + "NODE NOT FOUND for this name:"
                          + defaultUpdateNodeLabel
                          + " setting node id to default NodeID: 1");
                  node = new OnmsNode(); // TODO remove ossDao.makeExtendedOnmsNode();
                  node.setId(new Integer(1)); // node id cannot be null
                  onmsAlarm.setNode(node); //
                }
              } catch (Throwable ex) {
                log.error(
                    logheader
                        + " alarmUpdateBehaviour.equals(USE_TYPE_INSTANCE) Problem looking up Node for alarm Set to default nodeID:1"
                        + ex);
              }

            } else if (almUpdateBehaviour.equals(USE_TYPE_INSTANCE)) {
              // this will look for first match of node Managed object Instance and Managed Object
              // type
              // and set node id to this value.
              String managedObjectType = alarmValue.getManagedObjectClass();
              String managedObjectInstance = alarmValue.getManagedObjectInstance();

              if (log.isDebugEnabled())
                log.debug(
                    logheader
                        + " USE_TYPE_INSTANCE looking for node with managedObjectType:"
                        + managedObjectType
                        + " managedObjectInstance:"
                        + managedObjectInstance);
              try {
                node = ossDao.findNodeByInstanceAndType(managedObjectInstance, managedObjectType);

                if (node != null) {
                  if (log.isDebugEnabled())
                    log.debug(
                        logheader
                            + " alarmUpdateBehaviour.equals(USE_TYPE_INSTANCE):"
                            + "NODE FOUND for this RX Name:"
                            + defaultUpdateNodeLabel
                            + " setting node id to NodeLabel:"
                            + node.getLabel()
                            + " NodeID:"
                            + node.getId());
                  onmsAlarm.setNode(
                      node); // maps into FIRST instance of node with the same managedObjectInstance
                  // and managedObjectType
                } else {
                  log.error(
                      logheader
                          + " alarmUpdateBehaviour.equals(USE_TYPE_INSTANCE):"
                          + "NODE NOT FOUND for this managedObjectType:"
                          + managedObjectType
                          + " managedObjectInstance:"
                          + managedObjectInstance
                          + " setting node id to default NodeID: 1");
                  node = new OnmsNode(); // TODO remove ossDao.makeExtendedOnmsNode();
                  node.setId(new Integer(1)); // node id cannot be null
                  onmsAlarm.setNode(node); //
                }
              } catch (Throwable ex) {
                log.error(
                    logheader
                        + " alarmUpdateBehaviour.equals(USE_TYPE_INSTANCE) Problem looking up Node for alarm Set to default nodeID:1"
                        + ex);
              }
            } else {
              log.error(
                  logheader
                      + " Invalid value for alarmUpdateBehaviour:"
                      + almUpdateBehaviour
                      + " "
                      + getAlarmUpdateBehaviourForInt(almUpdateBehaviour)
                      + " defaulting to update nodeID:1");
            }
          }

          onmsAlarm.setMouseOverText(""); // needed?
          onmsAlarm.setManagedObjectType(alarmValue.getManagedObjectClass());
          onmsAlarm.setManagedObjectInstance(alarmValue.getManagedObjectInstance());
          onmsAlarm.setLogMsg(alarmValue.getSpecificProblem());

          // NOTE - this has no effect here as .setLastEvent nulls value
          // alarm.setLastEventTime(nnae.getEventTime());

          //					TODO REMOVED - DO NOT CREATE EVENT WITH HIBERNATE AlarmDAo
          //					OnmsEvent event= new OnmsEvent();
          //					//event.setId(new Integer(1));  // This is NOT set since unique constraint in
          // alarms table on Events table
          //					onmsAlarm.setLastEvent(event);

          onmsAlarm.setIpAddr(InetAddressUtils.getLocalHostAddress()); // needed?
          onmsAlarm.setId(null); // set null as updating alarm
          onmsAlarm.setFirstEventTime(alarmValue.getAlarmRaisedTime());
          onmsAlarm.setLastEventTime(alarmValue.getAlarmChangedTime());

          //					TODO removed - do create distpoller with hibernate dao
          //					onmsAlarm.setDistPoller(new OnmsDistPoller("undefined","localhost")); //simple
          // constructor
          onmsAlarm.setDistPoller(distPollerDao.get("localhost"));

          onmsAlarm.setDescription(
              alarmValue.getAdditionalText()); // TODO need Qosd Not to generate this if remote
          onmsAlarm.setCounter(new Integer(1));
          onmsAlarm.setApplicationDN(applicationDN);
          onmsAlarm.setAlarmType(new Integer(1)); // set to raise alarm
          // alarm.setAlarmAckUser(arg0);
          // alarm.setAlarmAckTime(arg0);

          if (log.isDebugEnabled()) log.debug(logheader + ": Creating Alarm: ");
        }
      }
    } catch (Throwable e) {
      log.error(logheader + " Error : ", e);
    }
    return onmsAlarm;
  }