Merge "BUG-6495 BC Grp wrong for E/W VLAN provider net"
[netvirt.git] / vpnservice / neutronvpn / 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 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;
26
27 import com.google.common.base.Optional;
28 import com.google.common.util.concurrent.CheckedFuture;
29
30 import java.util.Arrays;
31 import java.util.List;
32 import java.util.concurrent.ExecutionException;
33
34 @Command(scope = "vpnservice", name = "dhcp-configure", description = "configuring parameters for DHCP Service")
35 public class DhcpConfigureCommand extends OsgiCommandSupport {
36
37     final Logger Logger = LoggerFactory.getLogger(DhcpConfigureCommand.class);
38
39     @Option(name = "-ld", aliases = {"--leaseDuration"}, description = "Lease Duration", required = false, multiValued = false)
40     Integer leaseDuration;
41
42     @Option(name = "-dd", aliases = {"--defaultDomain"}, description = "Default Domain", required = false, multiValued = false)
43     String defaultDomain;
44
45     private DataBroker dataBroker;
46     private final String defDomain = "openstacklocal";
47     private final Integer defLeaseDuration = 86400;
48
49     public void setDataBroker(DataBroker broker) {
50         this.dataBroker = broker;
51     }
52
53     @Override
54     protected Object doExecute() throws Exception {
55         try {
56             if ((defaultDomain == null) && (leaseDuration == null)) {
57                 session.getConsole().println(getHelp());
58                 return null;
59             }
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();
71                 }
72                 if (dhcpConfig.getDefaultDomain() != null) {
73                     currDefDomain = dhcpConfig.getDefaultDomain();
74                 }
75             }
76
77             dccBuilder.setLeaseDuration((leaseDuration == null) ? currLeaseDuration : leaseDuration);
78             dccBuilder.setDefaultDomain((defaultDomain == null) ? currDefDomain : defaultDomain);
79
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);
86         }
87         return null;
88     }
89
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();
94         try {
95             futures.get();
96         } catch (InterruptedException | ExecutionException e) {
97             Logger.error("Error writing to datastore (path, data) : ({}, {})", iid, dhcpConfig);
98             throw new RuntimeException(e.getMessage());
99         }
100     }
101
102     private DhcpConfig read(InstanceIdentifier<DhcpConfig> iid) {
103
104         ReadOnlyTransaction tx = dataBroker.newReadOnlyTransaction();
105         Optional<DhcpConfig> result = Optional.absent();
106         try {
107             result = tx.read(LogicalDatastoreType.CONFIGURATION, iid).get();
108         } catch (Exception e) {
109             Logger.debug("DhcpConfig not present");
110             return null;
111         }
112         if (result.isPresent()) {
113             return result.get();
114         }
115         return null;
116     }
117
118     private String getHelp() {
119         StringBuilder help = new StringBuilder("Usage: ");
120
121         help.append("exec dhcp-configure ");
122         help.append("[-ld/--leaseDuration leaseTime] [-dd/--defaultDomain defaultDomain]");
123         return help.toString();
124     }
125
126 }