7af21a75651d2bb8309a0e7474d794fe3d6cf303
[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.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.openflowplugin.learningswitch.DataChangeListenerRegistrationHolder;
15 import org.opendaylight.openflowplugin.learningswitch.FlowCommitWrapper;
16 import org.opendaylight.openflowplugin.learningswitch.FlowCommitWrapperImpl;
17 import org.opendaylight.openflowplugin.learningswitch.LearningSwitchManager;
18 import org.opendaylight.openflowplugin.learningswitch.WakeupOnNode;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketProcessingService;
24 import org.opendaylight.yangtools.concepts.ListenerRegistration;
25 import org.opendaylight.yangtools.concepts.Registration;
26 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
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 DataBroker 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(DataBroker 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(LogicalDatastoreType.CONFIGURATION,
97                 InstanceIdentifier.builder(Nodes.class)
98                         .child(Node.class)
99                         .augmentation(FlowCapableNode.class)
100                         .child(Table.class).toInstance(),
101                 wakeupListener,
102                 DataBroker.DataChangeScope.SUBTREE);
103         LOG.debug("start() <--");
104     }
105
106     /**
107      * stopping learning switch
108      */
109     @Override
110     public void stop() {
111         LOG.debug("stop() -->");
112         //TODO: remove flow (created in #start())
113         try {
114             packetInRegistration.close();
115         } catch (Exception e) {
116             LOG.error(e.getMessage(), e);
117         }
118         try {
119             dataChangeListenerRegistration.close();
120         } catch (Exception e) {
121             LOG.error(e.getMessage(), e);
122         }
123         LOG.debug("stop() <--");
124     }
125
126
127     @Override
128     public ListenerRegistration<DataChangeListener> getDataChangeListenerRegistration() {
129         return dataChangeListenerRegistration;
130     }
131 }