Merge "lower log severity to warn where appropriate"
[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.DataChangeListener;
12 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
13 import org.opendaylight.controller.sal.binding.api.NotificationService;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketProcessingService;
19 import org.opendaylight.yangtools.concepts.ListenerRegistration;
20 import org.opendaylight.yangtools.concepts.Registration;
21 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 /**
26  * Listens to packetIn notification and
27  * <ul>
28  * <li>in HUB mode simply floods all switch ports (except ingress port)</li>
29  * <li>in LSWITCH mode collects source MAC address of packetIn and bind it with ingress port.
30  * If target MAC address is already bound then a flow is created (for direct communication between
31  * corresponding MACs)</li>
32  * </ul>
33  */
34 public class LearningSwitchManagerSimpleImpl implements DataChangeListenerRegistrationHolder,
35         LearningSwitchManager {
36
37     protected static final Logger LOG = LoggerFactory
38             .getLogger(LearningSwitchManagerSimpleImpl.class);
39
40     private NotificationService notificationService;
41     private PacketProcessingService packetProcessingService;
42     private DataBroker data;
43
44     private Registration packetInRegistration;
45
46     private ListenerRegistration<DataChangeListener> dataChangeListenerRegistration;
47
48     /**
49      * @param notificationService the notificationService to set
50      */
51     @Override
52     public void setNotificationService(NotificationService notificationService) {
53         this.notificationService = notificationService;
54     }
55
56     /**
57      * @param packetProcessingService the packetProcessingService to set
58      */
59     @Override
60     public void setPacketProcessingService(
61             PacketProcessingService packetProcessingService) {
62         this.packetProcessingService = packetProcessingService;
63     }
64
65     /**
66      * @param data the data to set
67      */
68     @Override
69     public void setDataBroker(DataBroker data) {
70         this.data = data;
71     }
72
73     /**
74      * starting learning switch
75      */
76     @Override
77     public void start() {
78         LOG.debug("start() -->");
79         FlowCommitWrapper dataStoreAccessor = new FlowCommitWrapperImpl(data);
80
81         LearningSwitchHandlerSimpleImpl learningSwitchHandler = new LearningSwitchHandlerSimpleImpl();
82         learningSwitchHandler.setRegistrationPublisher(this);
83         learningSwitchHandler.setDataStoreAccessor(dataStoreAccessor);
84         learningSwitchHandler.setPacketProcessingService(packetProcessingService);
85         packetInRegistration = notificationService.registerNotificationListener(learningSwitchHandler);
86
87         WakeupOnNode wakeupListener = new WakeupOnNode();
88         wakeupListener.setLearningSwitchHandler(learningSwitchHandler);
89         dataChangeListenerRegistration = data.registerDataChangeListener(LogicalDatastoreType.OPERATIONAL,
90                 InstanceIdentifier.builder(Nodes.class)
91                     .child(Node.class)
92                     .augmentation(FlowCapableNode.class)
93                     .child(Table.class).build(),
94                 wakeupListener,
95                 DataBroker.DataChangeScope.SUBTREE);
96         LOG.debug("start() <--");
97     }
98
99     /**
100      * stopping learning switch
101      */
102     @Override
103     public void stop() {
104         LOG.debug("stop() -->");
105         //TODO: remove flow (created in #start())
106         try {
107             packetInRegistration.close();
108         } catch (Exception e) {
109             LOG.warn("closing packetInRegistration failed: {}", e.getMessage());
110             LOG.debug("closing packetInRegistration failed..", e);
111         }
112         try {
113             dataChangeListenerRegistration.close();
114         } catch (Exception e) {
115             LOG.warn("failed to close dataChangeListenerRegistration: {}", e.getMessage());
116             LOG.debug("failed to close dataChangeListenerRegistration..", e);
117         }
118         LOG.debug("stop() <--");
119     }
120
121
122     @Override
123     public ListenerRegistration<DataChangeListener> getDataChangeListenerRegistration() {
124         return dataChangeListenerRegistration;
125     }
126 }