3 ############################################################################
4 # Copyright 2016 RIFT.IO Inc #
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 #
10 # http://www.apache.org/licenses/LICENSE-2.0 #
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 ############################################################################
30 def ping_set_rate(yaml_cfg
, logger
):
31 '''Use curl and set traffic rate on ping vnf'''
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" \
38 -d "{{ \\"rate\\":{ping_rate} }}" \
39 http://{ping_mgmt_ip}:{ping_mgmt_port}/api/v1/ping/rate
40 '''.format(ping_mgmt_ip
=mgmt_ip
,
44 logger
.debug("Executing cmd: %s", curl_cmd
)
45 subprocess
.check_call(curl_cmd
, shell
=True)
48 rate
= yaml_cfg
['parameter']['rate']
51 for index
, vnfr
in yaml_cfg
['vnfr'].items():
52 logger
.debug("VNFR {}: {}".format(index
, vnfr
))
54 # Check if it is pong vnf
55 if 'ping_vnfd' in vnfr
['name']:
57 set_rate(vnfr
['mgmt_ip_address'], port
, rate
)
60 def main(argv
=sys
.argv
[1:]):
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()
67 run_dir
= os
.path
.join(os
.environ
['RIFT_INSTALL'], "var/run/rift")
68 if not os
.path
.exists(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()
74 except Exception as e
:
75 print("Exception in {}: {}".format(__file__
, e
))
79 ch
= logging
.StreamHandler()
81 ch
.setLevel(logging
.DEBUG
)
83 ch
.setLevel(logging
.INFO
)
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
)
90 except Exception as e
:
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
))
100 ping_set_rate(yaml_cfg
, logger
)
102 except Exception as e
:
106 if __name__
== "__main__":