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 <[email protected]> --- 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(-) diff --git a/python/ovs/ovs_ofparse/datapath.py b/python/ovs/ovs_ofparse/datapath.py index c5bb43fa9..35c278c50 100644 --- a/python/ovs/ovs_ofparse/datapath.py +++ b/python/ovs/ovs_ofparse/datapath.py @@ -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 + + [email protected]() [email protected]_obj +def json(opts): + """Print the flows in JSON format""" + proc = JSONProcessor(opts, factory) + proc.process() + print(proc.json_string()) diff --git a/python/ovs/ovs_ofparse/openflow.py b/python/ovs/ovs_ofparse/openflow.py index 0035c8118..178c76a63 100644 --- a/python/ovs/ovs_ofparse/openflow.py +++ b/python/ovs/ovs_ofparse/openflow.py @@ -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 + + [email protected]() [email protected]_obj +def json(opts): + """Print the flows in JSON format""" + proc = JSONProcessor(opts, factory) + proc.process() + print(proc.json_string()) diff --git a/python/ovs/ovs_ofparse/process.py b/python/ovs/ovs_ofparse/process.py index 660dd7243..c01c4b510 100644 --- a/python/ovs/ovs_ofparse/process.py +++ b/python/ovs/ovs_ofparse/process.py @@ -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, + ) -- 2.31.1 _______________________________________________ dev mailing list [email protected] https://mail.openvswitch.org/mailman/listinfo/ovs-dev
