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 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;
27 import java.io.PrintStream;
28 import java.util.List;
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() {
69 ("usage: bgp-cache ["+LST+" vrfs | networks] ["+OFL+" file-name]");
76 public Object show() throws Exception {
81 protected Object doExecute() throws Exception {
82 if (!Commands.bgpRunning()) {
85 Bgp config = Commands.getBgpManager().getConfig();
86 boolean list_vrfs = false;
87 boolean list_nets = false;
88 PrintStream ps = System.out;
95 ps = new PrintStream(ofile);
96 } catch (Exception e) {
97 System.err.println("error: cannot create file "+ofile +"; exception: "+e);
102 for (String item : list) {
111 System.err.println("error: unknown value for "+LST+": "+item);
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) {
127 AsId a = config.getAsId();
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");
137 GracefulRestart g = config.getGracefulRestart();
139 s = g.getStalepathTime().intValue();
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);
146 Logging l = config.getLogging();
148 ps.printf("\t%-15s %s\n\t%-15s %s\n", LFSTR, l.getFile(),
149 LLSTR, l.getLevel());
152 List<Neighbors> n = config.getNeighbors();
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();
160 ps.printf("\t\t%-16s %d\n", EBSTR, en.getNhops().intValue());
162 UpdateSource us = nbr.getUpdateSource();
164 ps.printf("\t\t%-16s %s\n", USSTR, us.getSourceIp().getValue());
166 ps.printf("\t\t%-16s IPv4-Labeled-VPN", AFSTR);
167 List<AddressFamilies> afs = nbr.getAddressFamilies();
169 for (AddressFamilies af : afs) {
170 ps.printf(" %s", af.getSafi().intValue() == 4 ?
171 "IPv4-Labeled-Unicast" : "Unknown");
179 List<Vrfs> v = config.getVrfs();
181 ps.printf("\nVRFs\n");
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);
196 List<Networks> ln = config.getNetworks();
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);