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