Search in sources :

Example 1 with ManagerResponse

use of org.asteriskjava.manager.response.ManagerResponse in project opennms by OpenNMS.

the class AsteriskOriginator method originateCall.

/**
 * Originates a call based on properties set on this bean.
 *
 * @throws org.opennms.netmgt.asterisk.utils.AsteriskOriginatorException if any.
 */
public void originateCall() throws AsteriskOriginatorException {
    OriginateAction originateAction = buildOriginateAction();
    LOG.info("Logging in Asterisk manager connection");
    try {
        m_managerConnection.login();
    } catch (final IllegalStateException ise) {
        throw new AsteriskOriginatorException("Illegal state logging in Asterisk manager connection", ise);
    } catch (final IOException ioe) {
        throw new AsteriskOriginatorException("I/O exception logging in Asterisk manager connection", ioe);
    } catch (final AuthenticationFailedException afe) {
        throw new AsteriskOriginatorException("Authentication failure logging in Asterisk manager connection", afe);
    } catch (final TimeoutException toe) {
        throw new AsteriskOriginatorException("Timed out logging in Asterisk manager connection", toe);
    }
    LOG.info("Successfully logged in Asterisk manager connection");
    LOG.info("Originating a call to extension {}", m_legAExtension);
    LOG.debug(createCallLogMsg());
    LOG.debug("Originate action:\n\n{}", originateAction.toString());
    ManagerResponse managerResponse = null;
    try {
        managerResponse = m_managerConnection.sendAction(originateAction);
    } catch (final IllegalArgumentException iae) {
        m_managerConnection.logoff();
        throw new AsteriskOriginatorException("Illegal argument sending originate action", iae);
    } catch (final IllegalStateException ise) {
        m_managerConnection.logoff();
        throw new AsteriskOriginatorException("Illegal state sending originate action", ise);
    } catch (final IOException ioe) {
        m_managerConnection.logoff();
        throw new AsteriskOriginatorException("I/O exception sending originate action", ioe);
    } catch (final TimeoutException toe) {
        m_managerConnection.logoff();
        throw new AsteriskOriginatorException("Timed out sending originate action", toe);
    }
    LOG.info("Asterisk manager responded: {}", managerResponse.getResponse());
    LOG.info("Asterisk manager message: {}", managerResponse.getMessage());
    if (managerResponse.getResponse().toLowerCase().startsWith("error")) {
        m_managerConnection.logoff();
        throw new AsteriskOriginatorException("Got error response sending originate event. Response: " + managerResponse.getResponse() + "; Message: " + managerResponse.getMessage());
    }
    LOG.info("Logging off Asterisk manager connection");
    m_managerConnection.logoff();
    LOG.info("Successfully logged off Asterisk manager connection");
}
Also used : ManagerResponse(org.asteriskjava.manager.response.ManagerResponse) AuthenticationFailedException(org.asteriskjava.manager.AuthenticationFailedException) IOException(java.io.IOException) OriginateAction(org.asteriskjava.manager.action.OriginateAction) TimeoutException(org.asteriskjava.manager.TimeoutException)

Example 2 with ManagerResponse

use of org.asteriskjava.manager.response.ManagerResponse in project openmeetings by apache.

the class SipDao method get.

public String get(String confno) {
    String pin = null;
    DbGetAction da = new DbGetAction(ASTERISK_OM_FAMILY, getKey(confno));
    ManagerResponse r = exec(da);
    if (r != null) {
        pin = r.getResponse();
    }
    return pin;
}
Also used : ManagerResponse(org.asteriskjava.manager.response.ManagerResponse) DbGetAction(org.asteriskjava.manager.action.DbGetAction)

Example 3 with ManagerResponse

use of org.asteriskjava.manager.response.ManagerResponse in project camel by apache.

the class AsteriskProducer method process.

public void process(Exchange exchange) throws Exception {
    ManagerAction action;
    switch(endpoint.getAction()) {
        case QUEUE_STATUS:
            action = new QueueStatusAction();
            break;
        case SIP_PEERS:
            action = new SipPeersAction();
            break;
        case EXTENSION_STATE:
            action = new ExtensionStateAction((String) exchange.getIn().getHeader(AsteriskConstants.EXTENSION), (String) exchange.getIn().getHeader(AsteriskConstants.CONTEXT));
            break;
        default:
            throw new IllegalStateException("Unknown action");
    }
    LOG.debug("Asterisk, send action {} ", endpoint.getAction());
    ManagerResponse response = endpoint.sendAction(action);
    exchange.getIn().setBody(response);
}
Also used : ManagerResponse(org.asteriskjava.manager.response.ManagerResponse) ManagerAction(org.asteriskjava.manager.action.ManagerAction) QueueStatusAction(org.asteriskjava.manager.action.QueueStatusAction) SipPeersAction(org.asteriskjava.manager.action.SipPeersAction) ExtensionStateAction(org.asteriskjava.manager.action.ExtensionStateAction)

Example 4 with ManagerResponse

use of org.asteriskjava.manager.response.ManagerResponse in project opennms by OpenNMS.

the class AsteriskSIPPeerMonitor method poll.

/**
 * {@inheritDoc}
 *
 * <P>
 * Run the service monitor and return the poll status
 * </P>
 */
public PollStatus poll(MonitoredService svc, Map<String, Object> parameters) {
    // read configuration parameters
    String sipPeer = ParameterMap.getKeyedString(parameters, "sip-peer", DEFAULT_SIPPEER);
    if (sipPeer.equals(DEFAULT_SIPPEER)) {
        LOG.error("AsteriskMonitor: No sip-peer parameter in poller configuration");
        throw new RuntimeException("AsteriskMonitor: required parameter 'sip-peer' is not present in supplied properties.");
    }
    TimeoutTracker timeoutTracker = new TimeoutTracker(parameters, DEFAULT_RETRY, DEFAULT_TIMEOUT);
    AmiPeerFactory amiPeerFactory = AmiPeerFactory.getInstance();
    AmiAgentConfig amiConfig = amiPeerFactory.getAgentConfig(svc.getAddress());
    // setting up AMI connection
    LOG.debug("{}: Creating new AMI-Connection: {}:{}, {}/{}", svc.getSvcName(), svc.getIpAddr(), amiConfig.getPort(), amiConfig.getUsername(), amiConfig.getPassword());
    ManagerConnectionFactory factory = new ManagerConnectionFactory(svc.getIpAddr(), amiConfig.getPort().orElse(null), amiConfig.getUsername().orElse(null), amiConfig.getPassword().orElse(null));
    ManagerConnection managerConnection;
    if (amiConfig.getUseTls().orElse(false)) {
        managerConnection = factory.createSecureManagerConnection();
    } else {
        managerConnection = factory.createManagerConnection();
    }
    managerConnection.setSocketTimeout(new Long(timeoutTracker.getTimeoutInMillis()).intValue());
    // start with polling
    while (timeoutTracker.shouldRetry()) {
        timeoutTracker.nextAttempt();
        LOG.debug("{}: Attempt {}", svc.getSvcName(), timeoutTracker.getAttempt());
        try {
            LOG.debug("{}: AMI login", svc.getSvcName());
            managerConnection.login();
            LOG.debug("{}: AMI sendAction SipShowPeer", svc.getSvcName());
            ManagerResponse response = managerConnection.sendAction(new SipShowPeerAction(sipPeer));
            if (response.getAttribute("Status") == null) {
                LOG.debug("{}: service status down", svc.getSvcName());
                return PollStatus.decode("Down", "State of SIP Peer is unknown, because it was not found on the Asterisk server");
            }
            LOG.debug("{}: Response: {}", svc.getSvcName(), response.getAttribute("Status"));
            LOG.debug("{}: AMI logoff", svc.getSvcName());
            managerConnection.logoff();
            if (response.getAttribute("Status").startsWith("OK")) {
                LOG.debug("{}: service status up", svc.getSvcName());
                return PollStatus.decode("Up", "OK");
            } else {
                LOG.debug("{}: service status down", svc.getSvcName());
                return PollStatus.decode("Down", "State of SIP Peer is " + response.getAttribute("Status") + " and not OK");
            }
        } catch (AuthenticationFailedException e) {
            LOG.debug("{}: AMI AuthenticationError.", svc.getSvcName(), e);
            return PollStatus.decode("Down", "Could not get the state of SIP Peer: AMI AuthenticationError");
        } catch (TimeoutException e) {
            LOG.debug("{}: TimeOut reached.", svc.getSvcName(), e);
        } catch (SocketTimeoutException e) {
            LOG.debug("{}: TimeOut reached.", svc.getSvcName(), e);
        } catch (Exception e) {
            LOG.error("{}: An Unknown Exception Occurred.", svc.getSvcName(), e);
            return PollStatus.decode("Down", "Could not get the state of SIP Peer: " + e.toString());
        }
    }
    // If none of the retries worked
    return PollStatus.decode("Down", "Could not get the state of SIP Peer: Timeout exceeded");
}
Also used : ManagerConnection(org.asteriskjava.manager.ManagerConnection) ManagerResponse(org.asteriskjava.manager.response.ManagerResponse) AuthenticationFailedException(org.asteriskjava.manager.AuthenticationFailedException) ManagerConnectionFactory(org.asteriskjava.manager.ManagerConnectionFactory) AmiAgentConfig(org.opennms.netmgt.config.ami.AmiAgentConfig) SipShowPeerAction(org.asteriskjava.manager.action.SipShowPeerAction) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) TimeoutException(org.asteriskjava.manager.TimeoutException) SocketTimeoutException(java.net.SocketTimeoutException) AuthenticationFailedException(org.asteriskjava.manager.AuthenticationFailedException) SocketTimeoutException(java.net.SocketTimeoutException) TimeoutTracker(org.opennms.core.utils.TimeoutTracker) AmiPeerFactory(org.opennms.netmgt.config.AmiPeerFactory) TimeoutException(org.asteriskjava.manager.TimeoutException) SocketTimeoutException(java.net.SocketTimeoutException)

Example 5 with ManagerResponse

use of org.asteriskjava.manager.response.ManagerResponse in project openmeetings by apache.

the class SipDao method exec.

private ManagerResponse exec(ManagerAction action) {
    if (factory == null) {
        log.warn("There is no Asterisk configured");
        return null;
    }
    ManagerConnection con = getConnection();
    try {
        con.login();
        ManagerResponse r = con.sendAction(action);
        if (r != null) {
            log.debug(r.toString());
        }
        return (r instanceof ManagerError) ? null : r;
    } catch (Exception e) {
        log.error("Error while executing ManagerAction: " + action, e);
    } finally {
        try {
            con.logoff();
        } catch (Exception e) {
        // no-op
        }
    }
    return null;
}
Also used : ManagerError(org.asteriskjava.manager.response.ManagerError) ManagerConnection(org.asteriskjava.manager.ManagerConnection) DefaultManagerConnection(org.asteriskjava.manager.DefaultManagerConnection) ManagerResponse(org.asteriskjava.manager.response.ManagerResponse)

Aggregations

ManagerResponse (org.asteriskjava.manager.response.ManagerResponse)5 AuthenticationFailedException (org.asteriskjava.manager.AuthenticationFailedException)2 ManagerConnection (org.asteriskjava.manager.ManagerConnection)2 TimeoutException (org.asteriskjava.manager.TimeoutException)2 IOException (java.io.IOException)1 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)1 SocketTimeoutException (java.net.SocketTimeoutException)1 DefaultManagerConnection (org.asteriskjava.manager.DefaultManagerConnection)1 ManagerConnectionFactory (org.asteriskjava.manager.ManagerConnectionFactory)1 DbGetAction (org.asteriskjava.manager.action.DbGetAction)1 ExtensionStateAction (org.asteriskjava.manager.action.ExtensionStateAction)1 ManagerAction (org.asteriskjava.manager.action.ManagerAction)1 OriginateAction (org.asteriskjava.manager.action.OriginateAction)1 QueueStatusAction (org.asteriskjava.manager.action.QueueStatusAction)1 SipPeersAction (org.asteriskjava.manager.action.SipPeersAction)1 SipShowPeerAction (org.asteriskjava.manager.action.SipShowPeerAction)1 ManagerError (org.asteriskjava.manager.response.ManagerError)1 TimeoutTracker (org.opennms.core.utils.TimeoutTracker)1 AmiPeerFactory (org.opennms.netmgt.config.AmiPeerFactory)1 AmiAgentConfig (org.opennms.netmgt.config.ami.AmiAgentConfig)1