tpalfy commented on code in PR #6046: URL: https://github.com/apache/nifi/pull/6046#discussion_r911297463
########## nifi-nar-bundles/nifi-snmp-bundle/nifi-snmp-processors/src/main/java/org/apache/nifi/snmp/processors/SendTrapSNMP.java: ########## @@ -120,32 +122,58 @@ public void init(ProcessContext context) { @Override public void onTrigger(final ProcessContext context, final ProcessSession processSession) { final FlowFile flowFile = processSession.get(); - if (flowFile != null) { - try { - final int snmpVersion = SNMPUtils.getVersion(context.getProperty(BasicProperties.SNMP_VERSION).getValue()); - if (SnmpConstants.version1 == snmpVersion) { - V1TrapConfiguration v1TrapConfiguration = V1TrapConfiguration.builder() - .enterpriseOid(context.getProperty(V1TrapProperties.ENTERPRISE_OID).evaluateAttributeExpressions(flowFile).getValue()) - .agentAddress(context.getProperty(V1TrapProperties.AGENT_ADDRESS).evaluateAttributeExpressions(flowFile).getValue()) - .genericTrapType(context.getProperty(V1TrapProperties.GENERIC_TRAP_TYPE).evaluateAttributeExpressions(flowFile).getValue()) - .specificTrapType(context.getProperty(V1TrapProperties.SPECIFIC_TRAP_TYPE).evaluateAttributeExpressions(flowFile).getValue()) - .build(); - snmpHandler.sendTrap(flowFile.getAttributes(), v1TrapConfiguration); - } else { - V2TrapConfiguration v2TrapConfiguration = new V2TrapConfiguration( - context.getProperty(V2TrapProperties.TRAP_OID_VALUE).evaluateAttributeExpressions(flowFile).getValue() - ); - snmpHandler.sendTrap(flowFile.getAttributes(), v2TrapConfiguration); + final Map<String, String> attributes = new HashMap<>( + Optional.ofNullable(flowFile) + .map(FlowFile::getAttributes) + .orElse(Collections.emptyMap()) + ); + + try { + final int snmpVersion = SNMPUtils.getVersion(context.getProperty(BasicProperties.SNMP_VERSION).getValue()); + if (SnmpConstants.version1 == snmpVersion) { + + final String enterpriseOid = context.getProperty(V1TrapProperties.ENTERPRISE_OID).evaluateAttributeExpressions(flowFile).getValue(); + final String agentAddress = context.getProperty(V1TrapProperties.AGENT_ADDRESS).evaluateAttributeExpressions(flowFile).getValue(); + String genericTrapType = context.getProperty(V1TrapProperties.GENERIC_TRAP_TYPE).getValue(); + + if (genericTrapType.equals(V1TrapProperties.WITH_FLOW_FILE_ATTRIBUTE.getValue()) && flowFile != null) { + genericTrapType = flowFile.getAttribute(V1TrapProperties.GENERIC_TRAP_TYPE_FF_ATTRIBUTE); } + + final String specificTrapType = context.getProperty(V1TrapProperties.SPECIFIC_TRAP_TYPE).evaluateAttributeExpressions(flowFile).getValue(); + V1TrapConfiguration v1TrapConfiguration = V1TrapConfiguration.builder() + .enterpriseOid(enterpriseOid) + .agentAddress(agentAddress) + .genericTrapType(genericTrapType) + .specificTrapType(specificTrapType) + .build(); + attributes.put("agentAddress", agentAddress); + attributes.put("enterpriseOid", enterpriseOid); + attributes.put("genericTrapType", genericTrapType); + attributes.put("specificTrapType", specificTrapType); + snmpHandler.sendTrap(attributes, v1TrapConfiguration); + } else { + final String trapOidValue = context.getProperty(V2TrapProperties.TRAP_OID_VALUE).evaluateAttributeExpressions(flowFile).getValue(); + V2TrapConfiguration v2TrapConfiguration = new V2TrapConfiguration(trapOidValue); + attributes.put("trapOidValue", trapOidValue); + snmpHandler.sendTrap(attributes, v2TrapConfiguration); + } + if (flowFile == null) { + FlowFile outgoingFlowFile = processSession.create(); + processSession.putAllAttributes(outgoingFlowFile, attributes); + processSession.transfer(outgoingFlowFile, REL_SUCCESS); + } else { + processSession.putAllAttributes(flowFile, attributes); processSession.transfer(flowFile, REL_SUCCESS); - } catch (IOException e) { - getLogger().error("Failed to send request to the agent. Check if the agent supports the used version.", e); - processSession.transfer(processSession.penalize(flowFile), REL_FAILURE); - } catch (IllegalArgumentException e) { - getLogger().error("Invalid trap configuration.", e); - processSession.transfer(processSession.penalize(flowFile), REL_FAILURE); } + } catch (IOException e) { + getLogger().error("Failed to send request to the agent. Check if the agent supports the used version.", e); + processSession.transfer(processSession.penalize(flowFile), REL_FAILURE); Review Comment: Need to be careful with the `penalize` because `flowFile` can be `null` now! -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@nifi.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org