BgpRouter code enhancements
[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", "checkstyle:RegexpSinglelineJava"})
96     @Override
97     protected Object doExecute() {
98         boolean listVrfs = false;
99         boolean listNets = false;
100         PrintStream ps = System.out;
101
102         if (action != null) {
103             return usage();
104         }
105
106         PrintStream fileStream = null;
107         try {
108             if (ofile != null) {
109                 try {
110                     fileStream = new PrintStream(ofile);
111                     ps = fileStream;
112                 } catch (FileNotFoundException e) {
113                     System.out.println("error: cannot create file " + ofile + "; exception: " + e);
114                     return null;
115                 }
116             }
117             if (list != null) {
118                 for (String item : list) {
119                     switch (item) {
120                         case "vrfs":
121                             listVrfs = true;
122                             break;
123                         case "networks":
124                             listNets = true;
125                             break;
126                         default:
127                             System.out.println("error: unknown value for " + LST + ": " + item);
128                             if (fileStream != null) {
129                                 fileStream.close();
130                             }
131                             return null;
132                     }
133                 }
134             }
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) {
146                     fileStream.close();
147                 }
148
149                 return null;
150             }
151             AsId asId = config.getAsId();
152             if (asId != null) {
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.
160                 String bit = "ON";
161
162                 GracefulRestart gracefulRestart = config.getGracefulRestart();
163                 if (gracefulRestart != null) {
164                     spt = gracefulRestart.getStalepathTime().toJava();
165                 }
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);
170             }
171
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());
176             }
177
178             List<Neighbors> neighbors = config.getNeighbors();
179             if (neighbors != null) {
180                 ps.printf("%nNeighbors%n");
181                 for (Neighbors nbr : neighbors) {
182                     ps.printf("\t%s%n\t\t%-16s  %d%n", nbr.getAddress().getValue(),
183                             ASSTR, nbr.getRemoteAs());
184                     EbgpMultihop en = nbr.getEbgpMultihop();
185                     if (en != null) {
186                         ps.printf("\t\t%-16s  %d%n", EBSTR, en.getNhops().intValue());
187                     }
188                     UpdateSource us = nbr.getUpdateSource();
189                     if (us != null) {
190                         ps.printf("\t\t%-16s  %s%n", USSTR, us.getSourceIp().getValue());
191                     }
192                     ps.printf("\t\t%-16s  IPv4-Labeled-VPN", AFSTR);
193                     List<AddressFamilies> afs = nbr.getAddressFamilies();
194                     if (afs != null) {
195                         for (AddressFamilies af : afs) {
196                              // Should not print "unknown" in vpnv4 case
197                             if (!(af.getSafi().intValue() == 5 && af.getAfi().intValue() == 1)) {
198                                 if (af.getSafi().intValue() == 4 && af.getAfi().intValue() == 1) {
199                                     ps.printf(" %s", "IPv4-Labeled-Unicast");
200                                 } else if (af.getSafi().intValue() == 5 && af.getAfi().intValue() == 2) {
201                                     ps.printf(" %s", "IPv6-Labeled-VPN");
202                                 } else if (af.getSafi().intValue() == 6) {
203                                     ps.printf(" %s", "Ethernet-VPN");
204                                 }  else {
205                                     ps.printf(" %s", "Unknown");
206                                 }
207                             }
208                         }
209                     }
210                     ps.printf("%n");
211                 }
212             }
213
214             if (listVrfs) {
215                 List<Vrfs> vrfs = config.getVrfs();
216                 if (vrfs != null) {
217                     ps.printf("%nVRFs%n");
218                     for (Vrfs vrf : vrfs) {
219                         ps.printf("\t%s%n", vrf.getRd());
220                         ps.printf("\t\t%s  ", IRSTR);
221                         for (String rt : vrf.getImportRts()) {
222                             ps.printf("%s ", rt);
223                         }
224                         ps.printf("%n\t\t%s  ", ERSTR);
225                         for (String rt : vrf.getExportRts()) {
226                             ps.printf("%s ", rt);
227                         }
228                         for (AddressFamiliesVrf adf : vrf.getAddressFamiliesVrf()) {
229                             ps.printf("%n\t\tafi %d safi %d", adf.getAfi(), adf.getSafi());
230                         }
231                         ps.printf("%n");
232                     }
233                 }
234             }
235
236             if (listNets) {
237                 List<Networks> ln = config.getNetworks();
238                 if (ln != null) {
239                     ps.printf("%nNetworks%n");
240                     for (Networks net : ln) {
241                         String rd = net.getRd();
242                         String pfxlen = net.getPrefixLen();
243                         String nh = net.getNexthop().getValue();
244                         int label = net.getLabel().intValue();
245                         ps.printf("\t%s%n\t\t%-7s  %s%n\t\t%-7s  %s%n\t\t%-7s  %d%n",
246                                 pfxlen, RDSTR, rd, NHSTR, nh, LBSTR, label);
247                     }
248                 }
249             }
250
251             List<Multipath> mp = config.getMultipath();
252             List<VrfMaxpath> vrfm = config.getVrfMaxpath();
253             if (mp != null) {
254                 ps.printf("%nMultipath%n");
255                 for (Multipath multipath : mp) {
256                     int afi = multipath.getAfi().intValue();
257                     int safi = multipath.getSafi().intValue();
258                     Boolean enabled = multipath.isMultipathEnabled();
259                     if (enabled) {
260                         if (afi == 1 && safi == 5) {
261                             ps.printf("\t%-16s  %s%n%n", AFSTR, "vpnv4");
262                         } else if (afi == 2 && safi == 5) {
263                             ps.printf("\t%-16s  %s%n%n", AFSTR, "vpnv6");
264                         } else if (afi == 3 && safi == 6) {
265                             ps.printf("\t%-16s  %s%n%n", AFSTR, "evpn");
266                         } else {
267                             ps.printf("\t%-16s  %s%n%n", AFSTR, "Unknown");
268                         }
269                         if (vrfm != null) {
270                             ps.printf("\t%-16s  %s%n", RDSTR, MPSTR);
271                             for (VrfMaxpath vrfMaxpath : vrfm) {
272                                 String rd = vrfMaxpath.getRd();
273                                 int maxpath = vrfMaxpath.getMaxpaths().toJava();
274                                 ps.printf("\t%-16s  %d%n", rd, maxpath);
275                             }
276                         }
277                     }
278                 }
279             }
280         } finally {
281             if (fileStream != null) {
282                 fileStream.close();
283             }
284         }
285         return null;
286     }
287 }