Eliminate blueprint for southbound-cli commands
[openflowplugin.git] / applications / southbound-cli / src / main / java / org / opendaylight / openflowplugin / applications / southboundcli / cli / GetFlowGroupCacheCommand.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 import static org.opendaylight.openflowplugin.applications.southboundcli.util.ShellUtil.LINE_SEPARATOR;
12
13 import java.time.LocalDateTime;
14 import java.time.ZoneOffset;
15 import java.util.Collection;
16 import java.util.Formatter;
17 import java.util.Map;
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;
29
30 @Service
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")
34     String dpnId;
35
36     @Reference
37     Session session;
38     @Reference
39     FlowGroupInfoHistories histories;
40
41     @Override
42     public Object execute() {
43         if (histories == null) {
44             // not initialized
45             return null;
46         }
47         if (dpnId == null) {
48             printAllNodes();
49             return null;
50         }
51
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");
56             return null;
57         }
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);
61             return null;
62         }
63
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,
67             entries.size()));
68         session.getConsole().println(getLocalNodeHeaderOutput());
69         session.getConsole().println(LINE_SEPARATOR);
70
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()));
74             sb.setLength(0);
75         }
76         fmt.close();
77         return null;
78     }
79
80     private static LocalDateTime getTime(final FlowGroupInfo info) {
81         return LocalDateTime.ofInstant(info.getInstantUTC(), ZoneOffset.UTC);
82     }
83
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");
89             return;
90         }
91
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());
103                 sb.setLength(0);
104             }
105         }
106         fmt.close();
107     }
108
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();
113         formatter.close();
114         return header;
115     }
116
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();
121         formatter.close();
122         return header;
123     }
124 }