f9954043b0f33105a9acfe2b8db1ae9e68ab34ee
[vpnservice.git] / elanmanager / elanmanager-impl / src / main / java / org / opendaylight / vpnservice / 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.vpnservice.elan.cli.l2gw;
10
11 import java.util.concurrent.ConcurrentMap;
12
13 import org.apache.karaf.shell.commands.Command;
14 import org.apache.karaf.shell.commands.Option;
15 import org.apache.karaf.shell.console.OsgiCommandSupport;
16 import org.opendaylight.elanmanager.utils.ElanL2GwCacheUtils;
17 import org.opendaylight.vpnservice.neutronvpn.api.l2gw.L2GatewayDevice;
18 import org.opendaylight.vpnservice.neutronvpn.api.l2gw.utils.L2GatewayCacheUtils;
19 import org.opendaylight.vpnservice.utils.cache.CacheUtil;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 @Command(scope = "l2gw", name = "show-cache", description = "display l2gateways cache")
24 public class L2GwUtilsCacheCli extends OsgiCommandSupport {
25     private static final Logger logger = LoggerFactory.getLogger(L2GwUtilsCacheCli.class);
26
27     private static final String DEMARCATION = "=================================";
28
29     @Option(name = "-cache", aliases = {"--cache"}, description = "cache name",
30             required = false, multiValued = false)
31     String cacheName = null;
32
33     @Option(name = "-elan", aliases = {"--elan"}, description = "elan name",
34             required = false, multiValued = false)
35     String elanName;
36
37     @Override
38     protected Object doExecute() {
39         try {
40             if (cacheName == null) {
41                 System.out.println("Available caches");
42                 System.out.println(ElanL2GwCacheUtils.L2GATEWAY_CONN_CACHE_NAME);
43                 System.out.println(L2GatewayCacheUtils.L2GATEWAY_CACHE_NAME);
44                 return null;
45             }
46             switch (cacheName) {
47             case ElanL2GwCacheUtils.L2GATEWAY_CONN_CACHE_NAME:
48                 dumpElanL2GwCache();
49                 break;
50             case L2GatewayCacheUtils.L2GATEWAY_CACHE_NAME:
51                 dumpL2GwCache();
52                 break;
53             }
54         } catch (Exception e) {
55         }
56
57         return null;
58     }
59
60     private void dumpL2GwCache() {
61         ConcurrentMap<String, L2GatewayDevice> devices = (ConcurrentMap<String, L2GatewayDevice>) CacheUtil
62                 .getCache(L2GatewayCacheUtils.L2GATEWAY_CACHE_NAME);
63         if (devices == null) {
64             System.out.println("no devices are present in cache");
65             return;
66         }
67         for (String deviceName : devices.keySet()) {
68             System.out.println("device "+ devices.get(deviceName));
69         }
70     }
71
72     private void dumpElanL2GwCache() {
73         if (elanName == null) {
74             ConcurrentMap<String, ConcurrentMap<String, L2GatewayDevice>> cache =
75                     (ConcurrentMap<String, ConcurrentMap<String, L2GatewayDevice>>) CacheUtil.getCache(
76                             cacheName);
77             if (cache == null) {
78                 System.out.println("no devices are present in elan cache");
79             }
80             for (String elan : cache.keySet()) {
81                 print(elan, cache.get(elan));
82                 System.out.println(DEMARCATION);
83                 System.out.println(DEMARCATION);
84             }
85             return;
86         }
87         ConcurrentMap<String, L2GatewayDevice> elanDevices = ElanL2GwCacheUtils
88                 .getAllElanL2GatewayDevicesFromCache(elanName);
89         print(elanName, elanDevices);
90     }
91
92     private void print(String elan,
93             ConcurrentMap<String, L2GatewayDevice> devices) {
94         System.out.println("Elan name : "+elan);
95         System.out.println("No of devices in elan "+devices.keySet().size());
96         for (String deviceName : devices.keySet()) {
97             System.out.println("device "+ devices.get(deviceName));
98         }
99     }
100 }