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