| stevenvanrossem | 73efd19 | 2016-06-29 01:44:07 +0200 | [diff] [blame] | 1 | from requests import get,put, delete |
| 2 | from tabulate import tabulate |
| 3 | import pprint |
| 4 | import argparse |
| 5 | import json |
| 6 | |
| 7 | pp = pprint.PrettyPrinter(indent=4) |
| 8 | |
| 9 | class RestApiClient(): |
| 10 | |
| 11 | def __init__(self): |
| 12 | self.cmds = {} |
| 13 | |
| 14 | def execute_command(self, args): |
| 15 | if getattr(self, args["command"]) is not None: |
| 16 | # call the local method with the same name as the command arg |
| 17 | getattr(self, args["command"])(args) |
| 18 | else: |
| 19 | print("Command not implemented.") |
| 20 | |
| 21 | def add(self, args): |
| 22 | vnf_src_name = self._parse_vnf_name(args.get("source")) |
| 23 | vnf_dst_name = self._parse_vnf_name(args.get("destination")) |
| 24 | |
| 25 | params = self._create_dict( |
| 26 | vnf_src_interface=self._parse_vnf_interface(args.get("source")), |
| 27 | vnf_dst_interface=self._parse_vnf_interface(args.get("destination")), |
| 28 | weight=args.get("weight"), |
| 29 | match=args.get("match"), |
| 30 | bidirectional=args.get("bidirectional"), |
| 31 | cookie=args.get("cookie")) |
| 32 | |
| 33 | response = put("%s/restapi/network/%s/%s" % |
| 34 | (args.get("endpoint"), |
| 35 | vnf_src_name, |
| 36 | vnf_dst_name), |
| 37 | json=json.dumps(params)) |
| 38 | pp.pprint(response.json()) |
| 39 | |
| 40 | def remove(self, args): |
| 41 | vnf_src_name = self._parse_vnf_name(args.get("source")) |
| 42 | vnf_dst_name = self._parse_vnf_name(args.get("destination")) |
| 43 | |
| 44 | params = self._create_dict( |
| 45 | vnf_src_interface=self._parse_vnf_interface(args.get("source")), |
| 46 | vnf_dst_interface=self._parse_vnf_interface(args.get("destination")), |
| 47 | weight=args.get("weight"), |
| 48 | match=args.get("match"), |
| 49 | bidirectional=args.get("bidirectional"), |
| 50 | cookie=args.get("cookie")) |
| 51 | |
| 52 | response = delete("%s/restapi/network/%s/%s" % |
| 53 | (args.get("endpoint"), |
| 54 | vnf_src_name, |
| 55 | vnf_dst_name), |
| 56 | json=json.dumps(params)) |
| 57 | pp.pprint(response.json()) |
| 58 | |
| 59 | def _parse_vnf_name(self, vnf_name_str): |
| 60 | vnf_name = vnf_name_str.split(':')[0] |
| 61 | return vnf_name |
| 62 | |
| 63 | def _parse_vnf_interface(self, vnf_name_str): |
| 64 | try: |
| 65 | vnf_interface = vnf_name_str.split(':')[1] |
| 66 | except: |
| 67 | vnf_interface = None |
| 68 | |
| 69 | return vnf_interface |
| 70 | |
| 71 | def _create_dict(self, **kwargs): |
| 72 | return kwargs |
| 73 | |
| 74 | parser = argparse.ArgumentParser(description='son-emu network') |
| 75 | parser.add_argument( |
| 76 | "command", |
| 77 | choices=['add', 'remove'], |
| 78 | help="Action to be executed.") |
| 79 | parser.add_argument( |
| 80 | "--datacenter", "-d", dest="datacenter", |
| 81 | help="Data center to in which the network action should be initiated") |
| 82 | parser.add_argument( |
| 83 | "--source", "-src", dest="source", |
| 84 | help="vnf name of the source of the chain") |
| 85 | parser.add_argument( |
| 86 | "--destination", "-dst", dest="destination", |
| 87 | help="vnf name of the destination of the chain") |
| 88 | parser.add_argument( |
| 89 | "--weight", "-w", dest="weight", |
| 90 | help="weight metric to calculate the path") |
| 91 | parser.add_argument( |
| 92 | "--match", "-m", dest="match", |
| 93 | help="string holding extra matches for the flow entries") |
| 94 | parser.add_argument( |
| 95 | "--bidirectional", "-b", dest="bidirectional", |
| 96 | action='store_true', |
| 97 | help="add/remove the flow entries from src to dst and back") |
| 98 | parser.add_argument( |
| 99 | "--cookie", "-c", dest="cookie", |
| 100 | help="cookie for this flow, as easy to use identifier (eg. per tenant/service)") |
| 101 | parser.add_argument( |
| 102 | "--endpoint", "-e", dest="endpoint", |
| peusterm | 0a336cc | 2016-07-04 09:15:47 +0200 | [diff] [blame] | 103 | default="http://127.0.0.1:5001", |
| stevenvanrossem | 73efd19 | 2016-06-29 01:44:07 +0200 | [diff] [blame] | 104 | help="UUID of the plugin to be manipulated.") |
| 105 | |
| 106 | def main(argv): |
| 107 | args = vars(parser.parse_args(argv)) |
| 108 | c = RestApiClient() |
| 109 | c.execute_command(args) |