Remove use of org.opendaylight.genius.utils.cache.CacheUtil
[netvirt.git] / elanmanager / impl / src / main / java / org / opendaylight / netvirt / elan / cli / l2gw / L2GwUtilsCacheCli.java
1 /*
2  * Copyright (c) 2016 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.elan.cli.l2gw;
9
10 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
11 import java.io.File;
12 import java.io.FileOutputStream;
13 import java.io.IOException;
14 import java.io.PrintStream;
15 import java.util.Collection;
16 import java.util.Map;
17 import java.util.Map.Entry;
18 import java.util.concurrent.ConcurrentMap;
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.genius.utils.hwvtep.HwvtepNodeHACache;
24 import org.opendaylight.netvirt.elanmanager.utils.ElanL2GwCacheUtils;
25 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayCache;
26 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayDevice;
27 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
28 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
29
30 @Command(scope = "l2gw", name = "show-cache", description = "display l2gateways cache")
31 public class L2GwUtilsCacheCli extends OsgiCommandSupport {
32     private static final String L2GATEWAY_CACHE_NAME = "L2GW";
33     private static final String L2GATEWAY_CONN_CACHE_NAME = "L2GWCONN";
34
35     private static final String DEMARCATION = "=================================";
36
37     @Option(name = "-cache", aliases = {"--cache"}, description = "cache name",
38             required = false, multiValued = false)
39     String cacheName;
40
41     @Option(name = "-elan", aliases = {"--elan"}, description = "elan name",
42             required = false, multiValued = false)
43     String elanName;
44
45     private final L2GatewayCache l2GatewayCache;
46     private final HwvtepNodeHACache hwvtepNodeHACache;
47
48     public L2GwUtilsCacheCli(L2GatewayCache l2GatewayCache, HwvtepNodeHACache hwvtepNodeHACache) {
49         this.l2GatewayCache = l2GatewayCache;
50         this.hwvtepNodeHACache = hwvtepNodeHACache;
51     }
52
53     @Override
54     @Nullable
55     protected Object doExecute() throws IOException {
56         if (cacheName == null) {
57             session.getConsole().println("Available caches");
58             session.getConsole().println(L2GATEWAY_CONN_CACHE_NAME);
59             session.getConsole().println(L2GATEWAY_CACHE_NAME);
60             session.getConsole().println("HA");
61             session.getConsole().println("HA_EVENTS");
62             return null;
63         }
64         switch (cacheName) {
65             case L2GATEWAY_CONN_CACHE_NAME:
66                 if (elanName == null) {
67                     for (Entry<String, ConcurrentMap<String, L2GatewayDevice>> entry : ElanL2GwCacheUtils.getCaches()) {
68                         print(entry.getKey(), entry.getValue().values());
69                         session.getConsole().println(DEMARCATION);
70                         session.getConsole().println(DEMARCATION);
71                     }
72                 } else {
73                     print(elanName, ElanL2GwCacheUtils.getInvolvedL2GwDevices(elanName).values());
74                 }
75                 break;
76             case L2GATEWAY_CACHE_NAME:
77                 dumpL2GwCache();
78                 break;
79             case "HA":
80                 dumpHACache(session.getConsole());
81                 break;
82             case "HA_EVENTS":
83                 dumpHACacheEvents();
84                 break;
85             default:
86                 break;
87         }
88         return null;
89     }
90
91     @SuppressFBWarnings("DM_DEFAULT_ENCODING")
92     private void dumpHACacheEvents() throws IOException {
93         try (FileOutputStream fileOutputStream = new FileOutputStream(new File("hwvtep.events.txt"))) {
94             session.getConsole().println("Dumping to file hwvtep.events.txt");
95             PrintStream fos = new PrintStream(fileOutputStream);
96             dumpHACache(fos);
97             fos.close();
98             session.getConsole().println("Dumped to file " + new File("hwvtep.events.txt").getAbsolutePath());
99         }
100     }
101
102     private void dumpHACache(PrintStream printStream) {
103
104         printStream.println("HA enabled nodes");
105         for (InstanceIdentifier<Node> id : hwvtepNodeHACache.getHAChildNodes()) {
106             String nodeId = id.firstKeyOf(Node.class).getNodeId().getValue();
107             printStream.println(nodeId);
108         }
109
110         printStream.println("HA parent nodes");
111         for (InstanceIdentifier<Node> id : hwvtepNodeHACache.getHAParentNodes()) {
112             String nodeId = id.firstKeyOf(Node.class).getNodeId().getValue();
113             printStream.println(nodeId);
114             for (InstanceIdentifier<Node> childId : hwvtepNodeHACache.getChildrenForHANode(id)) {
115                 nodeId = childId.firstKeyOf(Node.class).getNodeId().getValue();
116                 printStream.println("    " + nodeId);
117             }
118         }
119
120         printStream.println("Connected Nodes");
121         Map<String, Boolean> nodes = hwvtepNodeHACache.getNodeConnectionStatuses();
122         for (Entry<String, Boolean> entry : nodes.entrySet()) {
123             printStream.print(entry.getKey());
124             printStream.print("    : connected : ");
125             printStream.println(entry.getValue());
126         }
127     }
128
129     private void dumpL2GwCache() {
130         Collection<L2GatewayDevice> devices = l2GatewayCache.getAll();
131         if (devices.isEmpty()) {
132             session.getConsole().println("no devices are present in cache");
133             return;
134         }
135         for (L2GatewayDevice device : devices) {
136             session.getConsole().println("device " + device);
137         }
138     }
139
140     private void print(String elan, Collection<L2GatewayDevice> devices) {
141         session.getConsole().println("Elan name : " + elan);
142         session.getConsole().println("No of devices in elan " + devices.size());
143         for (L2GatewayDevice device : devices) {
144             session.getConsole().println("device " + device);
145         }
146     }
147 }