Search in sources :

Example 6 with OnmsMinion

use of org.opennms.netmgt.model.minion.OnmsMinion in project opennms by OpenNMS.

the class HeartbeatConsumer method handleMessage.

@Override
@Transactional
public void handleMessage(MinionIdentityDTO minionHandle) {
    LOG.info("Received heartbeat for Minion with id: {} at location: {}", minionHandle.getId(), minionHandle.getLocation());
    OnmsMinion minion = minionDao.findById(minionHandle.getId());
    if (minion == null) {
        minion = new OnmsMinion();
        minion.setId(minionHandle.getId());
        // The real location is filled in below, but we set this to null
        // for now to detect requisition changes
        minion.setLocation(null);
    }
    final String prevLocation = minion.getLocation();
    final String nextLocation = minionHandle.getLocation();
    minion.setLocation(minionHandle.getLocation());
    // Provision the minions node before we alter the location
    this.provision(minion, prevLocation, nextLocation);
    if (minionHandle.getTimestamp() == null) {
        // The heartbeat does not contain a timestamp - use the current time
        minion.setLastUpdated(new Date());
        LOG.info("Received heartbeat without a timestamp: {}", minionHandle);
    } else if (minion.getLastUpdated() == null) {
        // The heartbeat does contain a timestamp, and we don't have
        // one set yet, so use whatever we've been given
        minion.setLastUpdated(minionHandle.getTimestamp());
    } else if (minionHandle.getTimestamp().after(minion.getLastUpdated())) {
        // The timestamp in the heartbeat is more recent than the one we
        // have stored, so update it
        minion.setLastUpdated(minionHandle.getTimestamp());
    } else {
        // The timestamp in the heartbeat is earlier than the
        // timestamp we have stored, so ignore it
        LOG.info("Ignoring stale timestamp from heartbeat: {}", minionHandle);
    }
    minionDao.saveOrUpdate(minion);
    if (prevLocation == null) {
        final EventBuilder eventBuilder = new EventBuilder(EventConstants.MONITORING_SYSTEM_ADDED_UEI, "OpenNMS.Minion.Heartbeat");
        eventBuilder.addParam(EventConstants.PARAM_MONITORING_SYSTEM_TYPE, OnmsMonitoringSystem.TYPE_MINION);
        eventBuilder.addParam(EventConstants.PARAM_MONITORING_SYSTEM_ID, minionHandle.getId());
        eventBuilder.addParam(EventConstants.PARAM_MONITORING_SYSTEM_LOCATION, nextLocation);
        try {
            eventProxy.send(eventBuilder.getEvent());
        } catch (final EventProxyException e) {
            throw new DataAccessResourceFailureException("Unable to send event", e);
        }
    } else if (!prevLocation.equals(nextLocation)) {
        final EventBuilder eventBuilder = new EventBuilder(EventConstants.MONITORING_SYSTEM_LOCATION_CHANGED_UEI, "OpenNMS.Minion.Heartbeat");
        eventBuilder.addParam(EventConstants.PARAM_MONITORING_SYSTEM_TYPE, OnmsMonitoringSystem.TYPE_MINION);
        eventBuilder.addParam(EventConstants.PARAM_MONITORING_SYSTEM_ID, minionHandle.getId());
        eventBuilder.addParam(EventConstants.PARAM_MONITORING_SYSTEM_PREV_LOCATION, prevLocation);
        eventBuilder.addParam(EventConstants.PARAM_MONITORING_SYSTEM_LOCATION, nextLocation);
        try {
            eventProxy.send(eventBuilder.getEvent());
        } catch (final EventProxyException e) {
            throw new DataAccessResourceFailureException("Unable to send event", e);
        }
    }
}
Also used : EventBuilder(org.opennms.netmgt.model.events.EventBuilder) OnmsMinion(org.opennms.netmgt.model.minion.OnmsMinion) DataAccessResourceFailureException(org.springframework.dao.DataAccessResourceFailureException) EventProxyException(org.opennms.netmgt.events.api.EventProxyException) Date(java.util.Date) Transactional(org.springframework.transaction.annotation.Transactional)

Example 7 with OnmsMinion

use of org.opennms.netmgt.model.minion.OnmsMinion in project opennms by OpenNMS.

the class MinionRestServiceIT method afterServletStart.

@Override
protected void afterServletStart() throws Exception {
    final OnmsMinion minion = new OnmsMinion("12345", "Here", "Started", new Date());
    minion.setProperty("Foo", "Bar");
    m_minionDao.save(minion);
    m_minionDao.save(new OnmsMinion("23456", "There", "Stopped", new Date()));
    m_minionDao.flush();
}
Also used : OnmsMinion(org.opennms.netmgt.model.minion.OnmsMinion) Date(java.util.Date)

Example 8 with OnmsMinion

use of org.opennms.netmgt.model.minion.OnmsMinion in project opennms by OpenNMS.

the class MinionRestServiceIT method beforeServletDestroy.

@Override
protected void beforeServletDestroy() throws Exception {
    final Collection<OnmsMinion> minions = m_minionDao.findAll();
    for (final OnmsMinion minion : minions) {
        m_minionDao.delete(minion);
    }
    m_minionDao.flush();
}
Also used : OnmsMinion(org.opennms.netmgt.model.minion.OnmsMinion)

Example 9 with OnmsMinion

use of org.opennms.netmgt.model.minion.OnmsMinion in project opennms by OpenNMS.

the class MinionDaoIT method testProperties.

@Test
@Transactional
public void testProperties() throws Exception {
    final Date now = new Date();
    final OnmsMinion a = new OnmsMinion(UUID.randomUUID().toString(), "TestLocation", "Started", now);
    final OnmsMinion b = new OnmsMinion(UUID.randomUUID().toString(), "OtherLocation", "Started", now);
    a.getProperties().put("Yes", "No");
    a.setProperty("Up", "Down");
    b.setProperty("Left", "Right");
    b.setProperty("Wrong", "Right");
    m_minionDao.save(a);
    m_minionDao.save(b);
    m_minionDao.flush();
    assertEquals(Integer.valueOf(4), m_jdbcTemplate.queryForObject("select count(*) from monitoringsystemsproperties", Integer.class));
    assertEquals(Integer.valueOf(2), m_jdbcTemplate.queryForObject("select count(*) from monitoringsystemsproperties where monitoringsystemid = ?", new Object[] { a.getId() }, Integer.class));
    assertEquals(Integer.valueOf(2), m_jdbcTemplate.queryForObject("select count(*) from monitoringsystemsproperties where monitoringsystemid = ?", new Object[] { b.getId() }, Integer.class));
    String prop = m_minionDao.findById(a.getId()).getProperties().get("Left");
    // a doesn't have that property, b does
    assertNull(prop);
    prop = m_minionDao.findById(b.getId()).getProperties().get("Left");
    assertNotNull(prop);
    assertEquals("Right", prop);
}
Also used : OnmsMinion(org.opennms.netmgt.model.minion.OnmsMinion) Date(java.util.Date) Test(org.junit.Test) Transactional(org.springframework.transaction.annotation.Transactional)

Example 10 with OnmsMinion

use of org.opennms.netmgt.model.minion.OnmsMinion in project opennms by OpenNMS.

the class MinionRestService method getMinionProperty.

@GET
@Produces(MediaType.TEXT_PLAIN)
@Path("{minionId}/{key}")
@Transactional
public String getMinionProperty(@PathParam("minionId") final String minionId, @PathParam("key") final String key) {
    final OnmsMinion minion = getMinion(minionId);
    final String value = minion.getProperties().get(key);
    if (value == null) {
        throw getException(Status.NOT_FOUND, "Property {} was not found on Minion {}.", key, minionId);
    }
    return value;
}
Also used : OnmsMinion(org.opennms.netmgt.model.minion.OnmsMinion) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

OnmsMinion (org.opennms.netmgt.model.minion.OnmsMinion)11 Date (java.util.Date)4 Test (org.junit.Test)3 Transactional (org.springframework.transaction.annotation.Transactional)3 Path (javax.ws.rs.Path)2 Response (javax.ws.rs.core.Response)2 EventProxyException (org.opennms.netmgt.events.api.EventProxyException)2 EventBuilder (org.opennms.netmgt.model.events.EventBuilder)2 InetSocketAddress (java.net.InetSocketAddress)1 List (java.util.List)1 DELETE (javax.ws.rs.DELETE)1 GET (javax.ws.rs.GET)1 Produces (javax.ws.rs.Produces)1 WebTarget (javax.ws.rs.client.WebTarget)1 GenericType (javax.ws.rs.core.GenericType)1 Before (org.junit.Before)1 Ignore (org.junit.Ignore)1 OnmsNode (org.opennms.netmgt.model.OnmsNode)1 PollStatus (org.opennms.netmgt.poller.PollStatus)1 RestClient (org.opennms.smoketest.utils.RestClient)1