Search in sources :

Example 6 with Value

use of org.opennms.netmgt.xml.event.Value in project opennms by OpenNMS.

the class AbstractEventUtil method getNumParmValue.

/**
 * Helper method.
 *
 * @param parm
 * @param event
 * @return The value of a parameter based on its ordinal position in the event's list of parameters
 */
protected static String getNumParmValue(String parm, Event event) {
    String retParmVal = null;
    final List<Parm> parms = event.getParmCollection();
    int end = parm.lastIndexOf(PARM_END_SUFFIX);
    if (end != -1 && parms != null && parms.size() > 0) {
        // Get the value between the '#' and ']'
        String eparmname = parm.substring(PARM_NUM_PREFIX_LENGTH, end);
        int parmNum = -1;
        try {
            parmNum = Integer.parseInt(eparmname);
        } catch (NumberFormatException nfe) {
            parmNum = -1;
            retParmVal = null;
        }
        if (parmNum > 0 && parmNum <= parms.size()) {
            final Parm evParm = parms.get(parmNum - 1);
            // get parm value
            Value eparmval = evParm.getValue();
            if (eparmval != null) {
                retParmVal = EventConstants.getValueAsString(eparmval);
            }
        } else {
            retParmVal = null;
        }
    }
    return retParmVal;
}
Also used : Value(org.opennms.netmgt.xml.event.Value) Parm(org.opennms.netmgt.xml.event.Parm)

Example 7 with Value

use of org.opennms.netmgt.xml.event.Value in project opennms by OpenNMS.

the class EventExpander method expandEvent.

/**
 * <p>
 * This method is invoked to check and configure a received event. The event
 * configuration manager is consulted to find the appropriate configuration
 * that is used to expand the event. In addition, the security parameters
 * from the configuration manager is consulted to ensure that secure files
 * are cleared out if necessary.
 * </p>
 *
 * <p>
 * Any secure fields that exists in the incoming event are cleared during
 * expansion.
 * </p>
 *
 * @param e
 *            The event to expand if necessary.
 */
public void expandEvent(Event e) {
    org.opennms.netmgt.xml.eventconf.Event econf = lookup(m_eventConfDao, e);
    if (econf != null) {
        if (m_eventConfDao.isSecureTag("mask")) {
            e.setMask(null);
        }
        if (e.getMask() == null && econf.getMask() != null) {
            e.setMask(transform(econf.getMask()));
        }
        // 
        if (e.getUei() == null) {
            e.setUei(econf.getUei());
        }
        // 
        if (e.getSnmp() == null && econf.getSnmp() != null) {
            e.setSnmp(transform(econf.getSnmp()));
        }
        // 
        if (m_eventConfDao.isSecureTag("descr")) {
            e.setDescr(null);
        }
        if (e.getDescr() == null && econf.getDescr() != null) {
            e.setDescr(econf.getDescr());
        }
        // 
        if (m_eventConfDao.isSecureTag("logmsg")) {
            e.setLogmsg(null);
        }
        if (e.getLogmsg() == null && econf.getLogmsg() != null) {
            e.setLogmsg(transform(econf.getLogmsg()));
        }
        // 
        if (m_eventConfDao.isSecureTag("severity")) {
            e.setSeverity(null);
        }
        if (e.getSeverity() == null && econf.getSeverity() != null) {
            e.setSeverity(econf.getSeverity());
        }
        // 
        if (m_eventConfDao.isSecureTag("correlation")) {
            e.setCorrelation(null);
        }
        if (e.getCorrelation() == null && econf.getCorrelation() != null) {
            e.setCorrelation(transform(econf.getCorrelation()));
        }
        // 
        if (m_eventConfDao.isSecureTag("operinstruct")) {
            e.setOperinstruct(null);
        }
        if (e.getOperinstruct() == null && econf.getOperinstruct() != null) {
            e.setOperinstruct(econf.getOperinstruct());
        }
        // 
        if (m_eventConfDao.isSecureTag("autoaction")) {
            e.removeAllAutoaction();
        }
        if (e.getAutoactionCount() == 0 && econf.getAutoactions().size() > 0) {
            econf.getAutoactions().forEach(aa -> {
                e.addAutoaction(transform(aa));
            });
        }
        // 
        if (m_eventConfDao.isSecureTag("operaction")) {
            e.removeAllOperaction();
        }
        if (e.getOperactionCount() == 0 && econf.getOperactions().size() > 0) {
            econf.getOperactions().forEach(oa -> {
                e.addOperaction(transform(oa));
            });
        }
        // 
        if (m_eventConfDao.isSecureTag("autoacknowledge")) {
            e.setAutoacknowledge(null);
        }
        if (e.getAutoacknowledge() == null && econf.getAutoacknowledge() != null) {
            e.setAutoacknowledge(transform(econf.getAutoacknowledge()));
        }
        // 
        if (m_eventConfDao.isSecureTag("loggroup")) {
            e.removeAllLoggroup();
        }
        if (e.getLoggroupCount() == 0 && econf.getLoggroups().size() > 0) {
            e.setLoggroup(econf.getLoggroups());
        }
        // 
        if (m_eventConfDao.isSecureTag("tticket")) {
            e.setTticket(null);
        }
        if (e.getTticket() == null && econf.getTticket() != null) {
            e.setTticket(transform(econf.getTticket()));
        }
        // 
        if (m_eventConfDao.isSecureTag("forward")) {
            e.removeAllForward();
        }
        if (e.getForwardCount() == 0 && econf.getForwards().size() > 0) {
            econf.getForwards().forEach(fc -> {
                e.addForward(transform(fc));
            });
        }
        // 
        if (m_eventConfDao.isSecureTag("script")) {
            e.removeAllScript();
        }
        if (e.getScriptCount() == 0 && econf.getScripts().size() > 0) {
            econf.getScripts().forEach(sc -> {
                e.addScript(transform(sc));
            });
        }
        // 
        if (m_eventConfDao.isSecureTag("mouseovertext")) {
            e.setMouseovertext(null);
        }
        if (e.getMouseovertext() == null && econf.getMouseovertext() != null) {
            e.setMouseovertext(econf.getMouseovertext());
        }
        if (e.getAlarmData() == null && econf.getAlarmData() != null) {
            AlarmData alarmData = new AlarmData();
            final org.opennms.netmgt.xml.eventconf.AlarmData econfAlarmData = econf.getAlarmData();
            alarmData.setAlarmType(econfAlarmData.getAlarmType());
            alarmData.setReductionKey(econfAlarmData.getReductionKey());
            alarmData.setAutoClean(econfAlarmData.getAutoClean());
            alarmData.setX733AlarmType(econfAlarmData.getX733AlarmType());
            alarmData.setX733ProbableCause(econfAlarmData.getX733ProbableCause());
            alarmData.setClearKey(econfAlarmData.getClearKey());
            List<org.opennms.netmgt.xml.eventconf.UpdateField> updateFieldList = econfAlarmData.getUpdateFields();
            if (updateFieldList.size() > 0) {
                List<UpdateField> updateFields = new ArrayList<>(updateFieldList.size());
                for (org.opennms.netmgt.xml.eventconf.UpdateField econfUpdateField : updateFieldList) {
                    UpdateField eventField = new UpdateField();
                    eventField.setFieldName(econfUpdateField.getFieldName());
                    eventField.setUpdateOnReduction(econfUpdateField.getUpdateOnReduction());
                    updateFields.add(eventField);
                }
                alarmData.setUpdateField(updateFields);
            }
            e.setAlarmData(alarmData);
        }
        if (econf.getParameters() != null && econf.getParameters().size() > 0) {
            if (e.getParmCollection() == null) {
                e.setParmCollection(new ArrayList<>(econf.getParameters().size()));
            }
            for (Parameter p : econf.getParameters()) {
                if (EventUtils.getParm(e, p.getName()) == null) {
                    Parm parm = new Parm();
                    parm.setParmName(p.getName());
                    Value v = new Value();
                    v.setContent(p.getValue());
                    v.setType("string");
                    v.setEncoding("text");
                    v.setExpand(p.getExpand());
                    parm.setValue(v);
                    e.addParm(parm);
                } else {
                    LOG.warn("expandEvent: the event {} already has a parameter named {}, the original content will be preserved. Check the event definition and rename the optional parameter.", e.getUei(), p.getName());
                }
            }
        }
    }
    Map<String, Map<String, String>> decode = new HashMap<String, Map<String, String>>();
    if (econf != null && econf.getVarbindsdecodes().size() > 0) {
        for (final Varbindsdecode element : econf.getVarbindsdecodes()) {
            List<Decode> decodeArray = element.getDecodes();
            Map<String, String> valueMap = new HashMap<String, String>();
            for (final Decode element2 : decodeArray) {
                valueMap.put(element2.getVarbindvalue(), element2.getVarbinddecodedstring());
            }
            decode.put(element.getParmid(), valueMap);
        }
    }
    // end fill of event using econf
    // do the event parm expansion
    expandParms(e, decode);
}
Also used : HashMap(java.util.HashMap) Varbindsdecode(org.opennms.netmgt.xml.eventconf.Varbindsdecode) UpdateField(org.opennms.netmgt.xml.event.UpdateField) ArrayList(java.util.ArrayList) Decode(org.opennms.netmgt.xml.eventconf.Decode) Parm(org.opennms.netmgt.xml.event.Parm) Value(org.opennms.netmgt.xml.event.Value) Parameter(org.opennms.netmgt.xml.eventconf.Parameter) HashMap(java.util.HashMap) Map(java.util.Map) AlarmData(org.opennms.netmgt.xml.event.AlarmData)

Example 8 with Value

use of org.opennms.netmgt.xml.event.Value in project opennms by OpenNMS.

the class EventParameterUtils method decode.

/**
 * <p>decode</p>
 *
 * @param eventparms an event parm string
 * @return a list of parameters
 */
public static List<Parm> decode(final String eventparms) {
    if (eventparms == null)
        return null;
    final List<Parm> parms = new ArrayList<>();
    String[] paramslistString = eventparms.split(Character.toString(EventDatabaseConstants.MULTIPLE_VAL_DELIM));
    if (paramslistString != null) {
        for (int i = 0; i < paramslistString.length; i++) {
            String[] paramEncoded = paramslistString[i].split(Character.toString(EventDatabaseConstants.NAME_VAL_DELIM));
            if (paramEncoded != null && paramEncoded.length == 2) {
                Parm parm = new Parm();
                parm.setParmName(paramEncoded[0]);
                Value value = new Value();
                int startParamType = paramEncoded[1].lastIndexOf('(');
                if (startParamType == -1) {
                    value.setContent(paramEncoded[1]);
                    value.setType("string");
                    value.setEncoding("text");
                } else {
                    value.setContent(paramEncoded[1].substring(0, startParamType));
                    String paramType = paramEncoded[1].substring(startParamType + 1);
                    String[] typeAndEncode = paramType.split(Character.toString(EventDatabaseConstants.DB_ATTRIB_DELIM));
                    if (typeAndEncode != null && typeAndEncode.length == 2) {
                        value.setType(typeAndEncode[0]);
                        value.setEncoding(typeAndEncode[1].split("\\)")[0]);
                    } else {
                        value.setType("string");
                        value.setEncoding("text");
                    }
                }
                parm.setValue(value);
                parms.add(parm);
            }
        }
    }
    return parms;
}
Also used : ArrayList(java.util.ArrayList) Value(org.opennms.netmgt.xml.event.Value) Parm(org.opennms.netmgt.xml.event.Parm)

Example 9 with Value

use of org.opennms.netmgt.xml.event.Value in project opennms by OpenNMS.

the class EventParameterUtils method format.

/**
 * Format each parameter
 *
 * @param parm
 *            the parameter
 * @return the formatted event parameter string
 */
public static String format(Parm parm) {
    Value pValue = parm.getValue();
    String type = pValue.getType();
    String encoding = pValue.getEncoding();
    String tmp = EventDatabaseConstants.escape(parm.getParmName(), EventDatabaseConstants.NAME_VAL_DELIM);
    String name = EventDatabaseConstants.escape(tmp, EventDatabaseConstants.MULTIPLE_VAL_DELIM);
    tmp = EventDatabaseConstants.escape(pValue.getContent(), EventDatabaseConstants.NAME_VAL_DELIM);
    String value = EventDatabaseConstants.escape(tmp, EventDatabaseConstants.MULTIPLE_VAL_DELIM);
    String empty = "";
    name = (name != null ? name.trim() : empty);
    value = (value != null ? value.trim() : empty);
    type = (type != null ? type.trim() : empty);
    encoding = (encoding != null ? encoding.trim() : empty);
    final StringBuilder buf = new StringBuilder();
    buf.append(name);
    buf.append(EventDatabaseConstants.NAME_VAL_DELIM);
    buf.append(value);
    buf.append('(');
    buf.append(type);
    buf.append(EventDatabaseConstants.DB_ATTRIB_DELIM);
    buf.append(encoding);
    buf.append(')');
    return buf.toString();
// return name + EventDatabaseConstants.NAME_VAL_DELIM + value + "(" + type +
// EventDatabaseConstants.DB_ATTRIB_DELIM + encoding + ")";
}
Also used : Value(org.opennms.netmgt.xml.event.Value)

Example 10 with Value

use of org.opennms.netmgt.xml.event.Value in project opennms by OpenNMS.

the class TrapHandlerITCase method testInterfaceReparentedModifiesIpMgr.

@Test
@DirtiesContext
public void testInterfaceReparentedModifiesIpMgr() throws Exception {
    final OnmsNode node = new OnmsNode(m_dbPopulator.getMonitoringLocationDao().getDefaultLocation(), "test123");
    final OnmsIpInterface iface = new OnmsIpInterface();
    iface.setIpAddress(m_ip);
    iface.setIsSnmpPrimary(PrimaryType.PRIMARY);
    node.addIpInterface(iface);
    int nodeId = m_dbPopulator.getNodeDao().save(node);
    anticipateEvent("uei.opennms.org/default/trap", m_ip, nodeId);
    Event event = anticipateEvent(EventConstants.INTERFACE_REPARENTED_EVENT_UEI, m_ip, nodeId);
    event.addParm(new Parm() {

        {
            this.setParmName(EventConstants.PARM_OLD_NODEID);
            this.setValue(new Value(Integer.toString(m_nodeId)));
        }
    });
    event.addParm(new Parm() {

        {
            this.setParmName(EventConstants.PARM_NEW_NODEID);
            this.setValue(new Value(Integer.toString(nodeId)));
        }
    });
    m_eventMgr.sendNow(event);
    try {
        Thread.sleep(100);
    } catch (InterruptedException e) {
    // do nothing
    }
    sendTrap("v1", null, 6, 1);
    finishUp();
}
Also used : OnmsNode(org.opennms.netmgt.model.OnmsNode) OnmsIpInterface(org.opennms.netmgt.model.OnmsIpInterface) Value(org.opennms.netmgt.xml.event.Value) SnmpValue(org.opennms.netmgt.snmp.SnmpValue) Event(org.opennms.netmgt.xml.event.Event) Parm(org.opennms.netmgt.xml.event.Parm) Test(org.junit.Test) DirtiesContext(org.springframework.test.annotation.DirtiesContext)

Aggregations

Value (org.opennms.netmgt.xml.event.Value)33 Parm (org.opennms.netmgt.xml.event.Parm)31 InetAddress (java.net.InetAddress)5 Event (org.opennms.netmgt.xml.event.Event)5 ArrayList (java.util.ArrayList)4 Date (java.util.Date)3 Test (org.junit.Test)3 OnmsNode (org.opennms.netmgt.model.OnmsNode)3 PollableNode (org.opennms.netmgt.poller.pollables.PollableNode)3 EventHandler (org.opennms.netmgt.events.api.annotations.EventHandler)2 OnmsEvent (org.opennms.netmgt.model.OnmsEvent)2 OnmsIpInterface (org.opennms.netmgt.model.OnmsIpInterface)2 PollableInterface (org.opennms.netmgt.poller.pollables.PollableInterface)2 SnmpValue (org.opennms.netmgt.snmp.SnmpValue)2 Logmsg (org.opennms.netmgt.xml.event.Logmsg)2 Snmp (org.opennms.netmgt.xml.event.Snmp)2 BufferedReader (java.io.BufferedReader)1 StringReader (java.io.StringReader)1 InetSocketAddress (java.net.InetSocketAddress)1 HashMap (java.util.HashMap)1