Architecture - Notifications

FriendlySNMP API presents notifications declared in MIBs as org.friendlysnmp.FNotification class objects.

The term notification is ambiguous. It could mean a notification declaration in the MIB or a notification message itself. The following discussion will use terms notification and notification object to clarify this ambiguity when it is required.

As a default the agent sends notifications as traps for SNMP v1 and v2, and as inform requests for SNMP v3. This default behavior could be modified using custom targets. The following discussion will use term notification for both notification request types.

How and where notification objects are created
The following discussion uses a notification weatherRain declared in DEMO-NOTIFY-WEATHER-MIB from a demo application as an example. This notification is defined in the MIB as follows:

weatherRain NOTIFICATION-TYPE
    STATUS  current
    DESCRIPTION "Notification: rain"
    ::= { demoMIBObjects 5 }

Tools discussed in MIB-to-Java generation convert the MIB file DEMO-NOTIFY-WEATHER-MIB into Java class DemoNotifyWeatherMibFriend. This class has the following structure:

public class DemoNotifyWeatherMibFriend extends BaseMib {
    private FNotification weatherRain;
    . . .
    public FNotification getWeatherRain() {
        return weatherRain;
    }
}
The notification object weatherRain is initialized when object of the class DemoNotifyWeatherMibFriend added to the SNMP agent.

How to access notification object
There are two techniques to get access to FNotification objects.

Technique 1. Direct access from the MIB object using getters generated for each notification. This technique is used most of the time:

DemoNotifyWeatherMibFriend mib = new DemoNotifyWeatherMibFriend();
. . .
FNotification notify = mib.getWeatherRain();

Technique 2. Indirect access from the agent object using notification OID. This technique is used very rear, usually when a MIB object is hidden, for example when MIB is internal to the agent and is not visible from the outside:

import org.snmp4j.mp.SnmpConstants;
. . .
FriendlyAgent agent = new FriendlyAgent();
. . .
FNotification notify = agent.getNotification(SnmpConstants.coldStart);

Notification internals
The FNotification class is a wrapper which hides SNMP4J org.snmp4j.agent.NotificationOriginator implementation. The class provides a set of sendNotification(..) methods with different arguments. Each notification object is associated with immutable identifier org.friendlysnmp.FID. See also FID. The notification FID could be accessed but cannot be modified.

Sending notification
Notification message could be sent only using notification object. See different techniques how to obtain this object above.

Failure to sent notification is reported to the exception listeners registered with agent. See section Events and Listeners for details.

Examples of code to send notification:

FNotification notify = ...
notify.sendNotification();
   -- or --
notify.sendNotification("Printer is out of paper");
   -- or --
notify.sendNotification(new String[] {
   "It's rain outside",
   "Stay home"
});