use of org.opennms.netmgt.model.OnmsHwEntity in project opennms by OpenNMS.
the class HardwareInventoryResource method updateHwEntity.
/**
* Update hardware entity.
*
* @param nodeCriteria the node criteria
* @param entPhysicalIndex the entity physical index
* @param params the parameters
* @return the response
*/
@PUT
@Path("{entPhysicalIndex}")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Response updateHwEntity(@PathParam("nodeCriteria") String nodeCriteria, @PathParam("entPhysicalIndex") Integer entPhysicalIndex, MultivaluedMapImpl params) {
writeLock();
try {
final OnmsNode node = getOnmsNode(nodeCriteria);
final OnmsHwEntity entity = getHwEntity(node.getId(), entPhysicalIndex);
boolean modified = true;
BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(entity);
for (String key : params.keySet()) {
if (key.startsWith("entPhysical")) {
if (wrapper.isWritableProperty(key)) {
String stringValue = params.getFirst(key);
Object value = wrapper.convertIfNecessary(stringValue, (Class<?>) wrapper.getPropertyType(key));
wrapper.setPropertyValue(key, value);
modified = true;
}
} else {
OnmsHwEntityAttribute attr = entity.getAttribute(key);
if (attr != null) {
attr.setValue(params.getFirst(key));
modified = true;
}
}
}
if (modified) {
m_hwEntityDao.save(entity);
return Response.noContent().build();
}
return Response.notModified().build();
} finally {
writeUnlock();
}
}
use of org.opennms.netmgt.model.OnmsHwEntity in project opennms by OpenNMS.
the class SnmpHardwareInventoryProvisioningAdapterIT method testDiscoverSnmpEntities.
/**
* Test discover SNMP entities.
*
* @throws Exception the exception
*/
@Test
@Transactional
public void testDiscoverSnmpEntities() throws Exception {
HwInventoryAdapterConfiguration config = m_adapter.getHwAdapterConfigDao().getConfiguration();
Assert.assertEquals(3, config.getExtensions().size());
Assert.assertEquals(5, config.getExtensions().get(0).getMibObjects().size());
Assert.assertEquals(12, config.getExtensions().get(1).getMibObjects().size());
Assert.assertEquals(10, config.getExtensions().get(2).getMibObjects().size());
HwExtension ext = config.getExtensions().get(0);
Assert.assertEquals("CISCO-ENTITY-EXT-MIB", ext.getName());
Assert.assertEquals(5, ext.getMibObjects().size());
ext = config.getExtensions().get(1);
Assert.assertEquals("CISCO-ENTITY-ASSET-MIB", ext.getName());
Assert.assertEquals(12, ext.getMibObjects().size());
Assert.assertEquals(27, m_adapter.getVendorAttributeMap().size());
for (TestOperation op : m_operations) {
m_adapter.processPendingOperationForNode(op.operation);
OnmsHwEntity root = m_entityDao.findRootByNodeId(op.nodeId);
Assert.assertNotNull(root);
Assert.assertTrue(root.isRoot());
FileWriter w = new FileWriter("target/" + op.nodeId + ".xml");
JaxbUtils.marshal(root, w);
w.close();
m_nodeDao.flush();
m_entityDao.flush();
}
Assert.assertEquals(112, m_entityDao.countAll());
}
use of org.opennms.netmgt.model.OnmsHwEntity in project opennms by OpenNMS.
the class AbstractSingleHardwareAdapterTest method performTest.
/**
* Perform test.
*
* @param expectedCount the expected count
* @throws Exception the exception
*/
protected void performTest(int expectedCount) throws Exception {
m_adapter.processPendingOperationForNode(m_operation);
OnmsHwEntity root = m_entityDao.findRootByNodeId(m_nodeId);
Assert.assertNotNull(root);
Assert.assertTrue(root.isRoot());
FileWriter w = new FileWriter("target/" + m_nodeId + ".xml");
JaxbUtils.marshal(root, w);
w.close();
m_nodeDao.flush();
m_entityDao.flush();
Assert.assertEquals(expectedCount, m_entityDao.countAll());
}
use of org.opennms.netmgt.model.OnmsHwEntity in project opennms by OpenNMS.
the class SnmpHardwareInventoryProvisioningAdapter method synchronizeInventory.
/**
* Synchronize inventory.
* <p>Obtain the ENTITY-MIB and vendor attributes from the target node through SNMP.</p>
* <p>If the node has a hardware inventory data on the database, this is going to be overridden only,
* if the gathered data differs from the data at the database, otherwise the gathered data will be
* discarded.</p>
*
* @param nodeId the node id
*/
private void synchronizeInventory(int nodeId) {
final OnmsNode node = m_nodeDao.get(nodeId);
if (node == null) {
throw new ProvisioningAdapterException("Failed to return node for given nodeId: " + nodeId);
}
final OnmsIpInterface intf = node.getPrimaryInterface();
if (intf == null) {
throw new ProvisioningAdapterException("Can't find the SNMP Primary IP address for nodeId: " + nodeId);
}
final InetAddress ipAddress = intf.getIpAddress();
EventBuilder ebldr = null;
try {
if (node.getSysObjectId() == null) {
LOG.warn("Skiping hardware discover because the node {} doesn't support SNMP", nodeId);
return;
}
OnmsMonitoringLocation location = node.getLocation();
String locationName = (location == null) ? null : location.getLocationName();
SnmpAgentConfig agentConfig = m_snmpConfigDao.getAgentConfig(ipAddress, locationName);
final OnmsHwEntity newRoot = getRootEntity(agentConfig, node);
newRoot.setNode(node);
final OnmsHwEntity currentRoot = m_hwEntityDao.findRootByNodeId(node.getId());
if (newRoot.equals(currentRoot)) {
LOG.info("No changes detected on the hardware inventory for nodeId {}", nodeId);
return;
}
if (currentRoot == null) {
LOG.info("Saving hardware inventory for nodeId {}", nodeId);
} else {
LOG.info("Updating hardware inventory for nodeId {}", nodeId);
m_hwEntityDao.delete(currentRoot);
m_hwEntityDao.flush();
}
m_hwEntityDao.saveOrUpdate(newRoot);
ebldr = new EventBuilder(EventConstants.HARDWARE_INVENTORY_SUCCESSFUL_UEI, PREFIX + NAME);
} catch (Throwable e) {
ebldr = new EventBuilder(EventConstants.HARDWARE_INVENTORY_FAILED_UEI, PREFIX + NAME);
ebldr.addParam(EventConstants.PARM_REASON, e.getMessage());
}
if (ebldr != null) {
ebldr.setNodeid(nodeId);
ebldr.setInterface(ipAddress);
getEventForwarder().sendNow(ebldr.getEvent());
}
}
use of org.opennms.netmgt.model.OnmsHwEntity in project opennms by OpenNMS.
the class SnmpHardwareInventoryProvisioningAdapter method getRootEntity.
/**
* Gets the root entity.
*
* @param agentConfig the agent configuration
* @param node the node
* @return the root entity
* @throws HardwareInventoryException the hardware inventory exception
*/
private OnmsHwEntity getRootEntity(SnmpAgentConfig agentConfig, OnmsNode node) throws SnmpHardwareInventoryException {
LOG.debug("getRootEntity: Getting ENTITY-MIB using {}", agentConfig);
final List<SnmpObjId> vendorOidList = m_hwInventoryAdapterConfigDao.getConfiguration().getVendorOid(node.getSysObjectId());
final Map<String, String> replacementMap = m_hwInventoryAdapterConfigDao.getConfiguration().getReplacementMap();
final SnmpObjId[] vendorOids = vendorOidList.toArray(new SnmpObjId[vendorOidList.size()]);
final SnmpObjId[] allOids = (SnmpObjId[]) ArrayUtils.addAll(EntityPhysicalTableRow.ELEMENTS, vendorOids);
final EntityPhysicalTableTracker tracker = new EntityPhysicalTableTracker(m_vendorAttributes, allOids, replacementMap);
final String trackerName = tracker.getClass().getSimpleName() + '_' + node.getLabel();
final CompletableFuture<EntityPhysicalTableTracker> future = m_locationAwareSnmpClient.walk(agentConfig, tracker).withDescription(trackerName).withLocation(node.getLocation() != null ? node.getLocation().getLocationName() : null).execute();
try {
future.get();
} catch (ExecutionException e) {
LOG.error("Aborting entities scan for " + agentConfig, e);
throw new SnmpHardwareInventoryException("Aborting entities scan: Agent failed while scanning the " + trackerName + " table: " + e.getMessage());
} catch (final InterruptedException e) {
throw new SnmpHardwareInventoryException("ENTITY-MIB node collection interrupted, exiting");
}
OnmsHwEntity root = tracker.getRootEntity();
if (root == null) {
throw new SnmpHardwareInventoryException("Cannot get root entity for node " + node.getLabel() + ", it seems like its SNMP agent does not have an implementation for the entPhysicalTable of the ENTITY-MIB, or it has an incorrect implementation of it.");
}
return root;
}
Aggregations