Refactored decoder models as per new design. Also modified abstract decoder to regist...
[l2switch.git] / packethandler / implementation / src / main / java / org / opendaylight / l2switch / packethandler / PacketHandlerProvider.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.l2switch.packethandler;
9
10 import com.google.common.collect.ImmutableSet;
11 import org.opendaylight.controller.sal.binding.api.AbstractBindingAwareProvider;
12 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
13 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
14 import org.opendaylight.l2switch.packethandler.decoders.AbstractPacketDecoder;
15 import org.opendaylight.l2switch.packethandler.decoders.ArpDecoder;
16 import org.opendaylight.l2switch.packethandler.decoders.EthernetDecoder;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 /**
21  * PacketHandlerProvider serves as the Activator for our L2Switch OSGI bundle.
22  */
23 public class PacketHandlerProvider extends AbstractBindingAwareProvider
24     implements AutoCloseable {
25
26   private final static Logger _logger = LoggerFactory.getLogger(PacketHandlerProvider.class);
27
28   ImmutableSet<AbstractPacketDecoder> decoders;
29
30
31   /**
32    * Setup the packet handler.
33    *
34    * @param providerContext The context of the L2Switch.
35    */
36   @Override
37   public void onSessionInitiated(BindingAwareBroker.ProviderContext providerContext) {
38
39     NotificationProviderService notificationProviderService =
40         providerContext.<NotificationProviderService>getSALService(NotificationProviderService.class);
41
42     initiateDecoders(notificationProviderService);
43
44   }
45
46   /**
47    * Cleanup the packet handler..
48    *
49    * @throws Exception occurs when the NotificationListener is closed
50    */
51   @Override
52   public void close() throws Exception {
53     closeDecoders();
54   }
55
56   private void initiateDecoders(NotificationProviderService notificationProviderService) {
57     decoders = new ImmutableSet.Builder<AbstractPacketDecoder>()
58         .add(new EthernetDecoder(notificationProviderService))
59         .add(new ArpDecoder(notificationProviderService))
60         .build();
61   }
62
63   private void closeDecoders() throws Exception {
64     if(decoders != null && !decoders.isEmpty()) {
65       for(AbstractPacketDecoder decoder : decoders) {
66         decoder.close();
67       }
68     }
69   }
70 }