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