Convert OF samples to use DTCL instead of DCL
[openflowplugin.git] / samples / learning-switch / src / main / java / org / opendaylight / openflowplugin / learningswitch / Activator.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.sal.binding.api.AbstractBindingAwareConsumer;
12 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ConsumerContext;
13 import org.opendaylight.controller.sal.binding.api.NotificationService;
14 import org.opendaylight.openflowplugin.learningswitch.multi.LearningSwitchManagerMultiImpl;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketProcessingService;
16 import org.osgi.framework.BundleContext;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 /**
21  * Learning switch activator
22  * Activator is derived from AbstractBindingAwareConsumer, which takes care
23  * of looking up MD-SAL in Service Registry and registering consumer
24  * when MD-SAL is present.
25  */
26 public class Activator extends AbstractBindingAwareConsumer implements AutoCloseable {
27
28     private static final Logger LOG = LoggerFactory.getLogger(Activator.class);
29     private LearningSwitchManager learningSwitch;
30
31     @Override
32     protected void startImpl(BundleContext context) {
33         LOG.info("startImpl() passing");
34     }
35
36     /**
37      * Invoked when consumer is registered to the MD-SAL.
38      */
39     @Override
40     public void onSessionInitialized(ConsumerContext session) {
41         LOG.info("inSessionInitialized() passing");
42         /**
43          * We create instance of our LearningSwitchManager
44          * and set all required dependencies,
45          *
46          * which are
47          *   Data Broker (data storage service) - for configuring flows and reading stored switch state
48          *   PacketProcessingService - for sending out packets
49          *   NotificationService - for receiving notifications such as packet in.
50          */
51         learningSwitch = new LearningSwitchManagerMultiImpl();
52         learningSwitch.setDataBroker(session.getSALService(DataBroker.class));
53         learningSwitch.setPacketProcessingService(session.getRpcService(PacketProcessingService.class));
54         learningSwitch.setNotificationService(session.getSALService(NotificationService.class));
55         learningSwitch.start();
56     }
57
58     @Override
59     public void close() {
60         LOG.info("close() passing");
61         if (learningSwitch != null) {
62             learningSwitch.stop();
63         }
64     }
65
66     @Override
67     protected void stopImpl(BundleContext context) {
68         close();
69         super.stopImpl(context);
70     }
71 }