java.lang.Object
org.snmp4j.fluent.SnmpBuilder
Builds a
Snmp instance through step-by-step configuration using a fluent interface design pattern.
This SnmpBuilder is the starting context. It creates the non-fluent Snmp instance that is
needed to as first parameter for SnmpCompletableFuture.send(Snmp, Target, PDU, Object...) which
actually sends the SNMP message to a Target.
Use TargetBuilder and PduBuilder to create the other two mandatory parameters. To get a
TargetBuilder call target(Address). The PduBuilder can be then retrieved from that
TargetBuilder by calling TargetBuilder.pdu().
For a complete code sample see https://snmp4j.org.
//Brief flow description of using the new SNMP4J fluent interface:
SnmpBuilder.udp()...build() => Snmp
SnmpBuilder.target(..) => TargetBuilder
TargetBuilder.user(..)...done()...build() => Target
TargetBuilder.pdu()...build() => PDUrequest
SnmpCompletableFuture.send(Snmp, Target, PDUrequest) => SnmpCompletableFuture
SnmpCompletableFuture.get() => PDUresponse
- Since:
- 3.5.0
- Version:
- 3.9.0
- Author:
- Frank Fock
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected CounterSupportprotected EngineBootsProviderprotected EngineIdProviderprotected byte[]protected ThreadPoolprotected Stringprotected final SecurityModelsprotected final SecurityProtocolsprotected final Snmp -
Constructor Summary
ConstructorsModifierConstructorDescriptionCreates anSnmpBuilderwith a default constructedSnmpinstance.protectedSnmpBuilder(Snmp snmp) Creates anSnmpBuilderwith an preconfiguredSnmpinstance, which must provide a nonnullSnmp.getMessageDispatcher(). -
Method Summary
Modifier and TypeMethodDescriptionaddSecurityProtocol(SecurityProtocol securityProtocol) Make an additionalSecurityProtocolavailable for theSnmpsession to be built.build()Build theSnmpinstance with all the previously called configurations from this fluent builder.counterSupport(CounterSupport counterSupport) Sets theCounterSupportwithSnmp.setCounterSupport(CounterSupport)when building theSnmpinstance usingbuild().dtls()dtls(TlsTmSecurityCallback<X509Certificate> securityCallback, String[] dtlsProtocolVersions, DtlsAddress... listenAddresses) dtls(TlsTmSecurityCallback<X509Certificate> securityCallback, DtlsAddress... listenAddresses) securityProtocols(SecurityProtocols.SecurityProtocolSet securityProtocolSet) Specifies which predefined set ofSecurityProtocolsshould be available for theSnmpsession to be built.<A extends Address>
TargetBuilder<A> target(A address) Creates aTargetBuilderfor the specified targetAddress.tcp()Add aDefaultTcpTransportMappingin client mode with an arbitrary local address amd port.tcp(TcpAddress... listenAddresses) Add theDefaultTcpTransportMappingserver mode TCP transport mappings for the specified listen addresses to theSnmpinstance to be built.threads(int numThreads) Configures theMultiThreadedMessageDispatcheras message dispatcher and thereby defines the number of threads in itsThreadPool.tls()tls(TlsTmSecurityCallback<X509Certificate> securityCallback, String[] tlsProtocolVersions, TlsAddress... listenAddresses) tls(TlsTmSecurityCallback<X509Certificate> securityCallback, TlsAddress... listenAddresses) tm(TransportMapping<?>... transportMappings) Configures the SNMP builder with the specified transport mappings.tsm(EngineIdProvider engineIdProvider, OctetString defaultLocalEngineID, boolean usePrefix) Creates a Transport Security Model as defined by RFC 5591 and adds it to the builtSnmpinstance.tsm(OctetString localEngineID, boolean usePrefix) Creates a Transport Security Model as defined by RFC 5591 and adds it to the builtSnmpinstance.udp()Adds a UDP transport mapping (DefaultUdpTransportMapping) with an operating system chosen local port on all local IP addresses.udp(UdpAddress... listenAddresses) Adds a set of UDP transport mappings (DefaultUdpTransportMapping) with the specified local listen addressesusm()Creates a USM for the specified engine ID and engine boots counter derived from the SNMP engine ID provider given byv3(EngineIdProvider, OctetString).usm(SnmpEngineIdProvider snmpEngineIdProvider, OctetString defaultLocalEngineID) Creates a USM using the specifiedSnmpEngineIdProvider.usm(OctetString localEngineID, int engineBoots) Creates a USM for the specified engine ID and engine boots counter.v1()Adds theMPv1security model toSnmp.getMessageDispatcher().v2c()Adds aMPv2csecurity model toSnmp.getMessageDispatcher().v3()Adds aMPv3with a randomly created local engine IDSnmp.getMessageDispatcher()ifusm(SnmpEngineIdProvider, OctetString)ortsm(EngineIdProvider, OctetString, boolean)have not been called yet to set anEngineIdProvider.v3(byte[] localEngineID) Sets the local engine ID for thisSnmpBuilderand creates the message processing model 3 (MPv3) by assigning theSecurityModelsandSecurityProtocolsconfigured by calls tousm(SnmpEngineIdProvider, OctetString)andtsm(EngineIdProvider, OctetString, boolean)for example as well assecurityProtocols(SecurityProtocols.SecurityProtocolSet).v3(EngineIdProvider engineIdProvider, OctetString id) Adds aMPv3message processing model with a random engine ID based on the supplied ID string.v3(OctetString id) Adds aMPv3message processing model with a random engine ID based on the supplied ID string.
-
Field Details
-
snmp
-
securityProtocols
-
securityModels
-
counterSupport
-
multiThreadedDispatcherPool
-
responderPoolName
-
engineIdProvider
-
engineBootsProvider
-
localEngineID
protected byte[] localEngineID
-
-
Constructor Details
-
SnmpBuilder
public SnmpBuilder()Creates anSnmpBuilderwith a default constructedSnmpinstance. -
SnmpBuilder
Creates anSnmpBuilderwith an preconfiguredSnmpinstance, which must provide a nonnullSnmp.getMessageDispatcher().CounterSupportis set toCounterSupport.getInstance(),SecurityProtocolsare initialized withSecurityProtocols.SecurityProtocolSet.defaultSecurity, andSecurityModelsis initialized with its default construct (i.e. no security models).- Parameters:
snmp- a non-nullSnmpinstance.
-
-
Method Details
-
target
Creates aTargetBuilderfor the specified targetAddress.- Type Parameters:
A- the transport address type.- Parameters:
address- aTransportIpAddress.- Returns:
- a
TargetBuilderinstance.
-
counterSupport
Sets theCounterSupportwithSnmp.setCounterSupport(CounterSupport)when building theSnmpinstance usingbuild().- Parameters:
counterSupport- theCounterSupport, defaults toCounterSupport.getInstance().- Returns:
- this builder.
-
v1
Adds theMPv1security model toSnmp.getMessageDispatcher().- Returns:
- this builder.
-
v2c
Adds aMPv2csecurity model toSnmp.getMessageDispatcher().- Returns:
- this builder.
-
v3
Adds aMPv3with a randomly created local engine IDSnmp.getMessageDispatcher()ifusm(SnmpEngineIdProvider, OctetString)ortsm(EngineIdProvider, OctetString, boolean)have not been called yet to set anEngineIdProvider.CAUTION: Randomly generated engine IDs work fairly well for command generator applications, but SHOULD NOT be used for command responder (i.e. agents). Instead use preferably
v3(OctetString)orv3(byte[]).Make sure to set the
SNMP4JSettings.setEnterpriseID(int)to the company's registered IANA ID before calling this method.- Returns:
- this builder.
-
v3
Adds aMPv3message processing model with a random engine ID based on the supplied ID string. Make sure to set theSNMP4JSettings.setEnterpriseID(int)to the company's registered IANA ID before calling this method.- Parameters:
id- an ID string (see RFC 3414) suitable to build an unique local engine ID.- Returns:
- this builder
-
v3
Adds aMPv3message processing model with a random engine ID based on the supplied ID string. Make sure to set theSNMP4JSettings.setEnterpriseID(int)to the company's registered IANA ID before calling this method.- Parameters:
engineIdProvider- a class that provides the persistently stored engine ID from the previous application execution and that provides and saves the current boot counter as well as a first time initialized engine ID.id- an ID string suitable to build a local engine ID.- Returns:
- this builder
-
v3
Sets the local engine ID for thisSnmpBuilderand creates the message processing model 3 (MPv3) by assigning theSecurityModelsandSecurityProtocolsconfigured by calls tousm(SnmpEngineIdProvider, OctetString)andtsm(EngineIdProvider, OctetString, boolean)for example as well assecurityProtocols(SecurityProtocols.SecurityProtocolSet).- Parameters:
localEngineID- the local engine ID of the SNMP entity represented by theSnmpinstance to be built.- Returns:
- this builder.
-
threads
Configures theMultiThreadedMessageDispatcheras message dispatcher and thereby defines the number of threads in itsThreadPool.- Parameters:
numThreads- the number of threads in theMultiThreadedMessageDispatcher'sThreadPoolwhich must be greater than 1.- Returns:
- this builder.
-
usm
Creates a USM for the specified engine ID and engine boots counter derived from the SNMP engine ID provider given byv3(EngineIdProvider, OctetString). This method requires that eitherv3(byte[])orv3(EngineIdProvider, OctetString)have been already called before, otherwise aNullPointerExceptionwill be thrown.- Returns:
- this builder.
-
usm
Creates a USM for the specified engine ID and engine boots counter.- Parameters:
localEngineID- the local engine ID.engineBoots- the number of engine boots.- Returns:
- this builder.
-
usm
Creates a USM using the specifiedSnmpEngineIdProvider.- Parameters:
snmpEngineIdProvider- the engine ID and boots counter provider, that stores and restores both values according to RFC 3414, i.e. by increasing engine boots on each application initialization.defaultLocalEngineID- the local engine ID if the givenSnmpEngineIdProviderhas not stored one yet.- Returns:
- this builder.
-
tsm
Creates a Transport Security Model as defined by RFC 5591 and adds it to the builtSnmpinstance.- Parameters:
localEngineID- the engine ID of the SNMP entity using this transport security model. The local engine ID must be globally unique.usePrefix- the snmpTsmConfigurationUsePrefix flag as defined in RFC 5591.- Returns:
- this builder.
-
tsm
public SnmpBuilder tsm(EngineIdProvider engineIdProvider, OctetString defaultLocalEngineID, boolean usePrefix) Creates a Transport Security Model as defined by RFC 5591 and adds it to the builtSnmpinstance.- Parameters:
engineIdProvider- the engine ID of the SNMP entity using this transport security model. The local engine ID must be globally unique and theEngineIdProvideris responsible to save the engine ID persistently.defaultLocalEngineID- the engine ID to be used and stored persistently if the providedengineIdProvidercannot provide an engine ID.usePrefix- the snmpTsmConfigurationUsePrefix flag as defined in RFC 5591.- Returns:
- this builder.
-
udp
Adds a UDP transport mapping (DefaultUdpTransportMapping) with an operating system chosen local port on all local IP addresses.- Returns:
- this builder.
- Throws:
IOException- if the socket could not be bound.
-
udp
Adds a set of UDP transport mappings (DefaultUdpTransportMapping) with the specified local listen addresses- Parameters:
listenAddresses- the local IPs and ports to listen for incoming UDP messages.- Returns:
- this builder.
- Throws:
IOException- if one of the sockets could not be bound.
-
tcp
Add aDefaultTcpTransportMappingin client mode with an arbitrary local address amd port.- Returns:
- this builder.
- Throws:
IOException- if the local port cannot be bound.
-
tcp
Add theDefaultTcpTransportMappingserver mode TCP transport mappings for the specified listen addresses to theSnmpinstance to be built.- Parameters:
listenAddresses- the local IPs and ports to listen for incoming TCP messages.- Returns:
- this builder.
- Throws:
IOException- if theDefaultTcpTransportMappingcannot bind all local ports.
-
dtls
- Returns:
- this builder.
- Throws:
IOException- if theDTLSTMcannot bind the local port.
-
dtls
public SnmpBuilder dtls(TlsTmSecurityCallback<X509Certificate> securityCallback, DtlsAddress... listenAddresses) throws IOException Add theDTLSTMserver mode DTLS transport mappings for the specified listen addresses to theSnmpinstance to be built. The DTLS protocol versions to be supported are defined byDTLSTM.DEFAULT_DTLSTM_PROTOCOLS.- Parameters:
securityCallback- the callback function to validate X509 certificates of communication peers, seeTlsTmSecurityCallback.listenAddresses- the local IPs and ports to listen for incoming DTLS messages.- Returns:
- this builder.
- Throws:
IOException- if theDTLSTMcannot bind all local ports.
-
dtls
public SnmpBuilder dtls(TlsTmSecurityCallback<X509Certificate> securityCallback, String[] dtlsProtocolVersions, DtlsAddress... listenAddresses) throws IOException Add theDTLSTMserver mode DTLS transport mappings for the specified listen addresses to theSnmpinstance to be built.- Parameters:
securityCallback- the callback function to validate X509 certificates of communication peers, seeTlsTmSecurityCallback.dtlsProtocolVersions- the DTLS protocol versions to be supported, default isDTLSTM.DEFAULT_DTLSTM_PROTOCOLS. That is used whennullis provided.listenAddresses- the local IPs and ports to listen for incoming DTLS messages.- Returns:
- this builder.
- Throws:
IOException- if theDTLSTMcannot bind all local ports.
-
tls
- Returns:
- this builder.
- Throws:
IOException- if theTLSTMcannot bind the local port.
-
tls
public SnmpBuilder tls(TlsTmSecurityCallback<X509Certificate> securityCallback, TlsAddress... listenAddresses) throws IOException Add theTLSTMserver mode TLS transport mappings for the specified listen addresses to theSnmpinstance to be built. The TLS protocol versions to be supported are defined byTLSTM.DEFAULT_TLSTM_PROTOCOLS.- Parameters:
securityCallback- the callback function to validate X509 certificates of communication peers, seeTlsTmSecurityCallback.listenAddresses- the local IPs and ports to listen for incoming TLS messages.- Returns:
- this builder.
- Throws:
IOException- if theTLSTMcannot bind all local ports.
-
tls
public SnmpBuilder tls(TlsTmSecurityCallback<X509Certificate> securityCallback, String[] tlsProtocolVersions, TlsAddress... listenAddresses) throws IOException Add theTLSTMserver mode TLS transport mappings for the specified listen addresses to theSnmpinstance to be built.- Parameters:
securityCallback- the callback function to validate X509 certificates of communication peers, seeTlsTmSecurityCallback.tlsProtocolVersions- the TLS protocol versions to be supported, default isTLSTM.DEFAULT_TLSTM_PROTOCOLS. That is used whennullis provided.listenAddresses- the local IPs and ports to listen for incoming TLS messages.- Returns:
- this builder.
- Throws:
IOException- if theTLSTMcannot bind all local ports.
-
tm
Configures the SNMP builder with the specified transport mappings. This method adds the transport mappings to the message dispatcher and sets the dispatcher as a listener for the provided mappings.- Parameters:
transportMappings- one or more TransportMapping objects to be added to the SNMP message dispatcher.- Returns:
- the current instance of SnmpBuilder for method chaining.
- Since:
- 3.9.3
-
securityProtocols
Specifies which predefined set ofSecurityProtocolsshould be available for theSnmpsession to be built. Already added or set security protocols will be removed before the specified set is added.- Parameters:
securityProtocolSet- a predefined set ofSecurityProtocol, default isSecurityProtocols.SecurityProtocolSet.defaultSecurity- Returns:
- this builder.
-
addSecurityProtocol
Make an additionalSecurityProtocolavailable for theSnmpsession to be built.- Parameters:
securityProtocol- aAuthenticationProtocolorPrivacyProtocolto add. Please note that privacy protocols require certain authentication protocol key length and you cannot use privacy without authentication protocol.- Returns:
- this builder
- Since:
- 3.9.0
-
build
Build theSnmpinstance with all the previously called configurations from this fluent builder.- Returns:
- a new
Snmpinstance, that now starts toSnmp.listen()for incoming requests/responses. - Throws:
IOException- if theSnmpinstance fails to listen.
-