update from RIFT as of 696b75d2fe9fb046261b08c616f1bcf6c0b54a9b second try
[osm/SO.git] / examples / ping_pong_ns / rift / mano / examples / ping_set_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_set_rate(yaml_cfg, logger):
31 '''Use curl and set traffic rate on ping vnf'''
32
33 def set_rate(mgmt_ip, port, rate):
34 curl_cmd = '''curl -D /dev/stdout \
35 -H "Accept: application/json" \
36 -H "Content-Type: application/json" \
37 -X POST \
38 -d "{{ \\"rate\\":{ping_rate} }}" \
39 http://{ping_mgmt_ip}:{ping_mgmt_port}/api/v1/ping/rate
40 '''.format(ping_mgmt_ip=mgmt_ip,
41 ping_mgmt_port=port,
42 ping_rate=rate)
43
44 logger.debug("Executing cmd: %s", curl_cmd)
45 subprocess.check_call(curl_cmd, shell=True)
46
47 # Get the ping rate
48 rate = yaml_cfg['parameter']['rate']
49
50 # Set ping rate
51 for index, vnfr in yaml_cfg['vnfr'].items():
52 logger.debug("VNFR {}: {}".format(index, vnfr))
53
54 # Check if it is pong vnf
55 if 'ping_vnfd' in vnfr['name']:
56 port = 18888
57 set_rate(vnfr['mgmt_ip_address'], port, rate)
58 break
59
60 def main(argv=sys.argv[1:]):
61 try:
62 parser = argparse.ArgumentParser()
63 parser.add_argument("yaml_cfg_file", type=argparse.FileType('r'))
64 parser.add_argument("-q", "--quiet", dest="verbose", action="store_false")
65 args = parser.parse_args()
66
67 run_dir = os.path.join(os.environ['RIFT_INSTALL'], "var/run/rift")
68 if not os.path.exists(run_dir):
69 os.makedirs(run_dir)
70 log_file = "{}/ping_set_rate-{}.log".format(run_dir, time.strftime("%Y%m%d%H%M%S"))
71 logging.basicConfig(filename=log_file, level=logging.DEBUG)
72 logger = logging.getLogger()
73
74 except Exception as e:
75 print("Exception in {}: {}".format(__file__, e))
76 sys.exit(1)
77
78 try:
79 ch = logging.StreamHandler()
80 if args.verbose:
81 ch.setLevel(logging.DEBUG)
82 else:
83 ch.setLevel(logging.INFO)
84
85 # create formatter and add it to the handlers
86 formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
87 ch.setFormatter(formatter)
88 logger.addHandler(ch)
89
90 except Exception as e:
91 logger.exception(e)
92 raise e
93
94 try:
95 yaml_str = args.yaml_cfg_file.read()
96 # logger.debug("Input YAML file:\n{}".format(yaml_str))
97 yaml_cfg = yaml.load(yaml_str)
98 logger.debug("Input YAML: {}".format(yaml_cfg))
99
100 ping_set_rate(yaml_cfg, logger)
101
102 except Exception as e:
103 logger.exception(e)
104 raise e
105
106 if __name__ == "__main__":
107 main()