use of org.opennms.netmgt.model.OnmsHwEntity in project opennms by OpenNMS.
the class HardwareInventoryResource method getHardwareInventory.
/**
* Gets the hardware inventory.
*
* @param nodeCriteria the node criteria
* @return the root hardware entity
*/
@GET
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public OnmsHwEntity getHardwareInventory(@PathParam("nodeCriteria") String nodeCriteria) {
final OnmsNode node = getOnmsNode(nodeCriteria);
final OnmsHwEntity entity = m_hwEntityDao.findRootByNodeId(node.getId());
if (entity == null) {
throw getException(Status.NOT_FOUND, "Can't find root hardware entity for node {}.", nodeCriteria);
}
return entity;
}
use of org.opennms.netmgt.model.OnmsHwEntity in project opennms by OpenNMS.
the class HardwareInventoryResource method deleteHwEntity.
/**
* Delete hardware entity.
*
* @param nodeCriteria the node criteria
* @param entPhysicalIndex the entity physical index
* @return the response
*/
@DELETE
@Path("{entPhysicalIndex}")
public Response deleteHwEntity(@PathParam("nodeCriteria") final String nodeCriteria, @PathParam("entPhysicalIndex") Integer entPhysicalIndex) {
writeLock();
try {
final OnmsNode node = getOnmsNode(nodeCriteria);
final OnmsHwEntity entity = getHwEntity(node.getId(), entPhysicalIndex);
m_hwEntityDao.delete(entity);
return Response.noContent().build();
} finally {
writeUnlock();
}
}
use of org.opennms.netmgt.model.OnmsHwEntity in project opennms by OpenNMS.
the class HardwareInventoryResource method setHardwareInventory.
/**
* Sets the hardware inventory (root object)
*
* @param nodeCriteria the node criteria
* @param entity the root entity object
* @return the response
*/
@POST
@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Response setHardwareInventory(@PathParam("nodeCriteria") String nodeCriteria, OnmsHwEntity entity) {
if (!entity.isRoot()) {
throw getException(Status.BAD_REQUEST, "The hardware entity is not a root entity {}.", entity.toString());
}
writeLock();
try {
final OnmsNode node = getOnmsNode(nodeCriteria);
fixEntity(node, entity);
final OnmsHwEntity existing = m_hwEntityDao.findRootByNodeId(node.getId());
if (existing != null && !entity.equals(existing)) {
LOG.debug("setHardwareInventory: removing existing hardware inventory from node {} ", nodeCriteria);
m_hwEntityDao.delete(existing);
m_hwEntityDao.flush();
}
m_hwEntityDao.save(entity);
return Response.noContent().build();
} finally {
writeUnlock();
}
}
use of org.opennms.netmgt.model.OnmsHwEntity in project opennms by OpenNMS.
the class HardwareInventoryResource method addOrReplaceChild.
/**
* Adds or replaces a child entity.
*
* @param nodeCriteria the node criteria
* @param parentEntPhysicalIndex the parent entity physical index
* @param child the child
* @return the response
*/
@POST
@Path("{parentEntPhysicalIndex}")
@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Response addOrReplaceChild(@PathParam("nodeCriteria") String nodeCriteria, @PathParam("parentEntPhysicalIndex") Integer parentEntPhysicalIndex, OnmsHwEntity child) {
writeLock();
try {
final OnmsNode node = getOnmsNode(nodeCriteria);
fixEntity(node, child);
final OnmsHwEntity parent = getHwEntity(node.getId(), parentEntPhysicalIndex);
OnmsHwEntity currentChild = parent.getChildByIndex(child.getEntPhysicalIndex());
if (currentChild != null) {
LOG.debug("addOrReplaceChild: removing entity {}", currentChild);
parent.removeChild(currentChild);
}
parent.addChildEntity(child);
LOG.debug("addOrReplaceChild: updating entity {}", child);
m_hwEntityDao.save(parent);
return Response.noContent().build();
} finally {
writeUnlock();
}
}
use of org.opennms.netmgt.model.OnmsHwEntity in project opennms by OpenNMS.
the class HwEntityDaoIT method testEntityCycle.
/**
* Test find entity.
*/
@Test
@Transactional
public void testEntityCycle() {
HwEntityAttributeType ram = new HwEntityAttributeType(".1.3.6.1.4.1.9.9.195.1.1.1.1", "ram", "integer");
m_hwEntityAttributeTypeDao.save(ram);
m_hwEntityAttributeTypeDao.flush();
OnmsNode node = getNode();
Assert.assertNotNull(node);
Assert.assertNotNull(node.getId());
OnmsHwEntity root = new OnmsHwEntity();
root.setEntPhysicalIndex(1);
root.setEntPhysicalClass("chassis");
root.setEntPhysicalName("Chassis");
OnmsHwEntity m1 = new OnmsHwEntity();
m1.setEntPhysicalIndex(2);
m1.setEntPhysicalClass("module");
m1.setEntPhysicalName("M1");
m1.addAttribute(ram, "4");
OnmsHwEntity m2 = new OnmsHwEntity();
m2.setEntPhysicalIndex(3);
m2.setEntPhysicalClass("module");
m2.setEntPhysicalName("M2");
m2.addAttribute(ram, "2");
root.addChildEntity(m1);
root.addChildEntity(m2);
Assert.assertNotNull(m1.getParent());
Assert.assertNotNull(m2.getParent());
root.setNode(node);
Assert.assertNotNull(root.getNode());
Assert.assertEquals(2, root.getChildren().size());
// Test saving root entity
m_hwEntityDao.saveOrUpdate(root);
m_hwEntityDao.flush();
// Test valid findRootByNodeId
OnmsHwEntity e1 = m_hwEntityDao.findRootByNodeId(node.getId());
Assert.assertNotNull(e1);
Assert.assertNotNull(e1.getNode());
Assert.assertEquals(e1.getNode().getId(), node.getId());
Assert.assertEquals(2, e1.getChildren().size());
Assert.assertEquals("chassis", e1.getEntPhysicalClass());
OnmsHwEntity c = e1.getChildren().iterator().next();
Assert.assertEquals("4", c.getAttributeValue("ram"));
// Test invalid findRootByNodeId
Assert.assertNull(m_hwEntityDao.findRootByNodeId(10000));
// Test findEntityByIndex
OnmsHwEntity e2 = m_hwEntityDao.findEntityByIndex(node.getId(), e1.getEntPhysicalIndex());
Assert.assertTrue(e1.equals(e2));
// Test findEntityByName
OnmsHwEntity e3 = m_hwEntityDao.findEntityByName(node.getId(), e1.getEntPhysicalName());
Assert.assertTrue(e1.equals(e3));
// Test getAttributeValue
Assert.assertEquals("Chassis", m_hwEntityDao.getAttributeValue(node.getId(), 1, "entPhysicalName"));
Assert.assertEquals("4", m_hwEntityDao.getAttributeValue(node.getId(), 2, "ram"));
Assert.assertEquals("chassis", m_hwEntityDao.getAttributeValue(node.getId(), "Chassis", "entPhysicalClass"));
Assert.assertEquals("4", m_hwEntityDao.getAttributeValue(node.getId(), "~^M1", "ram"));
// Test delete
m_hwEntityDao.flush();
m_hwEntityDao.delete(e2.getId());
m_hwEntityDao.flush();
Assert.assertNull(m_hwEntityDao.get(e2.getId()));
}
Aggregations