NETVIRT-1630 migrate to md-sal APIs
[netvirt.git] / vpnmanager / shell / src / main / java / org / opendaylight / netvirt / vpnmanager / shell / ShowVpnInstanceOpData.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.List;
13 import java.util.Map;
14 import java.util.Optional;
15 import java.util.concurrent.ExecutionException;
16 import org.apache.karaf.shell.commands.Command;
17 import org.apache.karaf.shell.commands.Option;
18 import org.apache.karaf.shell.console.OsgiCommandSupport;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.opendaylight.mdsal.binding.api.DataBroker;
21 import org.opendaylight.mdsal.binding.api.ReadTransaction;
22 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.VpnInstanceOpData;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn.instance.op.data.VpnInstanceOpDataEntry;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn.instance.op.data.vpn.instance.op.data.entry.VpnToDpnList;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.l3vpn.rev200204.VpnInstances;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.l3vpn.rev200204.vpn.instances.VpnInstance;
28 import org.opendaylight.yangtools.yang.binding.DataObject;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 @Command(scope = "vpnservice", name = "vpninstance-op-show", description = "List name of all vpnInstances that is "
34         + "present or absent in vpnInstanceOpDataEntry")
35 public class ShowVpnInstanceOpData extends OsgiCommandSupport {
36
37     @Option(name = "--detail", aliases = {"--vpnInstanceOp"}, description = "Display vpnInstanceOpDataEntry detail "
38             + "for given vpnInstanceName", required = false, multiValued = false)
39     private String detail;
40     private static final Logger LOG = LoggerFactory.getLogger(ShowVpnInstanceOpData.class);
41     private DataBroker dataBroker;
42     private List<VpnInstance> vpnInstanceList = new ArrayList<>();
43     private Map<String, VpnInstanceOpDataEntry> vpnInstanceOpDataEntryMap = new HashMap<>();
44
45     public void setDataBroker(DataBroker broker) {
46         this.dataBroker = broker;
47     }
48
49     @Override
50     @Nullable
51     protected Object doExecute() {
52         if (detail == null) {
53             getVpnInstanceOpData();
54             session.getConsole().println("For following vpnInstances vpnInstanceOpDataEntry is present: \n");
55             for (VpnInstance vpnInstance : vpnInstanceList) {
56                 VpnInstanceOpDataEntry check = vpnInstanceOpDataEntryMap.get(vpnInstance.getVpnInstanceName());
57                 if (check != null) {
58                     session.getConsole().println(vpnInstance.getVpnInstanceName() + "\n");
59                 }
60             }
61             session.getConsole().println("\n\nFor following vpnInstances vpnInstanceOpDataEntry is not present: \n");
62             for (VpnInstance vpnInstance : vpnInstanceList) {
63                 VpnInstanceOpDataEntry check = vpnInstanceOpDataEntryMap.get(vpnInstance.getVpnInstanceName());
64                 if (check == null) {
65                     session.getConsole().println(vpnInstance.getVpnInstanceName() + "\n");
66                 }
67             }
68             session.getConsole().println(getshowVpnCLIHelp());
69         } else {
70             getVpnInstanceOpData();
71             session.getConsole().println("Fetching details of given vpnInstance\n");
72             session.getConsole().println(
73                     "------------------------------------------------------------------------------");
74             VpnInstanceOpDataEntry check = vpnInstanceOpDataEntryMap.get(detail);
75             Long intfCount = 0L;
76             List<VpnToDpnList> dpnToVpns = check.getVpnToDpnList();
77             if (dpnToVpns != null) {
78                 for (VpnToDpnList dpn : dpnToVpns) {
79                     if (dpn.getVpnInterfaces() != null) {
80                         intfCount = intfCount + dpn.getVpnInterfaces().size();
81                     }
82                 }
83             }
84             session.getConsole().println(
85                     "VpnInstanceName: " + check.getVpnInstanceName() + "\nVpnId: " + check.getVpnId() + "\nVrfId: "
86                             + check.getVrfId() + "\nKey: " + check.key() + "\nVpnInterfaceCount: "
87                             + intfCount + "\nVpnToDpnList: " + check.getVpnToDpnList() + "\n");
88             session.getConsole().println(
89                     "------------------------------------------------------------------------------");
90         }
91
92         return null;
93     }
94
95     private void getVpnInstanceOpData() {
96         List<VpnInstanceOpDataEntry> vpnInstanceOpDataEntryList = new ArrayList<>();
97         InstanceIdentifier<VpnInstances> vpnsIdentifier = InstanceIdentifier.builder(VpnInstances.class).build();
98         InstanceIdentifier<VpnInstanceOpData> vpnInstanceOpDataEntryIdentifier =
99                 InstanceIdentifier.builder(VpnInstanceOpData.class).build();
100         Optional<VpnInstances> optionalVpnInstances = read(LogicalDatastoreType.CONFIGURATION, vpnsIdentifier);
101
102         if (!optionalVpnInstances.isPresent() || optionalVpnInstances.get().getVpnInstance() == null
103                 || optionalVpnInstances.get().getVpnInstance().isEmpty()) {
104             LOG.trace("No VPNInstances configured.");
105             session.getConsole().println("No VPNInstances configured.");
106         } else {
107             vpnInstanceList = optionalVpnInstances.get().getVpnInstance();
108         }
109
110         Optional<VpnInstanceOpData> optionalOpData = read(LogicalDatastoreType.OPERATIONAL,
111                 vpnInstanceOpDataEntryIdentifier);
112
113         if (!optionalOpData.isPresent()) {
114             LOG.trace("No VPNInstanceOpDataEntry present.");
115             session.getConsole().println("No VPNInstanceOpDataEntry present.");
116         } else {
117             vpnInstanceOpDataEntryList = optionalOpData.get().getVpnInstanceOpDataEntry();
118         }
119
120         for (VpnInstanceOpDataEntry vpnInstanceOpDataEntry : vpnInstanceOpDataEntryList) {
121             vpnInstanceOpDataEntryMap.put(vpnInstanceOpDataEntry.getVpnInstanceName(), vpnInstanceOpDataEntry);
122         }
123     }
124
125     private <T extends DataObject> Optional<T> read(LogicalDatastoreType datastoreType,
126             InstanceIdentifier<T> path) {
127         try (ReadTransaction tx = dataBroker.newReadOnlyTransaction()) {
128             return tx.read(datastoreType, path).get();
129         } catch (InterruptedException | ExecutionException e) {
130             throw new RuntimeException(e);
131         }
132     }
133
134     private String getshowVpnCLIHelp() {
135         return "\nUsage:"
136                 + "To display vpn-instance-op-data for given vpnInstanceName vpnInstanceOpData-show --detail "
137                 + "[<vpnInstanceName>]";
138     }
139 }