update from RIFT as of 696b75d2fe9fb046261b08c616f1bcf6c0b54a9b second try
[osm/SO.git] / examples / ping_pong_ns / rift / mano / examples / ping_rate.py
1 #!/usr/bin/env python3
2
3 ############################################################################
4 # Copyright 2016 RIFT.IO Inc #
5 # #
6 # Licensed under the Apache License, Version 2.0 (the "License"); #
7 # you may not use this file except in compliance with the License. #
8 # You may obtain a copy of the License at #
9 # #
10 # http://www.apache.org/licenses/LICENSE-2.0 #
11 # #
12 # Unless required by applicable law or agreed to in writing, software #
13 # distributed under the License is distributed on an "AS IS" BASIS, #
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
15 # See the License for the specific language governing permissions and #
16 # limitations under the License. #
17 ############################################################################
18
19
20 import argparse
21 import logging
22 import os
23 import subprocess
24 import sys
25 import time
26
27 import yaml
28
29
30 def ping_rate(yaml_cfg, logger):
31 '''Use curl to configure ping and set the ping rate'''
32
33 # Get the required and optional parameters
34 params = yaml_cfg['parameters']
35 mgmt_ip = params['mgmt_ip']
36 mgmt_port = 18888
37 if 'mgmt_port' in params:
38 mgmt_port = params['mgmt_port']
39 rate = 1
40 if 'rate' in params:
41 rate = params['rate']
42
43 cmd = 'curl -D /dev/stdout -H "Accept: application/json" ' \
44 '-H "Content-Type: application/json" ' \
45 '-X POST -d "{{\\"rate\\":{rate}}}" ' \
46 'http://{mgmt_ip}:{mgmt_port}/api/v1/ping/rate'. \
47 format(
48 mgmt_ip=mgmt_ip,
49 mgmt_port=mgmt_port,
50 rate=rate)
51
52 logger.debug("Executing cmd: %s", cmd)
53 proc = subprocess.Popen(cmd, shell=True,
54 stdout=subprocess.PIPE,
55 stderr=subprocess.PIPE)
56
57 proc.wait()
58
59 logger.debug("Process: {}".format(proc))
60
61 return proc.returncode
62
63
64 def main(argv=sys.argv[1:]):
65 try:
66 parser = argparse.ArgumentParser()
67 parser.add_argument("yaml_cfg_file", type=argparse.FileType('r'))
68 parser.add_argument("-q", "--quiet", dest="verbose", action="store_false")
69 args = parser.parse_args()
70
71 run_dir = os.path.join(os.environ['RIFT_INSTALL'], "var/run/rift")
72 if not os.path.exists(run_dir):
73 os.makedirs(run_dir)
74 log_file = "{}/ping_rate-{}.log".format(run_dir, time.strftime("%Y%m%d%H%M%S"))
75
76 # logging.basicConfig(filename=log_file, level=logging.DEBUG)
77 logger = logging.getLogger('ping-rate')
78 logger.setLevel(logging.DEBUG)
79
80 fh = logging.FileHandler(log_file)
81 fh.setLevel(logging.DEBUG)
82
83 ch = logging.StreamHandler()
84 if args.verbose:
85 ch.setLevel(logging.DEBUG)
86 else:
87 ch.setLevel(logging.INFO)
88
89 # create formatter and add it to the handlers
90 formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
91 fh.setFormatter(formatter)
92 ch.setFormatter(formatter)
93 logger.addHandler(fh)
94 logger.addHandler(ch)
95
96 except Exception as e:
97 logger.exception("Exception in {}: {}".format(__file__, e))
98 sys.exit(1)
99
100 try:
101 logger.debug("Input file: {}".format(args.yaml_cfg_file.name))
102 yaml_str = args.yaml_cfg_file.read()
103 yaml_cfg = yaml.load(yaml_str)
104 logger.debug("Input YAML: {}".format(yaml_cfg))
105
106 rc = ping_rate(yaml_cfg, logger)
107 logger.info("Return code: {}".format(rc))
108 sys.exit(rc)
109
110 except Exception as e:
111 logger.exception("Exception in {}: {}".format(__file__, e))
112 sys.exit(1)
113
114 if __name__ == "__main__":
115 main()