Search in sources :

Example 1 with SmiNamedNumber

use of org.jsmiparser.smi.SmiNamedNumber in project opennms by OpenNMS.

the class JsmiMibParser method getTrapEventDescr.

/**
 * Gets the trap event description.
 *
 * @param trap the trap object
 * @return the trap event description
 */
protected String getTrapEventDescr(Notification trap) {
    String description = trap.getDescription();
    if (description == null) {
        LOG.warn("The trap {} doesn't have a description field", trap.getOidStr());
    }
    // FIXME There a lot of detail here (like removing the last \n) that can go away when we don't need to match mib2opennms exactly
    final String descrEndingNewlines = description == null ? "No Description." : description.replaceAll("^", "\n<p>").replaceAll("$", "</p>\n");
    final StringBuffer dbuf = new StringBuffer(descrEndingNewlines);
    if (dbuf.charAt(dbuf.length() - 1) == '\n') {
        // delete the \n at the end
        dbuf.deleteCharAt(dbuf.length() - 1);
    }
    dbuf.append("<table>");
    dbuf.append("\n");
    int vbNum = 1;
    for (SmiVariable var : trap.getObjects()) {
        dbuf.append("\t<tr><td><b>\n\n\t").append(var.getId());
        dbuf.append("</b></td><td>\n\t%parm[#").append(vbNum).append("]%;</td><td><p>");
        SmiPrimitiveType type = var.getType().getPrimitiveType();
        if (type.equals(SmiPrimitiveType.ENUM)) {
            SortedMap<BigInteger, String> map = new TreeMap<BigInteger, String>();
            SmiType t = var.getType();
            while (t.getEnumValues() == null) {
                t = t.getBaseType();
            }
            List<SmiNamedNumber> enumValues = t.getEnumValues();
            if (enumValues != null) {
                for (SmiNamedNumber v : enumValues) {
                    map.put(v.getValue(), v.getId());
                }
            } else {
                // This is theoretically impossible, but in case of another bug in JSMIParser, better than an NPE.
                map.put(new BigInteger("0"), "Unable to derive list of possible values.");
            }
            dbuf.append("\n");
            for (Entry<BigInteger, String> entry : map.entrySet()) {
                dbuf.append("\t\t").append(entry.getValue()).append("(").append(entry.getKey()).append(")\n");
            }
            dbuf.append("\t");
        }
        dbuf.append("</p></td></tr>\n");
        vbNum++;
    }
    if (dbuf.charAt(dbuf.length() - 1) == '\n') {
        // delete the \n at the end
        dbuf.deleteCharAt(dbuf.length() - 1);
    }
    dbuf.append("</table>\n\t");
    return dbuf.toString();
}
Also used : SmiVariable(org.jsmiparser.smi.SmiVariable) SmiPrimitiveType(org.jsmiparser.smi.SmiPrimitiveType) BigInteger(java.math.BigInteger) SmiNamedNumber(org.jsmiparser.smi.SmiNamedNumber) TreeMap(java.util.TreeMap) SmiType(org.jsmiparser.smi.SmiType)

Example 2 with SmiNamedNumber

use of org.jsmiparser.smi.SmiNamedNumber in project opennms by OpenNMS.

the class JsmiMibParser method getTrapVarbindsDecode.

/**
 * Gets the trap varbinds decode.
 *
 * @param trap the trap object
 * @return the trap varbinds decode
 */
protected List<Varbindsdecode> getTrapVarbindsDecode(Notification trap) {
    Map<String, Varbindsdecode> decode = new LinkedHashMap<String, Varbindsdecode>();
    int vbNum = 1;
    for (SmiVariable var : trap.getObjects()) {
        String parmName = "parm[#" + vbNum + "]";
        SmiPrimitiveType type = var.getType().getPrimitiveType();
        if (type.equals(SmiPrimitiveType.ENUM)) {
            SortedMap<BigInteger, String> map = new TreeMap<BigInteger, String>();
            SmiType t = var.getType();
            while (t.getEnumValues() == null) {
                t = t.getBaseType();
            }
            List<SmiNamedNumber> enumValues = t.getEnumValues();
            if (enumValues != null) {
                for (SmiNamedNumber v : enumValues) {
                    map.put(v.getValue(), v.getId());
                }
                for (Entry<BigInteger, String> entry : map.entrySet()) {
                    if (!decode.containsKey(parmName)) {
                        Varbindsdecode newVarbind = new Varbindsdecode();
                        newVarbind.setParmid(parmName);
                        decode.put(newVarbind.getParmid(), newVarbind);
                    }
                    Decode d = new Decode();
                    d.setVarbinddecodedstring(entry.getValue());
                    d.setVarbindvalue(entry.getKey().toString());
                    decode.get(parmName).addDecode(d);
                }
            }
        }
        vbNum++;
    }
    return new ArrayList<Varbindsdecode>(decode.values());
}
Also used : Varbindsdecode(org.opennms.netmgt.xml.eventconf.Varbindsdecode) ArrayList(java.util.ArrayList) Decode(org.opennms.netmgt.xml.eventconf.Decode) SmiNamedNumber(org.jsmiparser.smi.SmiNamedNumber) TreeMap(java.util.TreeMap) LinkedHashMap(java.util.LinkedHashMap) SmiVariable(org.jsmiparser.smi.SmiVariable) SmiPrimitiveType(org.jsmiparser.smi.SmiPrimitiveType) BigInteger(java.math.BigInteger) SmiType(org.jsmiparser.smi.SmiType)

Aggregations

BigInteger (java.math.BigInteger)2 TreeMap (java.util.TreeMap)2 SmiNamedNumber (org.jsmiparser.smi.SmiNamedNumber)2 SmiPrimitiveType (org.jsmiparser.smi.SmiPrimitiveType)2 SmiType (org.jsmiparser.smi.SmiType)2 SmiVariable (org.jsmiparser.smi.SmiVariable)2 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 Decode (org.opennms.netmgt.xml.eventconf.Decode)1 Varbindsdecode (org.opennms.netmgt.xml.eventconf.Varbindsdecode)1