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