Merge "ITM changes - namespace change in config xml. - Tunnel interface name is trunc...
[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.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.dhcp.config.Configs;
11
12 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
13 import org.opendaylight.vpnservice.datastoreutils.AsyncDataChangeListenerBase;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.DhcpConfig;
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
17 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.vpnservice.dhcpservice.api.DHCPMConstants;
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;
24
25 public class DhcpConfigListener extends AsyncDataChangeListenerBase<DhcpConfig, DhcpConfigListener> implements AutoCloseable {
26
27     private static final Logger LOG = LoggerFactory.getLogger(DhcpConfigListener.class);
28
29     private ListenerRegistration<DataChangeListener> listenerRegistration;
30     private DhcpManager dhcpManager;
31
32     public DhcpConfigListener(final DataBroker db, final DhcpManager dhcpMgr) {
33         super(DhcpConfig.class, DhcpConfigListener.class);
34         dhcpManager = dhcpMgr;
35         registerListener(db);
36     }
37
38     private void registerListener(final DataBroker db) {
39         try {
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);
45         }
46     }
47
48     protected InstanceIdentifier<DhcpConfig> getWildCardPath() {
49         return InstanceIdentifier.create(DhcpConfig.class);
50     }
51
52     @Override
53     public void close() throws Exception {
54         if (listenerRegistration != null) {
55             try {
56                 listenerRegistration.close();
57             } catch (final Exception e) {
58                 LOG.error("Error when cleaning up DhcpConfigListener.", e);
59             }
60             listenerRegistration = null;
61         }
62         LOG.debug("DhcpConfig Listener Closed");
63     }
64
65     @Override
66     protected void remove(InstanceIdentifier<DhcpConfig> identifier, DhcpConfig del) {
67         LOG.trace("DhcpConfig removed: {}", del);
68         updateConfig(null);
69     }
70
71     @Override
72     protected void update(InstanceIdentifier<DhcpConfig> identifier, DhcpConfig original, DhcpConfig update) {
73         LOG.trace("DhcpConfig changed to {}", update);
74         updateConfig(update);
75     }
76
77     @Override
78     protected void add(InstanceIdentifier<DhcpConfig> identifier, DhcpConfig add) {
79         LOG.trace("DhcpConfig added {}", add);
80         updateConfig(add);
81     }
82
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);
88             return;
89         }
90         Configs config = update.getConfigs().get(0);
91         if(config.getLeaseDuration() != null) {
92             dhcpManager.setLeaseDuration(config.getLeaseDuration());
93         }
94         if(config.getDefaultDomain() != null) {
95             dhcpManager.setDefaultDomain(config.getDefaultDomain());
96             //TODO: What to do if string is ""
97         }
98     }
99
100     @Override
101     protected DataChangeListener getDataChangeListener() {
102         return DhcpConfigListener.this;
103     }
104
105     @Override
106     protected DataChangeScope getDataChangeScope() {
107         return AsyncDataBroker.DataChangeScope.BASE;
108     }
109 }