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