Search in sources :

Example 11 with EventProxyException

use of org.opennms.netmgt.events.api.EventProxyException in project opennms by OpenNMS.

the class OnmsSnmpInterfaceResource method updateSnmpInterface.

/**
     * <p>updateSnmpInterface</p>
     *
     * @param nodeCriteria a {@link java.lang.String} object.
     * @param ifIndex a int.
     * @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("{ifIndex}")
public Response updateSnmpInterface(@PathParam("nodeCriteria") final String nodeCriteria, @PathParam("ifIndex") final int ifIndex, 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 (ifIndex < 0)
            throw getException(Status.BAD_REQUEST, "Invalid ifIndex specified for SNMP interface on node {}: {}", nodeCriteria, Integer.toString(ifIndex));
        final OnmsSnmpInterface snmpInterface = node.getSnmpInterfaceWithIfIndex(ifIndex);
        if (snmpInterface == null)
            throw getException(Status.BAD_REQUEST, "Can't find SNMP interface with ifIndex {} for node {}", Integer.toString(ifIndex), nodeCriteria);
        LOG.debug("updateSnmpInterface: updating SNMP interface {}", snmpInterface);
        boolean modified = false;
        final BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(snmpInterface);
        for (final String key : params.keySet()) {
            // don't try setting the node data
            if ("nodeId".equals(key))
                continue;
            // don't try setting ipinterface data
            if ("ipInterface".equals(key) || "ipInterfaces".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("updateSnmpInterface: SNMP interface {} updated", snmpInterface);
            m_snmpInterfaceDao.saveOrUpdate(snmpInterface);
        }
        Event e = null;
        if (params.containsKey("collect")) {
            // TODO Is this still valid if the interface was not modified ?
            // we've updated the collection flag so we need to send an event to redo collection
            final EventBuilder bldr = new EventBuilder(EventConstants.REINITIALIZE_PRIMARY_SNMP_INTERFACE_EVENT_UEI, "ReST");
            bldr.setNode(node);
            // Bug NMS-4432 says that sometimes the primary SNMP interface is null
            // so we need to check for that before we set the interface
            final OnmsIpInterface iface = node.getPrimaryInterface();
            if (iface == null) {
                LOG.warn("updateSnmpInterface: Cannot send {} event because node {} has no primary SNMP interface", EventConstants.REINITIALIZE_PRIMARY_SNMP_INTERFACE_EVENT_UEI, node.getId());
            } else {
                bldr.setInterface(iface.getIpAddress());
                e = bldr.getEvent();
            }
        }
        if (e != null) {
            try {
                m_eventProxy.send(e);
            } catch (final EventProxyException ex) {
                throw getException(Response.Status.INTERNAL_SERVER_ERROR, "Exception occurred sending event {} : {}", e.getUei(), ex.getMessage());
            }
        }
        return modified ? Response.noContent().build() : Response.notModified().build();
    } finally {
        writeUnlock();
    }
}
Also used : BeanWrapper(org.springframework.beans.BeanWrapper) OnmsNode(org.opennms.netmgt.model.OnmsNode) EventBuilder(org.opennms.netmgt.model.events.EventBuilder) OnmsIpInterface(org.opennms.netmgt.model.OnmsIpInterface) EventProxyException(org.opennms.netmgt.events.api.EventProxyException) OnmsSnmpInterface(org.opennms.netmgt.model.OnmsSnmpInterface) Event(org.opennms.netmgt.xml.event.Event) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) PUT(javax.ws.rs.PUT)

Example 12 with EventProxyException

use of org.opennms.netmgt.events.api.EventProxyException in project opennms by OpenNMS.

the class NodeRestService method sendEvent.

private void sendEvent(final String uei, final int nodeId, String nodeLabel) {
    try {
        final EventBuilder bldr = new EventBuilder(uei, "ReST");
        bldr.setNodeid(nodeId);
        bldr.addParam("nodelabel", nodeLabel);
        m_eventProxy.send(bldr.getEvent());
    } catch (final EventProxyException e) {
        throw getException(Status.INTERNAL_SERVER_ERROR, "Cannot send event {} : {}", uei, e.getMessage());
    }
}
Also used : EventBuilder(org.opennms.netmgt.model.events.EventBuilder) EventProxyException(org.opennms.netmgt.events.api.EventProxyException)

Example 13 with EventProxyException

use of org.opennms.netmgt.events.api.EventProxyException in project opennms by OpenNMS.

the class DefaultGraphResultsService method sendEvent.

private void sendEvent(List<String> filesToPromote) {
    EventBuilder bldr = new EventBuilder(EventConstants.PROMOTE_QUEUE_DATA_UEI, "OpenNMS.Webapp");
    bldr.addParam(EventConstants.PARM_FILES_TO_PROMOTE, filesToPromote);
    try {
        m_eventProxy.send(bldr.getEvent());
    } catch (EventProxyException e) {
        LOG.warn("Unable to send promotion event to opennms daemon", e);
    }
}
Also used : EventBuilder(org.opennms.netmgt.model.events.EventBuilder) EventProxyException(org.opennms.netmgt.events.api.EventProxyException)

Example 14 with EventProxyException

use of org.opennms.netmgt.events.api.EventProxyException in project opennms by OpenNMS.

the class MinionRestService method delete.

@DELETE
@Path("{id}")
@Override
public Response delete(@PathParam("id") final String id) {
    final OnmsMinion minion = getDao().get(id);
    final String location = minion.getLocation();
    Response response = super.delete(id);
    final EventBuilder eventBuilder = new EventBuilder(EventConstants.MONITORING_SYSTEM_DELETED_UEI, "OpenNMS.WebUI");
    eventBuilder.addParam(EventConstants.PARAM_MONITORING_SYSTEM_TYPE, OnmsMonitoringSystem.TYPE_MINION);
    eventBuilder.addParam(EventConstants.PARAM_MONITORING_SYSTEM_ID, id);
    eventBuilder.addParam(EventConstants.PARAM_MONITORING_SYSTEM_LOCATION, location);
    try {
        m_eventProxy.send(eventBuilder.getEvent());
    } catch (final EventProxyException e) {
        LOG.warn(" Failed to send Event on Minion deletion " + e.getMessage(), e);
    }
    return response;
}
Also used : Response(javax.ws.rs.core.Response) EventBuilder(org.opennms.netmgt.model.events.EventBuilder) OnmsMinion(org.opennms.netmgt.model.minion.OnmsMinion) EventProxyException(org.opennms.netmgt.events.api.EventProxyException) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE)

Example 15 with EventProxyException

use of org.opennms.netmgt.events.api.EventProxyException in project opennms by OpenNMS.

the class AssetRecordResource method updateAssetRecord.

/**
     * <p>updateAssetRecord</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)
public Response updateAssetRecord(@PathParam("nodeCriteria") final String nodeCriteria, final MultivaluedMapImpl params) {
    OnmsNode node = m_nodeDao.get(nodeCriteria);
    if (node == null) {
        throw getException(Status.BAD_REQUEST, "updateAssetRecord: Can't find node " + nodeCriteria);
    }
    OnmsAssetRecord assetRecord = getAssetRecord(node);
    if (assetRecord == null) {
        throw getException(Status.BAD_REQUEST, "updateAssetRecord: Node " + node + " could not update ");
    }
    if (assetRecord.getGeolocation() == null) {
        assetRecord.setGeolocation(new OnmsGeolocation());
    }
    LOG.debug("updateAssetRecord: updating asset {}", assetRecord);
    boolean modified = false;
    BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(assetRecord);
    wrapper.registerCustomEditor(Date.class, new ISO8601DateEditor());
    for (String key : params.keySet()) {
        if (wrapper.isWritableProperty(key)) {
            String stringValue = params.getFirst(key);
            Object value = wrapper.convertIfNecessary(stringValue, (Class<?>) wrapper.getPropertyType(key));
            wrapper.setPropertyValue(key, value);
            modified = true;
        }
    }
    if (modified) {
        LOG.debug("updateAssetRecord: assetRecord {} updated", assetRecord);
        m_assetRecordDao.saveOrUpdate(assetRecord);
        try {
            sendEvent(EventConstants.ASSET_INFO_CHANGED_EVENT_UEI, node.getId());
        } catch (EventProxyException e) {
            throw getException(Status.INTERNAL_SERVER_ERROR, e.getMessage());
        }
        return Response.noContent().build();
    }
    return Response.notModified().build();
}
Also used : BeanWrapper(org.springframework.beans.BeanWrapper) OnmsNode(org.opennms.netmgt.model.OnmsNode) ISO8601DateEditor(org.opennms.web.api.ISO8601DateEditor) OnmsAssetRecord(org.opennms.netmgt.model.OnmsAssetRecord) EventProxyException(org.opennms.netmgt.events.api.EventProxyException) OnmsGeolocation(org.opennms.netmgt.model.OnmsGeolocation) Consumes(javax.ws.rs.Consumes) PUT(javax.ws.rs.PUT)

Aggregations

EventProxyException (org.opennms.netmgt.events.api.EventProxyException)15 EventBuilder (org.opennms.netmgt.model.events.EventBuilder)9 Transactional (org.springframework.transaction.annotation.Transactional)4 NCSComponent (org.opennms.netmgt.model.ncs.NCSComponent)3 DataAccessResourceFailureException (org.springframework.dao.DataAccessResourceFailureException)3 Date (java.util.Date)2 Consumes (javax.ws.rs.Consumes)2 PUT (javax.ws.rs.PUT)2 Path (javax.ws.rs.Path)2 OnmsNode (org.opennms.netmgt.model.OnmsNode)2 OnmsMinion (org.opennms.netmgt.model.minion.OnmsMinion)2 Event (org.opennms.netmgt.xml.event.Event)2 BeanWrapper (org.springframework.beans.BeanWrapper)2 OutputStreamWriter (java.io.OutputStreamWriter)1 Writer (java.io.Writer)1 ConnectException (java.net.ConnectException)1 URL (java.net.URL)1 LinkedList (java.util.LinkedList)1 DELETE (javax.ws.rs.DELETE)1 Response (javax.ws.rs.core.Response)1