Convert OF samples to use DTCL instead of DCL
[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 and
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      * @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         PacketInDispatcherImpl packetInDispatcher = new PacketInDispatcherImpl();
83         MultipleLearningSwitchHandlerFacadeImpl learningSwitchHandler = new MultipleLearningSwitchHandlerFacadeImpl();
84         learningSwitchHandler.setRegistrationPublisher(this);
85         learningSwitchHandler.setDataStoreAccessor(dataStoreAccessor);
86         learningSwitchHandler.setPacketProcessingService(packetProcessingService);
87         learningSwitchHandler.setPacketInDispatcher(packetInDispatcher);
88         packetInRegistration = notificationService.registerNotificationListener(packetInDispatcher);
89
90         WakeupOnNode wakeupListener = new WakeupOnNode();
91         wakeupListener.setLearningSwitchHandler(learningSwitchHandler);
92         final InstanceIdentifier<Table> instanceIdentifier = InstanceIdentifier.create(Nodes.class)
93                 .child(Node.class)
94                 .augmentation(FlowCapableNode.class)
95                 .child(Table.class);
96         final DataTreeIdentifier<Table> dataTreeIdentifier = new DataTreeIdentifier(LogicalDatastoreType.OPERATIONAL, instanceIdentifier);
97         dataTreeChangeListenerRegistration = data.registerDataTreeChangeListener(dataTreeIdentifier, wakeupListener);
98         LOG.debug("start() <--");
99     }
100
101     /**
102      * stopping learning switch
103      */
104     @Override
105     public void stop() {
106         LOG.debug("stop() -->");
107         //TODO: remove flow (created in #start())
108         try {
109             packetInRegistration.close();
110         } catch (Exception e) {
111             LOG.warn("Error unregistering packet in listener: {}", e.getMessage());
112             LOG.debug("Error unregistering packet in listener.. ", e);
113         }
114         try {
115             dataTreeChangeListenerRegistration.close();
116         } catch (Exception e) {
117             LOG.warn("Error unregistering data change listener: {}", e.getMessage());
118             LOG.debug("Error unregistering data change listener.. ", e);
119         }
120         LOG.debug("stop() <--");
121     }
122
123
124     @Override
125     public ListenerRegistration<DataTreeChangeListener> getDataTreeChangeListenerRegistration() {
126         return dataTreeChangeListenerRegistration;
127     }
128 }