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