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 pong_initial_config(yaml_cfg
, logger
):
31 '''Use curl to configure ping and set the ping rate'''
32 def find_vnfr(vnfr_dict
, name
):
34 for k
, v
in vnfr_dict
.items():
38 logger
.warn("Could not find vnfr for name : %s", name
)
40 def find_cp_ip(vnfr
, cp_name
):
41 for cp
in vnfr
['connection_point']:
42 logger
.debug("Connection point: %s", format(cp
))
43 if cp_name
in cp
['name']:
44 return cp
['ip_address']
46 raise ValueError("Could not find vnfd %s connection point %s", cp_name
)
48 def find_vnfr_mgmt_ip(vnfr
):
49 return vnfr
['mgmt_ip_address']
51 def get_vnfr_name(vnfr
):
54 def find_vdur_mgmt_ip(vnfr
):
55 return vnfr
['vdur'][0]['vm_management_ip']
57 def find_param_value(param_list
, input_param
):
58 for item
in param_list
:
59 logger
.debug("Parameter: %s", format(item
))
60 if item
['name'] == input_param
:
63 # Get the required and optional parameters
64 pong_vnfr
= find_vnfr(yaml_cfg
['vnfr'], yaml_cfg
['vnfr_name'])
65 pong_vnf_mgmt_ip
= find_vnfr_mgmt_ip(pong_vnfr
)
66 pong_vnf_svc_ip
= find_cp_ip(pong_vnfr
, "pong_vnfd/cp0")
69 # Get the required and optional parameters
70 mgmt_ip
= pong_vnf_mgmt_ip
72 service_ip
= pong_vnf_svc_ip
75 config_cmd
= 'curl -D /dev/stdout -H "Accept: application/vnd.yang.data' \
76 '+xml" -H "Content-Type: application/vnd.yang.data+json" ' \
77 '-X POST -d "{{\\"ip\\":\\"{service_ip}\\", \\"port\\":{service_port}}}" ' \
78 'http://{mgmt_ip}:{mgmt_port}/api/v1/pong/server'. \
82 service_ip
=service_ip
,
83 service_port
=service_port
)
85 logger
.debug("Executing cmd: %s", config_cmd
)
93 proc
= subprocess
.Popen(config_cmd
, shell
=True,
94 stdout
=subprocess
.PIPE
,
95 stderr
=subprocess
.PIPE
)
98 logger
.debug("Process: {}".format(proc
))
100 if proc
.returncode
== 0:
101 # Check if response is 200 OK
102 resp
= proc
.stdout
.read().decode()
103 if 'HTTP/1.1 200 OK' in resp
:
106 logger
.error("Got error response: {}".format(resp
))
110 elif proc
.returncode
== 7:
112 if count
>= max_tries
:
113 logger
.error("Connect failed for {}. Failing".format(count
))
122 def main(argv
=sys
.argv
[1:]):
124 parser
= argparse
.ArgumentParser()
125 parser
.add_argument("yaml_cfg_file", type=argparse
.FileType('r'))
126 parser
.add_argument("-q", "--quiet", dest
="verbose", action
="store_false")
127 args
= parser
.parse_args()
129 run_dir
= os
.path
.join(os
.environ
['RIFT_INSTALL'], "var/run/rift")
130 if not os
.path
.exists(run_dir
):
132 log_file
= "{}/pong_initial_config-{}.log".format(run_dir
, time
.strftime("%Y%m%d%H%M%S"))
134 # logging.basicConfig(filename=log_file, level=logging.DEBUG)
135 logger
= logging
.getLogger('pong-initial-config')
136 logger
.setLevel(logging
.DEBUG
)
138 fh
= logging
.FileHandler(log_file
)
139 fh
.setLevel(logging
.DEBUG
)
141 ch
= logging
.StreamHandler()
143 ch
.setLevel(logging
.DEBUG
)
145 ch
.setLevel(logging
.INFO
)
147 # create formatter and add it to the handlers
148 formatter
= logging
.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
149 fh
.setFormatter(formatter
)
150 ch
.setFormatter(formatter
)
151 logger
.addHandler(fh
)
152 logger
.addHandler(ch
)
154 except Exception as e
:
155 logger
.exception("Exception in {}: {}".format(__file__
, e
))
159 logger
.debug("Input file: {}".format(args
.yaml_cfg_file
.name
))
160 yaml_str
= args
.yaml_cfg_file
.read()
161 yaml_cfg
= yaml
.load(yaml_str
)
162 logger
.debug("Input YAML: {}".format(yaml_cfg
))
164 rc
= pong_initial_config(yaml_cfg
, logger
)
165 logger
.info("Return code: {}".format(rc
))
168 except Exception as e
:
169 logger
.exception("Exception in {}: {}".format(__file__
, e
))
172 if __name__
== "__main__":