Merge "Upgrade ietf-{inet,yang}-types to 2013-07-15"
[vpnservice.git] / bgpmanager / bgpmanager-impl / src / main / java / org / opendaylight / bgpmanager / commands / Misc.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-misc", 
16          description = "Add or delete miscellaneous BGP config options")
17 public class Misc extends OsgiCommandSupport {
18     private static final String LF = "--log-file";
19     private static final String LL = "--log-level";
20     private static final String SP = "--stalepath-time";
21
22     @Argument(name="add|del", description="The desired operation", 
23               required=true, multiValued = false)
24     private String action = null;
25
26     @Option(name=LF, aliases={"-f"}, 
27             description="Log file name", 
28             required=false, multiValued=false)
29     private String file = null;
30
31     @Option(name=LL, aliases={"-l"},
32             description="Log level", required=false, 
33             multiValued=false)
34     private String level = null;
35
36     @Option(name=SP, aliases={"-s"},
37             description="Stale-path time", required=false, 
38             multiValued=false)
39     private String spt = null;
40
41     private Object usage() {
42         System.err.println(
43         "usage: bgp-misc [<"+LF+" name> <"+LL+" level>] ["
44         +SP+" stale-path-time] <add | del>");
45         return null;
46     } 
47
48     private boolean isValidLevel(String level) {
49         switch (level) {
50             case "emergencies":
51             case "alerts":
52             case "critical":
53             case "errors":
54             case "warnings":
55             case "notifications":
56             case "informational":
57             case "debugging": 
58                 return true;
59             default: 
60                 break;
61         }
62         return false;
63     }
64
65     @Override
66     protected Object doExecute() throws Exception {
67         if (!Commands.bgpRunning()) {
68             return null;
69         }
70         if (spt == null && file == null && level == null) {
71             return usage();
72         }
73         if (file != null ^ level != null) {
74             return usage();
75         }
76         if (level != null && !isValidLevel(level)) { 
77             System.err.println("error: invalid value for "+LL);
78             return null;
79         }
80         BgpManager bm = Commands.getBgpManager();
81         switch (action) {
82             case "add" : 
83                 if (spt != null && Commands.isValid(spt, Commands.INT, SP)) {
84                     int s = Integer.valueOf(spt);
85                     bm.configureGR(s);
86                 }
87                 if (file != null && level != null) 
88                 bm.setQbgpLog(file, level);
89                 break;
90             case "del" :  
91                 if (spt != null) {
92                     bm.delGracefulRestart();
93                 }
94                 if (file != null && level != null) {
95                     bm.delLogging();
96                 }
97                 break;
98             default : 
99                 return usage();
100         }
101         return null;
102     }
103 }