| peusterm | 79ef6ae | 2016-07-08 13:53:57 +0200 | [diff] [blame] | 1 | """ |
| 2 | Copyright (c) 2015 SONATA-NFV |
| 3 | ALL RIGHTS RESERVED. |
| 4 | |
| 5 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | you may not use this file except in compliance with the License. |
| 7 | You may obtain a copy of the License at |
| 8 | |
| 9 | http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | |
| 11 | Unless required by applicable law or agreed to in writing, software |
| 12 | distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | See the License for the specific language governing permissions and |
| 15 | limitations under the License. |
| 16 | |
| 17 | Neither the name of the SONATA-NFV [, ANY ADDITIONAL AFFILIATION] |
| 18 | nor the names of its contributors may be used to endorse or promote |
| 19 | products derived from this software without specific prior written |
| 20 | permission. |
| 21 | |
| 22 | This work has been performed in the framework of the SONATA project, |
| 23 | funded by the European Commission under Grant number 671517 through |
| 24 | the Horizon 2020 and 5G-PPP programmes. The authors would like to |
| 25 | acknowledge the contributions of their colleagues of the SONATA |
| 26 | partner consortium (www.sonata-nfv.eu). |
| 27 | """ |
| stevenvanrossem | 73efd19 | 2016-06-29 01:44:07 +0200 | [diff] [blame] | 28 | from requests import get,put, delete |
| 29 | from tabulate import tabulate |
| 30 | import pprint |
| 31 | import argparse |
| 32 | import json |
| 33 | |
| 34 | pp = pprint.PrettyPrinter(indent=4) |
| 35 | |
| 36 | class RestApiClient(): |
| 37 | |
| 38 | def __init__(self): |
| 39 | self.cmds = {} |
| 40 | |
| 41 | def execute_command(self, args): |
| 42 | if getattr(self, args["command"]) is not None: |
| 43 | # call the local method with the same name as the command arg |
| 44 | getattr(self, args["command"])(args) |
| 45 | else: |
| 46 | print("Command not implemented.") |
| 47 | |
| 48 | def add(self, args): |
| 49 | vnf_src_name = self._parse_vnf_name(args.get("source")) |
| 50 | vnf_dst_name = self._parse_vnf_name(args.get("destination")) |
| 51 | |
| 52 | params = self._create_dict( |
| 53 | vnf_src_interface=self._parse_vnf_interface(args.get("source")), |
| 54 | vnf_dst_interface=self._parse_vnf_interface(args.get("destination")), |
| 55 | weight=args.get("weight"), |
| 56 | match=args.get("match"), |
| 57 | bidirectional=args.get("bidirectional"), |
| 58 | cookie=args.get("cookie")) |
| 59 | |
| 60 | response = put("%s/restapi/network/%s/%s" % |
| 61 | (args.get("endpoint"), |
| 62 | vnf_src_name, |
| 63 | vnf_dst_name), |
| 64 | json=json.dumps(params)) |
| 65 | pp.pprint(response.json()) |
| 66 | |
| 67 | def remove(self, args): |
| 68 | vnf_src_name = self._parse_vnf_name(args.get("source")) |
| 69 | vnf_dst_name = self._parse_vnf_name(args.get("destination")) |
| 70 | |
| 71 | params = self._create_dict( |
| 72 | vnf_src_interface=self._parse_vnf_interface(args.get("source")), |
| 73 | vnf_dst_interface=self._parse_vnf_interface(args.get("destination")), |
| 74 | weight=args.get("weight"), |
| 75 | match=args.get("match"), |
| 76 | bidirectional=args.get("bidirectional"), |
| 77 | cookie=args.get("cookie")) |
| 78 | |
| 79 | response = delete("%s/restapi/network/%s/%s" % |
| 80 | (args.get("endpoint"), |
| 81 | vnf_src_name, |
| 82 | vnf_dst_name), |
| 83 | json=json.dumps(params)) |
| 84 | pp.pprint(response.json()) |
| 85 | |
| 86 | def _parse_vnf_name(self, vnf_name_str): |
| 87 | vnf_name = vnf_name_str.split(':')[0] |
| 88 | return vnf_name |
| 89 | |
| 90 | def _parse_vnf_interface(self, vnf_name_str): |
| 91 | try: |
| 92 | vnf_interface = vnf_name_str.split(':')[1] |
| 93 | except: |
| 94 | vnf_interface = None |
| 95 | |
| 96 | return vnf_interface |
| 97 | |
| 98 | def _create_dict(self, **kwargs): |
| 99 | return kwargs |
| 100 | |
| 101 | parser = argparse.ArgumentParser(description='son-emu network') |
| 102 | parser.add_argument( |
| 103 | "command", |
| 104 | choices=['add', 'remove'], |
| 105 | help="Action to be executed.") |
| 106 | parser.add_argument( |
| 107 | "--datacenter", "-d", dest="datacenter", |
| 108 | help="Data center to in which the network action should be initiated") |
| 109 | parser.add_argument( |
| 110 | "--source", "-src", dest="source", |
| 111 | help="vnf name of the source of the chain") |
| 112 | parser.add_argument( |
| 113 | "--destination", "-dst", dest="destination", |
| 114 | help="vnf name of the destination of the chain") |
| 115 | parser.add_argument( |
| 116 | "--weight", "-w", dest="weight", |
| 117 | help="weight metric to calculate the path") |
| 118 | parser.add_argument( |
| 119 | "--match", "-m", dest="match", |
| 120 | help="string holding extra matches for the flow entries") |
| 121 | parser.add_argument( |
| 122 | "--bidirectional", "-b", dest="bidirectional", |
| 123 | action='store_true', |
| 124 | help="add/remove the flow entries from src to dst and back") |
| 125 | parser.add_argument( |
| 126 | "--cookie", "-c", dest="cookie", |
| 127 | help="cookie for this flow, as easy to use identifier (eg. per tenant/service)") |
| 128 | parser.add_argument( |
| 129 | "--endpoint", "-e", dest="endpoint", |
| peusterm | 0a336cc | 2016-07-04 09:15:47 +0200 | [diff] [blame] | 130 | default="http://127.0.0.1:5001", |
| stevenvanrossem | 73efd19 | 2016-06-29 01:44:07 +0200 | [diff] [blame] | 131 | help="UUID of the plugin to be manipulated.") |
| 132 | |
| 133 | def main(argv): |
| 134 | args = vars(parser.parse_args(argv)) |
| 135 | c = RestApiClient() |
| 136 | c.execute_command(args) |