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