use of org.springframework.beans.BeanWrapper 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.springframework.beans.BeanWrapper in project opennms by OpenNMS.
the class EntityPhysicalTableRow method getOnmsHwEntity.
/**
* Gets the hardware entity.
*
* @param vendorAttributes the vendor attributes
* @param replacementMap the replacement map
* @return the hardware entity
*/
public OnmsHwEntity getOnmsHwEntity(Map<SnmpObjId, HwEntityAttributeType> vendorAttributes, Map<String, String> replacementMap) {
SnmpValue v = null;
final OnmsHwEntity entity = new OnmsHwEntity();
entity.setEntPhysicalIndex(getEntPhysicalIndex());
entity.setEntPhysicalClass(getEntPhysicalClass());
v = getValue(entPhysicalDescr);
if (v != null && !v.toDisplayString().trim().isEmpty())
entity.setEntPhysicalDescr(v.toDisplayString().trim());
v = getValue(entPhysicalVendorType);
if (v != null && !v.toDisplayString().trim().isEmpty())
entity.setEntPhysicalVendorType(v.toDisplayString().trim());
v = getValue(entPhysicalContainedIn);
if (v != null)
entity.setEntPhysicalContainedIn(v.toInt());
v = getValue(entPhysicalParentRelPos);
if (v != null)
entity.setEntPhysicalParentRelPos(v.toInt());
v = getValue(entPhysicalName);
if (v != null && !v.toDisplayString().trim().isEmpty())
entity.setEntPhysicalName(v.toDisplayString().trim().trim());
v = getValue(entPhysicalHardwareRev);
if (v != null && !v.toDisplayString().trim().isEmpty())
entity.setEntPhysicalHardwareRev(v.toDisplayString().trim());
v = getValue(entPhysicalFirmwareRev);
if (v != null && !v.toDisplayString().trim().isEmpty())
entity.setEntPhysicalFirmwareRev(v.toDisplayString().trim());
v = getValue(entPhysicalSoftwareRev);
if (v != null && !v.toDisplayString().trim().isEmpty())
entity.setEntPhysicalSoftwareRev(v.toDisplayString().trim());
v = getValue(entPhysicalSerialNum);
if (v != null && !v.toDisplayString().trim().isEmpty())
entity.setEntPhysicalSerialNum(v.toDisplayString().trim());
v = getValue(entPhysicalMfgName);
if (v != null && !v.toDisplayString().trim().isEmpty())
entity.setEntPhysicalMfgName(v.toDisplayString().trim());
v = getValue(entPhysicalModelName);
if (v != null && !v.toDisplayString().trim().isEmpty())
entity.setEntPhysicalModelName(v.toDisplayString().trim());
v = getValue(entPhysicalAlias);
if (v != null && !v.toDisplayString().trim().isEmpty())
entity.setEntPhysicalAlias(v.toDisplayString().trim());
v = getValue(entPhysicalAssetID);
if (v != null && !v.toDisplayString().trim().isEmpty())
entity.setEntPhysicalAssetID(v.toDisplayString().trim());
v = getValue(entPhysicalIsFRU);
if (v != null)
entity.setEntPhysicalIsFRU(v.toInt() == 1 ? true : false);
v = getValue(entPhysicalUris);
if (v != null && !v.toDisplayString().trim().isEmpty())
entity.setEntPhysicalUris(v.toDisplayString());
if (vendorAttributes != null && vendorAttributes.size() > 0) {
BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(entity);
for (Map.Entry<SnmpObjId, HwEntityAttributeType> entry : vendorAttributes.entrySet()) {
v = getValue(entry.getKey());
if (v != null && !v.toDisplayString().trim().isEmpty()) {
String typeName = entry.getValue().getName();
if (replacementMap.containsKey(typeName)) {
String property = replacementMap.get(typeName);
if (wrapper.isWritableProperty(property)) {
wrapper.setPropertyValue(property, v.toDisplayString().trim());
}
} else {
entity.addAttribute(entry.getValue(), v.toDisplayString().trim());
}
}
}
}
return entity;
}
use of org.springframework.beans.BeanWrapper in project opennms by OpenNMS.
the class EmailNorthbounderConfigurationResource method updateEmailDestination.
/**
* Updates a specific email destination.
*
* @param destinationName the destination name
* @param params the parameters map
* @return the response
*/
@PUT
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Path("destinations/{destinationName}")
public Response updateEmailDestination(@PathParam("destinationName") final String destinationName, final MultivaluedMapImpl params) {
writeLock();
try {
boolean modified = false;
final EmailDestination destination = getEmailDestination(destinationName);
final BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(destination);
for (final String key : params.keySet()) {
if (wrapper.isWritableProperty(key)) {
final String stringValue = params.getFirst(key);
final Object value = wrapper.convertIfNecessary(stringValue, (Class<?>) wrapper.getPropertyType(key));
wrapper.setPropertyValue(key, value);
modified = true;
}
}
if (modified) {
saveConfiguration();
return Response.noContent().build();
}
return Response.notModified().build();
} finally {
writeUnlock();
}
}
use of org.springframework.beans.BeanWrapper in project opennms by OpenNMS.
the class JavamailConfigurationResource method updateConfiguration.
/**
* Update configuration.
*
* @param config the configuration object
* @param params the parameters
* @return true, if successful
*/
private boolean updateConfiguration(final Object config, final MultivaluedMapImpl params) {
boolean modified = false;
final BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(config);
for (final String key : params.keySet()) {
if (wrapper.isWritableProperty(key)) {
final String stringValue = params.getFirst(key);
final Object value = wrapper.convertIfNecessary(stringValue, (Class<?>) wrapper.getPropertyType(key));
wrapper.setPropertyValue(key, value);
modified = true;
}
}
return modified;
}
use of org.springframework.beans.BeanWrapper in project opennms by OpenNMS.
the class SnmpTrapNorthbounderConfigurationResource method updateSnmpTrapSink.
/**
* Update a specific SNMP trap sink.
*
* @param trapSinkName the trap sink name
* @param params the parameters map
* @return the response
*/
@PUT
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Path("trapsinks/{trapsinkName}")
public Response updateSnmpTrapSink(@PathParam("trapsinkName") final String trapSinkName, final MultivaluedMapImpl params) {
writeLock();
try {
boolean modified = false;
SnmpTrapSink trapSink = getSnmpTrapSink(trapSinkName);
final BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(trapSink);
for (final String key : params.keySet()) {
if (wrapper.isWritableProperty(key)) {
final String stringValue = params.getFirst(key);
final Object value = wrapper.convertIfNecessary(stringValue, (Class<?>) wrapper.getPropertyType(key));
wrapper.setPropertyValue(key, value);
modified = true;
}
}
if (modified) {
saveConfiguration();
return Response.noContent().build();
}
return Response.notModified().build();
} finally {
writeUnlock();
}
}
Aggregations