a34c1062c3c69c6d86ef5f3726c825b44c8257ef
[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 java.util.Map;
16 import org.apache.felix.service.command.CommandSession;
17 import org.apache.karaf.shell.commands.Argument;
18 import org.apache.karaf.shell.commands.Command;
19 import org.apache.karaf.shell.commands.Option;
20 import org.apache.karaf.shell.console.OsgiCommandSupport;
21 import org.opendaylight.netvirt.bgpmanager.BgpManager;
22 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.Bgp;
23 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.AsId;
24 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.GracefulRestart;
25 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.Logging;
26 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.multipathcontainer.Multipath;
27 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.multipathcontainer.MultipathKey;
28 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.neighborscontainer.Neighbors;
29 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.neighborscontainer.NeighborsKey;
30 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.neighborscontainer.neighbors.AddressFamilies;
31 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.neighborscontainer.neighbors.AddressFamiliesKey;
32 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.neighborscontainer.neighbors.EbgpMultihop;
33 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.neighborscontainer.neighbors.UpdateSource;
34 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.networkscontainer.Networks;
35 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.networkscontainer.NetworksKey;
36 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.vrfmaxpathcontainer.VrfMaxpath;
37 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.vrfmaxpathcontainer.VrfMaxpathKey;
38 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.vrfscontainer.Vrfs;
39 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.vrfscontainer.VrfsKey;
40 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.ebgp.rev150901.bgp.vrfscontainer.vrfs.AddressFamiliesVrf;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
42
43 @Command(scope = "odl", name = "bgp-cache",
44         description = "Text dump of BGP config cache")
45 @SuppressFBWarnings("DM_DEFAULT_ENCODING")
46 public class Cache extends OsgiCommandSupport {
47     private static final String LST = "--list";
48     private static final String OFL = "--out-file";
49
50     @Argument(name = "dummy", description = "Argument not needed",
51             required = false, multiValued = false)
52     private final String action = null;
53
54     @Option(name = LST, aliases = {"-l"},
55             description = "list vrfs and/or networks",
56             required = false, multiValued = true)
57     private final List<String> list = null;
58
59     @Option(name = OFL, aliases = {"-o"},
60             description = "output file",
61             required = false, multiValued = false)
62     private final String ofile = null;
63
64     private static final String HTSTR = "Host";
65     private static final String PTSTR = "Port";
66     private static final String ASSTR = "AS-Number";
67     private static final String RISTR = "Router-ID";
68     private static final String SPSTR = "Stale-Path-Time";
69     private static final String FBSTR = "F-bit";
70     private static final String LFSTR = "Log-File";
71     private static final String LLSTR = "Log-Level";
72     private static final String USSTR = "Update-Source";
73     private static final String EBSTR = "EBGP-Multihops";
74     private static final String AFSTR = "Address-Families";
75     private static final String ERSTR = "Export-RTs";
76     private static final String IRSTR = "Import-RTs";
77     private static final String NHSTR = "Nexthop";
78     private static final String LBSTR = "Label";
79     private static final String RDSTR = "RD";
80     private static final String MPSTR = "Maxpath";
81
82     private final BgpManager bgpManager;
83
84     public Cache(BgpManager bgpManager) {
85         this.bgpManager = bgpManager;
86     }
87
88     private Object usage() {
89         session.getConsole().println("usage: bgp-cache [" + LST + " vrfs | networks] [" + OFL + " file-name]");
90         return null;
91     }
92
93     public Object show(CommandSession session) {
94         this.session = session;
95         return doExecute();
96     }
97
98     public Object show() {
99         return doExecute();
100     }
101
102     @SuppressWarnings({"resource", "checkstyle:RegexpSinglelineJava"})
103     @Override
104     protected Object doExecute() {
105         boolean listVrfs = false;
106         boolean listNets = false;
107         PrintStream ps = System.out;
108
109         if (action != null) {
110             return usage();
111         }
112
113         PrintStream fileStream = null;
114         try {
115             if (ofile != null) {
116                 try {
117                     fileStream = new PrintStream(ofile);
118                     ps = fileStream;
119                 } catch (FileNotFoundException e) {
120                     System.out.println("error: cannot create file " + ofile + "; exception: " + e);
121                     return null;
122                 }
123             }
124             if (list != null) {
125                 for (String item : list) {
126                     switch (item) {
127                         case "vrfs":
128                             listVrfs = true;
129                             break;
130                         case "networks":
131                             listNets = true;
132                             break;
133                         default:
134                             System.out.println("error: unknown value for " + LST + ": " + item);
135                             if (fileStream != null) {
136                                 fileStream.close();
137                             }
138                             return null;
139                     }
140                 }
141             }
142             // we'd normally read this directly from 'config' but
143             // legacy behaviour forces to check for a connection
144             // that's initiated by default at startup without
145             // writing to config.
146             String configHost = bgpManager.getConfigHost();
147             int configPort = bgpManager.getConfigPort();
148             ps.printf("%nConfiguration Server%n\t%s  %s%n\t%s  %d%n",
149                     HTSTR, configHost, PTSTR, configPort);
150             Bgp config = bgpManager.getConfig();
151             if (config == null) {
152                 if (fileStream != null) {
153                     fileStream.close();
154                 }
155
156                 return null;
157             }
158             AsId asId = config.getAsId();
159             if (asId != null) {
160                 Long asNum = asId.getLocalAs().longValue();
161                 IpAddress routerId = asId.getRouterId();
162                 Long spt = asId.getStalepathTime().toJava();
163                 Boolean afb = asId.isAnnounceFbit();
164                 String rid = routerId == null ? "<n/a>" : routerId.stringValue();
165                 //F-bit is always set to ON (hardcoded), in SDN even though the controller is down
166                 //forwarding state shall be retained.
167                 String bit = "ON";
168
169                 GracefulRestart gracefulRestart = config.getGracefulRestart();
170                 if (gracefulRestart != null) {
171                     spt = gracefulRestart.getStalepathTime().toJava();
172                 }
173                 ps.printf("%nBGP Router%n");
174                 ps.printf("\t%-15s  %s%n\t%-15s  %s%n\t%-15s  %s%n\t%-15s  %s%n",
175                         ASSTR, asNum.toString(), RISTR, rid, SPSTR,
176                         spt == null || spt == 0 ? "default" : spt.toString(), FBSTR, bit);
177             }
178
179             Logging logging = config.getLogging();
180             if (logging != null) {
181                 ps.printf("\t%-15s  %s%n\t%-15s  %s%n", LFSTR, logging.getFile(),
182                         LLSTR, logging.getLevel());
183             }
184
185             Map<NeighborsKey, Neighbors> keyNeighborsMap = (config.getNeighborsContainer() ==  null) ? null
186                     : config.getNeighborsContainer().getNeighbors();
187             if (keyNeighborsMap != null) {
188                 ps.printf("%nNeighbors%n");
189                 for (Neighbors nbr : keyNeighborsMap.values()) {
190                     ps.printf("\t%s%n\t\t%-16s  %d%n", nbr.getAddress().getValue(),
191                             ASSTR, nbr.getRemoteAs());
192                     EbgpMultihop en = nbr.getEbgpMultihop();
193                     if (en != null) {
194                         ps.printf("\t\t%-16s  %d%n", EBSTR, en.getNhops().intValue());
195                     }
196                     UpdateSource us = nbr.getUpdateSource();
197                     if (us != null) {
198                         ps.printf("\t\t%-16s  %s%n", USSTR, us.getSourceIp().getValue());
199                     }
200                     ps.printf("\t\t%-16s  IPv4-Labeled-VPN", AFSTR);
201                     Map<AddressFamiliesKey, AddressFamilies> keyAddressFamiliesMap = nbr.getAddressFamilies();
202                     if (keyAddressFamiliesMap != null) {
203                         for (AddressFamilies af : keyAddressFamiliesMap.values()) {
204                              // Should not print "unknown" in vpnv4 case
205                             if (!(af.getSafi().intValue() == 5 && af.getAfi().intValue() == 1)) {
206                                 if (af.getSafi().intValue() == 4 && af.getAfi().intValue() == 1) {
207                                     ps.printf(" %s", "IPv4-Labeled-Unicast");
208                                 } else if (af.getSafi().intValue() == 5 && af.getAfi().intValue() == 2) {
209                                     ps.printf(" %s", "IPv6-Labeled-VPN");
210                                 } else if (af.getSafi().intValue() == 6) {
211                                     ps.printf(" %s", "Ethernet-VPN");
212                                 }  else {
213                                     ps.printf(" %s", "Unknown");
214                                 }
215                             }
216                         }
217                     }
218                     ps.printf("%n");
219                 }
220             }
221
222             if (listVrfs) {
223                 Map<VrfsKey, Vrfs> keyVrfsMap
224                         = (config.getVrfsContainer() == null) ? null : config.getVrfsContainer().getVrfs();
225                 if (keyVrfsMap != null) {
226                     ps.printf("%nVRFs%n");
227                     for (Vrfs vrf : keyVrfsMap.values()) {
228                         ps.printf("\t%s%n", vrf.getRd());
229                         ps.printf("\t\t%s  ", IRSTR);
230                         for (String rt : vrf.getImportRts()) {
231                             ps.printf("%s ", rt);
232                         }
233                         ps.printf("%n\t\t%s  ", ERSTR);
234                         for (String rt : vrf.getExportRts()) {
235                             ps.printf("%s ", rt);
236                         }
237                         for (AddressFamiliesVrf adf : vrf.getAddressFamiliesVrf().values()) {
238                             ps.printf("%n\t\tafi %d safi %d", adf.getAfi(), adf.getSafi());
239                         }
240                         ps.printf("%n");
241                     }
242                 }
243             }
244
245             if (listNets) {
246                 Map<NetworksKey, Networks> keyNetworksMap = (config.getNetworksContainer() == null) ? null
247                         : config.getNetworksContainer().getNetworks();
248                 if (keyNetworksMap != null) {
249                     ps.printf("%nNetworks%n");
250                     for (Networks net : keyNetworksMap.values()) {
251                         String rd = net.getRd();
252                         String pfxlen = net.getPrefixLen();
253                         String nh = net.getNexthop().getValue();
254                         int label = net.getLabel().intValue();
255                         ps.printf("\t%s%n\t\t%-7s  %s%n\t\t%-7s  %s%n\t\t%-7s  %d%n",
256                                 pfxlen, RDSTR, rd, NHSTR, nh, LBSTR, label);
257                     }
258                 }
259             }
260
261             Map<MultipathKey, Multipath> keyMultipathMap = config.getMultipathContainer() == null ? null
262                     : config.getMultipathContainer().getMultipath();
263             Map<VrfMaxpathKey, VrfMaxpath> keyVrfMaxpathMap = config.getVrfMaxpathContainer() == null ? null
264                     : config.getVrfMaxpathContainer().getVrfMaxpath();
265             if (keyMultipathMap != null) {
266                 ps.printf("%nMultipath%n");
267                 for (Multipath multipath : keyMultipathMap.values()) {
268                     int afi = multipath.getAfi().intValue();
269                     int safi = multipath.getSafi().intValue();
270                     Boolean enabled = multipath.isMultipathEnabled();
271                     if (enabled) {
272                         if (afi == 1 && safi == 5) {
273                             ps.printf("\t%-16s  %s%n%n", AFSTR, "vpnv4");
274                         } else if (afi == 2 && safi == 5) {
275                             ps.printf("\t%-16s  %s%n%n", AFSTR, "vpnv6");
276                         } else if (afi == 3 && safi == 6) {
277                             ps.printf("\t%-16s  %s%n%n", AFSTR, "evpn");
278                         } else {
279                             ps.printf("\t%-16s  %s%n%n", AFSTR, "Unknown");
280                         }
281                         if (keyVrfMaxpathMap != null) {
282                             ps.printf("\t%-16s  %s%n", RDSTR, MPSTR);
283                             for (VrfMaxpath vrfMaxpath : keyVrfMaxpathMap.values()) {
284                                 String rd = vrfMaxpath.getRd();
285                                 int maxpath = vrfMaxpath.getMaxpaths().toJava();
286                                 ps.printf("\t%-16s  %d%n", rd, maxpath);
287                             }
288                         }
289                     }
290                 }
291             }
292         } finally {
293             if (fileStream != null) {
294                 fileStream.close();
295             }
296         }
297         return null;
298     }
299 }