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 edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
12 import java.io.FileNotFoundException;
13 import java.io.PrintStream;
14 import java.util.List;
15 import org.apache.felix.service.command.CommandSession;
16 import org.apache.karaf.shell.commands.Argument;
17 import org.apache.karaf.shell.commands.Command;
18 import org.apache.karaf.shell.commands.Option;
19 import org.apache.karaf.shell.console.OsgiCommandSupport;
20 import org.opendaylight.netvirt.bgpmanager.BgpManager;
21 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.Bgp;
22 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.AsId;
23 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.GracefulRestart;
24 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.Logging;
25 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.multipathcontainer.Multipath;
26 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.neighborscontainer.Neighbors;
27 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.neighborscontainer.neighbors.AddressFamilies;
28 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.neighborscontainer.neighbors.EbgpMultihop;
29 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.neighborscontainer.neighbors.UpdateSource;
30 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.networkscontainer.Networks;
31 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.vrfmaxpathcontainer.VrfMaxpath;
32 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.vrfscontainer.Vrfs;
33 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.vrfscontainer.vrfs.AddressFamiliesVrf;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
36 @Command(scope = "odl", name = "bgp-cache",
37 description = "Text dump of BGP config cache")
38 @SuppressFBWarnings("DM_DEFAULT_ENCODING")
39 public class Cache extends OsgiCommandSupport {
40 private static final String LST = "--list";
41 private static final String OFL = "--out-file";
43 @Argument(name = "dummy", description = "Argument not needed",
44 required = false, multiValued = false)
45 private final String action = null;
47 @Option(name = LST, aliases = {"-l"},
48 description = "list vrfs and/or networks",
49 required = false, multiValued = true)
50 private final List<String> list = null;
52 @Option(name = OFL, aliases = {"-o"},
53 description = "output file",
54 required = false, multiValued = false)
55 private final String ofile = null;
57 private static final String HTSTR = "Host";
58 private static final String PTSTR = "Port";
59 private static final String ASSTR = "AS-Number";
60 private static final String RISTR = "Router-ID";
61 private static final String SPSTR = "Stale-Path-Time";
62 private static final String FBSTR = "F-bit";
63 private static final String LFSTR = "Log-File";
64 private static final String LLSTR = "Log-Level";
65 private static final String USSTR = "Update-Source";
66 private static final String EBSTR = "EBGP-Multihops";
67 private static final String AFSTR = "Address-Families";
68 private static final String ERSTR = "Export-RTs";
69 private static final String IRSTR = "Import-RTs";
70 private static final String NHSTR = "Nexthop";
71 private static final String LBSTR = "Label";
72 private static final String RDSTR = "RD";
73 private static final String MPSTR = "Maxpath";
75 private final BgpManager bgpManager;
77 public Cache(BgpManager bgpManager) {
78 this.bgpManager = bgpManager;
81 private Object usage() {
82 session.getConsole().println("usage: bgp-cache [" + LST + " vrfs | networks] [" + OFL + " file-name]");
86 public Object show(CommandSession session) {
87 this.session = session;
91 public Object show() {
95 @SuppressWarnings({"resource", "checkstyle:RegexpSinglelineJava"})
97 protected Object doExecute() {
98 boolean listVrfs = false;
99 boolean listNets = false;
100 PrintStream ps = System.out;
102 if (action != null) {
106 PrintStream fileStream = null;
110 fileStream = new PrintStream(ofile);
112 } catch (FileNotFoundException e) {
113 System.out.println("error: cannot create file " + ofile + "; exception: " + e);
118 for (String item : list) {
127 System.out.println("error: unknown value for " + LST + ": " + item);
128 if (fileStream != null) {
135 // we'd normally read this directly from 'config' but
136 // legacy behaviour forces to check for a connection
137 // that's initiated by default at startup without
138 // writing to config.
139 String configHost = bgpManager.getConfigHost();
140 int configPort = bgpManager.getConfigPort();
141 ps.printf("%nConfiguration Server%n\t%s %s%n\t%s %d%n",
142 HTSTR, configHost, PTSTR, configPort);
143 Bgp config = bgpManager.getConfig();
144 if (config == null) {
145 if (fileStream != null) {
151 AsId asId = config.getAsId();
153 Long asNum = asId.getLocalAs().longValue();
154 IpAddress routerId = asId.getRouterId();
155 Long spt = asId.getStalepathTime().toJava();
156 Boolean afb = asId.isAnnounceFbit();
157 String rid = routerId == null ? "<n/a>" : routerId.stringValue();
158 //F-bit is always set to ON (hardcoded), in SDN even though the controller is down
159 //forwarding state shall be retained.
162 GracefulRestart gracefulRestart = config.getGracefulRestart();
163 if (gracefulRestart != null) {
164 spt = gracefulRestart.getStalepathTime().toJava();
166 ps.printf("%nBGP Router%n");
167 ps.printf("\t%-15s %s%n\t%-15s %s%n\t%-15s %s%n\t%-15s %s%n",
168 ASSTR, asNum.toString(), RISTR, rid, SPSTR,
169 spt == null || spt == 0 ? "default" : spt.toString(), FBSTR, bit);
172 Logging logging = config.getLogging();
173 if (logging != null) {
174 ps.printf("\t%-15s %s%n\t%-15s %s%n", LFSTR, logging.getFile(),
175 LLSTR, logging.getLevel());
178 List<Neighbors> neighbors = (config.getNeighborsContainer() == null) ? null
179 : config.getNeighborsContainer().getNeighbors();
180 if (neighbors != null) {
181 ps.printf("%nNeighbors%n");
182 for (Neighbors nbr : neighbors) {
183 ps.printf("\t%s%n\t\t%-16s %d%n", nbr.getAddress().getValue(),
184 ASSTR, nbr.getRemoteAs());
185 EbgpMultihop en = nbr.getEbgpMultihop();
187 ps.printf("\t\t%-16s %d%n", EBSTR, en.getNhops().intValue());
189 UpdateSource us = nbr.getUpdateSource();
191 ps.printf("\t\t%-16s %s%n", USSTR, us.getSourceIp().getValue());
193 ps.printf("\t\t%-16s IPv4-Labeled-VPN", AFSTR);
194 List<AddressFamilies> afs = nbr.getAddressFamilies();
196 for (AddressFamilies af : afs) {
197 // Should not print "unknown" in vpnv4 case
198 if (!(af.getSafi().intValue() == 5 && af.getAfi().intValue() == 1)) {
199 if (af.getSafi().intValue() == 4 && af.getAfi().intValue() == 1) {
200 ps.printf(" %s", "IPv4-Labeled-Unicast");
201 } else if (af.getSafi().intValue() == 5 && af.getAfi().intValue() == 2) {
202 ps.printf(" %s", "IPv6-Labeled-VPN");
203 } else if (af.getSafi().intValue() == 6) {
204 ps.printf(" %s", "Ethernet-VPN");
206 ps.printf(" %s", "Unknown");
216 List<Vrfs> vrfs = (config.getVrfsContainer() == null) ? null : config.getVrfsContainer().getVrfs();
218 ps.printf("%nVRFs%n");
219 for (Vrfs vrf : vrfs) {
220 ps.printf("\t%s%n", vrf.getRd());
221 ps.printf("\t\t%s ", IRSTR);
222 for (String rt : vrf.getImportRts()) {
223 ps.printf("%s ", rt);
225 ps.printf("%n\t\t%s ", ERSTR);
226 for (String rt : vrf.getExportRts()) {
227 ps.printf("%s ", rt);
229 for (AddressFamiliesVrf adf : vrf.getAddressFamiliesVrf()) {
230 ps.printf("%n\t\tafi %d safi %d", adf.getAfi(), adf.getSafi());
238 List<Networks> ln = (config.getNetworksContainer() == null) ? null
239 : config.getNetworksContainer().getNetworks();
241 ps.printf("%nNetworks%n");
242 for (Networks net : ln) {
243 String rd = net.getRd();
244 String pfxlen = net.getPrefixLen();
245 String nh = net.getNexthop().getValue();
246 int label = net.getLabel().intValue();
247 ps.printf("\t%s%n\t\t%-7s %s%n\t\t%-7s %s%n\t\t%-7s %d%n",
248 pfxlen, RDSTR, rd, NHSTR, nh, LBSTR, label);
253 List<Multipath> mp = config.getMultipathContainer() == null ? null
254 : config.getMultipathContainer().getMultipath();
255 List<VrfMaxpath> vrfm = config.getVrfMaxpathContainer() == null ? null
256 : config.getVrfMaxpathContainer().getVrfMaxpath();
258 ps.printf("%nMultipath%n");
259 for (Multipath multipath : mp) {
260 int afi = multipath.getAfi().intValue();
261 int safi = multipath.getSafi().intValue();
262 Boolean enabled = multipath.isMultipathEnabled();
264 if (afi == 1 && safi == 5) {
265 ps.printf("\t%-16s %s%n%n", AFSTR, "vpnv4");
266 } else if (afi == 2 && safi == 5) {
267 ps.printf("\t%-16s %s%n%n", AFSTR, "vpnv6");
268 } else if (afi == 3 && safi == 6) {
269 ps.printf("\t%-16s %s%n%n", AFSTR, "evpn");
271 ps.printf("\t%-16s %s%n%n", AFSTR, "Unknown");
274 ps.printf("\t%-16s %s%n", RDSTR, MPSTR);
275 for (VrfMaxpath vrfMaxpath : vrfm) {
276 String rd = vrfMaxpath.getRd();
277 int maxpath = vrfMaxpath.getMaxpaths().toJava();
278 ps.printf("\t%-16s %d%n", rd, maxpath);
285 if (fileStream != null) {