8c33907e0807b814c1e9b0131867968b5f609dbc
[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 java.util.ArrayList;
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.infrautils.utils.concurrent.Executors;
16 import org.opendaylight.mdsal.binding.api.DataBroker;
17 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
18 import org.opendaylight.netvirt.dhcpservice.api.DhcpMConstants;
19 import org.opendaylight.serviceutils.tools.listener.AbstractClusteredAsyncDataTreeChangeListener;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.DhcpConfig;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.dhcp.config.Configs;
22 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 @Singleton
27 public class DhcpConfigListener extends AbstractClusteredAsyncDataTreeChangeListener<DhcpConfig> {
28
29     private static final Logger LOG = LoggerFactory.getLogger(DhcpConfigListener.class);
30
31     private final DhcpManager dhcpManager;
32     private final DataBroker dataBroker;
33
34     @Inject
35     public DhcpConfigListener(final DataBroker db, final DhcpManager dhcpMgr) {
36         super(db, LogicalDatastoreType.CONFIGURATION, InstanceIdentifier.create(DhcpConfig.class),
37                 Executors.newListeningSingleThreadExecutor("DhcpConfigListener", LOG));
38         dhcpManager = dhcpMgr;
39         this.dataBroker = db;
40     }
41
42     public void init() {
43         LOG.info("{} close", getClass().getSimpleName());
44     }
45
46     @Override
47     @PreDestroy
48     public void close() {
49         super.close();
50         Executors.shutdownAndAwaitTermination(getExecutorService());
51         LOG.debug("DhcpConfig Listener Closed");
52     }
53
54     @Override
55     public void remove(InstanceIdentifier<DhcpConfig> identifier, DhcpConfig del) {
56         LOG.trace("DhcpConfig removed: {}", del);
57         updateConfig(null);
58     }
59
60     @Override
61     public void update(InstanceIdentifier<DhcpConfig> identifier, DhcpConfig original, DhcpConfig update) {
62         LOG.trace("DhcpConfig changed to {}", update);
63         updateConfig(update);
64     }
65
66     @Override
67     public void add(InstanceIdentifier<DhcpConfig> identifier, DhcpConfig add) {
68         LOG.trace("DhcpConfig added {}", add);
69         updateConfig(add);
70     }
71
72     private void updateConfig(@Nullable DhcpConfig update) {
73         //TODO: Update operational with actual values
74         if (update == null || update.getConfigs() == null || update.getConfigs().isEmpty()) {
75             dhcpManager.setLeaseDuration(DhcpMConstants.DEFAULT_LEASE_TIME);
76             dhcpManager.setDefaultDomain(DhcpMConstants.DEFAULT_DOMAIN_NAME);
77             return;
78         }
79         Configs config = new ArrayList<Configs>(update.getConfigs().values()).get(0);
80         if (config.getLeaseDuration() != null) {
81             dhcpManager.setLeaseDuration(config.getLeaseDuration());
82         }
83         if (config.getDefaultDomain() != null) {
84             dhcpManager.setDefaultDomain(config.getDefaultDomain());
85             //TODO: What to do if string is ""
86         }
87     }
88 }