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