710201b49f87843f06cf075ffe09bbadba0cf47b
[netvirt.git] /
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 java.io.FileNotFoundException;
12 import java.io.PrintStream;
13 import java.util.List;
14 import org.apache.karaf.shell.commands.Argument;
15 import org.apache.karaf.shell.commands.Command;
16 import org.apache.karaf.shell.commands.Option;
17 import org.apache.karaf.shell.console.OsgiCommandSupport;
18 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.Bgp;
19 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.AsId;
20 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.GracefulRestart;
21 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.Logging;
22 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.Neighbors;
23 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.Networks;
24 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.Vrfs;
25 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.neighbors.AddressFamilies;
26 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.neighbors.EbgpMultihop;
27 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.neighbors.UpdateSource;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
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         session.getConsole().println("usage: bgp-cache [" + LST + " vrfs | networks] [" + OFL + " file-name]");
69         return null;
70     }
71
72     public Cache() {
73     }
74
75     public Object show() throws Exception {
76         return doExecute();
77     }
78
79     @Override
80     protected Object doExecute() throws Exception {
81         if (!Commands.bgpRunning(session.getConsole())) {
82             return null;
83         }
84         boolean listVrfs = false;
85         boolean listNets = false;
86         PrintStream ps = session.getConsole();
87
88         if (action != null) {
89             return usage();
90         }
91         if (ofile != null) {
92             try {
93                 ps = new PrintStream(ofile);
94             } catch (FileNotFoundException e) {
95                 session.getConsole().println("error: cannot create file " + ofile + "; exception: " + e);
96                 return null;
97             }
98         }
99         if (list != null) {
100             for (String item : list) {
101                 switch (item) {
102                     case "vrfs":
103                         listVrfs = true;
104                         break;
105                     case "networks":
106                         listNets = true;
107                         break;
108                     default:
109                         session.getConsole().println("error: unknown value for " + LST + ": " + item);
110                         return null;
111                 }
112             }
113         }
114         // we'd normally read this directly from 'config' but
115         // legacy behaviour forces to check for a connection
116         // that's initiated by default at startup without
117         // writing to config.
118         String configHost = Commands.getBgpManager().getConfigHost();
119         int configPort = Commands.getBgpManager().getConfigPort();
120         ps.printf("\nConfiguration Server\n\t%s  %s\n\t%s  %d\n",
121                 HTSTR, configHost, PTSTR, configPort);
122         Bgp config = Commands.getBgpManager().getConfig();
123         if (config == null) {
124             return null;
125         }
126         AsId asId = config.getAsId();
127         if (asId != null) {
128             int asNum = asId.getLocalAs().intValue();
129             IpAddress routerId = asId.getRouterId();
130             Long spt = asId.getStalepathTime();
131             Boolean afb = asId.isAnnounceFbit();
132             String rid = (routerId == null) ? "<n/a>" : new String(routerId.getValue());
133             //F-bit is always set to ON (hardcoded), in SDN even though the controller is down
134             //forwarding state shall be retained.
135             String bit = "ON";
136
137             GracefulRestart gracefulRestart = config.getGracefulRestart();
138             if (gracefulRestart != null) {
139                 spt = gracefulRestart.getStalepathTime();
140             }
141             ps.printf("\nBGP Router\n");
142             ps.printf("\t%-15s  %d\n\t%-15s  %s\n\t%-15s  %s\n\t%-15s  %s\n",
143                     ASSTR, asNum, RISTR, rid, SPSTR, (spt == null || spt == 0) ? "default" : spt.toString(), FBSTR,
144                     bit);
145         }
146
147         Logging logging = config.getLogging();
148         if (logging != null) {
149             ps.printf("\t%-15s  %s\n\t%-15s  %s\n", LFSTR, logging.getFile(),
150                     LLSTR, logging.getLevel());
151         }
152
153         List<Neighbors> neighbors = config.getNeighbors();
154         if (neighbors != null) {
155             ps.printf("\nNeighbors\n");
156             for (Neighbors nbr : neighbors) {
157                 ps.printf("\t%s\n\t\t%-16s  %d\n", nbr.getAddress().getValue(),
158                         ASSTR, nbr.getRemoteAs().intValue());
159                 EbgpMultihop en = nbr.getEbgpMultihop();
160                 if (en != null) {
161                     ps.printf("\t\t%-16s  %d\n", EBSTR, en.getNhops().intValue());
162                 }
163                 UpdateSource us = nbr.getUpdateSource();
164                 if (us != null) {
165                     ps.printf("\t\t%-16s  %s\n", USSTR, us.getSourceIp().getValue());
166                 }
167                 ps.printf("\t\t%-16s  IPv4-Labeled-VPN", AFSTR);
168                 List<AddressFamilies> afs = nbr.getAddressFamilies();
169                 if (afs != null) {
170                     for (AddressFamilies af : afs) {
171                         ps.printf(" %s", af.getSafi().intValue() == 4 ? "IPv4-Labeled-Unicast" : "Unknown");
172                     }
173                 }
174                 ps.printf("\n");
175             }
176         }
177
178         if (listVrfs) {
179             List<Vrfs> vrfs = config.getVrfs();
180             if (vrfs != null) {
181                 ps.printf("\nVRFs\n");
182                 for (Vrfs vrf : vrfs) {
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                     }
188                     ps.printf("\n\t\t%s  ", ERSTR);
189                     for (String rt : vrf.getExportRts()) {
190                         ps.printf("%s ", rt);
191                     }
192                     ps.printf("\n");
193                 }
194             }
195         }
196
197         if (listNets) {
198             List<Networks> ln = config.getNetworks();
199             if (ln != null) {
200                 ps.printf("\nNetworks\n");
201                 for (Networks net : ln) {
202                     String rd = net.getRd();
203                     String pfxlen = net.getPrefixLen();
204                     String nh = net.getNexthop().getValue();
205                     int label = net.getLabel().intValue();
206                     ps.printf("\t%s\n\t\t%-7s  %s\n\t\t%-7s  %s\n\t\t%-7s  %d\n",
207                             pfxlen, RDSTR, rd, NHSTR, nh, LBSTR, label);
208                 }
209             }
210         }
211         if (ofile != null) {
212             ps.close();
213         }
214         return null;
215     }
216 }