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 start_traffic(yaml_cfg
, logger
):
31 '''Use curl and set admin status to enable on pong and ping vnfs'''
33 def enable_service(mgmt_ip
, port
, vnf_type
):
34 curl_cmd
= 'curl -D /dev/stdout -H "Accept: application/vnd.yang.data' \
35 '+xml" -H "Content-Type: application/vnd.yang.data+json" ' \
36 '-X POST -d "{{\\"enable\\":true}}" http://{mgmt_ip}:' \
37 '{mgmt_port}/api/v1/{vnf_type}/adminstatus/state'. \
43 logger
.debug("Executing cmd: %s", curl_cmd
)
44 subprocess
.check_call(curl_cmd
, shell
=True)
46 # Enable pong service first
47 for index
, vnfr
in yaml_cfg
['vnfr'].items():
48 logger
.debug("VNFR {}: {}".format(index
, vnfr
))
50 # Check if it is pong vnf
51 if 'pong_vnfd' in vnfr
['name']:
54 enable_service(vnfr
['mgmt_ip_address'], port
, vnf_type
)
57 # Add a delay to provide pong port to come up
60 # Enable ping service next
61 for index
, vnfr
in yaml_cfg
['vnfr'].items():
62 logger
.debug("VNFR {}: {}".format(index
, vnfr
))
64 # Check if it is pong vnf
65 if 'ping_vnfd' in vnfr
['name']:
68 enable_service(vnfr
['mgmt_ip_address'], port
, vnf_type
)
71 def main(argv
=sys
.argv
[1:]):
73 parser
= argparse
.ArgumentParser()
74 parser
.add_argument("yaml_cfg_file", type=argparse
.FileType('r'))
75 parser
.add_argument("-q", "--quiet", dest
="verbose", action
="store_false")
76 args
= parser
.parse_args()
78 run_dir
= os
.path
.join(os
.environ
['RIFT_INSTALL'], "var/run/rift")
79 if not os
.path
.exists(run_dir
):
81 log_file
= "{}/ping_pong_start_traffic-{}.log".format(run_dir
, time
.strftime("%Y%m%d%H%M%S"))
82 logging
.basicConfig(filename
=log_file
, level
=logging
.DEBUG
)
83 logger
= logging
.getLogger()
85 except Exception as e
:
86 print("Exception in {}: {}".format(__file__
, e
))
90 ch
= logging
.StreamHandler()
92 ch
.setLevel(logging
.DEBUG
)
94 ch
.setLevel(logging
.INFO
)
96 # create formatter and add it to the handlers
97 formatter
= logging
.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
98 ch
.setFormatter(formatter
)
101 except Exception as e
:
106 yaml_str
= args
.yaml_cfg_file
.read()
107 # logger.debug("Input YAML file:\n{}".format(yaml_str))
108 yaml_cfg
= yaml
.load(yaml_str
)
109 logger
.debug("Input YAML: {}".format(yaml_cfg
))
111 start_traffic(yaml_cfg
, logger
)
113 except Exception as e
:
117 if __name__
== "__main__":