Migrate NatApp dev docs to rst
[docs.git] / manuals / developer-guide / src / main / asciidoc / openflowplugin / odl-ofp-wiring-up-notifications.adoc
1 === Wiring up notifications
2
3 ==== Introduction
4
5 We need to translate OpenFlow messages coming up from the
6 <<_openflow_protocol_library_developer_guide,OpenFlow Protocol Library>>
7 into MD-SAL Notification objects and then publish them to the
8 MD-SAL.
9
10 [[mechanics]]
11 ==== Mechanics
12
13 .  Create a Translator class
14 .  Register the Translator
15 .  Register the notificationPopListener to handle your Notification
16 Objects
17
18 [[create-a-translator-class]]
19 ===== Create a Translator class
20
21 You can see an example in
22 https://git.opendaylight.org/gerrit/gitweb?p=openflowplugin.git;a=blob;f=openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/core/translator/PacketInTranslator.java;hb=refs/heads/stable/boron[PacketInTranslator.java].
23
24 First, simply create the class
25
26 ---------------------------------------------------------------------------------------------
27 public class PacketInTranslator implements IMDMessageTranslator<OfHeader, List<DataObject>> {
28 ---------------------------------------------------------------------------------------------
29
30 Then implement the translate function:
31
32 ---------------------------------------------------------------------------------------------
33 public class PacketInTranslator implements IMDMessageTranslator<OfHeader, List<DataObject>> {
34
35     protected static final Logger LOG = LoggerFactory
36             .getLogger(PacketInTranslator.class);
37     @Override
38     public PacketReceived translate(SwitchConnectionDistinguisher cookie,
39             SessionContext sc, OfHeader msg) { 
40             ...
41     }
42 ---------------------------------------------------------------------------------------------
43
44 Make sure to check that you are dealing with the expected type and cast
45 it:
46
47 ---------------------------------------------------------------------------
48 if(msg instanceof PacketInMessage) {
49     PacketInMessage message = (PacketInMessage)msg;
50     List<DataObject> list = new CopyOnWriteArrayList<DataObject>();
51 ---------------------------------------------------------------------------
52
53 Do your transation work and return
54
55 -------------------------------------------------------------
56 PacketReceived pktInEvent = pktInBuilder.build();
57 list.add(pktInEvent);
58 return list;
59 -------------------------------------------------------------
60
61 [[register-your-translator-class]]
62 ===== Register your Translator Class
63
64 Next you need to go to
65 https://git.opendaylight.org/gerrit/gitweb?p=openflowplugin.git;a=blob;f=openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/core/MDController.java;hb=refs/heads/stable/boron[MDController.java]
66 and in init() add register your Translator:
67
68 -----------------------------------------------------------------------------------
69 public void init() {
70         LOG.debug("Initializing!");
71         messageTranslators = new ConcurrentHashMap<>();
72         popListeners = new ConcurrentHashMap<>();
73         //TODO: move registration to factory
74         addMessageTranslator(ErrorMessage.class, OF10, new ErrorTranslator());
75         addMessageTranslator(ErrorMessage.class, OF13, new ErrorTranslator());
76         addMessageTranslator(PacketInMessage.class,OF10, new PacketInTranslator());
77         addMessageTranslator(PacketInMessage.class,OF13, new PacketInTranslator());
78 -----------------------------------------------------------------------------------
79
80 Notice that there is a separate registration for each of OpenFlow 1.0 and OpenFlow 1.3.
81 Basically, you indicate the type of OpenFlow Protocol Library message you wish to
82 translate for, the OpenFlow version, and an instance of your Translator.
83
84 [[register-your-md-sal-message-for-notification-to-the-md-sal]]
85 ===== Register your MD-SAL Message for Notification to the MD-SAL
86
87 Now, also in MDController.init() register to have the
88 notificationPopListener handle your MD-SAL Message:
89
90 ---------------------------------------------------------------------------------------
91 addMessagePopListener(PacketReceived.class, new NotificationPopListener<DataObject>());
92 ---------------------------------------------------------------------------------------
93
94 [[you-are-done]]
95 ===== You are done
96
97 That's all there is to it. Now when a message comes up from the
98 OpenFlow Protocol Library, it will be translated and published to the MD-SAL.