Merge "Initial commit for DHCPService"
[vpnservice.git] / dhcpservice / dhcpservice-impl / src / main / java / org / opendaylight / vpnservice / dhcpservice / DhcpProvider.java
1 /*
2  * Copyright (c) 2015 Ericsson India Global Services Pvt Ltd. 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.vpnservice.dhcpservice;
9
10 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
11
12 import org.opendaylight.yangtools.concepts.Registration;
13 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
14 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
15 import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
16 import org.opendaylight.vpnservice.mdsalutil.interfaces.IMdsalApiManager;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 public class DhcpProvider implements BindingAwareProvider, AutoCloseable {
21
22     private static final Logger LOG = LoggerFactory.getLogger(DhcpProvider.class);
23     private IMdsalApiManager mdsalManager;
24     private DhcpPktHandler dhcpPktHandler;
25     private Registration packetListener = null;
26     private NotificationProviderService notificationService;
27
28     @Override
29     public void onSessionInitiated(ProviderContext session) {
30         LOG.info("DhcpProvider Session Initiated");
31         try {
32             final  DataBroker dataBroker = session.getSALService(DataBroker.class);
33             dhcpPktHandler = new DhcpPktHandler(dataBroker);
34             packetListener = notificationService.registerNotificationListener(dhcpPktHandler);
35             } catch (Exception e) {
36             LOG.error("Error initializing services", e);
37         }
38     }
39
40
41     public void setMdsalManager(IMdsalApiManager mdsalManager) {
42         this.mdsalManager = mdsalManager;
43     }
44
45     @Override
46     public void close() throws Exception {
47         if(packetListener != null) {
48             packetListener.close();
49         }
50         if(dhcpPktHandler != null) {
51             dhcpPktHandler.close();
52         }
53         LOG.info("DhcpProvider closed");
54     }
55
56     public void setNotificationProviderService(NotificationProviderService notificationServiceDependency) {
57         this.notificationService = notificationServiceDependency;
58     }
59
60 }