BUG-432: migrate users of Registration as appropriate
[controller.git] / opendaylight / md-sal / samples / l2switch / implementation / src / main / java / org / opendaylight / controller / sample / l2switch / md / L2SwitchProvider.java
1 /*
2  * Copyright (c) 2014 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.controller.sample.l2switch.md;
9
10 import org.opendaylight.controller.sample.l2switch.md.addresstracker.AddressTracker;
11 import org.opendaylight.controller.sample.l2switch.md.flow.FlowWriterService;
12 import org.opendaylight.controller.sample.l2switch.md.flow.FlowWriterServiceImpl;
13 import org.opendaylight.controller.sample.l2switch.md.inventory.InventoryService;
14 import org.opendaylight.controller.sample.l2switch.md.packet.PacketHandler;
15 import org.opendaylight.controller.sample.l2switch.md.topology.NetworkGraphDijkstra;
16 import org.opendaylight.controller.sample.l2switch.md.topology.NetworkGraphService;
17 import org.opendaylight.controller.sample.l2switch.md.topology.TopologyLinkDataChangeHandler;
18 import org.opendaylight.controller.sal.binding.api.AbstractBindingAwareConsumer;
19 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
20 import org.opendaylight.controller.sal.binding.api.NotificationService;
21 import org.opendaylight.controller.sal.binding.api.data.DataBrokerService;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketProcessingService;
23 import org.opendaylight.yangtools.concepts.ListenerRegistration;
24 import org.opendaylight.yangtools.yang.binding.NotificationListener;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 /**
29  * L2SwitchProvider serves as the Activator for our L2Switch OSGI bundle.
30  */
31 public class L2SwitchProvider extends AbstractBindingAwareConsumer
32                               implements AutoCloseable {
33
34   private final static Logger _logger = LoggerFactory.getLogger(L2SwitchProvider.class);
35
36   private ListenerRegistration<NotificationListener> listenerRegistration;
37   private AddressTracker addressTracker;
38   private TopologyLinkDataChangeHandler topologyLinkDataChangeHandler;
39
40
41   /**
42    * Setup the L2Switch.
43    * @param consumerContext  The context of the L2Switch.
44    */
45   @Override
46   public void onSessionInitialized(BindingAwareBroker.ConsumerContext consumerContext) {
47     DataBrokerService dataService = consumerContext.<DataBrokerService>getSALService(DataBrokerService.class);
48     addressTracker = new AddressTracker(dataService);
49
50     NetworkGraphService networkGraphService = new NetworkGraphDijkstra();
51     FlowWriterService flowWriterService = new FlowWriterServiceImpl(dataService, networkGraphService);
52
53     NotificationService notificationService =
54             consumerContext.<NotificationService>getSALService(NotificationService.class);
55     PacketProcessingService packetProcessingService =
56             consumerContext.<PacketProcessingService>getRpcService(PacketProcessingService.class);
57     PacketHandler packetHandler = new PacketHandler();
58     packetHandler.setAddressTracker(addressTracker);
59     packetHandler.setFlowWriterService(flowWriterService);
60     packetHandler.setPacketProcessingService(packetProcessingService);
61     packetHandler.setInventoryService(new InventoryService(dataService));
62
63     this.listenerRegistration = notificationService.registerNotificationListener(packetHandler);
64     this.topologyLinkDataChangeHandler = new TopologyLinkDataChangeHandler(dataService, networkGraphService);
65     topologyLinkDataChangeHandler.registerAsDataChangeListener();
66   }
67
68   /**
69    * Cleanup the L2Switch.
70    * @throws Exception  occurs when the NotificationListener is closed
71    */
72   @Override
73   public void close() throws Exception {
74     if (listenerRegistration != null)
75       listenerRegistration.close();
76   }
77 }