2 * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
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
8 package org.opendaylight.vpnservice.dhcpservice;
10 import org.opendaylight.controller.md.sal.binding.api.ClusteredDataChangeListener;
11 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
12 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
13 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
14 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
15 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
16 import org.opendaylight.vpnservice.datastoreutils.AsyncClusteredDataChangeListenerBase;
17 import org.opendaylight.vpnservice.dhcpservice.api.DHCPMConstants;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.DhcpConfig;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.dhcp.config.Configs;
20 import org.opendaylight.yangtools.concepts.ListenerRegistration;
21 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
25 public class DhcpConfigListener extends AsyncClusteredDataChangeListenerBase<DhcpConfig, DhcpConfigListener> implements AutoCloseable {
27 private static final Logger LOG = LoggerFactory.getLogger(DhcpConfigListener.class);
29 private ListenerRegistration<DataChangeListener> listenerRegistration;
30 private DhcpManager dhcpManager;
32 public DhcpConfigListener(final DataBroker db, final DhcpManager dhcpMgr) {
33 super(DhcpConfig.class, DhcpConfigListener.class);
34 dhcpManager = dhcpMgr;
38 private void registerListener(final DataBroker db) {
40 listenerRegistration = db.registerDataChangeListener(LogicalDatastoreType.CONFIGURATION,
41 getWildCardPath(), DhcpConfigListener.this, AsyncDataBroker.DataChangeScope.SUBTREE);
42 } catch (final Exception e) {
43 LOG.error("NodeListener: DataChange listener registration fail!", e);
44 throw new IllegalStateException("NodeListener: registration Listener failed.", e);
48 protected InstanceIdentifier<DhcpConfig> getWildCardPath() {
49 return InstanceIdentifier.create(DhcpConfig.class);
53 public void close() throws Exception {
54 if (listenerRegistration != null) {
56 listenerRegistration.close();
57 } catch (final Exception e) {
58 LOG.error("Error when cleaning up DhcpConfigListener.", e);
60 listenerRegistration = null;
62 LOG.debug("DhcpConfig Listener Closed");
66 protected void remove(InstanceIdentifier<DhcpConfig> identifier, DhcpConfig del) {
67 LOG.trace("DhcpConfig removed: {}", del);
72 protected void update(InstanceIdentifier<DhcpConfig> identifier, DhcpConfig original, DhcpConfig update) {
73 LOG.trace("DhcpConfig changed to {}", update);
78 protected void add(InstanceIdentifier<DhcpConfig> identifier, DhcpConfig add) {
79 LOG.trace("DhcpConfig added {}", add);
83 private void updateConfig(DhcpConfig update) {
84 //TODO: Update operational with actual values
85 if(update == null || update.getConfigs() == null || update.getConfigs().isEmpty()) {
86 dhcpManager.setLeaseDuration(DHCPMConstants.DEFAULT_LEASE_TIME);
87 dhcpManager.setDefaultDomain(DHCPMConstants.DEFAULT_DOMAIN_NAME);
90 Configs config = update.getConfigs().get(0);
91 if(config.getLeaseDuration() != null) {
92 dhcpManager.setLeaseDuration(config.getLeaseDuration());
94 if(config.getDefaultDomain() != null) {
95 dhcpManager.setDefaultDomain(config.getDefaultDomain());
96 //TODO: What to do if string is ""
101 protected ClusteredDataChangeListener getDataChangeListener() {
102 return DhcpConfigListener.this;
106 protected DataChangeScope getDataChangeScope() {
107 return AsyncDataBroker.DataChangeScope.BASE;