2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.openflowplugin.learningswitch;
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.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;
27 * Listens to packetIn notification and
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>
35 public class LearningSwitchManagerSimpleImpl implements DataTreeChangeListenerRegistrationHolder, LearningSwitchManager {
37 private static final Logger LOG = LoggerFactory.getLogger(LearningSwitchManagerSimpleImpl.class);
38 private NotificationService notificationService;
39 private PacketProcessingService packetProcessingService;
40 private DataBroker data;
41 private Registration packetInRegistration;
42 private ListenerRegistration<DataTreeChangeListener> dataTreeChangeListenerRegistration;
45 * @param notificationService the notificationService to set
48 public void setNotificationService(NotificationService notificationService) {
49 this.notificationService = notificationService;
53 * @param packetProcessingService the packetProcessingService to set
56 public void setPacketProcessingService(
57 PacketProcessingService packetProcessingService) {
58 this.packetProcessingService = packetProcessingService;
62 * @param data the data to set
65 public void setDataBroker(DataBroker data) {
70 * starting learning switch
74 LOG.debug("start() -->");
75 FlowCommitWrapper dataStoreAccessor = new FlowCommitWrapperImpl(data);
77 LearningSwitchHandlerSimpleImpl learningSwitchHandler = new LearningSwitchHandlerSimpleImpl();
78 learningSwitchHandler.setRegistrationPublisher(this);
79 learningSwitchHandler.setDataStoreAccessor(dataStoreAccessor);
80 learningSwitchHandler.setPacketProcessingService(packetProcessingService);
81 packetInRegistration = notificationService.registerNotificationListener(learningSwitchHandler);
83 WakeupOnNode wakeupListener = new WakeupOnNode();
84 wakeupListener.setLearningSwitchHandler(learningSwitchHandler);
85 final InstanceIdentifier<Table> instanceIdentifier = InstanceIdentifier.create(Nodes.class)
87 .augmentation(FlowCapableNode.class)
89 final DataTreeIdentifier<Table> dataTreeIdentifier = new DataTreeIdentifier(LogicalDatastoreType.OPERATIONAL, instanceIdentifier);
90 dataTreeChangeListenerRegistration = data.registerDataTreeChangeListener(dataTreeIdentifier, wakeupListener);
91 LOG.debug("start() <--");
95 * stopping learning switch
99 LOG.debug("stop() -->");
100 //TODO: remove flow (created in #start())
102 packetInRegistration.close();
103 } catch (Exception e) {
104 LOG.warn("closing packetInRegistration failed: {}", e.getMessage());
105 LOG.debug("closing packetInRegistration failed..", e);
108 dataTreeChangeListenerRegistration.close();
109 } catch (Exception e) {
110 LOG.warn("failed to close dataTreeChangeListenerRegistration: {}", e.getMessage());
111 LOG.debug("failed to close dataTreeChangeListenerRegistration..", e);
113 LOG.debug("stop() <--");
117 public ListenerRegistration<DataTreeChangeListener> getDataTreeChangeListenerRegistration() {
118 return dataTreeChangeListenerRegistration;