2 * Copyright (c) 2020 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
8 package org.opendaylight.openflowplugin.applications.southboundcli.cli;
10 import static org.opendaylight.openflowplugin.applications.frm.util.FrmUtil.OPENFLOW_PREFIX;
12 import java.time.LocalDateTime;
13 import java.time.ZoneOffset;
14 import java.util.Collection;
15 import java.util.Formatter;
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;
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 + "------------------------------";
33 @Option(name = "-d", description = "Node Id")
36 private final FlowGroupInfoHistories histories;
38 public GetFlowGroupCacheProvider(final FlowGroupInfoHistories histories) {
39 this.histories = histories;
43 @SuppressWarnings("checkstyle:RegexpSinglelineJava")
44 protected Object doExecute() {
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");
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);
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,
66 System.out.println(getLocalNodeHeaderOutput());
67 System.out.println(LINE_SEPARATOR);
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());
78 private static LocalDateTime getTime(final FlowGroupInfo info) {
79 return LocalDateTime.ofInstant(info.getInstantUTC(), ZoneOffset.UTC);
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");
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());
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();
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();