Removing Blind imports across the module
[netvirt.git] / vpnservice / bgpmanager / bgpmanager-impl / src / main / java / org / opendaylight / netvirt / bgpmanager / commands / Cache.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.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.Bgp;
16 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.AsId;
17 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.GracefulRestart;
18 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.Logging;
19 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.Neighbors;
20 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.Networks;
21 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.Vrfs;
22 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.neighbors.AddressFamilies;
23 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.neighbors.EbgpMultihop;
24 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.neighbors.UpdateSource;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
26
27 import java.io.PrintStream;
28 import java.util.List;
29
30 @Command(scope = "odl", name = "bgp-cache",
31          description = "Text dump of BGP config cache")
32 public class Cache extends OsgiCommandSupport {
33     private static final String LST = "--list";
34     private static final String OFL = "--out-file";
35
36     @Argument(name="dummy", description="Argument not needed",
37               required=false, multiValued = false)
38     private String action = null;
39
40     @Option(name=LST, aliases={"-l"},
41             description="list vrfs and/or networks", 
42             required=false, multiValued=true)
43     private List<String> list = null;
44
45     @Option(name=OFL, aliases={"-o"}, 
46             description="output file", 
47             required=false, multiValued=false)
48     private String ofile = null;
49
50     private static final String HTSTR = "Host";
51     private static final String PTSTR = "Port";
52     private static final String ASSTR = "AS-Number";
53     private static final String RISTR = "Router-ID";
54     private static final String SPSTR = "Stale-Path-Time";
55     private static final String FBSTR = "F-bit";
56     private static final String LFSTR = "Log-File";
57     private static final String LLSTR = "Log-Level";
58     private static final String USSTR = "Update-Source";
59     private static final String EBSTR = "EBGP-Multihops";
60     private static final String AFSTR = "Address-Families";
61     private static final String ERSTR = "Export-RTs";
62     private static final String IRSTR = "Import-RTs";
63     private static final String NHSTR = "Nexthop";
64     private static final String LBSTR = "Label";
65     private static final String RDSTR = "RD";
66
67     private Object usage() {
68         System.err.println
69             ("usage: bgp-cache ["+LST+" vrfs | networks] ["+OFL+" file-name]");
70         return null;
71     }
72
73     public Cache() {
74     }
75
76     public Object show() throws Exception {
77         return doExecute();
78     } 
79
80     @Override
81     protected Object doExecute() throws Exception {
82         if (!Commands.bgpRunning()) {
83             return null;
84         }
85         Bgp config = Commands.getBgpManager().getConfig();
86         boolean list_vrfs = false;
87         boolean list_nets = false;
88         PrintStream ps = System.out;
89
90         if (action != null) {
91             return usage();
92         }
93         if (ofile != null) {
94             try {
95                 ps = new PrintStream(ofile);
96             } catch (Exception e) {
97                 System.err.println("error: cannot create file "+ofile +"; exception: "+e);
98                 return null;
99             }
100         }
101         if (list != null) {
102             for (String item : list) {
103                 switch (item) {
104                     case "vrfs" : 
105                         list_vrfs = true;
106                         break;
107                     case "networks" : 
108                         list_nets = true;
109                         break;
110                     default: 
111                         System.err.println("error: unknown value for "+LST+": "+item);
112                     return null;
113                 }
114             }
115         }
116         // we'd normally read this directly from 'config' but
117         // legacy behaviour forces to check for a connection
118         // that's initiated by default at startup without
119         // writing to config. 
120         String cHost = Commands.getBgpManager().getConfigHost();
121         int cPort = Commands.getBgpManager().getConfigPort();
122         ps.printf("\nConfiguration Server\n\t%s  %s\n\t%s  %d\n",
123                   HTSTR, cHost, PTSTR, cPort); 
124         if (config == null) {
125             return null;
126         }
127         AsId a = config.getAsId();
128         if (a != null) {
129             int asNum = a.getLocalAs().intValue();
130             Ipv4Address routerId = a.getRouterId();
131             Long spt = a.getStalepathTime();
132             Boolean afb = a.isAnnounceFbit();
133             String rid = (routerId == null) ? "<n/a>" : routerId.getValue();
134             int s = (spt == null) ? 0 : spt.intValue();
135             String bit = (afb == null) ? "OFF" : (afb.booleanValue() ? "ON" : "OFF");
136
137             GracefulRestart g = config.getGracefulRestart();
138             if (g != null) {
139                 s = g.getStalepathTime().intValue();
140             }
141             ps.printf("\nBGP Router\n");
142             ps.printf("\t%-15s  %d\n\t%-15s  %s\n\t%-15s  %d\n\t%-15s  %s\n",
143                       ASSTR, asNum, RISTR, rid, SPSTR, s, FBSTR, bit);
144         }
145
146         Logging l = config.getLogging();
147         if (l != null) {
148             ps.printf("\t%-15s  %s\n\t%-15s  %s\n", LFSTR, l.getFile(), 
149             LLSTR, l.getLevel());
150         }
151                                                                                 
152         List<Neighbors> n = config.getNeighbors();
153         if (n != null)  {
154             ps.printf("\nNeighbors\n");
155             for (Neighbors nbr : n) {
156                 ps.printf("\t%s\n\t\t%-16s  %d\n", nbr.getAddress().getValue(),
157                           ASSTR, nbr.getRemoteAs().intValue());
158                 EbgpMultihop en = nbr.getEbgpMultihop();
159                 if (en != null) {
160                     ps.printf("\t\t%-16s  %d\n", EBSTR, en.getNhops().intValue());
161                 }
162                 UpdateSource us = nbr.getUpdateSource();
163                 if (us != null) {
164                     ps.printf("\t\t%-16s  %s\n", USSTR, us.getSourceIp().getValue());
165                 }
166                 ps.printf("\t\t%-16s  IPv4-Labeled-VPN", AFSTR);
167                 List<AddressFamilies> afs = nbr.getAddressFamilies();
168                 if (afs != null) {
169                     for (AddressFamilies af : afs) {
170                         ps.printf(" %s", af.getSafi().intValue() == 4 ? 
171                                             "IPv4-Labeled-Unicast" : "Unknown");
172                     }
173                 }
174                 ps.printf("\n");
175             }
176         }
177
178         if (list_vrfs) {
179             List<Vrfs> v = config.getVrfs();
180             if (v != null) {
181                 ps.printf("\nVRFs\n"); 
182                 for (Vrfs vrf : v)  {
183                     ps.printf("\t%s\n",vrf.getRd());
184                     ps.printf("\t\t%s  ", IRSTR);
185                     for (String rt : vrf.getImportRts()) 
186                     ps.printf("%s ", rt);
187                     ps.printf("\n\t\t%s  ", ERSTR);
188                     for (String rt : vrf.getExportRts()) 
189                     ps.printf("%s ", rt);
190                     ps.printf("\n");
191                 }
192             }
193         }
194      
195         if (list_nets) {
196             List<Networks> ln = config.getNetworks();
197             if (ln != null) {
198                 ps.printf("\nNetworks\n");
199                 for (Networks net : ln) {
200                     String rd = net.getRd();
201                     String pfxlen = net.getPrefixLen();
202                     String nh = net.getNexthop().getValue();
203                     int label = net.getLabel().intValue();
204                     ps.printf("\t%s\n\t\t%-7s  %s\n\t\t%-7s  %s\n\t\t%-7s  %d\n",
205                               pfxlen, RDSTR, rd, NHSTR, nh, LBSTR, label);
206                 }
207             }
208         }
209         if (ofile != null) {
210             ps.close();
211         }
212         return null;
213     }
214 }