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 ############################################################################
31 def ssh(cmd
, host
, user
, password
):
32 """ Run an arbitrary command over SSH. """
34 client
= paramiko
.SSHClient()
35 client
.set_missing_host_key_policy(paramiko
.AutoAddPolicy())
37 client
.connect(host
, port
=22, username
=user
, password
=password
)
39 stdin
, stdout
, stderr
= client
.exec_command(cmd
, get_pty
=True)
40 retcode
= stdout
.channel
.recv_exit_status()
45 stdout
.read().decode('utf-8').strip(),
46 stderr
.read().decode('utf-8').strip()
50 def start_traffic(yaml_cfg
, logger
):
51 '''Use curl and set admin status to enable on pong and ping vnfs'''
53 curl_fmt
= 'curl -D /dev/stdout -H "Accept: application/vnd.yang.data' \
54 '+xml" -H "Content-Type: application/vnd.yang.data+json" ' \
55 '-X POST -d "{{ {data} }}" http://127.0.0.1:' \
56 '{mgmt_port}/api/v1/{vnf_type}/{url}'
58 # Get userid and password for the VNF
59 user
= yaml_cfg
['parameter']['ssh-username']
60 passwd
= yaml_cfg
['parameter']['ssh-password']
62 # Get port from user parameter
63 service_port
= yaml_cfg
['parameter']['port']
67 def exec_cmd(vnf_type
, mgmt_ip
, port
, url
, data
):
68 curl_cmd
= curl_fmt
.format(
75 logger
.debug("Executing cmd: %s", curl_cmd
)
76 rc
, out
, err
= ssh(curl_cmd
, mgmt_ip
, user
, passwd
)
79 logger
.error("cmd={}, rc={}, stderr={}, stdout={}".
80 format(curl_cmd
, rc
, err
, out
))
82 logger
.debug("cmd={}, rc={}, stderr={}, stdout={}".
83 format(curl_cmd
, rc
, err
, out
))
87 def setup_service(mgmt_ip
, port
, vnf_type
):
88 data
= '\\"ip\\":\\"{}\\", \\"port\\":5555'.format(service_ip
)
89 return exec_cmd(vnf_type
, mgmt_ip
, port
, 'server', data
)
91 def enable_service(mgmt_ip
, port
, vnf_type
):
92 data
='\\"enable\\":true'
93 url
='adminstatus/state'
94 return exec_cmd(vnf_type
, mgmt_ip
, port
, url
, data
)
96 # Enable pong service first
97 for index
, vnfr
in yaml_cfg
['vnfr'].items():
98 logger
.debug("VNFR {}: {}".format(index
, vnfr
))
100 def get_cp_ip(cp_name
):
101 for cp
in vnfr
['connection_point']:
102 if cp
['name'].endswith(cp_name
):
103 return cp
['ip_address']
105 # Check if it is pong vnf
106 if 'pong_vnf' in vnfr
['name']:
108 mgmt_ip
= vnfr
['mgmt_ip_address']
109 port
= vnfr
['mgmt_port']
110 service_ip
= get_cp_ip('cp1')
114 while tries
< max_tries
:
115 rc
= setup_service(mgmt_ip
, port
, vnf_type
)
118 logger
.error("Setup service for pong failed ({}): {}".
123 time
.sleep(1) # Sleep for 1 seconds
127 rc
= enable_service(mgmt_ip
, port
, vnf_type
)
129 logger
.error("Enable service for pong failed: {}".
133 # Add a delay to provide pong port to come up
136 # Enable ping service next
137 for index
, vnfr
in yaml_cfg
['vnfr'].items():
138 logger
.debug("VNFR {}: {}".format(index
, vnfr
))
140 # Check if it is pong vnf
141 if 'ping_vnf' in vnfr
['name']:
143 mgmt_ip
= vnfr
['mgmt_ip_address']
144 port
= vnfr
['mgmt_port']
145 if service_ip
is None:
146 logger
.error("Did not find pong ip!!")
151 while tries
< max_tries
:
152 rc
= setup_service(mgmt_ip
, port
, vnf_type
)
155 logger
.error("Setup service for ping failed ({}): {}".
160 time
.sleep(1) # Sleep for 1 seconds
164 rc
= enable_service(mgmt_ip
, port
, vnf_type
)
166 logger
.error("Enable service for ping failed: {}".
172 def main(argv
=sys
.argv
[1:]):
174 parser
= argparse
.ArgumentParser()
175 parser
.add_argument("yaml_cfg_file", type=argparse
.FileType('r'))
176 parser
.add_argument("-q", "--quiet", dest
="verbose", action
="store_false")
177 args
= parser
.parse_args()
179 run_dir
= os
.path
.join(os
.environ
['RIFT_INSTALL'], "var/run/rift")
180 if not os
.path
.exists(run_dir
):
182 log_file
= "{}/ping_pong_start_traffic-{}.log".format(run_dir
, time
.strftime("%Y%m%d%H%M%S"))
184 # logging.basicConfig(filename=log_file, level=logging.DEBUG)
185 logger
= logging
.getLogger('ping-pong-start-traffic')
186 logger
.setLevel(logging
.DEBUG
)
188 fh
= logging
.FileHandler(log_file
)
189 fh
.setLevel(logging
.DEBUG
)
191 ch
= logging
.StreamHandler()
193 ch
.setLevel(logging
.DEBUG
)
195 ch
.setLevel(logging
.INFO
)
197 # create formatter and add it to the handlers
198 formatter
= logging
.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
199 fh
.setFormatter(formatter
)
200 ch
.setFormatter(formatter
)
201 logger
.addHandler(fh
)
202 logger
.addHandler(ch
)
204 except Exception as e
:
205 logger
.exception("Exception in {}: {}".format(__file__
, e
))
209 logger
.debug("Input file: {}".format(args
.yaml_cfg_file
.name
))
210 yaml_str
= args
.yaml_cfg_file
.read()
211 yaml_cfg
= yaml
.load(yaml_str
)
212 logger
.debug("Input YAML: {}".format(yaml_cfg
))
214 rc
= start_traffic(yaml_cfg
, logger
)
215 logger
.info("Return code: {}".format(rc
))
218 except Exception as e
:
219 logger
.exception("Exception in {}: {}".format(__file__
, e
))
222 if __name__
== "__main__":