Search in sources :

Example 6 with Argument

use of org.opennms.netmgt.model.notifd.Argument in project opennms by OpenNMS.

the class AsteriskOriginateNotificationStrategy method buildOriginator.

private AsteriskOriginator buildOriginator(final List<Argument> arguments) throws AsteriskOriginatorException {
    final AsteriskOriginator ao = new AsteriskOriginator();
    for (final Argument arg : arguments) {
        final String argSwitch = arg.getSwitch();
        final String argValue = arg.getValue();
        if (NotificationManager.PARAM_WORK_PHONE.equals(argSwitch)) {
            LOG.debug("Found: PARAM_WORK_PHONE => {}", argValue);
            ao.setLegAExtension(argValue);
        } else if (NotificationManager.PARAM_HOME_PHONE.equals(argSwitch)) {
            LOG.debug("Found: PARAM_HOME_PHONE => {}", argValue);
            ao.setLegAExtension(argValue);
        } else if (NotificationManager.PARAM_MOBILE_PHONE.equals(argSwitch)) {
            LOG.debug("Found: PARAM_MOBILE_PHONE => {}", argValue);
            ao.setLegAExtension(argValue);
        } else if (NotificationManager.PARAM_SUBJECT.equals(argSwitch)) {
            LOG.debug("Found: PARAM_SUBJECT => {}", argValue);
            ao.setSubject(argValue);
            ao.setChannelVariable(BaseOnmsAgiScript.VAR_OPENNMS_NOTIFY_SUBJECT, argValue);
        } else if (NotificationManager.PARAM_TEXT_MSG.equals(argSwitch)) {
            LOG.debug("Found: PARAM_TEXT_MSG => {}", argValue);
            ao.setMessageText(argValue);
            ao.setChannelVariable(BaseOnmsAgiScript.VAR_OPENNMS_NOTIFY_BODY, argValue);
        } else if (NotificationManager.PARAM_TUI_PIN.equals(argSwitch)) {
            LOG.debug("Found: PARAM_TUI_PIN => {}", argValue);
            ao.setChannelVariable(BaseOnmsAgiScript.VAR_OPENNMS_USER_PIN, argValue);
        } else if (NotificationManager.PARAM_DESTINATION.equals(argSwitch)) {
            LOG.debug("Found: PARAM_DESTINATION => {}", argValue);
            ao.setChannelVariable(BaseOnmsAgiScript.VAR_OPENNMS_USERNAME, argValue);
        } else if (NotificationManager.PARAM_NODE.equals(argSwitch)) {
            LOG.debug("Found: PARAM_NODE => {}", argValue);
            ao.setChannelVariable(BaseOnmsAgiScript.VAR_OPENNMS_NODEID, argValue);
            try {
                final NodeDao nodeDao = BeanUtils.getBean("notifdContext", "nodeDao", NodeDao.class);
                ao.setChannelVariable(BaseOnmsAgiScript.VAR_OPENNMS_NODELABEL, nodeDao.get(argValue).getLabel());
            } catch (final Throwable t) {
                ao.setChannelVariable(BaseOnmsAgiScript.VAR_OPENNMS_NODELABEL, null);
            }
        } else if (NotificationManager.PARAM_INTERFACE.equals(argSwitch)) {
            LOG.debug("Found: PARAM_INTERFACE => {}", argValue);
            ao.setChannelVariable(BaseOnmsAgiScript.VAR_OPENNMS_INTERFACE, argValue);
        } else if (NotificationManager.PARAM_SERVICE.equals(argSwitch)) {
            LOG.debug("Found: PARAM_SERVICE => {}", argValue);
            ao.setChannelVariable(BaseOnmsAgiScript.VAR_OPENNMS_SERVICE, argValue);
        } else {
            LOG.debug("Unconsumed arg: {} => {}", String.valueOf(argSwitch), String.valueOf(argValue));
        }
    }
    return ao;
}
Also used : NodeDao(org.opennms.netmgt.dao.api.NodeDao) Argument(org.opennms.netmgt.model.notifd.Argument) AsteriskOriginator(org.opennms.netmgt.asterisk.utils.AsteriskOriginator)

Example 7 with Argument

use of org.opennms.netmgt.model.notifd.Argument in project opennms by OpenNMS.

the class MockNotificationStrategy method send.

/* (non-Javadoc)
     * @see org.opennms.netmgt.notifd.NotificationStrategy#send(java.util.List)
     */
@Override
public int send(List<Argument> arguments) {
    MockUtil.println("Message sent with arguments:" + arguments);
    MockNotification notification = new MockNotification();
    Iterator<Argument> it = arguments.iterator();
    while (it.hasNext()) {
        Argument arg = it.next();
        if (arg.getSwitch().equals(NotificationManager.PARAM_SUBJECT)) {
            notification.setSubject(arg.getValue());
        } else if (arg.getSwitch().equals(NotificationManager.PARAM_EMAIL)) {
            notification.setEmail(arg.getValue());
        } else if (arg.getSwitch().equals(NotificationManager.PARAM_TEXT_MSG)) {
            notification.setTextMsg(arg.getValue());
        }
    }
    notification.setExpectedTime(System.currentTimeMillis());
    NotificationAnticipator anticipator = getAnticipator();
    if (anticipator != null) {
        anticipator.notificationReceived(notification);
    } else {
        throw new NullPointerException("anticipator is null");
    }
    return 0;
}
Also used : MockNotification(org.opennms.netmgt.mock.MockNotification) Argument(org.opennms.netmgt.model.notifd.Argument) NotificationAnticipator(org.opennms.netmgt.mock.NotificationAnticipator)

Example 8 with Argument

use of org.opennms.netmgt.model.notifd.Argument in project opennms by OpenNMS.

the class BSFNotificationStrategy method declareBeans.

private static void declareBeans(BSFNotificationStrategy obj) throws BSFException {
    // Retrieve the parameters before accessing them
    obj.retrieveParams();
    Integer nodeId;
    try {
        nodeId = Integer.valueOf(obj.m_notifParams.get(NotificationManager.PARAM_NODE));
    } catch (NumberFormatException nfe) {
        nodeId = null;
    }
    OnmsNode node = null;
    OnmsAssetRecord assets = null;
    final List<String> categories = new ArrayList<>();
    String nodeLabel = null;
    String foreignSource = null;
    String foreignId = null;
    if (nodeId != null) {
        final BeanFactoryReference bf = BeanUtils.getBeanFactory("notifdContext");
        final NodeDao nodeDao = BeanUtils.getBean(bf, "nodeDao", NodeDao.class);
        final TransactionTemplate transTemplate = BeanUtils.getBean(bf, "transactionTemplate", TransactionTemplate.class);
        try {
            // Redeclare the node id as final
            final int theNodeId = nodeId;
            node = transTemplate.execute(new TransactionCallback<OnmsNode>() {

                @Override
                public OnmsNode doInTransaction(final TransactionStatus status) {
                    final OnmsNode node = nodeDao.get(theNodeId);
                    // Retrieve the categories in the context of the transaction
                    if (node != null) {
                        for (OnmsCategory cat : node.getCategories()) {
                            categories.add(cat.getName());
                        }
                    }
                    return node;
                }
            });
            if (node == null) {
                LOG.error("Could not find a node with id: {}", theNodeId);
            } else {
                nodeLabel = node.getLabel();
                assets = node.getAssetRecord();
                foreignSource = node.getForeignSource();
                foreignId = node.getForeignId();
            }
        } catch (final RuntimeException e) {
            LOG.error("Error while retrieving node with id {}", nodeId, e);
        }
    }
    s_bsfManager.declareBean("bsf_notif_strategy", obj, BSFNotificationStrategy.class);
    s_bsfManager.declareBean("logger", LOG, Logger.class);
    s_bsfManager.declareBean("notif_params", obj.m_notifParams, Map.class);
    s_bsfManager.declareBean("node_label", nodeLabel, String.class);
    s_bsfManager.declareBean("foreign_source", foreignSource, String.class);
    s_bsfManager.declareBean("foreign_id", foreignId, String.class);
    s_bsfManager.declareBean("node_assets", assets, OnmsAssetRecord.class);
    s_bsfManager.declareBean("node_categories", categories, List.class);
    s_bsfManager.declareBean("node", node, OnmsNode.class);
    for (Argument arg : obj.m_arguments) {
        if (NotificationManager.PARAM_TEXT_MSG.equals(arg.getSwitch()))
            s_bsfManager.declareBean("text_message", arg.getValue(), String.class);
        if (NotificationManager.PARAM_NUM_MSG.equals(arg.getSwitch()))
            s_bsfManager.declareBean("numeric_message", arg.getValue(), String.class);
        if (NotificationManager.PARAM_NODE.equals(arg.getSwitch()))
            s_bsfManager.declareBean("node_id", arg.getValue(), String.class);
        if (NotificationManager.PARAM_INTERFACE.equals(arg.getSwitch()))
            s_bsfManager.declareBean("ip_addr", arg.getValue(), String.class);
        if (NotificationManager.PARAM_SERVICE.equals(arg.getSwitch()))
            s_bsfManager.declareBean("svc_name", arg.getValue(), String.class);
        if (NotificationManager.PARAM_SUBJECT.equals(arg.getSwitch()))
            s_bsfManager.declareBean("subject", arg.getValue(), String.class);
        if (NotificationManager.PARAM_EMAIL.equals(arg.getSwitch()))
            s_bsfManager.declareBean("email", arg.getValue(), String.class);
        if (NotificationManager.PARAM_PAGER_EMAIL.equals(arg.getSwitch()))
            s_bsfManager.declareBean("pager_email", arg.getValue(), String.class);
        if (NotificationManager.PARAM_XMPP_ADDRESS.equals(arg.getSwitch()))
            s_bsfManager.declareBean("xmpp_address", arg.getValue(), String.class);
        if (NotificationManager.PARAM_TEXT_PAGER_PIN.equals(arg.getSwitch()))
            s_bsfManager.declareBean("text_pin", arg.getValue(), String.class);
        if (NotificationManager.PARAM_NUM_PAGER_PIN.equals(arg.getSwitch()))
            s_bsfManager.declareBean("numeric_pin", arg.getValue(), String.class);
        if (NotificationManager.PARAM_WORK_PHONE.equals(arg.getSwitch()))
            s_bsfManager.declareBean("work_phone", arg.getValue(), String.class);
        if (NotificationManager.PARAM_HOME_PHONE.equals(arg.getSwitch()))
            s_bsfManager.declareBean("home_phone", arg.getValue(), String.class);
        if (NotificationManager.PARAM_MOBILE_PHONE.equals(arg.getSwitch()))
            s_bsfManager.declareBean("mobile_phone", arg.getValue(), String.class);
        if (NotificationManager.PARAM_TUI_PIN.equals(arg.getSwitch()))
            s_bsfManager.declareBean("phone_pin", arg.getValue(), String.class);
        if (NotificationManager.PARAM_MICROBLOG_USERNAME.equals(arg.getSwitch()))
            s_bsfManager.declareBean("microblog_username", arg.getValue(), String.class);
    }
}
Also used : OnmsNode(org.opennms.netmgt.model.OnmsNode) Argument(org.opennms.netmgt.model.notifd.Argument) OnmsAssetRecord(org.opennms.netmgt.model.OnmsAssetRecord) ArrayList(java.util.ArrayList) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus) BeanFactoryReference(org.springframework.beans.factory.access.BeanFactoryReference) NodeDao(org.opennms.netmgt.dao.api.NodeDao) TransactionCallback(org.springframework.transaction.support.TransactionCallback) OnmsCategory(org.opennms.netmgt.model.OnmsCategory)

Example 9 with Argument

use of org.opennms.netmgt.model.notifd.Argument in project opennms by OpenNMS.

the class HttpNotificationStrategy method getPostArguments.

private List<NameValuePair> getPostArguments() {
    List<Argument> args = getArgsByPrefix("post-");
    List<NameValuePair> retval = new ArrayList<>();
    for (Argument arg : args) {
        String argSwitch = arg.getSwitch().substring("post-".length());
        if (arg.getValue() == null) {
            arg.setValue("");
        }
        retval.add(new BasicNameValuePair(argSwitch, getValue(arg.getValue())));
    }
    return retval;
}
Also used : BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) Argument(org.opennms.netmgt.model.notifd.Argument) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) ArrayList(java.util.ArrayList)

Example 10 with Argument

use of org.opennms.netmgt.model.notifd.Argument in project opennms by OpenNMS.

the class XMPPNotificationStrategy method send.

/*
	 * (non-Javadoc)
	 * 
	 * @see org.opennms.netmgt.notifd.NotificationStrategy#send(java.util.List)
	 */
/**
 * {@inheritDoc}
 */
@Override
public int send(List<Argument> arguments) {
    try {
        String[] parsedArgs = this.parseArguments(arguments);
        if (parsedArgs[XMPP_TO] == null || "".equals(parsedArgs[XMPP_TO])) {
            final StringBuilder argumentString = new StringBuilder();
            boolean first = true;
            for (Argument argument : arguments) {
                if (!first)
                    argumentString.append(", ");
                first = false;
                argumentString.append(argument == null ? "[null]" : "\"" + argument.toString() + "\"");
            }
            LOG.warn("Blank XMPP address on notification: {}", argumentString);
            return 1;
        }
        XMPPNotificationManager xmppManager = XMPPNotificationManager.getInstance();
        xmppManager.sendMessage(parsedArgs[XMPP_TO], parsedArgs[XMPP_MESSAGE]);
    } catch (Throwable e) {
        LOG.error(e.getMessage());
        return 1;
    }
    return 0;
}
Also used : Argument(org.opennms.netmgt.model.notifd.Argument)

Aggregations

Argument (org.opennms.netmgt.model.notifd.Argument)29 ArrayList (java.util.ArrayList)12 NotificationStrategy (org.opennms.netmgt.model.notifd.NotificationStrategy)8 Test (org.junit.Test)7 Ignore (org.junit.Ignore)3 JUnitHttpServer (org.opennms.core.test.http.annotations.JUnitHttpServer)3 JSONObject (org.json.simple.JSONObject)2 NodeDao (org.opennms.netmgt.dao.api.NodeDao)2 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 IOException (java.io.IOException)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Date (java.util.Date)1 NameValuePair (org.apache.http.NameValuePair)1 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)1 JavaMailer (org.opennms.javamail.JavaMailer)1 AsteriskOriginator (org.opennms.netmgt.asterisk.utils.AsteriskOriginator)1 MockNotification (org.opennms.netmgt.mock.MockNotification)1 NotificationAnticipator (org.opennms.netmgt.mock.NotificationAnticipator)1 OnmsAssetRecord (org.opennms.netmgt.model.OnmsAssetRecord)1