2 * Copyright (c) 2015 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
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
9 package org.opendaylight.netvirt.bgpmanager.commands;
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;
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";
36 @Argument(name = "dummy", description = "Argument not needed",
37 required = false, multiValued = false)
38 private String action = null;
40 @Option(name = LST, aliases = {"-l"},
41 description = "list vrfs and/or networks",
42 required = false, multiValued = true)
43 private List<String> list = null;
45 @Option(name = OFL, aliases = {"-o"},
46 description = "output file",
47 required = false, multiValued = false)
48 private String ofile = null;
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";
67 private Object usage() {
68 session.getConsole().println("usage: bgp-cache [" + LST + " vrfs | networks] [" + OFL + " file-name]");
75 public Object show() throws Exception {
80 protected Object doExecute() throws Exception {
81 if (!Commands.bgpRunning(session.getConsole())) {
84 boolean listVrfs = false;
85 boolean listNets = false;
86 PrintStream ps = session.getConsole();
93 ps = new PrintStream(ofile);
94 } catch (FileNotFoundException e) {
95 session.getConsole().println("error: cannot create file " + ofile + "; exception: " + e);
100 for (String item : list) {
109 session.getConsole().println("error: unknown value for " + LST + ": " + item);
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) {
126 AsId asId = config.getAsId();
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.
137 GracefulRestart gracefulRestart = config.getGracefulRestart();
138 if (gracefulRestart != null) {
139 spt = gracefulRestart.getStalepathTime();
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,
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());
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();
161 ps.printf("\t\t%-16s %d\n", EBSTR, en.getNhops().intValue());
163 UpdateSource us = nbr.getUpdateSource();
165 ps.printf("\t\t%-16s %s\n", USSTR, us.getSourceIp().getValue());
167 ps.printf("\t\t%-16s IPv4-Labeled-VPN", AFSTR);
168 List<AddressFamilies> afs = nbr.getAddressFamilies();
170 for (AddressFamilies af : afs) {
171 ps.printf(" %s", af.getSafi().intValue() == 4 ? "IPv4-Labeled-Unicast" : "Unknown");
179 List<Vrfs> vrfs = config.getVrfs();
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);
188 ps.printf("\n\t\t%s ", ERSTR);
189 for (String rt : vrf.getExportRts()) {
190 ps.printf("%s ", rt);
198 List<Networks> ln = config.getNetworks();
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);