Search in sources :

Example 21 with BeanWrapper

use of org.springframework.beans.BeanWrapper in project opennms by OpenNMS.

the class SnmpTrapNorthbounderConfigurationResource method updateImportMapping.

/**
 * Update import mapping.
 *
 * @param trapSinkName the trap sink name
 * @param mappingName the mapping name
 * @param params the parameters map
 * @return the response
 */
@PUT
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Path("trapsinks/{trapsinkName}/import-mappings/{mappingName}")
public Response updateImportMapping(@PathParam("trapsinkName") final String trapSinkName, @PathParam("mappingName") final String mappingName, final MultivaluedMapImpl params) {
    writeLock();
    try {
        SnmpTrapSink trapSink = getSnmpTrapSink(trapSinkName);
        SnmpTrapMappingGroup mappingGroup = null;
        try {
            mappingGroup = trapSink.getImportMapping(mappingName);
        } catch (Throwable t) {
            throw getException(Status.INTERNAL_SERVER_ERROR, t);
        }
        if (mappingGroup == null) {
            return Response.status(404).build();
        }
        boolean modified = false;
        final BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(mappingGroup);
        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) {
            try {
                trapSink.addImportMapping(mappingGroup);
            } catch (Throwable t) {
                throw getException(Status.INTERNAL_SERVER_ERROR, t);
            }
            saveConfiguration();
            return Response.noContent().build();
        }
        return Response.notModified().build();
    } finally {
        writeUnlock();
    }
}
Also used : SnmpTrapMappingGroup(org.opennms.netmgt.alarmd.northbounder.snmptrap.SnmpTrapMappingGroup) 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)

Example 22 with BeanWrapper

use of org.springframework.beans.BeanWrapper 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();
    }
}
Also used : OnmsHwEntity(org.opennms.netmgt.model.OnmsHwEntity) BeanWrapper(org.springframework.beans.BeanWrapper) OnmsNode(org.opennms.netmgt.model.OnmsNode) OnmsHwEntityAttribute(org.opennms.netmgt.model.OnmsHwEntityAttribute) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) PUT(javax.ws.rs.PUT)

Example 23 with BeanWrapper

use of org.springframework.beans.BeanWrapper in project opennms by OpenNMS.

the class NodeRestService method updateNode.

/**
 * <p>updateNode</p>
 *
 * @param nodeCriteria a {@link java.lang.String} object.
 * @param params a {@link org.opennms.web.rest.support.MultivaluedMapImpl} object.
 * @return a {@link javax.ws.rs.core.Response} object.
 */
@PUT
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Path("{nodeCriteria}")
public Response updateNode(@PathParam("nodeCriteria") final String nodeCriteria, final MultivaluedMapImpl params) {
    writeLock();
    try {
        final OnmsNode node = m_nodeDao.get(nodeCriteria);
        if (node == null)
            throw getException(Status.BAD_REQUEST, "Node {} was not found.", nodeCriteria);
        if (node.getAssetRecord().getGeolocation() == null) {
            node.getAssetRecord().setGeolocation(new OnmsGeolocation());
        }
        LOG.debug("updateNode: updating node {}", node);
        boolean modified = false;
        final BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(node);
        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) {
            LOG.debug("updateNode: node {} updated", node);
            m_nodeDao.saveOrUpdate(node);
            return Response.noContent().build();
        }
        return Response.notModified().build();
    } finally {
        writeUnlock();
    }
}
Also used : BeanWrapper(org.springframework.beans.BeanWrapper) OnmsNode(org.opennms.netmgt.model.OnmsNode) OnmsGeolocation(org.opennms.netmgt.model.OnmsGeolocation) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) PUT(javax.ws.rs.PUT)

Example 24 with BeanWrapper

use of org.springframework.beans.BeanWrapper in project opennms by OpenNMS.

the class OnmsIpInterfaceResource method updateIpInterface.

/**
 * <p>updateIpInterface</p>
 *
 * @param nodeCriteria a {@link java.lang.String} object.
 * @param ipAddress a {@link java.lang.String} object.
 * @param params a {@link org.opennms.web.rest.support.MultivaluedMapImpl} object.
 * @return a {@link javax.ws.rs.core.Response} object.
 */
@PUT
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Path("{ipAddress}")
public Response updateIpInterface(@PathParam("nodeCriteria") final String nodeCriteria, @PathParam("ipAddress") final String ipAddress, final MultivaluedMapImpl params) {
    writeLock();
    try {
        final OnmsNode node = m_nodeDao.get(nodeCriteria);
        if (node == null) {
            throw getException(Status.BAD_REQUEST, "Node {} was not found.", nodeCriteria);
        }
        final OnmsIpInterface ipInterface = node.getIpInterfaceByIpAddress(ipAddress);
        if (ipInterface == null) {
            throw getException(Status.CONFLICT, "Can't find interface with IP address {} for node {}.", ipAddress, nodeCriteria);
        }
        LOG.debug("updateIpInterface: updating ip interface {}", ipInterface);
        final BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(ipInterface);
        boolean modified = false;
        for (final String key : params.keySet()) {
            // skip nodeId since we already know the node this is associated with and don't want to overwrite it
            if ("nodeId".equals(key)) {
                continue;
            }
            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) {
            LOG.debug("updateIpInterface: ip interface {} updated", ipInterface);
            m_ipInterfaceDao.saveOrUpdate(ipInterface);
            return Response.noContent().build();
        }
        return Response.notModified().build();
    } finally {
        writeUnlock();
    }
}
Also used : BeanWrapper(org.springframework.beans.BeanWrapper) OnmsNode(org.opennms.netmgt.model.OnmsNode) OnmsIpInterface(org.opennms.netmgt.model.OnmsIpInterface) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) PUT(javax.ws.rs.PUT)

Example 25 with BeanWrapper

use of org.springframework.beans.BeanWrapper in project opennms by OpenNMS.

the class OnmsMonitoredServiceResource method updateService.

/**
 * <p>updateService</p>
 *
 * @param nodeCriteria a {@link java.lang.String} object.
 * @param ipAddress a {@link java.lang.String} object.
 * @param serviceName a {@link java.lang.String} object.
 * @param params a {@link org.opennms.web.rest.support.MultivaluedMapImpl} object.
 * @return a {@link javax.ws.rs.core.Response} object.
 */
@PUT
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Path("{service}")
public Response updateService(@PathParam("nodeCriteria") String nodeCriteria, @PathParam("ipAddress") String ipAddress, @PathParam("service") String serviceName, MultivaluedMapImpl params) {
    writeLock();
    try {
        OnmsNode node = m_nodeDao.get(nodeCriteria);
        if (node == null)
            throw getException(Status.BAD_REQUEST, "Node {} was not found.", nodeCriteria);
        OnmsIpInterface intf = node.getIpInterfaceByIpAddress(ipAddress);
        if (intf == null)
            throw getException(Status.BAD_REQUEST, "IP Interface {} was not found on node {}.", ipAddress, nodeCriteria);
        OnmsMonitoredService service = intf.getMonitoredServiceByServiceType(serviceName);
        if (service == null)
            throw getException(Status.BAD_REQUEST, "Monitored Service {} was not found on IP Interface {} and node {}.", serviceName, ipAddress, nodeCriteria);
        LOG.debug("updateService: updating service {}", service);
        boolean modified = false;
        BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(service);
        for (String key : params.keySet()) {
            if (wrapper.isWritableProperty(key)) {
                String stringValue = params.getFirst(key);
                Object value = wrapper.convertIfNecessary(stringValue, (Class<?>) wrapper.getPropertyType(key));
                if (key.equals("status")) {
                    if ("S".equals(value) || ("A".equals(service.getStatus()) && "F".equals(value))) {
                        LOG.debug("updateService: suspending polling for service {} on node with IP {}", service.getServiceName(), service.getIpAddress().getHostAddress());
                        value = "F";
                        sendEvent(EventConstants.SUSPEND_POLLING_SERVICE_EVENT_UEI, service);
                    }
                    if ("R".equals(value) || ("F".equals(service.getStatus()) && "A".equals(value))) {
                        LOG.debug("updateService: resuming polling for service {} on node with IP {}", service.getServiceName(), service.getIpAddress().getHostAddress());
                        value = "A";
                        sendEvent(EventConstants.RESUME_POLLING_SERVICE_EVENT_UEI, service);
                    }
                }
                wrapper.setPropertyValue(key, value);
                modified = true;
            }
        }
        if (modified) {
            LOG.debug("updateSservice: service {} updated", service);
            m_serviceDao.saveOrUpdate(service);
            return Response.noContent().build();
        }
        return Response.notModified().build();
    } finally {
        writeUnlock();
    }
}
Also used : BeanWrapper(org.springframework.beans.BeanWrapper) OnmsNode(org.opennms.netmgt.model.OnmsNode) OnmsIpInterface(org.opennms.netmgt.model.OnmsIpInterface) OnmsMonitoredService(org.opennms.netmgt.model.OnmsMonitoredService) 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