Search in sources :

Example 16 with BeanWrapper

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();
}
Also used : SnmpAgentConfig(org.opennms.netmgt.snmp.SnmpAgentConfig) BeanWrapper(org.springframework.beans.BeanWrapper) OnmsNode(org.opennms.netmgt.model.OnmsNode) OnmsAssetRecord(org.opennms.netmgt.model.OnmsAssetRecord) AssetField(org.opennms.netmgt.config.snmpAsset.adapter.AssetField) TransactionStatus(org.springframework.transaction.TransactionStatus) InetAddress(java.net.InetAddress) BeansException(org.springframework.beans.BeansException)

Example 17 with BeanWrapper

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;
}
Also used : OnmsHwEntity(org.opennms.netmgt.model.OnmsHwEntity) BeanWrapper(org.springframework.beans.BeanWrapper) SnmpValue(org.opennms.netmgt.snmp.SnmpValue) SnmpObjId(org.opennms.netmgt.snmp.SnmpObjId) HwEntityAttributeType(org.opennms.netmgt.model.HwEntityAttributeType) Map(java.util.Map)

Example 18 with BeanWrapper

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();
    }
}
Also used : BeanWrapper(org.springframework.beans.BeanWrapper) EmailDestination(org.opennms.netmgt.alarmd.northbounder.email.EmailDestination) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) PUT(javax.ws.rs.PUT)

Example 19 with BeanWrapper

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;
}
Also used : BeanWrapper(org.springframework.beans.BeanWrapper)

Example 20 with BeanWrapper

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();
    }
}
Also used : BeanWrapper(org.springframework.beans.BeanWrapper) SnmpTrapSink(org.opennms.netmgt.alarmd.northbounder.snmptrap.SnmpTrapSink) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) PUT(javax.ws.rs.PUT)

Aggregations

BeanWrapper (org.springframework.beans.BeanWrapper)144 BeanWrapperImpl (org.springframework.beans.BeanWrapperImpl)79 Test (org.junit.jupiter.api.Test)31 NumberTestBean (org.springframework.beans.testfixture.beans.NumberTestBean)21 BooleanTestBean (org.springframework.beans.testfixture.beans.BooleanTestBean)20 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)19 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)18 IndexedTestBean (org.springframework.beans.testfixture.beans.IndexedTestBean)18 TestBean (org.springframework.beans.testfixture.beans.TestBean)18 Consumes (javax.ws.rs.Consumes)16 PUT (javax.ws.rs.PUT)16 Path (javax.ws.rs.Path)16 PropertyEditorSupport (java.beans.PropertyEditorSupport)14 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)14 HashSet (java.util.HashSet)12 BeansException (org.springframework.beans.BeansException)12 PropertyDescriptor (java.beans.PropertyDescriptor)11 OnmsNode (org.opennms.netmgt.model.OnmsNode)9 Test (org.junit.Test)6 BigDecimal (java.math.BigDecimal)5