learning switch shifted to new data broker API
[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  * <p/>
23  * Activator is derived from AbstractBindingAwareConsumer, which takes care
24  * of looking up MD-SAL in Service Registry and registering consumer
25  * when MD-SAL is present.
26  */
27 public class Activator extends AbstractBindingAwareConsumer implements AutoCloseable {
28
29     private static final Logger LOG = LoggerFactory.getLogger(Activator.class);
30
31     private LearningSwitchManager learningSwitch;
32
33
34     @Override
35     protected void startImpl(BundleContext context) {
36         LOG.info("startImpl() passing");
37     }
38
39     /**
40      * Invoked when consumer is registered to the MD-SAL.
41      */
42     @Override
43     public void onSessionInitialized(ConsumerContext session) {
44         LOG.info("inSessionInitialized() passing");
45         /**
46          * We create instance of our LearningSwitchManager
47          * and set all required dependencies,
48          *
49          * which are 
50          *   Data Broker (data storage service) - for configuring flows and reading stored switch state
51          *   PacketProcessingService - for sending out packets
52          *   NotificationService - for receiving notifications such as packet in.
53          *
54          */
55         learningSwitch = new LearningSwitchManagerMultiImpl();
56         learningSwitch.setDataBroker(session.getSALService(DataBroker.class));
57         learningSwitch.setPacketProcessingService(session.getRpcService(PacketProcessingService.class));
58         learningSwitch.setNotificationService(session.getSALService(NotificationService.class));
59         learningSwitch.start();
60     }
61
62     @Override
63     public void close() {
64         LOG.info("close() passing");
65         if (learningSwitch != null) {
66             learningSwitch.stop();
67         }
68     }
69
70     @Override
71     protected void stopImpl(BundleContext context) {
72         close();
73         super.stopImpl(context);
74     }
75 }