use of org.opennms.netmgt.model.OnmsAssetRecord in project opennms by OpenNMS.
the class SnmpAssetProvisioningAdapter method doAddNode.
/**
* <p>doAdd</p>
*
* @param nodeId a int.
* @param retry a boolean.
* @throws org.opennms.netmgt.provision.ProvisioningAdapterException if any.
*/
@Override
public void doAddNode(final int nodeId) throws ProvisioningAdapterException {
LOG.debug("doAdd: adding nodeid: {}", nodeId);
final OnmsNode node = m_nodeDao.get(nodeId);
Assert.notNull(node, "doAdd: failed to return node for given nodeId:" + nodeId);
InetAddress ipaddress = m_template.execute(new TransactionCallback<InetAddress>() {
@Override
public InetAddress doInTransaction(TransactionStatus arg0) {
return getIpForNode(node);
}
});
SnmpAgentConfig agentConfig = null;
String locationName = node.getLocation() != null ? node.getLocation().getLocationName() : null;
agentConfig = m_snmpConfigDao.getAgentConfig(ipaddress, locationName);
final OnmsAssetRecord asset = node.getAssetRecord();
m_config.getReadLock().lock();
try {
for (final AssetField field : m_config.getAssetFieldsForAddress(ipaddress, node.getSysObjectId())) {
try {
final String value = fetchSnmpAssetString(m_locationAwareSnmpClient, agentConfig, locationName, field.getMibObjs(), field.getFormatString());
LOG.debug("doAdd: Setting asset field \" {} \" to value: {}", field.getName(), value);
// Use Spring bean-accessor classes to set the field value
final BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(asset);
try {
wrapper.setPropertyValue(field.getName(), value);
} catch (final BeansException e) {
LOG.warn("doAdd: Could not set property \" {} \" on asset object {}", field.getName(), e.getMessage(), e);
}
} catch (final Throwable t) {
// This exception is thrown if the SNMP operation fails or an incorrect number of
// parameters is returned by the agent or because of a misconfiguration.
LOG.warn("doAdd: Could not set value for asset field \" {} \": {}", field.getName(), t.getMessage(), t);
}
}
} finally {
m_config.getReadLock().unlock();
}
node.setAssetRecord(asset);
m_nodeDao.saveOrUpdate(node);
m_nodeDao.flush();
}
use of org.opennms.netmgt.model.OnmsAssetRecord in project opennms by OpenNMS.
the class SnmpAssetProvisioningAdapter method doUpdateNode.
/**
* <p>doUpdate</p>
*
* @param nodeId a int.
* @param retry a boolean.
* @throws org.opennms.netmgt.provision.ProvisioningAdapterException if any.
*/
@Override
public void doUpdateNode(final int nodeId) throws ProvisioningAdapterException {
LOG.debug("doUpdate: updating nodeid: {}", nodeId);
final OnmsNode node = m_nodeDao.get(nodeId);
Assert.notNull(node, "doUpdate: failed to return node for given nodeId:" + nodeId);
final InetAddress ipaddress = m_template.execute(new TransactionCallback<InetAddress>() {
@Override
public InetAddress doInTransaction(final TransactionStatus arg0) {
return getIpForNode(node);
}
});
final String locationName = node.getLocation() != null ? node.getLocation().getLocationName() : null;
final SnmpAgentConfig agentConfig = m_snmpConfigDao.getAgentConfig(ipaddress, locationName);
final OnmsAssetRecord asset = node.getAssetRecord();
m_config.getReadLock().lock();
try {
for (AssetField field : m_config.getAssetFieldsForAddress(ipaddress, node.getSysObjectId())) {
try {
String value = fetchSnmpAssetString(m_locationAwareSnmpClient, agentConfig, locationName, field.getMibObjs(), field.getFormatString());
LOG.debug("doUpdate: Setting asset field \" {} \" to value: {}", value, field.getName());
// Use Spring bean-accessor classes to set the field value
BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(asset);
try {
wrapper.setPropertyValue(field.getName(), value);
} catch (BeansException e) {
LOG.warn("doUpdate: Could not set property \" {} \" on asset object: {}", field.getName(), e.getMessage(), e);
}
} catch (Throwable t) {
// This exception is thrown if the SNMP operation fails or an incorrect number of
// parameters is returned by the agent or because of a misconfiguration.
LOG.warn("doUpdate: Could not set value for asset field \" {} \": {}", field.getName(), t.getMessage(), t);
}
}
} finally {
m_config.getReadLock().unlock();
}
node.setAssetRecord(asset);
m_nodeDao.saveOrUpdate(node);
m_nodeDao.flush();
}
use of org.opennms.netmgt.model.OnmsAssetRecord in project opennms by OpenNMS.
the class EventUtilHibernateIT method testGetAssetFieldValue.
@Test
public void testGetAssetFieldValue() {
OnmsNode node1 = m_populator.getNode1();
OnmsAssetRecord asset1 = node1.getAssetRecord();
asset1.setAdmin("some-adm1n-label");
asset1.setSerialNumber("42");
m_assetRecordDao.saveOrUpdate(asset1);
String asset = eventUtilDaoImpl.getAssetFieldValue("asset[admin]", node1.getId());
assertEquals("some-adm1n-label", asset);
asset = eventUtilDaoImpl.getAssetFieldValue("asset[serialNumber]", node1.getId());
assertEquals("42", asset);
// Checking case sensitivity
asset = eventUtilDaoImpl.getAssetFieldValue("asset[serialnumber]", node1.getId());
assertEquals("42", asset);
}
use of org.opennms.netmgt.model.OnmsAssetRecord in project opennms by OpenNMS.
the class HttpDataCollectionIT method setUp.
/**
* Sets the up.
*
* @throws Exception the exception
*/
@Before
public void setUp() throws Exception {
MockLogAppender.setupLogging();
DefaultDataCollectionConfigDao dao = new DefaultDataCollectionConfigDao();
dao.setConfigDirectory("src/test/resources/etc/datacollection");
dao.setConfigResource(new FileSystemResource("src/test/resources/etc/datacollection-config.xml"));
dao.afterPropertiesSet();
DataCollectionConfigFactory.setInstance(dao);
m_rrdStrategy = new JRobinRrdStrategy();
m_resourceStorageDao = new FilesystemResourceStorageDao();
m_resourceStorageDao.setRrdDirectory(m_temporaryFolder.getRoot());
m_temporaryFolder.newFolder("snmp");
m_persisterFactory = new RrdPersisterFactory();
m_persisterFactory.setResourceStorageDao(m_resourceStorageDao);
m_persisterFactory.setRrdStrategy(m_rrdStrategy);
m_collectionAgent = new MockCollectionAgent(1, "mynode.local", InetAddrUtils.addr("127.0.0.1"));
m_nodeDao = EasyMock.createMock(NodeDao.class);
OnmsNode node = new OnmsNode();
node.setId(1);
node.setLabel("mynode.local");
node.setAssetRecord(new OnmsAssetRecord());
EasyMock.expect(m_nodeDao.get(1)).andReturn(node).anyTimes();
EasyMock.replay(m_nodeDao);
}
use of org.opennms.netmgt.model.OnmsAssetRecord in project opennms by OpenNMS.
the class AssetRecordDaoIT method testAddAutoenable.
@Test
@Transactional
public void testAddAutoenable() {
OnmsNode onmsNode = new OnmsNode(m_locationDao.getDefaultLocation(), "myNode");
m_nodeDao.save(onmsNode);
OnmsAssetRecord assetRecord = onmsNode.getAssetRecord();
assetRecord.setAssetNumber("imported-id: 7");
assetRecord.setUsername("antonio");
assetRecord.setPassword("password");
assetRecord.setAutoenable(OnmsAssetRecord.AUTOENABLED);
assetRecord.setConnection(OnmsAssetRecord.TELNET_CONNECTION);
m_assetRecordDao.update(assetRecord);
m_assetRecordDao.flush();
//Test findAll method
int id = assetRecord.getId();
OnmsAssetRecord assetRecordFromDb = m_assetRecordDao.get(id);
assertEquals(assetRecord.getUsername(), assetRecordFromDb.getUsername());
assertEquals(assetRecord.getPassword(), assetRecordFromDb.getPassword());
assertEquals(assetRecord.getAutoenable(), assetRecordFromDb.getAutoenable());
assertEquals(assetRecord.getConnection(), assetRecordFromDb.getConnection());
}
Aggregations