6f443ef14a759801c5df38907f02f120c67e9f93
[openflowplugin.git] / applications / southbound-cli / src / main / java / org / opendaylight / openflowplugin / applications / southboundcli / cli / GetFlowGroupCacheProvider.java
1 /*
2  * Copyright (c) 2020 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.openflowplugin.applications.southboundcli.cli;
9
10 import static org.opendaylight.openflowplugin.applications.frm.util.FrmUtil.OPENFLOW_PREFIX;
11
12 import java.time.LocalDateTime;
13 import java.time.ZoneOffset;
14 import java.util.Collection;
15 import java.util.Formatter;
16 import java.util.Map;
17 import java.util.Map.Entry;
18 import org.apache.karaf.shell.commands.Command;
19 import org.apache.karaf.shell.commands.Option;
20 import org.apache.karaf.shell.console.OsgiCommandSupport;
21 import org.opendaylight.openflowplugin.api.openflow.FlowGroupInfo;
22 import org.opendaylight.openflowplugin.api.openflow.FlowGroupInfoHistories;
23 import org.opendaylight.openflowplugin.api.openflow.FlowGroupInfoHistory;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
25
26 @Command(scope = "openflow", name = "getflownodecache", description = "Print all flow/group cache")
27 public class GetFlowGroupCacheProvider extends OsgiCommandSupport {
28     // FIXME: use String.repeat(), this does look arbitrary
29     private static final String LINE_SEPARATOR =
30         "--------------------------------------------------------------------------------------------------------------"
31         + "------------------------------";
32
33     @Option(name = "-d", description = "Node Id")
34     String dpnId;
35
36     private final FlowGroupInfoHistories histories;
37
38     public GetFlowGroupCacheProvider(final FlowGroupInfoHistories histories) {
39         this.histories = histories;
40     }
41
42     @Override
43     @SuppressWarnings("checkstyle:RegexpSinglelineJava")
44     protected Object doExecute() {
45         if (dpnId == null) {
46             printAllNodes();
47             return null;
48         }
49
50         final String nodeId = OPENFLOW_PREFIX + dpnId;
51         final FlowGroupInfoHistory history = histories.getFlowGroupHistory(new NodeId(nodeId));
52         if (history == null) {
53             session.getConsole().println("No node available for this NodeID");
54             return null;
55         }
56         final Collection<FlowGroupInfo> entries = history.readEntries();
57         if (entries.isEmpty()) {
58             session.getConsole().println("No flow/group is programmed yet for the the node " + nodeId);
59             return null;
60         }
61
62         StringBuilder sb = new StringBuilder();
63         Formatter fmt = new Formatter(sb);
64         System.out.println(String.format("Number of flows and groups in cache for node %s : %d", nodeId,
65             entries.size()));
66         System.out.println(getLocalNodeHeaderOutput());
67         System.out.println(LINE_SEPARATOR);
68
69         for (FlowGroupInfo entry : entries) {
70             System.out.println(fmt.format("%-10s %1s %-8s %1s %-23s %1s %-60s", entry.getDescription(), "",
71                 entry.getStatus(), "", getTime(entry), "", entry.getId()).toString());
72             sb.setLength(0);
73         }
74         fmt.close();
75         return null;
76     }
77
78     private static LocalDateTime getTime(final FlowGroupInfo info) {
79         return LocalDateTime.ofInstant(info.getInstantUTC(), ZoneOffset.UTC);
80     }
81
82     @SuppressWarnings("checkstyle:RegexpSinglelineJava")
83     private void printAllNodes() {
84         final Map<NodeId, FlowGroupInfoHistory> allHistories = histories.getAllFlowGroupHistories();
85         if (allHistories.isEmpty()) {
86             session.getConsole().println("No flow/group is programmed yet");
87             return;
88         }
89
90         StringBuilder sb = new StringBuilder();
91         Formatter fmt = new Formatter(sb);
92         System.out.println(getAllLocalNodesHeaderOutput());
93         System.out.println(LINE_SEPARATOR);
94         for (Entry<NodeId, FlowGroupInfoHistory> entry : allHistories.entrySet()) {
95             // FIXME: just seek/substring
96             String[] temp = entry.getKey().getValue().split(":");
97             String node = temp[1];
98             for (FlowGroupInfo info : entry.getValue().readEntries()) {
99                 System.out.println(fmt.format("%-15s %1s %-10s %1s %-8s %1s %-21s %1s %-60s", node, "",
100                     info.getDescription(), "", info.getStatus(), "", getTime(info), "", info.getId()).toString());
101                 sb.setLength(0);
102             }
103         }
104         fmt.close();
105     }
106
107     private static String getLocalNodeHeaderOutput() {
108         Formatter formatter = new Formatter();
109         String header = formatter.format("%-10s %1s %-8s %1s %-23s %1s %-60s",
110                 "TableId", "", "Status", "", "Time", "", "Flow/Group Id").toString();
111         formatter.close();
112         return header;
113     }
114
115     private static String getAllLocalNodesHeaderOutput() {
116         Formatter formatter = new Formatter();
117         String header = formatter.format("%-15s %1s %-10s %1s %-8s %1s %-23s %1s %-60s",
118                 "DpnId", "", "TableId", "", "Status", "", "Time", "", "Flow/Group Id").toString();
119         formatter.close();
120         return header;
121     }
122 }