BUG-432 Registration change
[openflowplugin.git] / samples / learning-switch / src / main / java / org / opendaylight / openflowplugin / learningswitch / multi / LearningSwitchManagerMultiImpl.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.multi;
9
10 import org.opendaylight.controller.sal.binding.api.NotificationService;
11 import org.opendaylight.controller.sal.binding.api.data.DataBrokerService;
12 import org.opendaylight.controller.sal.binding.api.data.DataChangeListener;
13 import org.opendaylight.openflowplugin.learningswitch.DataChangeListenerRegistrationHolder;
14 import org.opendaylight.openflowplugin.learningswitch.LearningSwitchManager;
15 import org.opendaylight.openflowplugin.learningswitch.FlowCommitWrapper;
16 import org.opendaylight.openflowplugin.learningswitch.FlowCommitWrapperImpl;
17 import org.opendaylight.openflowplugin.learningswitch.WakeupOnNode;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketProcessingService;
23 import org.opendaylight.yangtools.concepts.ListenerRegistration;
24 import org.opendaylight.yangtools.concepts.Registration;
25 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
26 import org.opendaylight.yangtools.yang.binding.NotificationListener;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * Listens to packetIn notification and 
32  * <ul>
33  * <li>in HUB mode simply floods all switch ports (except ingress port)</li>
34  * <li>in LSWITCH mode collects source MAC address of packetIn and bind it with ingress port. 
35  * If target MAC address is already bound then a flow is created (for direct communication between 
36  * corresponding MACs)</li>
37  * </ul>
38  */
39 public class LearningSwitchManagerMultiImpl implements DataChangeListenerRegistrationHolder,
40         LearningSwitchManager {
41     
42     protected static final Logger LOG = LoggerFactory
43             .getLogger(LearningSwitchManagerMultiImpl.class);
44
45     private NotificationService notificationService;
46     private PacketProcessingService packetProcessingService;
47     private DataBrokerService data;
48
49     private Registration packetInRegistration;
50
51     private ListenerRegistration<DataChangeListener> dataChangeListenerRegistration; 
52     
53     /**
54      * @param notificationService the notificationService to set
55      */
56     @Override
57     public void setNotificationService(NotificationService notificationService) {
58         this.notificationService = notificationService;
59     }
60
61     /**
62      * @param packetProcessingService the packetProcessingService to set
63      */
64     @Override
65     public void setPacketProcessingService(
66             PacketProcessingService packetProcessingService) {
67         this.packetProcessingService = packetProcessingService;
68     }
69     
70     /**
71      * @param data the data to set
72      */
73     @Override
74     public void setDataBroker(DataBrokerService data) {
75         this.data = data;
76     }
77
78     /**
79      * starting learning switch
80      */
81     @Override
82     public void start() {
83         LOG.debug("start() -->");
84         FlowCommitWrapper dataStoreAccessor = new FlowCommitWrapperImpl(data);
85
86         PacketInDispatcherImpl packetInDispatcher = new PacketInDispatcherImpl();
87         MultipleLearningSwitchHandlerFacadeImpl learningSwitchHandler = new MultipleLearningSwitchHandlerFacadeImpl();
88         learningSwitchHandler.setRegistrationPublisher(this);
89         learningSwitchHandler.setDataStoreAccessor(dataStoreAccessor);
90         learningSwitchHandler.setPacketProcessingService(packetProcessingService);
91         learningSwitchHandler.setPacketInDispatcher(packetInDispatcher);
92         packetInRegistration = notificationService.registerNotificationListener(packetInDispatcher);
93         
94         WakeupOnNode wakeupListener = new WakeupOnNode();
95         wakeupListener.setLearningSwitchHandler(learningSwitchHandler);
96         dataChangeListenerRegistration = data.registerDataChangeListener(
97                 InstanceIdentifier.builder(Nodes.class)
98                     .child(Node.class)
99                     .augmentation(FlowCapableNode.class)
100                     .child(Table.class).toInstance(),
101                 wakeupListener);
102         LOG.debug("start() <--");
103     }
104     
105     /**
106      * stopping learning switch 
107      */
108     @Override
109     public void stop() {
110         LOG.debug("stop() -->");
111         //TODO: remove flow (created in #start())
112         try {
113             packetInRegistration.close();
114         } catch (Exception e) {
115             LOG.error(e.getMessage(), e);
116         }
117         try {
118             dataChangeListenerRegistration.close();
119         } catch (Exception e) {
120             LOG.error(e.getMessage(), e);
121         }
122         LOG.debug("stop() <--");
123     }
124     
125    
126     @Override
127     public ListenerRegistration<DataChangeListener> getDataChangeListenerRegistration() {
128         return dataChangeListenerRegistration;
129     }
130 }