Beispiel #1
0
  @Override
  public Msg parseMsgFromXml(Boolean request, Element root, Runner runner) throws Exception {
    // header
    Element header = root.element("header");
    String version = header.attributeValue("version");
    String community = header.attributeValue("community");

    if (version == null) // get version given in config if not present in sendMessage
    version = getConfig().getString("protocol.version", "1");

    if (!Utils.isInteger(version)) throw new Exception("attribute version must be integer");

    Integer versionInt = Integer.parseInt(version);
    if (versionInt == 1) versionInt = SnmpConstants.version1;
    else if (versionInt == 2) versionInt = SnmpConstants.version2c;
    else if (versionInt == 3) versionInt = SnmpConstants.version3;
    else throw new Exception("possible version for SNMP are 1, 2 or 3 exclusively");

    Element pdu = root.element("pdu");
    String name = pdu.attributeValue("name");
    String type = pdu.attributeValue("type");
    String requestId = pdu.attributeValue("requestId");
    String errorStatus = pdu.attributeValue("errorStatus");
    String errorIndex = pdu.attributeValue("errorIndex");
    String nonRepeaters = pdu.attributeValue("nonRepeaters");
    String maxRepetitions = pdu.attributeValue("maxRepetitions");

    if ((type != null) && (name != null))
      throw new Exception("type and name of the message " + name + " must not be set both");

    if ((type == null) && (name == null))
      throw new Exception("One of the parameter type or name of the message header must be set");

    if ((type != null) && !Utils.isInteger(type))
      throw new Exception("attribute type must be integer");

    Integer typeInt = null;
    if (type == null) {
      if (name.equalsIgnoreCase("trap") && (versionInt == SnmpConstants.version1))
        typeInt = PDU.V1TRAP;
      else {
        typeInt = PDU.getTypeFromString(name.toUpperCase());
        if (typeInt < -100)
          throw new Exception("the type " + name + " specified is unknown in the SNMP protocol");
      }
    } else typeInt = Integer.parseInt(type);

    if (((errorStatus != null) || (errorIndex != null))
        && ((nonRepeaters != null) || (maxRepetitions != null)))
      throw new Exception(
          "The attributes errorStatus or errorIndex must not be set with nonRepeaters or maxRepetitions");

    if ((requestId == null) || !Utils.isInteger(requestId))
      throw new Exception("attribute requestId must be integer");

    if (((errorStatus != null) && !Utils.isInteger(errorStatus))
        || ((errorIndex != null) && !Utils.isInteger(errorIndex)))
      throw new Exception("attribute errorStatus and errorIndex must be integer");

    if (((nonRepeaters != null) && !Utils.isInteger(nonRepeaters))
        || ((maxRepetitions != null) && !Utils.isInteger(maxRepetitions)))
      throw new Exception("attribute nonRepeaters and maxRepetitions must be integer");

    Integer requestIdInt = (requestId != null) ? Integer.parseInt(requestId) : null;
    Integer errorStatusInt = (errorStatus != null) ? Integer.parseInt(errorStatus) : null;
    Integer errorIndexInt = (errorIndex != null) ? Integer.parseInt(errorIndex) : null;
    Integer nonRepeatersInt = (nonRepeaters != null) ? Integer.parseInt(nonRepeaters) : null;
    Integer maxRepetitionsInt = (maxRepetitions != null) ? Integer.parseInt(maxRepetitions) : null;

    MsgSnmp msgSmtp =
        new MsgSnmp(
            versionInt,
            community,
            typeInt,
            requestIdInt,
            errorStatusInt,
            errorIndexInt,
            nonRepeatersInt,
            maxRepetitionsInt);

    // specific parameter for snmpv1 Trap  to check and add to pdu
    if ((msgSmtp.getPdu() instanceof PDUv1) && (typeInt == PDU.V1TRAP)) {
      String enterprise = pdu.attributeValue("enterprise");
      String agentAddress = pdu.attributeValue("agentAddress");
      String genericTrap = pdu.attributeValue("genericTrap");
      String specificTrap = pdu.attributeValue("specificTrap");
      String timestamp = pdu.attributeValue("timestamp");

      if (genericTrap.equalsIgnoreCase("enterpriseSpecific") && !Utils.isInteger(specificTrap))
        throw new Exception(
            "specificTrap attribute must be an integer when enterpriseSpecific if given for genereicTrap in SNMPV1 TRAP message");
      if (!Utils.isInteger(timestamp)) throw new Exception("timestamp must be an integer");

      int genericTrapInt = 0;
      if (genericTrap.equalsIgnoreCase("coldStart")) genericTrapInt = PDUv1.COLDSTART;
      else if (genericTrap.equalsIgnoreCase("warmStart")) genericTrapInt = PDUv1.WARMSTART;
      else if (genericTrap.equalsIgnoreCase("linkDown")) genericTrapInt = PDUv1.LINKDOWN;
      else if (genericTrap.equalsIgnoreCase("linkUp")) genericTrapInt = PDUv1.LINKUP;
      else if (genericTrap.equalsIgnoreCase("authenticationFailure"))
        genericTrapInt = PDUv1.AUTHENTICATIONFAILURE;
      else if (genericTrap.equalsIgnoreCase("egpNeighborLoss"))
        genericTrapInt = 5; // specified in rfc 1157, but not present in snmp4j stack
      else if (genericTrap.equalsIgnoreCase("enterpriseSpecific"))
        genericTrapInt = PDUv1.ENTERPRISE_SPECIFIC;
      else throw new Exception("genericTrap attribute is unknown");

      ((PDUv1) msgSmtp.getPdu()).setEnterprise(new OID(enterprise));
      ((PDUv1) msgSmtp.getPdu()).setAgentAddress(new IpAddress(agentAddress));
      ((PDUv1) msgSmtp.getPdu()).setGenericTrap(genericTrapInt);
      ((PDUv1) msgSmtp.getPdu()).setSpecificTrap(Integer.parseInt(specificTrap));
      ((PDUv1) msgSmtp.getPdu()).setTimestamp(Integer.parseInt(timestamp));
    }

    List<Element> variables = pdu.elements("variableBinding");
    for (Element var : variables) {
      parseVariableBinding(var, msgSmtp.getPdu());
    }
    return msgSmtp;
  }