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