Convert OF samples to use DTCL instead of DCL
[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.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;
25
26 /**
27  * Listens to packetIn notification and
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 implements DataTreeChangeListenerRegistrationHolder, LearningSwitchManager {
36
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;
43
44     /**
45      * @param notificationService the notificationService to set
46      */
47     @Override
48     public void setNotificationService(NotificationService notificationService) {
49         this.notificationService = notificationService;
50     }
51
52     /**
53      * @param packetProcessingService the packetProcessingService to set
54      */
55     @Override
56     public void setPacketProcessingService(
57             PacketProcessingService packetProcessingService) {
58         this.packetProcessingService = packetProcessingService;
59     }
60
61     /**
62      * @param data the data to set
63      */
64     @Override
65     public void setDataBroker(DataBroker data) {
66         this.data = data;
67     }
68
69     /**
70      * starting learning switch
71      */
72     @Override
73     public void start() {
74         LOG.debug("start() -->");
75         FlowCommitWrapper dataStoreAccessor = new FlowCommitWrapperImpl(data);
76
77         LearningSwitchHandlerSimpleImpl learningSwitchHandler = new LearningSwitchHandlerSimpleImpl();
78         learningSwitchHandler.setRegistrationPublisher(this);
79         learningSwitchHandler.setDataStoreAccessor(dataStoreAccessor);
80         learningSwitchHandler.setPacketProcessingService(packetProcessingService);
81         packetInRegistration = notificationService.registerNotificationListener(learningSwitchHandler);
82
83         WakeupOnNode wakeupListener = new WakeupOnNode();
84         wakeupListener.setLearningSwitchHandler(learningSwitchHandler);
85         final InstanceIdentifier<Table> instanceIdentifier = InstanceIdentifier.create(Nodes.class)
86                 .child(Node.class)
87                 .augmentation(FlowCapableNode.class)
88                 .child(Table.class);
89         final DataTreeIdentifier<Table> dataTreeIdentifier = new DataTreeIdentifier(LogicalDatastoreType.OPERATIONAL, instanceIdentifier);
90         dataTreeChangeListenerRegistration = data.registerDataTreeChangeListener(dataTreeIdentifier, wakeupListener);
91         LOG.debug("start() <--");
92     }
93
94     /**
95      * stopping learning switch
96      */
97     @Override
98     public void stop() {
99         LOG.debug("stop() -->");
100         //TODO: remove flow (created in #start())
101         try {
102             packetInRegistration.close();
103         } catch (Exception e) {
104             LOG.warn("closing packetInRegistration failed: {}", e.getMessage());
105             LOG.debug("closing packetInRegistration failed..", e);
106         }
107         try {
108             dataTreeChangeListenerRegistration.close();
109         } catch (Exception e) {
110             LOG.warn("failed to close dataTreeChangeListenerRegistration: {}", e.getMessage());
111             LOG.debug("failed to close dataTreeChangeListenerRegistration..", e);
112         }
113         LOG.debug("stop() <--");
114     }
115
116     @Override
117     public ListenerRegistration<DataTreeChangeListener> getDataTreeChangeListenerRegistration() {
118         return dataTreeChangeListenerRegistration;
119     }
120 }