27c5c6547d8a9b6039451a8c094c746cdeeaf44e
[netvirt.git] / ipv6service / shell / src / main / java / org / opendaylight / netvirt / ipv6service / shell / ShowIpv6Command.java
1 /*
2  * Copyright (c) 2017 Red Hat, Inc. 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 package org.opendaylight.netvirt.ipv6service.shell;
9
10 import java.util.List;
11 import java.util.stream.Collectors;
12 import javax.annotation.Nullable;
13 import org.apache.karaf.shell.commands.Argument;
14 import org.apache.karaf.shell.commands.Command;
15 import org.apache.karaf.shell.console.OsgiCommandSupport;
16 import org.opendaylight.infrautils.utils.TablePrinter;
17 import org.opendaylight.netvirt.ipv6service.api.ElementCache;
18 import org.opendaylight.netvirt.ipv6service.api.IVirtualNetwork;
19 import org.opendaylight.netvirt.ipv6service.api.IVirtualPort;
20 import org.opendaylight.netvirt.ipv6service.api.IVirtualRouter;
21 import org.opendaylight.netvirt.ipv6service.api.IVirtualSubnet;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Address;
23
24 @Command(scope = "ipv6service", name = "ipv6CacheShow", description = "Displays the IPv6Service Cache info")
25 public class ShowIpv6Command extends OsgiCommandSupport {
26     private ElementCache elementCache;
27
28     @Argument(name = "resource", description = "List the various resource specific cache, where resource "
29             + "could be <networks/subnets/routers>", required = false, multiValued = false)
30     private final String listResource = null;
31
32     public void setElementCache(ElementCache elementCache) {
33         this.elementCache = elementCache;
34     }
35
36     private static String getPortIpv6Addresses(IVirtualPort vport) {
37         List<Ipv6Address> ipv6Addresses = vport.getIpv6Addresses();
38         return ipv6Addresses.stream().map(Ipv6Address::getValue).collect(Collectors.joining("  "));
39     }
40
41     @Override
42     @Nullable
43     protected Object doExecute() {
44         TablePrinter tp = new TablePrinter();
45
46         if (listResource != null) {
47             if (listResource.equalsIgnoreCase("networks")
48                     || listResource.equalsIgnoreCase("net")) {
49                 tp.setTitle("Network Cache List");
50                 tp.setColumnNames("Sno", "NetworkId", "dpnId");
51                 int count = 1;
52                 List<IVirtualNetwork> vnetworks = elementCache.getNetworkCache();
53                 for (IVirtualNetwork vnet: vnetworks) {
54                     tp.addRow(count++, String.valueOf(vnet.getNetworkUuid().getValue()), vnet.getDpnsHostingNetwork());
55                 }
56                 session.getConsole().print(tp.toString());
57             } else if (listResource.equalsIgnoreCase("subnets")
58                     || listResource.equalsIgnoreCase("subnet")) {
59                 tp.setTitle("Subnet Cache List");
60                 tp.setColumnNames("Sno", "SubnetId", "SubnetCIDR", "ipVersion");
61                 int count = 1;
62                 List<IVirtualSubnet> vsubnets = elementCache.getSubnetCache();
63                 for (IVirtualSubnet vsubnet : vsubnets) {
64                     tp.addRow(count++, String.valueOf(vsubnet.getSubnetUUID().getValue()),
65                             vsubnet.getSubnetCidr().stringValue(), vsubnet.getIpVersion());
66                 }
67                 session.getConsole().print(tp.toString());
68             } else if (listResource.equalsIgnoreCase("routers")
69                     || listResource.equalsIgnoreCase("router")) {
70                 tp.setTitle("Router Cache List");
71                 tp.setColumnNames("Sno", "RouterId");
72                 List<IVirtualRouter> vrouters = elementCache.getRouterCache();
73                 int count = 1;
74                 for (IVirtualRouter vrouter : vrouters) {
75                     tp.addRow(count++, String.valueOf(vrouter.getRouterUUID().getValue()));
76                 }
77                 session.getConsole().print(tp.toString());
78             }
79         } else {
80             tp.setTitle("Interface Cache List");
81             tp.setColumnNames("Sno", "PortId", "Mac Address", "Owner", "dpnId", "FixedIPs");
82             List<IVirtualPort> vports = elementCache.getInterfaceCache();
83             int count = 1;
84             for (IVirtualPort vport: vports) {
85                 String str = vport.getDeviceOwner();
86                 tp.addRow(count++, String.valueOf(vport.getIntfUUID().getValue()), vport.getMacAddress(),
87                         str.startsWith("network:") ? str.substring(str.lastIndexOf(':') + 1) : "compute",
88                         vport.getDpId(), getPortIpv6Addresses(vport));
89             }
90             session.getConsole().print(tp.toString());
91         }
92         return null;
93     }
94 }