update from RIFT as of 696b75d2fe9fb046261b08c616f1bcf6c0b54a9b second try
[osm/SO.git] / common / python / rift / mano / tosca_translator / test / data / tosca_ping_pong_epa / scripts / 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/vnd.yang.data+xml" \
36 -H "Content-Type: application/vnd.yang.data+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 vnf_type = 'ping'
57 port = 18888
58 set_rate(vnfr['mgmt_ip_address'], port, rate)
59 break
60
61 def main(argv=sys.argv[1:]):
62 try:
63 parser = argparse.ArgumentParser()
64 parser.add_argument("yaml_cfg_file", type=argparse.FileType('r'))
65 parser.add_argument("-q", "--quiet", dest="verbose", action="store_false")
66 args = parser.parse_args()
67
68 run_dir = os.path.join(os.environ['RIFT_INSTALL'], "var/run/rift")
69 if not os.path.exists(run_dir):
70 os.makedirs(run_dir)
71 log_file = "{}/ping_set_rate-{}.log".format(run_dir, time.strftime("%Y%m%d%H%M%S"))
72 logging.basicConfig(filename=log_file, level=logging.DEBUG)
73 logger = logging.getLogger()
74
75 except Exception as e:
76 print("Exception in {}: {}".format(__file__, e))
77 sys.exit(1)
78
79 try:
80 ch = logging.StreamHandler()
81 if args.verbose:
82 ch.setLevel(logging.DEBUG)
83 else:
84 ch.setLevel(logging.INFO)
85
86 # create formatter and add it to the handlers
87 formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
88 ch.setFormatter(formatter)
89 logger.addHandler(ch)
90
91 except Exception as e:
92 logger.exception(e)
93 raise e
94
95 try:
96 yaml_str = args.yaml_cfg_file.read()
97 # logger.debug("Input YAML file:\n{}".format(yaml_str))
98 yaml_cfg = yaml.load(yaml_str)
99 logger.debug("Input YAML: {}".format(yaml_cfg))
100
101 ping_set_rate(yaml_cfg, logger)
102
103 except Exception as e:
104 logger.exception(e)
105 raise e
106
107 if __name__ == "__main__":
108 main()