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