update from RIFT as of 696b75d2fe9fb046261b08c616f1bcf6c0b54a9b second try
[osm/SO.git] / common / python / rift / mano / tosca_translator / test / data / tosca_ping_pong_epa / scripts / start_traffic.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 start_traffic(yaml_cfg, logger):
31 '''Use curl and set admin status to enable on pong and ping vnfs'''
32
33 def enable_service(mgmt_ip, port, vnf_type):
34 curl_cmd = 'curl -D /dev/null -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'. \
38 format(
39 mgmt_ip=mgmt_ip,
40 mgmt_port=port,
41 vnf_type=vnf_type)
42
43 logger.debug("Executing cmd: %s", curl_cmd)
44 subprocess.check_call(curl_cmd, shell=True)
45
46 # Enable pong service first
47 for index, vnfr in yaml_cfg['vnfr'].items():
48 logger.debug("VNFR {}: {}".format(index, vnfr))
49
50 # Check if it is pong vnf
51 if 'pong_vnfd' in vnfr['name']:
52 vnf_type = 'pong'
53 port = 18889
54 enable_service(vnfr['mgmt_ip_address'], port, vnf_type)
55 break
56
57 # Add a delay to provide pong port to come up
58 time.sleep(0.1)
59
60 # Enable ping service next
61 for index, vnfr in yaml_cfg['vnfr'].items():
62 logger.debug("VNFR {}: {}".format(index, vnfr))
63
64 # Check if it is pong vnf
65 if 'ping_vnfd' in vnfr['name']:
66 vnf_type = 'ping'
67 port = 18888
68 enable_service(vnfr['mgmt_ip_address'], port, vnf_type)
69 break
70
71 def main(argv=sys.argv[1:]):
72 try:
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()
77
78 run_dir = os.path.join(os.environ['RIFT_INSTALL'], "var/run/rift")
79 if not os.path.exists(run_dir):
80 os.makedirs(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()
84
85 except Exception as e:
86 print("Exception in {}: {}".format(__file__, e))
87 sys.exit(1)
88
89 try:
90 ch = logging.StreamHandler()
91 if args.verbose:
92 ch.setLevel(logging.DEBUG)
93 else:
94 ch.setLevel(logging.INFO)
95
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)
99 logger.addHandler(ch)
100
101 except Exception as e:
102 logger.exception(e)
103 raise e
104
105 try:
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))
110
111 start_traffic(yaml_cfg, logger)
112
113 except Exception as e:
114 logger.exception(e)
115 raise e
116
117 if __name__ == "__main__":
118 main()