Refactoring: Made complete codebase PEP8 compatible.
[osm/vim-emu.git] / src / emuvim / cli / rest / monitor.py
1 # Copyright (c) 2015 SONATA-NFV and Paderborn University
2 # ALL RIGHTS RESERVED.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 #
16 # Neither the name of the SONATA-NFV, Paderborn University
17 # nor the names of its contributors may be used to endorse or promote
18 # products derived from this software without specific prior written
19 # permission.
20 #
21 # This work has been performed in the framework of the SONATA project,
22 # funded by the European Commission under Grant number 671517 through
23 # the Horizon 2020 and 5G-PPP programmes. The authors would like to
24 # acknowledge the contributions of their colleagues of the SONATA
25 # partner consortium (www.sonata-nfv.eu).
26 from requests import get, put
27 import pprint
28 import argparse
29 from emuvim.cli import prometheus
30
31 pp = pprint.PrettyPrinter(indent=4)
32
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 setup_metric(self, args):
47
48 params = self._create_dict(
49 vnf_name=self._parse_vnf_name(args.get("vnf_name")),
50 vnf_interface=self._parse_vnf_interface(args.get("vnf_name")),
51 metric=args.get("metric"))
52
53 url = "{0}/restapi/monitor/interface".format(args.get("endpoint"))
54 response = put(url, params=params)
55 pp.pprint(response.text)
56
57 def stop_metric(self, args):
58 params = self._create_dict(
59 vnf_name=self._parse_vnf_name(args.get("vnf_name")),
60 vnf_interface=self._parse_vnf_interface(args.get("vnf_name")),
61 metric=args.get("metric"))
62
63 url = "{0}/restapi/monitor/interface".format(args.get("endpoint"))
64 response = put(url, params=params)
65 pp.pprint(response.text)
66
67 def setup_flow(self, args):
68 params = self._create_dict(
69 vnf_name=self._parse_vnf_name(args.get("vnf_name")),
70 vnf_interface=self._parse_vnf_interface(args.get("vnf_name")),
71 metric=args.get("metric"),
72 cookie=args.get("cookie"))
73
74 url = "{0}/restapi/monitor/flow".format(args.get("endpoint"))
75 response = put(url, params=params)
76 pp.pprint(response.text)
77
78 def stop_flow(self, args):
79 params = self._create_dict(
80 vnf_name=self._parse_vnf_name(args.get("vnf_name")),
81 vnf_interface=self._parse_vnf_interface(args.get("vnf_name")),
82 metric=args.get("metric"),
83 cookie=args.get("cookie"))
84
85 url = "{0}/restapi/monitor/flow".format(args.get("endpoint"))
86 response = put(url, params=params)
87 pp.pprint(response.text)
88
89 def prometheus(self, args):
90 # This functions makes it more user-friendly to create the correct prometheus query
91 # <uuid> is replaced by the correct uuid of the deployed vnf container
92 vnf_name = self._parse_vnf_name(args.get("vnf_name"))
93 query = args.get("query")
94
95 vnf_status = get("%s/restapi/compute/%s/%s" %
96 (args.get("endpoint"),
97 args.get("datacenter"),
98 vnf_name)).json()
99 uuid = vnf_status['id']
100 query = query.replace('<uuid>', uuid)
101
102 response = prometheus.query_Prometheus(query)
103 pp.pprint(response)
104
105 def _parse_vnf_name(self, vnf_name_str):
106 vnf_name = vnf_name_str.split(':')[0]
107 return vnf_name
108
109 def _parse_vnf_interface(self, vnf_name_str):
110 try:
111 vnf_interface = vnf_name_str.split(':')[1]
112 except BaseException:
113 vnf_interface = None
114
115 return vnf_interface
116
117 def _create_dict(self, **kwargs):
118 return kwargs
119
120
121 parser = argparse.ArgumentParser(description='son-emu-cli monitor')
122 parser.add_argument(
123 "command",
124 choices=['setup_metric', 'stop_metric',
125 'setup_flow', 'stop_flow', 'prometheus'],
126 help="setup/stop a metric/flow to be monitored or query Prometheus")
127 parser.add_argument(
128 "--vnf_name", "-vnf", dest="vnf_name",
129 help="vnf name:interface to be monitored")
130 parser.add_argument(
131 "--metric", "-m", dest="metric",
132 help="tx_bytes, rx_bytes, tx_packets, rx_packets")
133 parser.add_argument(
134 "--cookie", "-c", dest="cookie",
135 help="flow cookie to monitor")
136 parser.add_argument(
137 "--query", "-q", dest="query",
138 help="prometheus query")
139 parser.add_argument(
140 "--datacenter", "-d", dest="datacenter",
141 help="Data center where the vnf is deployed")
142 parser.add_argument(
143 "--endpoint", "-e", dest="endpoint",
144 default="http://127.0.0.1:5001",
145 help="REST API endpoint of son-emu (default:http://127.0.0.1:5001)")
146
147
148 def main(argv):
149 args = vars(parser.parse_args(argv))
150 c = RestApiClient()
151 c.execute_command(args)