1f649b165d9ba5eaa4c905f955aa049ec5fab2d1
[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.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;
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.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();
186                     if (en != null) {
187                         ps.printf("\t\t%-16s  %d%n", EBSTR, en.getNhops().intValue());
188                     }
189                     UpdateSource us = nbr.getUpdateSource();
190                     if (us != null) {
191                         ps.printf("\t\t%-16s  %s%n", USSTR, us.getSourceIp().getValue());
192                     }
193                     ps.printf("\t\t%-16s  IPv4-Labeled-VPN", AFSTR);
194                     List<AddressFamilies> afs = nbr.getAddressFamilies();
195                     if (afs != null) {
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");
205                                 }  else {
206                                     ps.printf(" %s", "Unknown");
207                                 }
208                             }
209                         }
210                     }
211                     ps.printf("%n");
212                 }
213             }
214
215             if (listVrfs) {
216                 List<Vrfs> vrfs = (config.getVrfsContainer() == null) ? null : config.getVrfsContainer().getVrfs();
217                 if (vrfs != null) {
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);
224                         }
225                         ps.printf("%n\t\t%s  ", ERSTR);
226                         for (String rt : vrf.getExportRts()) {
227                             ps.printf("%s ", rt);
228                         }
229                         for (AddressFamiliesVrf adf : vrf.getAddressFamiliesVrf()) {
230                             ps.printf("%n\t\tafi %d safi %d", adf.getAfi(), adf.getSafi());
231                         }
232                         ps.printf("%n");
233                     }
234                 }
235             }
236
237             if (listNets) {
238                 List<Networks> ln = (config.getNetworksContainer() == null) ? null
239                         : config.getNetworksContainer().getNetworks();
240                 if (ln != null) {
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);
249                     }
250                 }
251             }
252
253             List<Multipath> mp = config.getMultipathContainer() == null ? null
254                     : config.getMultipathContainer().getMultipath();
255             List<VrfMaxpath> vrfm = config.getVrfMaxpathContainer() == null ? null
256                     : config.getVrfMaxpathContainer().getVrfMaxpath();
257             if (mp != null) {
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();
263                     if (enabled) {
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");
270                         } else {
271                             ps.printf("\t%-16s  %s%n%n", AFSTR, "Unknown");
272                         }
273                         if (vrfm != null) {
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);
279                             }
280                         }
281                     }
282                 }
283             }
284         } finally {
285             if (fileStream != null) {
286                 fileStream.close();
287             }
288         }
289         return null;
290     }
291 }