14d13f2a7645c98d733c6ec4bbeccba1e7762224
[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.PreDestroy;
11 import javax.inject.Inject;
12 import javax.inject.Singleton;
13 import org.eclipse.jdt.annotation.Nullable;
14 import org.opendaylight.infrautils.utils.concurrent.Executors;
15 import org.opendaylight.mdsal.binding.api.DataBroker;
16 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
17 import org.opendaylight.netvirt.dhcpservice.api.DhcpMConstants;
18 import org.opendaylight.serviceutils.tools.listener.AbstractClusteredAsyncDataTreeChangeListener;
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 AbstractClusteredAsyncDataTreeChangeListener<DhcpConfig> {
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(db, LogicalDatastoreType.CONFIGURATION, InstanceIdentifier.create(DhcpConfig.class),
36                 Executors.newListeningSingleThreadExecutor("DhcpConfigListener", LOG));
37         dhcpManager = dhcpMgr;
38         this.dataBroker = db;
39     }
40
41     public void init() {
42         LOG.info("{} close", getClass().getSimpleName());
43     }
44
45     @Override
46     @PreDestroy
47     public void close() {
48         super.close();
49         Executors.shutdownAndAwaitTermination(getExecutorService());
50         LOG.debug("DhcpConfig Listener Closed");
51     }
52
53     @Override
54     public void remove(InstanceIdentifier<DhcpConfig> identifier, DhcpConfig del) {
55         LOG.trace("DhcpConfig removed: {}", del);
56         updateConfig(null);
57     }
58
59     @Override
60     public void update(InstanceIdentifier<DhcpConfig> identifier, DhcpConfig original, DhcpConfig update) {
61         LOG.trace("DhcpConfig changed to {}", update);
62         updateConfig(update);
63     }
64
65     @Override
66     public void add(InstanceIdentifier<DhcpConfig> identifier, DhcpConfig add) {
67         LOG.trace("DhcpConfig added {}", add);
68         updateConfig(add);
69     }
70
71     private void updateConfig(@Nullable DhcpConfig update) {
72         //TODO: Update operational with actual values
73         if (update == null || update.getConfigs() == null || update.getConfigs().isEmpty()) {
74             dhcpManager.setLeaseDuration(DhcpMConstants.DEFAULT_LEASE_TIME);
75             dhcpManager.setDefaultDomain(DhcpMConstants.DEFAULT_DOMAIN_NAME);
76             return;
77         }
78         Configs config = update.getConfigs().get(0);
79         if (config.getLeaseDuration() != null) {
80             dhcpManager.setLeaseDuration(config.getLeaseDuration());
81         }
82         if (config.getDefaultDomain() != null) {
83             dhcpManager.setDefaultDomain(config.getDefaultDomain());
84             //TODO: What to do if string is ""
85         }
86     }
87 }