diff mbox series

[ovs-dev,RFC,09/10] python/ovs-ofparse: add openflow html format

Message ID 20211122150157.2029096-10-amorenoz@redhat.com
State RFC
Headers show
Series Introduce ovs-ofparse utility | expand

Commit Message

Adrian Moreno Nov. 22, 2021, 3:01 p.m. UTC
Create a flow table that uses a hyperlink from resubmit() to the target
table

Signed-off-by: Adrian Moreno <amorenoz@redhat.com>
---
 python/ovs/ovs_ofparse/openflow.py | 70 ++++++++++++++++++++++++++++++
 1 file changed, 70 insertions(+)
diff mbox series

Patch

diff --git a/python/ovs/ovs_ofparse/openflow.py b/python/ovs/ovs_ofparse/openflow.py
index 766d3ad41..bb0cf4019 100644
--- a/python/ovs/ovs_ofparse/openflow.py
+++ b/python/ovs/ovs_ofparse/openflow.py
@@ -5,9 +5,11 @@  from ovs.flows.ofp import OFPFlowFactory
 from ovs.ovs_ofparse.ofp_logic import LogicFlowProcessor
 from ovs.ovs_ofparse.main import maincli
 from ovs.ovs_ofparse.process import (
+    FlowProcessor,
     JSONProcessor,
     ConsoleProcessor,
 )
+from ovs.ovs_ofparse.html import HTMLBuffer, HTMLFormatter, HTMLStyle
 
 
 factory = OFPFlowFactory()
@@ -90,3 +92,71 @@  def logic(opts, show_flows, cookie_flag, heat_map):
     processor = LogicFlowProcessor(opts, factory, cookie_flag)
     processor.process()
     processor.print(show_flows, heat_map)
+
+
+class HTMLProcessor(FlowProcessor):
+    def __init__(self, opts, factory):
+        super().__init__(opts, factory)
+        self.data = dict()
+
+    def start_file(self, name, filename):
+        self.tables = dict()
+
+    def stop_file(self, name, filename):
+        self.data[name] = self.tables
+
+    def process_flow(self, flow, name):
+        table = flow.info.get("table") or 0
+        if not self.tables.get(table):
+            self.tables[table] = list()
+        self.tables[table].append(flow)
+
+    def html(self):
+        html_obj = ""
+        for name, tables in self.data.items():
+            name = name.replace(" ", "_")
+            html_obj += "<h1>{}</h1>".format(name)
+            html_obj += "<div id=flow_list>"
+            for table, flows in tables.items():
+                formatter = HTMLFormatter(self.opts)
+
+                def anchor(x):
+                    return "#table_%s_%s" % (name, x.value["table"])
+
+                formatter.style.set_value_style(
+                    "resubmit",
+                    HTMLStyle(
+                        formatter.style.get("value.resubmit"),
+                        anchor_gen=anchor,
+                    ),
+                )
+                html_obj += (
+                    "<h2 id=table_{name}_{table}> Table {table}</h2>".format(
+                        name=name, table=table
+                    )
+                )
+                html_obj += "<ul id=table_{}_flow_list>".format(table)
+                for flow in flows:
+                    html_obj += "<li id=flow_{}>".format(flow.id)
+                    highlighted = None
+                    if self.opts.get("highlight"):
+                        result = self.opts.get("highlight").evaluate(flow)
+                        if result:
+                            highlighted = result.kv
+                    buf = HTMLBuffer()
+                    formatter.format_flow(buf, flow, highlighted)
+                    html_obj += buf.text
+                    html_obj += "</li>"
+                html_obj += "</ul>"
+            html_obj += "</div>"
+
+        return html_obj
+
+
+@openflow.command()
+@click.pass_obj
+def html(opts):
+    """Print the flows in an HTML list"""
+    processor = HTMLProcessor(opts, factory)
+    processor.process()
+    print(processor.html())