Switch to MD-SAL APIs
[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.mdsal.binding.api.DataBroker;
11 import org.opendaylight.mdsal.binding.api.DataTreeChangeListener;
12 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
13 import org.opendaylight.mdsal.binding.api.NotificationService;
14 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketProcessingService;
20 import org.opendaylight.yangtools.concepts.ListenerRegistration;
21 import org.opendaylight.yangtools.concepts.Registration;
22 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 /**
27  * Listens to packetIn notification.
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
36         implements DataTreeChangeListenerRegistrationHolder, LearningSwitchManager {
37
38     private static final Logger LOG = LoggerFactory.getLogger(LearningSwitchManagerSimpleImpl.class);
39     private NotificationService notificationService;
40     private PacketProcessingService packetProcessingService;
41     private DataBroker data;
42     private Registration packetInRegistration;
43     private ListenerRegistration<DataTreeChangeListener> dataTreeChangeListenerRegistration;
44
45     /**
46      * Sets the NotificationService.
47      *
48      * @param notificationService the notificationService to set
49      */
50     @Override
51     public void setNotificationService(NotificationService notificationService) {
52         this.notificationService = notificationService;
53     }
54
55     /**
56      * Sets the PacketProcessingService.
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      * Sets the DataBroker.
68      */
69     @Override
70     public void setDataBroker(DataBroker broker) {
71         this.data = broker;
72     }
73
74     /**
75      * Starts 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(dataStoreAccessor,
83                 packetProcessingService, this);
84         packetInRegistration = notificationService.registerNotificationListener(learningSwitchHandler);
85
86         WakeupOnNode wakeupListener = new WakeupOnNode();
87         wakeupListener.setLearningSwitchHandler(learningSwitchHandler);
88         final InstanceIdentifier<Table> instanceIdentifier = InstanceIdentifier.create(Nodes.class)
89                 .child(Node.class)
90                 .augmentation(FlowCapableNode.class)
91                 .child(Table.class);
92         final DataTreeIdentifier<Table> dataTreeIdentifier =
93                 DataTreeIdentifier.create(LogicalDatastoreType.OPERATIONAL, instanceIdentifier);
94         dataTreeChangeListenerRegistration = data.registerDataTreeChangeListener(dataTreeIdentifier, wakeupListener);
95         LOG.debug("start() <--");
96     }
97
98     /**
99      * Stops the learning switch.
100      */
101     @Override
102     public void stop() {
103         LOG.debug("stop() -->");
104         //TODO: remove flow (created in #start())
105
106         packetInRegistration.close();
107
108         dataTreeChangeListenerRegistration.close();
109
110         LOG.debug("stop() <--");
111     }
112
113     @Override
114     public ListenerRegistration<DataTreeChangeListener> getDataTreeChangeListenerRegistration() {
115         return dataTreeChangeListenerRegistration;
116     }
117 }