3267c0de6867f2cde05dd6d26cd78ea01b310d42
[netvirt.git] / dhcpservice / impl / src / main / java / org / opendaylight / netvirt / 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.netvirt.dhcpservice;
9
10 import javax.annotation.PostConstruct;
11 import javax.annotation.PreDestroy;
12 import javax.inject.Inject;
13 import javax.inject.Singleton;
14 import org.eclipse.jdt.annotation.Nullable;
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.genius.datastoreutils.AsyncClusteredDataTreeChangeListenerBase;
18 import org.opendaylight.netvirt.dhcpservice.api.DhcpMConstants;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.DhcpConfig;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.dhcp.config.Configs;
21 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 @Singleton
26 public class DhcpConfigListener extends AsyncClusteredDataTreeChangeListenerBase<DhcpConfig, DhcpConfigListener> {
27
28     private static final Logger LOG = LoggerFactory.getLogger(DhcpConfigListener.class);
29
30     private final DhcpManager dhcpManager;
31     private final DataBroker dataBroker;
32
33     @Inject
34     public DhcpConfigListener(final DataBroker db, final DhcpManager dhcpMgr) {
35         super(DhcpConfig.class, DhcpConfigListener.class);
36         dhcpManager = dhcpMgr;
37         this.dataBroker = db;
38     }
39
40     @PostConstruct
41     public void init() {
42         registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
43     }
44
45     @Override
46     protected InstanceIdentifier<DhcpConfig> getWildCardPath() {
47         return InstanceIdentifier.create(DhcpConfig.class);
48     }
49
50     @Override
51     @PreDestroy
52     public void close() {
53         super.close();
54         LOG.debug("DhcpConfig Listener Closed");
55     }
56
57     @Override
58     protected void remove(InstanceIdentifier<DhcpConfig> identifier, DhcpConfig del) {
59         LOG.trace("DhcpConfig removed: {}", del);
60         updateConfig(null);
61     }
62
63     @Override
64     protected void update(InstanceIdentifier<DhcpConfig> identifier, DhcpConfig original, DhcpConfig update) {
65         LOG.trace("DhcpConfig changed to {}", update);
66         updateConfig(update);
67     }
68
69     @Override
70     protected void add(InstanceIdentifier<DhcpConfig> identifier, DhcpConfig add) {
71         LOG.trace("DhcpConfig added {}", add);
72         updateConfig(add);
73     }
74
75     private void updateConfig(@Nullable DhcpConfig update) {
76         //TODO: Update operational with actual values
77         if (update == null || update.getConfigs() == null || update.getConfigs().isEmpty()) {
78             dhcpManager.setLeaseDuration(DhcpMConstants.DEFAULT_LEASE_TIME);
79             dhcpManager.setDefaultDomain(DhcpMConstants.DEFAULT_DOMAIN_NAME);
80             return;
81         }
82         Configs config = update.getConfigs().get(0);
83         if (config.getLeaseDuration() != null) {
84             dhcpManager.setLeaseDuration(config.getLeaseDuration());
85         }
86         if (config.getDefaultDomain() != null) {
87             dhcpManager.setDefaultDomain(config.getDefaultDomain());
88             //TODO: What to do if string is ""
89         }
90     }
91
92     @Override
93     protected DhcpConfigListener getDataTreeChangeListener() {
94         return DhcpConfigListener.this;
95     }
96 }