move vpnservice and cleanup poms
[netvirt.git] / bgpmanager / bgpmanager-impl / src / main / java / org / opendaylight / netvirt / bgpmanager / commands / Connect.java
1 /*
2  * Copyright (c) 2015 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.bgpmanager.commands;
10
11 import org.apache.karaf.shell.commands.Argument;
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.bgpmanager.BgpManager;
16
17 @Command(scope = "odl", name = "bgp-connect",
18         description = "Add or delete client connection to BGP Config Server")
19 public class Connect extends OsgiCommandSupport {
20     private static final String HOST = "--host";
21     private static final String PORT = "--port";
22
23     @Argument(name = "add|del", description = "The desired operation",
24             required = true, multiValued = false)
25     String action = null;
26
27     @Option(name = HOST, aliases = {"-h"},
28             description = "IP address of the server",
29             required = false, multiValued = false)
30     String host = null;
31
32     @Option(name = PORT, aliases = {"-p"},
33             description = "Thrift port", required = false,
34             multiValued = false)
35     String port = null;
36
37     private final BgpManager bgpManager;
38
39     public Connect(BgpManager bgpManager) {
40         this.bgpManager = bgpManager;
41     }
42
43     private Object usage() {
44         session.getConsole().println(
45                 "usage: bgp-connect [" + HOST + " h] [" + PORT + " p] <add | del>");
46         return null;
47     }
48
49     @Override
50     protected Object doExecute() throws Exception {
51         switch (action) {
52             case "add":
53                 if (host == null || port == null) {
54                     session.getConsole().println("error: " + HOST + " and " + PORT + " needed");
55                     return null;
56                 }
57                 if (!Commands.isValid(session.getConsole(), host, Commands.Validators.IPADDR, HOST)
58                         || !Commands.isValid(session.getConsole(), port, Commands.Validators.INT, PORT)) {
59                     return null;
60                 }
61                 // check: already connected?
62                 bgpManager.startConfig(host, Integer.parseInt(port));
63                 break;
64             case "del":
65                 if (host != null || port != null) {
66                     session.getConsole().println("note: option(s) not needed; ignored");
67                 }
68                 // check: nothing to stop?
69                 bgpManager.stopConfig();
70                 break;
71             default:
72                 return usage();
73         }
74         return null;
75     }
76 }