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 curl_fmt
= 'curl -D /dev/stdout -H "Accept: application/vnd.yang.data' \
34 '+xml" -H "Content-Type: application/vnd.yang.data+json" ' \
35 '-X POST -d "{{ {data} }}" http://{mgmt_ip}:' \
36 '{mgmt_port}/api/v1/{vnf_type}/{url}'
38 def setup_service(mgmt_ip
, port
, vnf_type
, service_ip
, service_port
):
39 data
= '\\"ip\\":\\"{}\\", \\"port\\":5555'.format(service_ip
)
40 curl_cmd
= curl_fmt
.format(
48 logger
.debug("Executing cmd: %s", curl_cmd
)
49 proc
= subprocess
.run(curl_cmd
, shell
=True,
50 stdout
=subprocess
.PIPE
,
51 stderr
=subprocess
.PIPE
)
53 logger
.debug("Process: {}".format(proc
))
55 return proc
.returncode
57 def enable_service(mgmt_ip
, port
, vnf_type
):
58 curl_cmd
= curl_fmt
.format(
62 data
='\\"enable\\":true',
63 url
='adminstatus/state'
66 logger
.debug("Executing cmd: %s", curl_cmd
)
67 proc
= subprocess
.run(curl_cmd
, shell
=True,
68 stdout
=subprocess
.PIPE
,
69 stderr
=subprocess
.PIPE
)
71 logger
.debug("Process: {}".format(proc
))
73 return proc
.returncode
75 # Get port from user parameter
76 service_port
= yaml_cfg
['parameter']['port']
79 # Enable pong service first
80 for index
, vnfr
in yaml_cfg
['vnfr'].items():
81 logger
.debug("VNFR {}: {}".format(index
, vnfr
))
83 def get_cp_ip(cp_name
):
84 for cp
in vnfr
['connection_point']:
85 if cp
['name'].endswith(cp_name
):
86 return cp
['ip_address']
88 # Check if it is pong vnf
89 if 'pong_vnf' in vnfr
['name']:
91 mgmt_ip
= vnfr
['mgmt_ip_address']
92 port
= vnfr
['mgmt_port']
93 service_ip
= get_cp_ip('cp1')
97 while tries
< max_tries
:
98 rc
= setup_service(mgmt_ip
, port
, vnf_type
, service_ip
, service_port
)
101 logger
.error("Setup service for pong failed ({}): {}".
106 time
.sleep(1) # Sleep for 1 seconds
108 rc
= enable_service(mgmt_ip
, port
, vnf_type
)
110 logger
.error("Enable service for pong failed: {}".
115 # Add a delay to provide pong port to come up
118 # Enable ping service next
119 for index
, vnfr
in yaml_cfg
['vnfr'].items():
120 logger
.debug("VNFR {}: {}".format(index
, vnfr
))
122 # Check if it is pong vnf
123 if 'ping_vnf' in vnfr
['name']:
125 mgmt_ip
= vnfr
['mgmt_ip_address']
126 port
= vnfr
['mgmt_port']
127 if service_ip
is None:
128 logger
.error("Did not find pong ip!!")
133 while tries
< max_tries
:
134 rc
= setup_service(mgmt_ip
, port
, vnf_type
, service_ip
, service_port
)
137 logger
.error("Setup service for ping failed ({}): {}".
142 time
.sleep(1) # Sleep for 1 seconds
144 rc
= enable_service(mgmt_ip
, port
, vnf_type
)
146 logger
.error("Enable service for ping failed: {}".
153 def main(argv
=sys
.argv
[1:]):
155 parser
= argparse
.ArgumentParser()
156 parser
.add_argument("yaml_cfg_file", type=argparse
.FileType('r'))
157 parser
.add_argument("-q", "--quiet", dest
="verbose", action
="store_false")
158 args
= parser
.parse_args()
160 run_dir
= os
.path
.join(os
.environ
['RIFT_INSTALL'], "var/run/rift")
161 if not os
.path
.exists(run_dir
):
163 log_file
= "{}/ping_pong_start_traffic-{}.log".format(run_dir
, time
.strftime("%Y%m%d%H%M%S"))
165 # logging.basicConfig(filename=log_file, level=logging.DEBUG)
166 logger
= logging
.getLogger('ping-pong-start-traffic')
167 logger
.setLevel(logging
.DEBUG
)
169 fh
= logging
.FileHandler(log_file
)
170 fh
.setLevel(logging
.DEBUG
)
172 ch
= logging
.StreamHandler()
174 ch
.setLevel(logging
.DEBUG
)
176 ch
.setLevel(logging
.INFO
)
178 # create formatter and add it to the handlers
179 formatter
= logging
.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
180 fh
.setFormatter(formatter
)
181 ch
.setFormatter(formatter
)
182 logger
.addHandler(fh
)
183 logger
.addHandler(ch
)
185 except Exception as e
:
186 logger
.exception("Exception in {}: {}".format(__file__
, e
))
190 logger
.debug("Input file: {}".format(args
.yaml_cfg_file
.name
))
191 yaml_str
= args
.yaml_cfg_file
.read()
192 yaml_cfg
= yaml
.load(yaml_str
)
193 logger
.debug("Input YAML: {}".format(yaml_cfg
))
195 rc
= start_traffic(yaml_cfg
, logger
)
196 logger
.info("Return code: {}".format(rc
))
199 except Exception as e
:
200 logger
.exception("Exception in {}: {}".format(__file__
, e
))
203 if __name__
== "__main__":