17c993e642a0112470a56edca8dfd3a25ac1a679
[netvirt.git] / fibmanager / shell / src / main / java / org / opendaylight / netvirt / fibmanager / shell / ConfTransportL3VPNCommand.java
1 /*
2  * Copyright (c) 2015 - 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 package org.opendaylight.netvirt.fibmanager.shell;
9
10 import java.util.Locale;
11 import javax.annotation.Nullable;
12 import org.apache.karaf.shell.commands.Command;
13 import org.apache.karaf.shell.commands.Option;
14 import org.apache.karaf.shell.console.OsgiCommandSupport;
15 import org.opendaylight.netvirt.fibmanager.api.IFibManager;
16 import org.opendaylight.netvirt.fibmanager.api.L3VPNTransportTypes;
17
18 @Command(scope = "vpnservice", name = "configureTransportType",
19     description = "Configure Preferred Transport Type for L3VPN service")
20 public class ConfTransportL3VPNCommand extends OsgiCommandSupport {
21     private final IFibManager fibManager;
22
23     @Option(name = "-s", aliases = {"--service"}, description = "Service", required = false, multiValued = false)
24     String service;
25
26     @Option(name = "-t", aliases = {"--type"}, description = "Configure Transport Type",
27         required = false, multiValued = false)
28     String transportType;
29
30     public ConfTransportL3VPNCommand(IFibManager fibManager) {
31         this.fibManager = fibManager;
32     }
33
34     @Override
35     @Nullable
36     protected Object doExecute() {
37
38         if (service == null || service.isEmpty() || !"L3VPN".equalsIgnoreCase(service)) {
39             session.getConsole().println("Please provide valid input for service ");
40             session.getConsole().println(
41                 "exec configure-transport-type (-s | --service)  <L3VPN> (-t | --type) <VxLAN/GRE>");
42             return null;
43         }
44         if (transportType == null || transportType.isEmpty()
45             || L3VPNTransportTypes.validateTransportType(transportType.toUpperCase(Locale.getDefault()))
46                     == L3VPNTransportTypes.Invalid) {
47             session.getConsole().println("Please provide valid input for Transport type");
48             return null;
49         }
50
51         String cachedTransType = fibManager.getConfTransType();
52         if (cachedTransType.equalsIgnoreCase(transportType)) {
53             session.getConsole().println("Transport type already configured as " + cachedTransType);
54             return null;
55         }
56
57         if (cachedTransType.equals(L3VPNTransportTypes.Invalid.getTransportType())
58             || !fibManager.isVPNConfigured()) {
59             fibManager.setConfTransType(service, transportType.toUpperCase(Locale.getDefault()));
60             fibManager.writeConfTransTypeConfigDS();
61         } else {
62             session.getConsole().println("VPN service already configured with " + cachedTransType
63                 + " as the transport type. Please remove vpn service and configure"
64                 + " again. Changes were discarded.");
65         }
66         return null;
67     }
68 }