2 * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
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
9 package org.opendaylight.netvirt.elan.cli.l2gw;
11 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
13 import java.io.FileOutputStream;
14 import java.io.IOException;
15 import java.io.PrintStream;
16 import java.util.Collection;
18 import java.util.Map.Entry;
19 import java.util.concurrent.ConcurrentMap;
20 import org.apache.karaf.shell.commands.Command;
21 import org.apache.karaf.shell.commands.Option;
22 import org.apache.karaf.shell.console.OsgiCommandSupport;
23 import org.opendaylight.genius.utils.cache.CacheUtil;
24 import org.opendaylight.genius.utils.hwvtep.HwvtepNodeHACache;
25 import org.opendaylight.netvirt.elanmanager.utils.ElanL2GwCacheUtils;
26 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayCache;
27 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayDevice;
28 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 @Command(scope = "l2gw", name = "show-cache", description = "display l2gateways cache")
32 public class L2GwUtilsCacheCli extends OsgiCommandSupport {
33 private static final String L2GATEWAY_CACHE_NAME = "L2GW";
34 private static final String DEMARCATION = "=================================";
36 @Option(name = "-cache", aliases = {"--cache"}, description = "cache name",
37 required = false, multiValued = false)
40 @Option(name = "-elan", aliases = {"--elan"}, description = "elan name",
41 required = false, multiValued = false)
44 private final L2GatewayCache l2GatewayCache;
45 private final HwvtepNodeHACache hwvtepNodeHACache;
47 public L2GwUtilsCacheCli(L2GatewayCache l2GatewayCache, HwvtepNodeHACache hwvtepNodeHACache) {
48 this.l2GatewayCache = l2GatewayCache;
49 this.hwvtepNodeHACache = hwvtepNodeHACache;
53 protected Object doExecute() throws IOException {
54 if (cacheName == null) {
55 session.getConsole().println("Available caches");
56 session.getConsole().println(ElanL2GwCacheUtils.L2GATEWAY_CONN_CACHE_NAME);
57 session.getConsole().println(L2GATEWAY_CACHE_NAME);
58 session.getConsole().println("HA");
59 session.getConsole().println("HA_EVENTS");
63 case ElanL2GwCacheUtils.L2GATEWAY_CONN_CACHE_NAME:
66 case L2GATEWAY_CACHE_NAME:
70 dumpHACache(session.getConsole());
81 @SuppressFBWarnings("DM_DEFAULT_ENCODING")
82 private void dumpHACacheEvents() throws IOException {
83 try (FileOutputStream fileOutputStream = new FileOutputStream(new File("hwvtep.events.txt"))) {
84 session.getConsole().println("Dumping to file hwvtep.events.txt");
85 PrintStream fos = new PrintStream(fileOutputStream);
88 session.getConsole().println("Dumped to file " + new File("hwvtep.events.txt").getAbsolutePath());
92 private void dumpHACache(PrintStream printStream) {
94 printStream.println("HA enabled nodes");
95 for (InstanceIdentifier<Node> id : hwvtepNodeHACache.getHAChildNodes()) {
96 String nodeId = id.firstKeyOf(Node.class).getNodeId().getValue();
97 printStream.println(nodeId);
100 printStream.println("HA parent nodes");
101 for (InstanceIdentifier<Node> id : hwvtepNodeHACache.getHAParentNodes()) {
102 String nodeId = id.firstKeyOf(Node.class).getNodeId().getValue();
103 printStream.println(nodeId);
104 for (InstanceIdentifier<Node> childId : hwvtepNodeHACache.getChildrenForHANode(id)) {
105 nodeId = childId.firstKeyOf(Node.class).getNodeId().getValue();
106 printStream.println(" " + nodeId);
110 printStream.println("Connected Nodes");
111 Map<String, Boolean> nodes = hwvtepNodeHACache.getNodeConnectionStatuses();
112 for (Entry<String, Boolean> entry : nodes.entrySet()) {
113 printStream.print(entry.getKey());
114 printStream.print(" : connected : ");
115 printStream.println(entry.getValue());
119 private void dumpL2GwCache() {
120 Collection<L2GatewayDevice> devices = l2GatewayCache.getAll();
121 if (devices.isEmpty()) {
122 session.getConsole().println("no devices are present in cache");
125 for (L2GatewayDevice device : devices) {
126 session.getConsole().println("device " + device);
130 private void dumpElanL2GwCache() {
131 if (elanName == null) {
132 ConcurrentMap<String, ConcurrentMap<String, L2GatewayDevice>> cache =
133 (ConcurrentMap<String, ConcurrentMap<String, L2GatewayDevice>>) CacheUtil.getCache(
136 session.getConsole().println("no devices are present in elan cache");
138 for (Entry<String, ConcurrentMap<String, L2GatewayDevice>> entry : cache.entrySet()) {
139 print(entry.getKey(), entry.getValue());
140 session.getConsole().println(DEMARCATION);
141 session.getConsole().println(DEMARCATION);
146 ConcurrentMap<String, L2GatewayDevice> elanDevices = ElanL2GwCacheUtils
147 .getInvolvedL2GwDevices(elanName);
148 print(elanName, elanDevices);
151 private void print(String elan, ConcurrentMap<String, L2GatewayDevice> devices) {
152 session.getConsole().println("Elan name : " + elan);
153 session.getConsole().println("No of devices in elan " + devices.keySet().size());
154 for (L2GatewayDevice device : devices.values()) {
155 session.getConsole().println("device " + device);