Switch to JDT annotations for Nullable and NonNull
[netvirt.git] / vpnmanager / shell / src / main / java / org / opendaylight / netvirt / vpnmanager / shell / ShowVpn.java
1 /*
2  * Copyright © 2016, 2017 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 package org.opendaylight.netvirt.vpnmanager.shell;
9
10 import com.google.common.base.Optional;
11 import java.util.ArrayList;
12 import java.util.HashMap;
13 import java.util.HashSet;
14 import java.util.List;
15 import java.util.Map;
16 import java.util.Objects;
17 import java.util.Set;
18 import java.util.concurrent.ExecutionException;
19 import org.apache.karaf.shell.commands.Command;
20 import org.apache.karaf.shell.commands.Option;
21 import org.apache.karaf.shell.console.OsgiCommandSupport;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
24 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
25 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
26 import org.opendaylight.netvirt.vpnmanager.api.VpnHelper;
27 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.VpnAfConfig;
28 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.VpnInstances;
29 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.VpnInterfaces;
30 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.instances.VpnInstance;
31 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.interfaces.VpnInterface;
32 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.interfaces.vpn._interface.VpnInstanceNames;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.VpnInterfaceOpData;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn._interface.op.data.VpnInterfaceOpDataEntry;
35 import org.opendaylight.yangtools.yang.binding.DataObject;
36 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 @Command(scope = "vpnservice", name = "vpn-show", description = "Display all present vpnInstances with their "
41         + "respective count of oper and config vpnInterfaces")
42 public class ShowVpn extends OsgiCommandSupport {
43
44     @Option(name = "--detail", aliases = {"--vpninstance"}, description = "Display oper and config interfaces for "
45             + "given vpnInstanceName", required = false, multiValued = false)
46     private String detail;
47
48     private static final Logger LOG = LoggerFactory.getLogger(ShowVpn.class);
49     private DataBroker dataBroker;
50     private int configCount = 0;
51     private int operCount = 0;
52     private int totalOperCount = 0;
53     private int totalConfigCount = 0;
54     private Integer ifPresent;
55     private List<org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.instances.VpnInstance>
56             vpnInstanceList = new ArrayList<>();
57     private List<VpnInterface> vpnInterfaceConfigList = new ArrayList<>();
58     private List<VpnInterfaceOpDataEntry> vpnInterfaceOpList = new ArrayList<>();
59
60     public void setDataBroker(DataBroker broker) {
61         this.dataBroker = broker;
62     }
63
64     @Override
65     @Nullable
66     protected Object doExecute() {
67         Map<String, Integer> vpnNameToConfigInterfaceMap = new HashMap<>();
68         Map<String, Integer> vpnNameToOperInterfaceMap = new HashMap<>();
69         if (detail == null) {
70             showVpn();
71             Set<String> vpnInstances = new HashSet<>();
72             for (VpnInterface vpnInterface : vpnInterfaceConfigList) {
73                 if (vpnInterface.getVpnInstanceNames() != null) {
74                     for (VpnInstanceNames vpnInterfaceVpnInstance : vpnInterface.getVpnInstanceNames()) {
75                         String vpnName = vpnInterfaceVpnInstance.getVpnName();
76                         if (vpnName != null) {
77                             vpnInstances.add(vpnName);
78                         }
79                     }
80                 }
81             }
82             for (String routerId : vpnInstances) {
83                 ifPresent = vpnNameToConfigInterfaceMap.get(routerId);
84                 if (ifPresent == null) {
85                     vpnNameToConfigInterfaceMap.put(routerId, 1);
86                 } else {
87                     vpnNameToConfigInterfaceMap.put(routerId,
88                                       vpnNameToConfigInterfaceMap.get(routerId) + 1);
89                 }
90             }
91             for (VpnInterfaceOpDataEntry vpnInterfaceOp : vpnInterfaceOpList) {
92                 ifPresent = vpnNameToOperInterfaceMap.get(vpnInterfaceOp.getVpnInstanceName());
93                 if (ifPresent == null) {
94                     vpnNameToOperInterfaceMap.put(vpnInterfaceOp.getVpnInstanceName(), 1);
95                 } else {
96                     vpnNameToOperInterfaceMap.put(vpnInterfaceOp.getVpnInstanceName(),
97                         vpnNameToOperInterfaceMap.get(vpnInterfaceOp.getVpnInstanceName()) + 1);
98                 }
99             }
100             session.getConsole().println("-----------------------------------------------------------------------");
101             session.getConsole().println(
102                     String.format("         %s   %14s  %5s  %5s", "VpnInstanceName", "RD", "Config Count",
103                             "Oper Count"));
104             session.getConsole().println(
105                     "\n-----------------------------------------------------------------------");
106             for (VpnInstance vpnInstance : vpnInstanceList) {
107                 configCount = 0;
108                 operCount = 0;
109                 Integer count = vpnNameToConfigInterfaceMap.get(vpnInstance.getVpnInstanceName());
110                 if (count != null) {
111                     configCount = vpnNameToConfigInterfaceMap.get(vpnInstance.getVpnInstanceName());
112                     totalConfigCount = totalConfigCount + configCount;
113                 }
114                 count = vpnNameToOperInterfaceMap.get(vpnInstance.getVpnInstanceName());
115                 if (count != null) {
116                     operCount = vpnNameToOperInterfaceMap.get(vpnInstance.getVpnInstanceName());
117                     totalOperCount = totalOperCount + operCount;
118                 }
119                 VpnAfConfig addrFamily = vpnInstance.getIpv4Family() != null ? vpnInstance.getIpv4Family() :
120                     vpnInstance.getIpv6Family();
121
122                 session.getConsole().println(
123                         String.format("%-32s  %-10s  %-10s  %-10s", vpnInstance.getVpnInstanceName(),
124                                 addrFamily.getRouteDistinguisher(), configCount, operCount));
125             }
126             session.getConsole().println("-----------------------------------------------------------------------");
127             session.getConsole().println(
128                     String.format("Total Count:                    %19s    %8s", totalConfigCount, totalOperCount));
129             session.getConsole().println(getshowVpnCLIHelp());
130         } else {
131             showVpn();
132             session.getConsole().println("Present Config VpnInterfaces are:");
133             for (VpnInterface vpnInterface : vpnInterfaceConfigList) {
134                 if (VpnHelper.doesVpnInterfaceBelongToVpnInstance(detail, vpnInterface.getVpnInstanceNames())) {
135                     session.getConsole().println(vpnInterface.getName());
136                 }
137             }
138             session.getConsole().println("Present Oper VpnInterfaces are:");
139             for (VpnInterfaceOpDataEntry vpnInterfaceOp : vpnInterfaceOpList) {
140                 if (Objects.equals(vpnInterfaceOp.getVpnInstanceName(), detail)) {
141                     session.getConsole().println(vpnInterfaceOp.getName());
142                 }
143             }
144         }
145         return null;
146     }
147
148     private <T extends DataObject> Optional<T> read(LogicalDatastoreType datastoreType,
149             InstanceIdentifier<T> path) {
150         try (ReadOnlyTransaction tx = dataBroker.newReadOnlyTransaction()) {
151             return tx.read(datastoreType, path).get();
152         } catch (InterruptedException | ExecutionException e) {
153             throw new RuntimeException(e);
154         }
155     }
156
157     private void showVpn() {
158         InstanceIdentifier<VpnInstances> vpnsIdentifier = InstanceIdentifier.builder(VpnInstances.class).build();
159         InstanceIdentifier<VpnInterfaces> vpnInterfacesIdentifier = InstanceIdentifier.builder(VpnInterfaces
160                 .class).build();
161         Optional<VpnInstances> optionalVpnInstances = read(LogicalDatastoreType.CONFIGURATION, vpnsIdentifier);
162
163         if (!optionalVpnInstances.isPresent()) {
164             LOG.trace("No VPNInstances configured.");
165             session.getConsole().println("No VPNInstances configured.");
166         } else {
167             vpnInstanceList = optionalVpnInstances.get().getVpnInstance();
168         }
169
170         Optional<VpnInterfaces> optionalVpnInterfacesConfig =
171                 read(LogicalDatastoreType.CONFIGURATION, vpnInterfacesIdentifier);
172
173         if (!optionalVpnInterfacesConfig.isPresent()) {
174             LOG.trace("No Config VpnInterface is present");
175             session.getConsole().println("No Config VpnInterface is present");
176         } else {
177             vpnInterfaceConfigList = optionalVpnInterfacesConfig.get().getVpnInterface();
178         }
179
180
181         InstanceIdentifier<VpnInterfaceOpData> id = InstanceIdentifier.create(VpnInterfaceOpData.class);
182         Optional<VpnInterfaceOpData> optionalVpnInterfacesOper = read(LogicalDatastoreType.OPERATIONAL, id);
183
184         if (!optionalVpnInterfacesOper.isPresent()) {
185             LOG.trace("No Oper VpnInterface is present");
186             session.getConsole().println("No Oper VpnInterface is present");
187         } else {
188             vpnInterfaceOpList = optionalVpnInterfacesOper.get().getVpnInterfaceOpDataEntry();
189         }
190     }
191
192     private String getshowVpnCLIHelp() {
193         StringBuilder help = new StringBuilder("\nUsage:");
194         help.append("To display vpn-interfaces for a particular vpnInstance vpn-show --detail [<vpnInstanceName>]");
195         return help.toString();
196     }
197 }