NETVIRT-1630 migrate to md-sal APIs
[netvirt.git] / neutronvpn / shell / src / main / java / org / opendaylight / netvirt / neutronvpn / shell / DhcpShowCommand.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
9 package org.opendaylight.netvirt.neutronvpn.shell;
10
11 import static org.opendaylight.mdsal.common.api.LogicalDatastoreType.CONFIGURATION;
12
13 import org.apache.karaf.shell.commands.Command;
14 import org.apache.karaf.shell.console.OsgiCommandSupport;
15 import org.opendaylight.genius.datastoreutils.SingleTransactionDataBroker;
16 import org.opendaylight.mdsal.binding.api.DataBroker;
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.yangtools.yang.binding.InstanceIdentifier;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 @Command(scope = "vpnservice", name = "dhcp-show", description = "showing parameters for DHCP Service")
24 public class DhcpShowCommand extends OsgiCommandSupport {
25
26     private static final Logger LOG = LoggerFactory.getLogger(DhcpShowCommand.class);
27
28     private DataBroker dataBroker;
29     private Integer leaseDuration = null;
30     private String defDomain = null;
31
32     public void setDataBroker(DataBroker broker) {
33         this.dataBroker = broker;
34     }
35
36     @Override
37     protected Object doExecute() throws Exception {
38         InstanceIdentifier<DhcpConfig> iid = InstanceIdentifier.create(DhcpConfig.class);
39         DhcpConfig dhcpConfig = SingleTransactionDataBroker.syncRead(dataBroker, CONFIGURATION, iid);
40         if (isDhcpConfigAvailable(dhcpConfig)) {
41             leaseDuration = dhcpConfig.getConfigs().get(0).getLeaseDuration();
42             defDomain = dhcpConfig.getConfigs().get(0).getDefaultDomain();
43         } else {
44             session.getConsole().println("DHCP Config not present");
45             LOG.error("doExecute: DHCP Config not present");
46         }
47         session.getConsole().println(
48                 "Lease Duration: " + (leaseDuration != null ? leaseDuration : DhcpMConstants.DEFAULT_LEASE_TIME));
49         session.getConsole().println(
50                 "Default Domain: " + (defDomain != null ? defDomain : DhcpMConstants.DEFAULT_DOMAIN_NAME));
51         return null;
52     }
53
54     private boolean isDhcpConfigAvailable(DhcpConfig dhcpConfig) {
55         return dhcpConfig != null && dhcpConfig.getConfigs() != null
56                 && !dhcpConfig.getConfigs().isEmpty();
57     }
58
59 }