| 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 |
| stevenvanrossem | 73efd19 | 2016-06-29 01:44:07 +0200 | [diff] [blame] | 29 | import pprint |
| 30 | import argparse |
| stevenvanrossem | 73efd19 | 2016-06-29 01:44:07 +0200 | [diff] [blame] | 31 | |
| 32 | pp = pprint.PrettyPrinter(indent=4) |
| 33 | |
| 34 | class RestApiClient(): |
| 35 | |
| 36 | def __init__(self): |
| 37 | self.cmds = {} |
| 38 | |
| 39 | def execute_command(self, args): |
| 40 | if getattr(self, args["command"]) is not None: |
| 41 | # call the local method with the same name as the command arg |
| 42 | getattr(self, args["command"])(args) |
| 43 | else: |
| 44 | print("Command not implemented.") |
| 45 | |
| 46 | def add(self, args): |
| stevenvanrossem | 73efd19 | 2016-06-29 01:44:07 +0200 | [diff] [blame] | 47 | params = self._create_dict( |
| stevenvanrossem | f693a3b | 2017-06-01 15:15:59 +0200 | [diff] [blame] | 48 | vnf_src_name=self._parse_vnf_name(args.get("source")), |
| 49 | vnf_dst_name = self._parse_vnf_name(args.get("destination")), |
| stevenvanrossem | 73efd19 | 2016-06-29 01:44:07 +0200 | [diff] [blame] | 50 | vnf_src_interface=self._parse_vnf_interface(args.get("source")), |
| 51 | vnf_dst_interface=self._parse_vnf_interface(args.get("destination")), |
| 52 | weight=args.get("weight"), |
| 53 | match=args.get("match"), |
| 54 | bidirectional=args.get("bidirectional"), |
| peusterm | 76e052b | 2016-09-08 10:53:36 +0200 | [diff] [blame] | 55 | cookie=args.get("cookie"), |
| 56 | priority=args.get("priority")) |
| stevenvanrossem | 73efd19 | 2016-06-29 01:44:07 +0200 | [diff] [blame] | 57 | |
| stevenvanrossem | f693a3b | 2017-06-01 15:15:59 +0200 | [diff] [blame] | 58 | response = put("{0}/restapi/network".format(args.get("endpoint")), |
| stevenvanrossem | 1085e7e | 2017-06-01 16:37:52 +0200 | [diff] [blame^] | 59 | params=params) |
| stevenvanrossem | f693a3b | 2017-06-01 15:15:59 +0200 | [diff] [blame] | 60 | pp.pprint(response.text) |
| stevenvanrossem | 73efd19 | 2016-06-29 01:44:07 +0200 | [diff] [blame] | 61 | |
| 62 | def remove(self, args): |
| stevenvanrossem | 73efd19 | 2016-06-29 01:44:07 +0200 | [diff] [blame] | 63 | params = self._create_dict( |
| stevenvanrossem | f693a3b | 2017-06-01 15:15:59 +0200 | [diff] [blame] | 64 | vnf_src_name = self._parse_vnf_name(args.get("source")), |
| 65 | vnf_dst_name = self._parse_vnf_name(args.get("destination")), |
| stevenvanrossem | 73efd19 | 2016-06-29 01:44:07 +0200 | [diff] [blame] | 66 | vnf_src_interface=self._parse_vnf_interface(args.get("source")), |
| 67 | vnf_dst_interface=self._parse_vnf_interface(args.get("destination")), |
| 68 | weight=args.get("weight"), |
| 69 | match=args.get("match"), |
| 70 | bidirectional=args.get("bidirectional"), |
| peusterm | 76e052b | 2016-09-08 10:53:36 +0200 | [diff] [blame] | 71 | cookie=args.get("cookie"), |
| 72 | priority=args.get("priority")) |
| stevenvanrossem | 73efd19 | 2016-06-29 01:44:07 +0200 | [diff] [blame] | 73 | |
| stevenvanrossem | f693a3b | 2017-06-01 15:15:59 +0200 | [diff] [blame] | 74 | response = delete("{0}/restapi/network".format(args.get("endpoint")), |
| stevenvanrossem | 1085e7e | 2017-06-01 16:37:52 +0200 | [diff] [blame^] | 75 | params=params) |
| stevenvanrossem | f693a3b | 2017-06-01 15:15:59 +0200 | [diff] [blame] | 76 | pp.pprint(response.text) |
| stevenvanrossem | 73efd19 | 2016-06-29 01:44:07 +0200 | [diff] [blame] | 77 | |
| 78 | def _parse_vnf_name(self, vnf_name_str): |
| 79 | vnf_name = vnf_name_str.split(':')[0] |
| 80 | return vnf_name |
| 81 | |
| 82 | def _parse_vnf_interface(self, vnf_name_str): |
| 83 | try: |
| 84 | vnf_interface = vnf_name_str.split(':')[1] |
| 85 | except: |
| 86 | vnf_interface = None |
| 87 | |
| 88 | return vnf_interface |
| 89 | |
| 90 | def _create_dict(self, **kwargs): |
| 91 | return kwargs |
| 92 | |
| stevenvanrossem | f693a3b | 2017-06-01 15:15:59 +0200 | [diff] [blame] | 93 | parser = argparse.ArgumentParser(description='son-emu-cli network') |
| stevenvanrossem | 73efd19 | 2016-06-29 01:44:07 +0200 | [diff] [blame] | 94 | parser.add_argument( |
| 95 | "command", |
| 96 | choices=['add', 'remove'], |
| 97 | help="Action to be executed.") |
| 98 | parser.add_argument( |
| 99 | "--datacenter", "-d", dest="datacenter", |
| 100 | help="Data center to in which the network action should be initiated") |
| 101 | parser.add_argument( |
| 102 | "--source", "-src", dest="source", |
| 103 | help="vnf name of the source of the chain") |
| 104 | parser.add_argument( |
| 105 | "--destination", "-dst", dest="destination", |
| 106 | help="vnf name of the destination of the chain") |
| 107 | parser.add_argument( |
| 108 | "--weight", "-w", dest="weight", |
| stevenvanrossem | 9c8a412 | 2016-07-16 03:23:13 +0200 | [diff] [blame] | 109 | help="weight edge attribute to calculate the path") |
| stevenvanrossem | 73efd19 | 2016-06-29 01:44:07 +0200 | [diff] [blame] | 110 | parser.add_argument( |
| peusterm | 76e052b | 2016-09-08 10:53:36 +0200 | [diff] [blame] | 111 | "--priority", "-p", dest="priority", default="0", |
| 112 | help="priority of flow rule") |
| 113 | parser.add_argument( |
| stevenvanrossem | 73efd19 | 2016-06-29 01:44:07 +0200 | [diff] [blame] | 114 | "--match", "-m", dest="match", |
| 115 | help="string holding extra matches for the flow entries") |
| 116 | parser.add_argument( |
| peusterm | a307071 | 2016-07-18 10:58:31 +0200 | [diff] [blame] | 117 | "--bidirectional", "-b", dest="bidirectional", action='store_true', |
| stevenvanrossem | 73efd19 | 2016-06-29 01:44:07 +0200 | [diff] [blame] | 118 | help="add/remove the flow entries from src to dst and back") |
| 119 | parser.add_argument( |
| 120 | "--cookie", "-c", dest="cookie", |
| 121 | help="cookie for this flow, as easy to use identifier (eg. per tenant/service)") |
| 122 | parser.add_argument( |
| 123 | "--endpoint", "-e", dest="endpoint", |
| peusterm | 0a336cc | 2016-07-04 09:15:47 +0200 | [diff] [blame] | 124 | default="http://127.0.0.1:5001", |
| stevenvanrossem | f693a3b | 2017-06-01 15:15:59 +0200 | [diff] [blame] | 125 | help="REST API endpoint of son-emu (default:http://127.0.0.1:5001)") |
| stevenvanrossem | 73efd19 | 2016-06-29 01:44:07 +0200 | [diff] [blame] | 126 | |
| 127 | def main(argv): |
| 128 | args = vars(parser.parse_args(argv)) |
| 129 | c = RestApiClient() |
| peusterm | 76e052b | 2016-09-08 10:53:36 +0200 | [diff] [blame] | 130 | c.execute_command(args) |