NETVIRT-1630 migrate to md-sal APIs
[netvirt.git] / neutronvpn / shell / src / main / java / org / opendaylight / netvirt / neutronvpn / shell / DhcpConfigureCommand.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.Collections;
14 import java.util.List;
15 import org.apache.karaf.shell.commands.Command;
16 import org.apache.karaf.shell.commands.Option;
17 import org.apache.karaf.shell.console.OsgiCommandSupport;
18 import org.opendaylight.genius.datastoreutils.SingleTransactionDataBroker;
19 import org.opendaylight.mdsal.binding.api.DataBroker;
20 import org.opendaylight.netvirt.dhcpservice.api.DhcpMConstants;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.DhcpConfig;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.DhcpConfigBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.dhcp.config.Configs;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.dhcp.config.ConfigsBuilder;
25 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 @Command(scope = "vpnservice", name = "dhcp-configure", description = "configuring parameters for DHCP Service")
30 public class DhcpConfigureCommand extends OsgiCommandSupport {
31
32     private static final Logger LOG = LoggerFactory.getLogger(DhcpConfigureCommand.class);
33
34     @Option(name = "-ld", aliases = {"--leaseDuration"}, description = "Lease Duration", required = false,
35         multiValued = false)
36     Integer leaseDuration;
37
38     @Option(name = "-dd", aliases = {"--defaultDomain"}, description = "Default Domain", required = false,
39         multiValued = false)
40     String defaultDomain;
41
42     private DataBroker dataBroker;
43
44     public void setDataBroker(DataBroker broker) {
45         this.dataBroker = broker;
46     }
47
48     @Override
49     protected Object doExecute() throws Exception {
50         if (defaultDomain == null && leaseDuration == null) {
51             session.getConsole().println(getHelp());
52             return null;
53         }
54         Integer currLeaseDuration = DhcpMConstants.DEFAULT_LEASE_TIME;
55         String currDefDomain = DhcpMConstants.DEFAULT_DOMAIN_NAME;
56         ConfigsBuilder dccBuilder = new ConfigsBuilder();
57         InstanceIdentifier<DhcpConfig> iid = InstanceIdentifier.create(DhcpConfig.class);
58         DhcpConfig currentConfig = SingleTransactionDataBroker.syncRead(dataBroker, CONFIGURATION, iid);
59         if (currentConfig != null && currentConfig.getConfigs() != null
60             && !currentConfig.getConfigs().isEmpty()) {
61             Configs dhcpConfig = currentConfig.getConfigs().get(0);
62             if (dhcpConfig.getLeaseDuration() != null) {
63                 currLeaseDuration = dhcpConfig.getLeaseDuration();
64             }
65             if (dhcpConfig.getDefaultDomain() != null) {
66                 currDefDomain = dhcpConfig.getDefaultDomain();
67             }
68         } else {
69             LOG.error("doExecute: DHCP config not present");
70         }
71
72         dccBuilder.setLeaseDuration(leaseDuration == null ? currLeaseDuration : leaseDuration);
73         dccBuilder.setDefaultDomain(defaultDomain == null ? currDefDomain : defaultDomain);
74
75         List<Configs> configList = Collections.singletonList(dccBuilder.build());
76         DhcpConfigBuilder dcBuilder = new DhcpConfigBuilder();
77         dcBuilder.setConfigs(configList);
78         SingleTransactionDataBroker.syncWrite(dataBroker, CONFIGURATION, iid, dcBuilder.build());
79         return null;
80     }
81
82     private String getHelp() {
83         return "Usage: \n"
84                 + "exec dhcp-configure \n"
85                 + "[-ld/--leaseDuration leaseTime] [-dd/--defaultDomain defaultDomain]";
86     }
87
88 }