@@ -1,10 +1,25 @@
import click
-
from ovs.ovs_ofparse.main import maincli
+from ovs.flows.odp import ODPFlowFactory
+from ovs.ovs_ofparse.process import (
+ JSONProcessor,
+)
+
+factory = ODPFlowFactory()
+
@maincli.group(subcommand_metavar="FORMAT")
@click.pass_obj
def datapath(opts):
"""Process DPIF Flows"""
pass
+
+
+@datapath.command()
+@click.pass_obj
+def json(opts):
+ """Print the flows in JSON format"""
+ proc = JSONProcessor(opts, factory)
+ proc.process()
+ print(proc.json_string())
@@ -1,7 +1,14 @@
import click
+from ovs.flows.ofp import OFPFlowFactory
from ovs.ovs_ofparse.main import maincli
+from ovs.ovs_ofparse.process import (
+ JSONProcessor,
+)
+
+
+factory = OFPFlowFactory()
@maincli.group(subcommand_metavar="FORMAT")
@@ -9,3 +16,12 @@ from ovs.ovs_ofparse.main import maincli
def openflow(opts):
"""Process OpenFlow Flows"""
pass
+
+
+@openflow.command()
+@click.pass_obj
+def json(opts):
+ """Print the flows in JSON format"""
+ proc = JSONProcessor(opts, factory)
+ proc.process()
+ print(proc.json_string())
@@ -1,8 +1,11 @@
""" Defines common flow processing functionality
"""
import sys
+import json
import click
+from ovs.flows.decoders import FlowEncoder
+
class FlowProcessor(object):
"""Base class for file-based Flow processing. It is able to create flows
@@ -111,3 +114,36 @@ class FlowProcessor(object):
self.process_flow(flow, alias)
self.stop_file("stdin", "stdin")
self.end()
+
+
+class JSONProcessor(FlowProcessor):
+ """A generic JsonProcessor"""
+
+ def __init__(self, opts, factory):
+ super().__init__(opts, factory)
+ self.flows = dict()
+
+ def start_file(self, name, filename):
+ self.flows_list = list()
+
+ def stop_file(self, name, filename):
+ self.flows[name] = self.flows_list
+
+ def process_flow(self, flow, name):
+ self.flows_list.append(flow)
+
+ def json_string(self):
+ if len(self.flows.keys()) > 1:
+ return json.dumps(
+ [
+ {"name": name, "flows": [flow.dict() for flow in flows]}
+ for name, flows in self.flows.items()
+ ],
+ indent=4,
+ cls=FlowEncoder,
+ )
+ return json.dumps(
+ [flow.dict() for flow in self.flows_list],
+ indent=4,
+ cls=FlowEncoder,
+ )
Add a FlowProcessor that is able to return the flows in json format and a subcommand to both "datapath" and "openflow" to print flows in that format Signed-off-by: Adrian Moreno <amorenoz@redhat.com> --- python/ovs/ovs_ofparse/datapath.py | 17 +++++++++++++- python/ovs/ovs_ofparse/openflow.py | 16 +++++++++++++ python/ovs/ovs_ofparse/process.py | 36 ++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 1 deletion(-)