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;
11 import static org.opendaylight.openflowplugin.applications.southboundcli.util.ShellUtil.LINE_SEPARATOR;
13 import java.time.LocalDateTime;
14 import java.time.ZoneOffset;
15 import java.util.Collection;
16 import java.util.Formatter;
18 import java.util.Map.Entry;
19 import org.apache.karaf.shell.api.action.Action;
20 import org.apache.karaf.shell.api.action.Command;
21 import org.apache.karaf.shell.api.action.Option;
22 import org.apache.karaf.shell.api.action.lifecycle.Reference;
23 import org.apache.karaf.shell.api.action.lifecycle.Service;
24 import org.apache.karaf.shell.api.console.Session;
25 import org.opendaylight.openflowplugin.api.openflow.FlowGroupInfo;
26 import org.opendaylight.openflowplugin.api.openflow.FlowGroupInfoHistories;
27 import org.opendaylight.openflowplugin.api.openflow.FlowGroupInfoHistory;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
31 @Command(scope = "openflow", name = "getflownodecache", description = "Print all flow/group cache")
32 public final class GetFlowGroupCacheCommand implements Action {
33 @Option(name = "-d", description = "Node Id")
39 FlowGroupInfoHistories histories;
42 public Object execute() {
43 if (histories == null) {
52 final String nodeId = OPENFLOW_PREFIX + dpnId;
53 final FlowGroupInfoHistory history = histories.getFlowGroupHistory(new NodeId(nodeId));
54 if (history == null) {
55 session.getConsole().println("No node available for this NodeID");
58 final Collection<FlowGroupInfo> entries = history.readEntries();
59 if (entries.isEmpty()) {
60 session.getConsole().println("No flow/group is programmed yet for the the node " + nodeId);
64 StringBuilder sb = new StringBuilder();
65 Formatter fmt = new Formatter(sb);
66 session.getConsole().println(String.format("Number of flows and groups in cache for node %s : %d", nodeId,
68 session.getConsole().println(getLocalNodeHeaderOutput());
69 session.getConsole().println(LINE_SEPARATOR);
71 for (FlowGroupInfo entry : entries) {
72 session.getConsole().println(fmt.format("%-10s %1s %-8s %1s %-23s %1s %-60s", entry.getDescription(), "",
73 entry.getStatus(), "", getTime(entry), "", entry.getId()));
80 private static LocalDateTime getTime(final FlowGroupInfo info) {
81 return LocalDateTime.ofInstant(info.getInstantUTC(), ZoneOffset.UTC);
84 @SuppressWarnings("checkstyle:RegexpSinglelineJava")
85 private void printAllNodes() {
86 final Map<NodeId, FlowGroupInfoHistory> allHistories = histories.getAllFlowGroupHistories();
87 if (allHistories.isEmpty()) {
88 session.getConsole().println("No flow/group is programmed yet");
92 StringBuilder sb = new StringBuilder();
93 Formatter fmt = new Formatter(sb);
94 session.getConsole().println(getAllLocalNodesHeaderOutput());
95 session.getConsole().println(LINE_SEPARATOR);
96 for (Entry<NodeId, FlowGroupInfoHistory> entry : allHistories.entrySet()) {
97 // FIXME: just seek/substring
98 String[] temp = entry.getKey().getValue().split(":");
99 String node = temp[1];
100 for (FlowGroupInfo info : entry.getValue().readEntries()) {
101 session.getConsole().println(fmt.format("%-15s %1s %-10s %1s %-8s %1s %-21s %1s %-60s", node, "",
102 info.getDescription(), "", info.getStatus(), "", getTime(info), "", info.getId()).toString());
109 private static String getLocalNodeHeaderOutput() {
110 Formatter formatter = new Formatter();
111 String header = formatter.format("%-10s %1s %-8s %1s %-23s %1s %-60s",
112 "TableId", "", "Status", "", "Time", "", "Flow/Group Id").toString();
117 private static String getAllLocalNodesHeaderOutput() {
118 Formatter formatter = new Formatter();
119 String header = formatter.format("%-15s %1s %-10s %1s %-8s %1s %-23s %1s %-60s",
120 "DpnId", "", "TableId", "", "Status", "", "Time", "", "Flow/Group Id").toString();