6b035924dff9c9c831221ad82d56caea13424f90
[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
9 package org.opendaylight.netvirt.elan.cli.l2gw;
10
11 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
12 import java.io.File;
13 import java.io.FileOutputStream;
14 import java.io.IOException;
15 import java.io.PrintStream;
16 import java.util.Collection;
17 import java.util.Map;
18 import java.util.Map.Entry;
19 import java.util.concurrent.ConcurrentMap;
20 import javax.annotation.Nullable;
21 import org.apache.karaf.shell.commands.Command;
22 import org.apache.karaf.shell.commands.Option;
23 import org.apache.karaf.shell.console.OsgiCommandSupport;
24 import org.opendaylight.genius.utils.cache.CacheUtil;
25 import org.opendaylight.genius.utils.hwvtep.HwvtepNodeHACache;
26 import org.opendaylight.netvirt.elanmanager.utils.ElanL2GwCacheUtils;
27 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayCache;
28 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayDevice;
29 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31
32 @Command(scope = "l2gw", name = "show-cache", description = "display l2gateways cache")
33 public class L2GwUtilsCacheCli extends OsgiCommandSupport {
34     private static final String L2GATEWAY_CACHE_NAME = "L2GW";
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(ElanL2GwCacheUtils.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 ElanL2GwCacheUtils.L2GATEWAY_CONN_CACHE_NAME:
66                 dumpElanL2GwCache();
67                 break;
68             case L2GATEWAY_CACHE_NAME:
69                 dumpL2GwCache();
70                 break;
71             case "HA":
72                 dumpHACache(session.getConsole());
73                 break;
74             case "HA_EVENTS":
75                 dumpHACacheEvents();
76                 break;
77             default:
78                 break;
79         }
80         return null;
81     }
82
83     @SuppressFBWarnings("DM_DEFAULT_ENCODING")
84     private void dumpHACacheEvents() throws IOException {
85         try (FileOutputStream fileOutputStream = new FileOutputStream(new File("hwvtep.events.txt"))) {
86             session.getConsole().println("Dumping to file hwvtep.events.txt");
87             PrintStream fos = new PrintStream(fileOutputStream);
88             dumpHACache(fos);
89             fos.close();
90             session.getConsole().println("Dumped to file " + new File("hwvtep.events.txt").getAbsolutePath());
91         }
92     }
93
94     private void dumpHACache(PrintStream printStream) {
95
96         printStream.println("HA enabled nodes");
97         for (InstanceIdentifier<Node> id : hwvtepNodeHACache.getHAChildNodes()) {
98             String nodeId = id.firstKeyOf(Node.class).getNodeId().getValue();
99             printStream.println(nodeId);
100         }
101
102         printStream.println("HA parent nodes");
103         for (InstanceIdentifier<Node> id : hwvtepNodeHACache.getHAParentNodes()) {
104             String nodeId = id.firstKeyOf(Node.class).getNodeId().getValue();
105             printStream.println(nodeId);
106             for (InstanceIdentifier<Node> childId : hwvtepNodeHACache.getChildrenForHANode(id)) {
107                 nodeId = childId.firstKeyOf(Node.class).getNodeId().getValue();
108                 printStream.println("    " + nodeId);
109             }
110         }
111
112         printStream.println("Connected Nodes");
113         Map<String, Boolean> nodes = hwvtepNodeHACache.getNodeConnectionStatuses();
114         for (Entry<String, Boolean> entry : nodes.entrySet()) {
115             printStream.print(entry.getKey());
116             printStream.print("    : connected : ");
117             printStream.println(entry.getValue());
118         }
119     }
120
121     private void dumpL2GwCache() {
122         Collection<L2GatewayDevice> devices = l2GatewayCache.getAll();
123         if (devices.isEmpty()) {
124             session.getConsole().println("no devices are present in cache");
125             return;
126         }
127         for (L2GatewayDevice device : devices) {
128             session.getConsole().println("device " + device);
129         }
130     }
131
132     private void dumpElanL2GwCache() {
133         if (elanName == null) {
134             ConcurrentMap<String, ConcurrentMap<String, L2GatewayDevice>> cache =
135                     (ConcurrentMap<String, ConcurrentMap<String, L2GatewayDevice>>) CacheUtil.getCache(
136                             cacheName);
137             if (cache == null) {
138                 session.getConsole().println("no devices are present in elan cache");
139             } else {
140                 for (Entry<String, ConcurrentMap<String, L2GatewayDevice>> entry : cache.entrySet()) {
141                     print(entry.getKey(), entry.getValue());
142                     session.getConsole().println(DEMARCATION);
143                     session.getConsole().println(DEMARCATION);
144                 }
145             }
146             return;
147         }
148         ConcurrentMap<String, L2GatewayDevice> elanDevices = ElanL2GwCacheUtils
149                 .getInvolvedL2GwDevices(elanName);
150         print(elanName, elanDevices);
151     }
152
153     private void print(String elan, ConcurrentMap<String, L2GatewayDevice> devices) {
154         session.getConsole().println("Elan name : " + elan);
155         session.getConsole().println("No of devices in elan " + devices.keySet().size());
156         for (L2GatewayDevice device : devices.values()) {
157             session.getConsole().println("device " + device);
158         }
159     }
160 }