[sdc] docker file fix for cassandra
[sdc.git] / openecomp-be / lib / openecomp-sdc-translator-lib / openecomp-sdc-translator-core / src / test / resources / mock / services / heattotosca / securityrulestoportconnection / securityrulestoportconnectiongetresource / inputfiles / call_home.py
1 import argparse
2 import json
3 import re
4 import requests
5 from socket import getfqdn
6 from sys import platform
7 from time import sleep
8
9
10 PARSER = argparse.ArgumentParser()
11 PARSER.add_argument("manager_ip", help="The IPv4 Address where one can read the MaveriQConductor.")
12 PARSER.add_argument("--mockupfile", type=str, help="The path of the json mockupfile to use.")
13 ARGS = PARSER.parse_args()
14
15 URL = "http://{0}:8084/MaveriQConductor/machine/create".format(ARGS.manager_ip)
16 URL_AVAIL = "http://{0}:8084/MaveriQConductor/isReady".format(ARGS.manager_ip)
17 HEADERS = {
18     'Accept': 'text/plain',
19     'Content-type': 'application/json',
20     'Connection': 'close'
21 }
22
23 NETWORK_MAP = {
24     "$$OAM_NET_IP$$": "oam_private_network_ip",
25     "$$BACKEND_NET_IP$$": "backend_interconnect_network_ip",
26     "$$PACKET_MIRROR_NET_IP$$": "packet_mirror_network_ip",
27     "$$CDR_NET_IP$$": "cdr_network_ip",
28     "$$VERTICA_NET_IP$$": "vertica_private_network_ip",
29     "$$PACKET_INTERNAL_NET_IP$$": "packet_internal_network_ip",
30     "$$OAM_PROTECTED_NET_IP$$": "oam_protected_network_ip"
31 }
32
33
34 def map_ips_to_networks(p_meta_data):
35     network_to_ip = {}
36     for network_name in NETWORK_MAP.keys():
37         if NETWORK_MAP[network_name] in p_meta_data:
38             network_to_ip[network_name] = str(p_meta_data[NETWORK_MAP[network_name]])
39     return network_to_ip
40
41
42 def check_availability():
43     is_connected = False
44     while is_connected is False:
45         try:
46             if requests.get(URL_AVAIL, headers={'Connection': 'close'}).status_code is 200:
47                 is_connected = True
48             sleep(2)
49         except requests.exceptions.ConnectionError:
50             sleep(2)
51
52
53 def post_request(p_json_data, p_headers):
54     req = requests.post(url=URL, data=p_json_data, headers=p_headers)
55     return req.status_code
56
57
58 def multiple_replace(regex_dictionary, text):
59     regex = re.compile("(%s)" % "|".join(map(re.escape, regex_dictionary.keys())))
60     return regex.sub(
61         lambda x: regex_dictionary[x.string[x.start():x.end()]], text
62     )
63
64
65 def main():
66     # Depending on platform, load the dependencies and meta.js files.
67     if ARGS.mockupfile:
68         with open(ARGS.mockupfile, 'r') as mockup_file:
69             mockup_file_data = mockup_file.read()
70         return post_request(mockup_file_data, HEADERS)
71     else:
72         if platform.startswith('linux'):
73             with open(r'/root/dependencies.json', 'r') as json_file:
74                 json_data = json_file.read()
75             with open(r'/meta.js', 'r') as json_file:
76                 meta_data = json.load(json_file)
77
78         elif platform == 'cygwin' or platform == 'win32':
79             with open(r'c:\\dependencies.json', 'r') as json_file:
80                 json_data = json_file.read()
81             with open(r'c:\\meta.js', 'r') as json_file:
82                 meta_data = json.load(json_file)
83         else:
84             json_data = {}
85             meta_data = {}
86             raise Exception('Unsupported platform')
87         # Build dictionary mapping each IP to network.
88         regex_dict = map_ips_to_networks(meta_data)
89         regex_dict['$$HOSTNAME$$'] = getfqdn()
90
91         # Perform any replacement needed.
92         json_data = multiple_replace(regex_dict, json_data)
93         print json_data
94         check_availability()
95         return post_request(p_json_data=json_data, p_headers=HEADERS)
96
97 print main()