Convert OF samples to use DTCL instead of DCL
[openflowplugin.git] / samples / learning-switch / src / main / java / org / opendaylight / openflowplugin / learningswitch / multi / PacketInDispatcherImpl.java
1 /**
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.openflowplugin.learningswitch.multi;
10
11 import java.util.HashMap;
12 import java.util.Map;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketProcessingListener;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived;
16 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
17
18 public class PacketInDispatcherImpl implements PacketProcessingListener {
19
20     private Map<InstanceIdentifier<Node>, PacketProcessingListener> handlerMapping;
21
22     /**
23      * default constructor
24      */
25     public PacketInDispatcherImpl() {
26         handlerMapping = new HashMap<>();
27     }
28
29     @Override
30     public void onPacketReceived(PacketReceived notification) {
31         // find corresponding handler
32         /**
33          * Notification contains reference to ingress port
34          * in a form of path in inventory: /nodes/node/node-connector
35          *
36          * In order to get path we shorten path to the first node reference
37          * by using firstIdentifierOf helper method provided by InstanceIdentifier,
38          * this will effectively shorten the path to /nodes/node.
39          */
40         InstanceIdentifier<?> ingressPort = notification.getIngress().getValue();
41         InstanceIdentifier<Node> nodeOfPacket = ingressPort.firstIdentifierOf(Node.class);
42         /**
43          * We lookup up the the packet-in listener for this node.
44          */
45         PacketProcessingListener nodeHandler = handlerMapping.get(nodeOfPacket);
46
47         /**
48          * If we have packet-processing listener, we delegate notification.
49          */
50         if (nodeHandler != null) {
51             nodeHandler.onPacketReceived(notification);
52         }
53     }
54
55     /**
56      * @return the handlerMapping
57      */
58     public Map<InstanceIdentifier<Node>, PacketProcessingListener> getHandlerMapping() {
59         return handlerMapping;
60     }
61 }