2d187055ac88bcdb7d548941eb7e24dab6065c6e
[vpnservice.git] / dhcpservice / dhcpservice-impl / src / main / java / org / opendaylight / vpnservice / dhcpservice / DhcpConfigListener.java
1 /*
2  * Copyright (c) 2016 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.md.sal.binding.api.ClusteredDataChangeListener;
11
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;
26
27 public class DhcpConfigListener extends AsyncClusteredDataChangeListenerBase<DhcpConfig, DhcpConfigListener> implements AutoCloseable {
28
29     private static final Logger LOG = LoggerFactory.getLogger(DhcpConfigListener.class);
30
31     private ListenerRegistration<DataChangeListener> listenerRegistration;
32     private DhcpManager dhcpManager;
33
34     public DhcpConfigListener(final DataBroker db, final DhcpManager dhcpMgr) {
35         super(DhcpConfig.class, DhcpConfigListener.class);
36         dhcpManager = dhcpMgr;
37         registerListener(db);
38     }
39
40     private void registerListener(final DataBroker db) {
41         try {
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);
47         }
48     }
49
50     protected InstanceIdentifier<DhcpConfig> getWildCardPath() {
51         return InstanceIdentifier.create(DhcpConfig.class);
52     }
53
54     @Override
55     public void close() throws Exception {
56         if (listenerRegistration != null) {
57             try {
58                 listenerRegistration.close();
59             } catch (final Exception e) {
60                 LOG.error("Error when cleaning up DhcpConfigListener.", e);
61             }
62             listenerRegistration = null;
63         }
64         LOG.debug("DhcpConfig Listener Closed");
65     }
66
67     @Override
68     protected void remove(InstanceIdentifier<DhcpConfig> identifier, DhcpConfig del) {
69         LOG.trace("DhcpConfig removed: {}", del);
70         updateConfig(null);
71     }
72
73     @Override
74     protected void update(InstanceIdentifier<DhcpConfig> identifier, DhcpConfig original, DhcpConfig update) {
75         LOG.trace("DhcpConfig changed to {}", update);
76         updateConfig(update);
77     }
78
79     @Override
80     protected void add(InstanceIdentifier<DhcpConfig> identifier, DhcpConfig add) {
81         LOG.trace("DhcpConfig added {}", add);
82         updateConfig(add);
83     }
84
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);
90             return;
91         }
92         Configs config = update.getConfigs().get(0);
93         if(config.getLeaseDuration() != null) {
94             dhcpManager.setLeaseDuration(config.getLeaseDuration());
95         }
96         if(config.getDefaultDomain() != null) {
97             dhcpManager.setDefaultDomain(config.getDefaultDomain());
98             //TODO: What to do if string is ""
99         }
100     }
101
102     @Override
103     protected ClusteredDataChangeListener getDataChangeListener() {
104         return DhcpConfigListener.this;
105     }
106
107     @Override
108     protected DataChangeScope getDataChangeScope() {
109         return AsyncDataBroker.DataChangeScope.BASE;
110     }
111 }