2 * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
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
9 package org.opendaylight.netvirt.neutronvpn.shell;
11 import org.apache.karaf.shell.commands.Command;
12 import org.apache.karaf.shell.commands.Option;
13 import org.apache.karaf.shell.console.OsgiCommandSupport;
14 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
15 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
16 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
17 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
18 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
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.DhcpConfigBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.dhcp.config.Configs;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.dhcp.config.ConfigsBuilder;
23 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
27 import com.google.common.base.Optional;
28 import com.google.common.util.concurrent.CheckedFuture;
30 import java.util.Arrays;
31 import java.util.List;
32 import java.util.concurrent.ExecutionException;
34 @Command(scope = "vpnservice", name = "dhcp-configure", description = "configuring parameters for DHCP Service")
35 public class DhcpConfigureCommand extends OsgiCommandSupport {
37 final Logger Logger = LoggerFactory.getLogger(DhcpConfigureCommand.class);
39 @Option(name = "-ld", aliases = {"--leaseDuration"}, description = "Lease Duration", required = false, multiValued = false)
40 Integer leaseDuration;
42 @Option(name = "-dd", aliases = {"--defaultDomain"}, description = "Default Domain", required = false, multiValued = false)
45 private DataBroker dataBroker;
46 private final String defDomain = "openstacklocal";
47 private final Integer defLeaseDuration = 86400;
49 public void setDataBroker(DataBroker broker) {
50 this.dataBroker = broker;
54 protected Object doExecute() throws Exception {
56 if ((defaultDomain == null) && (leaseDuration == null)) {
57 session.getConsole().println(getHelp());
60 Integer currLeaseDuration = defLeaseDuration;
61 String currDefDomain = defDomain;
62 DhcpConfigBuilder dcBuilder = new DhcpConfigBuilder();
63 ConfigsBuilder dccBuilder = new ConfigsBuilder();
64 InstanceIdentifier<DhcpConfig> iid = InstanceIdentifier.create(DhcpConfig.class);
65 DhcpConfig currentConfig = read(iid);
66 if (currentConfig != null && currentConfig.getConfigs() != null &&
67 !currentConfig.getConfigs().isEmpty()) {
68 Configs dhcpConfig = currentConfig.getConfigs().get(0);
69 if (dhcpConfig.getLeaseDuration() != null) {
70 currLeaseDuration = dhcpConfig.getLeaseDuration();
72 if (dhcpConfig.getDefaultDomain() != null) {
73 currDefDomain = dhcpConfig.getDefaultDomain();
77 dccBuilder.setLeaseDuration((leaseDuration == null) ? currLeaseDuration : leaseDuration);
78 dccBuilder.setDefaultDomain((defaultDomain == null) ? currDefDomain : defaultDomain);
80 List<Configs> configList = Arrays.asList(dccBuilder.build());
81 dcBuilder.setConfigs(configList);
82 write(iid, dcBuilder.build());
83 } catch (Exception e) {
84 session.getConsole().println("Failed to configure. Try again");
85 Logger.error ("Failed to configure DHCP parameters",e);
90 private void write(InstanceIdentifier<DhcpConfig> iid, DhcpConfig dhcpConfig) {
91 WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
92 tx.put(LogicalDatastoreType.CONFIGURATION, iid, dhcpConfig);
93 CheckedFuture<Void, TransactionCommitFailedException> futures = tx.submit();
96 } catch (InterruptedException | ExecutionException e) {
97 Logger.error("Error writing to datastore (path, data) : ({}, {})", iid, dhcpConfig);
98 throw new RuntimeException(e.getMessage());
102 private DhcpConfig read(InstanceIdentifier<DhcpConfig> iid) {
104 ReadOnlyTransaction tx = dataBroker.newReadOnlyTransaction();
105 Optional<DhcpConfig> result = Optional.absent();
107 result = tx.read(LogicalDatastoreType.CONFIGURATION, iid).get();
108 } catch (Exception e) {
109 Logger.debug("DhcpConfig not present");
112 if (result.isPresent()) {
118 private String getHelp() {
119 StringBuilder help = new StringBuilder("Usage: ");
121 help.append("exec dhcp-configure ");
122 help.append("[-ld/--leaseDuration leaseTime] [-dd/--defaultDomain defaultDomain]");
123 return help.toString();