Do not use PacketProcessingListener in learning-switch
[openflowplugin.git] / samples / learning-switch / src / main / java / org / opendaylight / openflowplugin / learningswitch / LearningSwitchManagerSimpleImpl.java
1 /*
2  * Copyright (c) 2013 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 package org.opendaylight.openflowplugin.learningswitch;
9
10 import org.opendaylight.mdsal.binding.api.DataBroker;
11 import org.opendaylight.mdsal.binding.api.DataTreeChangeListener;
12 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
13 import org.opendaylight.mdsal.binding.api.NotificationService;
14 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketProcessingService;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived;
21 import org.opendaylight.yangtools.concepts.ListenerRegistration;
22 import org.opendaylight.yangtools.concepts.Registration;
23 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 /**
28  * Listens to packetIn notification.
29  * <ul>
30  * <li>in HUB mode simply floods all switch ports (except ingress port)</li>
31  * <li>in LSWITCH mode collects source MAC address of packetIn and bind it with ingress port.
32  * If target MAC address is already bound then a flow is created (for direct communication between
33  * corresponding MACs)</li>
34  * </ul>
35  */
36 public class LearningSwitchManagerSimpleImpl
37         implements DataTreeChangeListenerRegistrationHolder, LearningSwitchManager {
38
39     private static final Logger LOG = LoggerFactory.getLogger(LearningSwitchManagerSimpleImpl.class);
40     private NotificationService notificationService;
41     private PacketProcessingService packetProcessingService;
42     private DataBroker data;
43     private Registration packetInRegistration;
44     private ListenerRegistration<DataTreeChangeListener> dataTreeChangeListenerRegistration;
45
46     /**
47      * Sets the NotificationService.
48      *
49      * @param notificationService the notificationService to set
50      */
51     @Override
52     public void setNotificationService(NotificationService notificationService) {
53         this.notificationService = notificationService;
54     }
55
56     /**
57      * Sets the PacketProcessingService.
58      *
59      * @param packetProcessingService the packetProcessingService to set
60      */
61     @Override
62     public void setPacketProcessingService(
63             PacketProcessingService packetProcessingService) {
64         this.packetProcessingService = packetProcessingService;
65     }
66
67     /**
68      * Sets the DataBroker.
69      */
70     @Override
71     public void setDataBroker(DataBroker broker) {
72         data = broker;
73     }
74
75     /**
76      * Starts learning switch.
77      */
78     @Override
79     public void start() {
80         LOG.debug("start() -->");
81         FlowCommitWrapper dataStoreAccessor = new FlowCommitWrapperImpl(data);
82
83         LearningSwitchHandlerSimpleImpl learningSwitchHandler = new LearningSwitchHandlerSimpleImpl(dataStoreAccessor,
84                 packetProcessingService, this);
85         packetInRegistration = notificationService.registerListener(PacketReceived.class, learningSwitchHandler);
86
87         WakeupOnNode wakeupListener = new WakeupOnNode();
88         wakeupListener.setLearningSwitchHandler(learningSwitchHandler);
89         final InstanceIdentifier<Table> instanceIdentifier = InstanceIdentifier.create(Nodes.class)
90                 .child(Node.class)
91                 .augmentation(FlowCapableNode.class)
92                 .child(Table.class);
93         final DataTreeIdentifier<Table> dataTreeIdentifier =
94                 DataTreeIdentifier.create(LogicalDatastoreType.OPERATIONAL, instanceIdentifier);
95         dataTreeChangeListenerRegistration = data.registerDataTreeChangeListener(dataTreeIdentifier, wakeupListener);
96         LOG.debug("start() <--");
97     }
98
99     /**
100      * Stops the learning switch.
101      */
102     @Override
103     public void stop() {
104         LOG.debug("stop() -->");
105         //TODO: remove flow (created in #start())
106
107         packetInRegistration.close();
108
109         dataTreeChangeListenerRegistration.close();
110
111         LOG.debug("stop() <--");
112     }
113
114     @Override
115     public ListenerRegistration<DataTreeChangeListener> getDataTreeChangeListenerRegistration() {
116         return dataTreeChangeListenerRegistration;
117     }
118 }