9799643e3105941ff888f53572474309dfb1f69b
[netvirt.git] / bgpmanager / impl / src / main / java / org / opendaylight / netvirt / 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.netvirt.bgpmanager.commands;
10
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.Multipath;
26 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.Neighbors;
27 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.Networks;
28 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.VrfMaxpath;
29 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.Vrfs;
30 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.neighbors.AddressFamilies;
31 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.neighbors.EbgpMultihop;
32 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.neighbors.UpdateSource;
33 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.vrfs.AddressFamiliesVrf;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
35
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";
42
43     @Argument(name = "dummy", description = "Argument not needed",
44             required = false, multiValued = false)
45     private final String action = null;
46
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;
51
52     @Option(name = OFL, aliases = {"-o"},
53             description = "output file",
54             required = false, multiValued = false)
55     private final String ofile = null;
56
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";
74
75     private final BgpManager bgpManager;
76
77     public Cache(BgpManager bgpManager) {
78         this.bgpManager = bgpManager;
79     }
80
81     private Object usage() {
82         session.getConsole().println("usage: bgp-cache [" + LST + " vrfs | networks] [" + OFL + " file-name]");
83         return null;
84     }
85
86     public Object show(CommandSession session) {
87         this.session = session;
88         return doExecute();
89     }
90
91     public Object show() {
92         return doExecute();
93     }
94
95     @SuppressWarnings("resource")
96     @Override
97     protected Object doExecute() {
98         boolean listVrfs = false;
99         boolean listNets = false;
100         PrintStream ps = session.getConsole();
101
102         if (action != null) {
103             return usage();
104         }
105
106         PrintStream fileStream = null;
107         if (ofile != null) {
108             try {
109                 fileStream = new PrintStream(ofile);
110                 ps = fileStream;
111             } catch (FileNotFoundException e) {
112                 session.getConsole().println("error: cannot create file " + ofile + "; exception: " + e);
113                 return null;
114             }
115         }
116         if (list != null) {
117             for (String item : list) {
118                 switch (item) {
119                     case "vrfs":
120                         listVrfs = true;
121                         break;
122                     case "networks":
123                         listNets = true;
124                         break;
125                     default:
126                         session.getConsole().println("error: unknown value for " + LST + ": " + item);
127                         if (fileStream != null) {
128                             fileStream.close();
129                         }
130                         return null;
131                 }
132             }
133         }
134         // we'd normally read this directly from 'config' but
135         // legacy behaviour forces to check for a connection
136         // that's initiated by default at startup without
137         // writing to config.
138         String configHost = bgpManager.getConfigHost();
139         int configPort = bgpManager.getConfigPort();
140         ps.printf("%nConfiguration Server%n\t%s  %s%n\t%s  %d%n",
141                 HTSTR, configHost, PTSTR, configPort);
142         Bgp config = bgpManager.getConfig();
143         if (config == null) {
144             if (fileStream != null) {
145                 fileStream.close();
146             }
147
148             return null;
149         }
150         AsId asId = config.getAsId();
151         if (asId != null) {
152             int asNum = asId.getLocalAs().intValue();
153             IpAddress routerId = asId.getRouterId();
154             Long spt = asId.getStalepathTime();
155             Boolean afb = asId.isAnnounceFbit();
156             String rid = routerId == null ? "<n/a>" : new String(routerId.getValue());
157             //F-bit is always set to ON (hardcoded), in SDN even though the controller is down
158             //forwarding state shall be retained.
159             String bit = "ON";
160
161             GracefulRestart gracefulRestart = config.getGracefulRestart();
162             if (gracefulRestart != null) {
163                 spt = gracefulRestart.getStalepathTime();
164             }
165             ps.printf("%nBGP Router%n");
166             ps.printf("\t%-15s  %d%n\t%-15s  %s%n\t%-15s  %s%n\t%-15s  %s%n",
167                     ASSTR, asNum, RISTR, rid, SPSTR, spt == null || spt == 0 ? "default" : spt.toString(), FBSTR,
168                     bit);
169         }
170
171         Logging logging = config.getLogging();
172         if (logging != null) {
173             ps.printf("\t%-15s  %s%n\t%-15s  %s%n", LFSTR, logging.getFile(),
174                     LLSTR, logging.getLevel());
175         }
176
177         List<Neighbors> neighbors = config.getNeighbors();
178         if (neighbors != null) {
179             ps.printf("%nNeighbors%n");
180             for (Neighbors nbr : neighbors) {
181                 ps.printf("\t%s%n\t\t%-16s  %d%n", nbr.getAddress().getValue(),
182                         ASSTR, nbr.getRemoteAs().intValue());
183                 EbgpMultihop en = nbr.getEbgpMultihop();
184                 if (en != null) {
185                     ps.printf("\t\t%-16s  %d%n", EBSTR, en.getNhops().intValue());
186                 }
187                 UpdateSource us = nbr.getUpdateSource();
188                 if (us != null) {
189                     ps.printf("\t\t%-16s  %s%n", USSTR, us.getSourceIp().getValue());
190                 }
191                 ps.printf("\t\t%-16s  IPv4-Labeled-VPN", AFSTR);
192                 List<AddressFamilies> afs = nbr.getAddressFamilies();
193                 if (afs != null) {
194                     for (AddressFamilies af : afs) {
195                          // Should not print "unknown" in vpnv4 case
196                         if (!(af.getSafi().intValue() == 5 && af.getAfi().intValue() == 1)) {
197                             if (af.getSafi().intValue() == 4 && af.getAfi().intValue() == 1) {
198                                 ps.printf(" %s", "IPv4-Labeled-Unicast");
199                             } else if (af.getSafi().intValue() == 5 && af.getAfi().intValue() == 2) {
200                                 ps.printf(" %s", "IPv6-Labeled-VPN");
201                             } else if (af.getSafi().intValue() == 6) {
202                                 ps.printf(" %s", "Ethernet-VPN");
203                             }  else {
204                                 ps.printf(" %s", "Unknown");
205                             }
206                         }
207                     }
208                 }
209                 ps.printf("%n");
210             }
211         }
212
213         if (listVrfs) {
214             List<Vrfs> vrfs = config.getVrfs();
215             if (vrfs != null) {
216                 ps.printf("%nVRFs%n");
217                 for (Vrfs vrf : vrfs) {
218                     ps.printf("\t%s%n", vrf.getRd());
219                     ps.printf("\t\t%s  ", IRSTR);
220                     for (String rt : vrf.getImportRts()) {
221                         ps.printf("%s ", rt);
222                     }
223                     ps.printf("%n\t\t%s  ", ERSTR);
224                     for (String rt : vrf.getExportRts()) {
225                         ps.printf("%s ", rt);
226                     }
227                     for (AddressFamiliesVrf adf : vrf.getAddressFamiliesVrf()) {
228                         ps.printf("%n\t\tafi %d safi %d", adf.getAfi(), adf.getSafi());
229                     }
230                     ps.printf("%n");
231                 }
232             }
233         }
234
235         if (listNets) {
236             List<Networks> ln = config.getNetworks();
237             if (ln != null) {
238                 ps.printf("%nNetworks%n");
239                 for (Networks net : ln) {
240                     String rd = net.getRd();
241                     String pfxlen = net.getPrefixLen();
242                     String nh = net.getNexthop().getValue();
243                     int label = net.getLabel().intValue();
244                     ps.printf("\t%s%n\t\t%-7s  %s%n\t\t%-7s  %s%n\t\t%-7s  %d%n",
245                             pfxlen, RDSTR, rd, NHSTR, nh, LBSTR, label);
246                 }
247             }
248         }
249
250         List<Multipath> mp = config.getMultipath();
251         List<VrfMaxpath> vrfm = config.getVrfMaxpath();
252         if (mp != null) {
253             ps.printf("%nMultipath%n");
254             for (Multipath multipath : mp) {
255                 int afi = multipath.getAfi().intValue();
256                 int safi = multipath.getSafi().intValue();
257                 Boolean enabled = multipath.isMultipathEnabled();
258                 if (enabled) {
259                     if (afi == 1 && safi == 5) {
260                         ps.printf("\t%-16s  %s%n%n", AFSTR, "vpnv4");
261                     } else if (afi == 2 && safi == 5) {
262                         ps.printf("\t%-16s  %s%n%n", AFSTR, "vpnv6");
263                     } else if (afi == 3 && safi == 6) {
264                         ps.printf("\t%-16s  %s%n%n", AFSTR, "evpn");
265                     } else {
266                         ps.printf("\t%-16s  %s%n%n", AFSTR, "Unknown");
267                     }
268                     if (vrfm != null) {
269                         ps.printf("\t%-16s  %s%n", RDSTR, MPSTR);
270                         for (VrfMaxpath vrfMaxpath : vrfm) {
271                             String rd = vrfMaxpath.getRd();
272                             int maxpath = vrfMaxpath.getMaxpaths();
273                             ps.printf("\t%-16s  %d%n", rd, maxpath);
274                         }
275                     }
276                 }
277             }
278         }
279         if (fileStream != null) {
280             fileStream.close();
281         }
282         return null;
283     }
284 }