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;
12 import org.opendaylight.vpnservice.datastoreutils.AsyncClusteredDataChangeListenerBase;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.dhcp.config.Configs;
14 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
15 import org.opendaylight.vpnservice.datastoreutils.AsyncDataChangeListenerBase;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.DhcpConfig;
17 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
18 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
19 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
21 import org.opendaylight.vpnservice.dhcpservice.api.DHCPMConstants;
22 import org.opendaylight.yangtools.concepts.ListenerRegistration;
23 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
27 public class DhcpConfigListener extends AsyncClusteredDataChangeListenerBase<DhcpConfig, DhcpConfigListener> implements AutoCloseable {
29 private static final Logger LOG = LoggerFactory.getLogger(DhcpConfigListener.class);
31 private ListenerRegistration<DataChangeListener> listenerRegistration;
32 private DhcpManager dhcpManager;
34 public DhcpConfigListener(final DataBroker db, final DhcpManager dhcpMgr) {
35 super(DhcpConfig.class, DhcpConfigListener.class);
36 dhcpManager = dhcpMgr;
40 private void registerListener(final DataBroker db) {
42 listenerRegistration = db.registerDataChangeListener(LogicalDatastoreType.CONFIGURATION,
43 getWildCardPath(), DhcpConfigListener.this, AsyncDataBroker.DataChangeScope.SUBTREE);
44 } catch (final Exception e) {
45 LOG.error("NodeListener: DataChange listener registration fail!", e);
46 throw new IllegalStateException("NodeListener: registration Listener failed.", e);
50 protected InstanceIdentifier<DhcpConfig> getWildCardPath() {
51 return InstanceIdentifier.create(DhcpConfig.class);
55 public void close() throws Exception {
56 if (listenerRegistration != null) {
58 listenerRegistration.close();
59 } catch (final Exception e) {
60 LOG.error("Error when cleaning up DhcpConfigListener.", e);
62 listenerRegistration = null;
64 LOG.debug("DhcpConfig Listener Closed");
68 protected void remove(InstanceIdentifier<DhcpConfig> identifier, DhcpConfig del) {
69 LOG.trace("DhcpConfig removed: {}", del);
74 protected void update(InstanceIdentifier<DhcpConfig> identifier, DhcpConfig original, DhcpConfig update) {
75 LOG.trace("DhcpConfig changed to {}", update);
80 protected void add(InstanceIdentifier<DhcpConfig> identifier, DhcpConfig add) {
81 LOG.trace("DhcpConfig added {}", add);
85 private void updateConfig(DhcpConfig update) {
86 //TODO: Update operational with actual values
87 if(update == null || update.getConfigs() == null || update.getConfigs().isEmpty()) {
88 dhcpManager.setLeaseDuration(DHCPMConstants.DEFAULT_LEASE_TIME);
89 dhcpManager.setDefaultDomain(DHCPMConstants.DEFAULT_DOMAIN_NAME);
92 Configs config = update.getConfigs().get(0);
93 if(config.getLeaseDuration() != null) {
94 dhcpManager.setLeaseDuration(config.getLeaseDuration());
96 if(config.getDefaultDomain() != null) {
97 dhcpManager.setDefaultDomain(config.getDefaultDomain());
98 //TODO: What to do if string is ""
103 protected ClusteredDataChangeListener getDataChangeListener() {
104 return DhcpConfigListener.this;
108 protected DataChangeScope getDataChangeScope() {
109 return AsyncDataBroker.DataChangeScope.BASE;