MRI version bumpup for Aluminium
[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 java.util.ArrayList;
14 import org.apache.karaf.shell.commands.Command;
15 import org.apache.karaf.shell.console.OsgiCommandSupport;
16 import org.opendaylight.genius.datastoreutils.SingleTransactionDataBroker;
17 import org.opendaylight.mdsal.binding.api.DataBroker;
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 @Command(scope = "vpnservice", name = "dhcp-show", description = "showing parameters for DHCP Service")
26 public class DhcpShowCommand extends OsgiCommandSupport {
27
28     private static final Logger LOG = LoggerFactory.getLogger(DhcpShowCommand.class);
29
30     private DataBroker dataBroker;
31     private Integer leaseDuration = null;
32     private String defDomain = null;
33
34     public void setDataBroker(DataBroker broker) {
35         this.dataBroker = broker;
36     }
37
38     @Override
39     protected Object doExecute() throws Exception {
40         InstanceIdentifier<DhcpConfig> iid = InstanceIdentifier.create(DhcpConfig.class);
41         DhcpConfig dhcpConfig = SingleTransactionDataBroker.syncRead(dataBroker, CONFIGURATION, iid);
42         if (isDhcpConfigAvailable(dhcpConfig)) {
43             leaseDuration = new ArrayList<Configs>(dhcpConfig.getConfigs().values()).get(0).getLeaseDuration();
44             defDomain = new ArrayList<Configs>(dhcpConfig.getConfigs().values()).get(0).getDefaultDomain();
45         } else {
46             session.getConsole().println("DHCP Config not present");
47             LOG.error("doExecute: DHCP Config not present");
48         }
49         session.getConsole().println(
50                 "Lease Duration: " + (leaseDuration != null ? leaseDuration : DhcpMConstants.DEFAULT_LEASE_TIME));
51         session.getConsole().println(
52                 "Default Domain: " + (defDomain != null ? defDomain : DhcpMConstants.DEFAULT_DOMAIN_NAME));
53         return null;
54     }
55
56     private boolean isDhcpConfigAvailable(DhcpConfig dhcpConfig) {
57         return dhcpConfig != null && dhcpConfig.getConfigs() != null
58                 && !dhcpConfig.getConfigs().isEmpty();
59     }
60
61 }