summaryrefslogtreecommitdiffstats
path: root/miss_htbt_service/config_notif.py
blob: 8a10a15e3ab30ec181a3d545ea4ccd9c432ed52f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#!/usr/bin/env python3
# Copyright 2018 AT&T Intellectual Property, Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# 
#  Author Prakash Hosangady (ph553f)
#  Read the hb_common table
#  Update the state to RECONFIGURATION and save the hb_common table

import os
import sched, datetime, time
import string
import sys
import socket
import yaml
import json
import psycopg2
from pathlib import Path
import os.path as path
from mod.trapd_get_cbs_config import get_cbs_config
import mod.trapd_settings as tds

hb_properties_file =  path.abspath(path.join(__file__, "../config/hbproperties.yaml"))

def postgres_db_open(username,password,host,port,database_name):
    envPytest = os.getenv('pytest', "")
    if (envPytest == 'test'):
        return True
    try: #pragma: no cover
       connection = psycopg2.connect(database=database_name, user = username, password = password, host = host, port =port)
    except Exception as e:
       print("HB_Notif::postgress connect error:", e)
       connection = True
    return connection

def db_table_creation_check(connection_db,table_name):
    envPytest = os.getenv('pytest', "")
    if (envPytest == 'test'):
        return True
    try: #pragma: no cover
        cur = connection_db.cursor()
        query_db = "select * from information_schema.tables where table_name='%s'" %(table_name)
        cur.execute(query_db)
        database_names = cur.fetchone()
        if(database_names is not None):
            if(table_name in database_names):
                print("HB_Notif::Postgres has already has table -", table_name)
                return True
        else:
            print("HB_Notif::Postgres does not have table - ", table_name)
            return False
    except (psycopg2.DatabaseError, e):
        print('COMMON:Error %s' % e)
    finally:
        cur.close()

def commit_and_close_db(connection_db):
    envPytest = os.getenv('pytest', "")
    if (envPytest == 'test'):
        return True
    try: #pragma: no cover
        connection_db.commit() # <--- makes sure the change is shown in the database
        connection_db.close()
        return True
    except(psycopg2.DatabaseError, e):
        return False

def read_hb_properties_default():
        #Read the hbproperties.yaml for postgress and CBS related data
        s=open(hb_properties_file, 'r')
        a=yaml.load(s)
        if((os.getenv('pg_ipAddress') is None) or (os.getenv('pg_portNum') is None) or (os.getenv('pg_userName') is None) or (os.getenv('pg_passwd') is None)):
           ip_address = a['pg_ipAddress']
           port_num = a['pg_portNum']
           user_name = a['pg_userName']
           password = a['pg_passwd']
        else:
          ip_address =  os.getenv('pg_ipAddress')
          port_num =  os.getenv('pg_portNum')
          user_name =  os.getenv('pg_userName')
          password =  os.getenv('pg_passwd')

        dbName = a['pg_dbName']
        db_name = dbName.lower()
        cbs_polling_required = a['CBS_polling_allowed']
        cbs_polling_interval = a['CBS_polling_interval']
        s.close()
        return ip_address, port_num, user_name, password, db_name, cbs_polling_required, cbs_polling_interval

def read_hb_properties(jsfile):
    try:
        with open(jsfile, 'r') as outfile:
           cfg = json.load(outfile)
    except(Exception) as err:
        print("Json file read error - %s",err)
        return read_hb_properties_default()
    try:
        ip_address = str(cfg['pg_ipAddress'])
        port_num = str(cfg['pg_portNum'])
        user_name = str(cfg['pg_userName'])
        password = str(cfg['pg_passwd'])
        dbName = str(cfg['pg_dbName'])
        db_name = dbName.lower()
        cbs_polling_required = str(cfg['CBS_polling_allowed'])
        cbs_polling_interval = str(cfg['CBS_polling_interval'])
        if("SERVICE_NAME" in cfg.keys()):
           os.environ['SERVICE_NAME'] = str(cfg['SERVICE_NAME'])
    except(Exception) as err:
        print("Json file read parameter error -%s ",err)
        return read_hb_properties_default()
    return ip_address, port_num, user_name, password, db_name, cbs_polling_required, cbs_polling_interval

def read_hb_common(user_name,password,ip_address,port_num,db_name):
    envPytest = os.getenv('pytest', "")
    if (envPytest == 'test'):
        hbc_pid = 10
        hbc_srcName = "srvc_name"
        hbc_time = 1541234567
        hbc_state = "RUNNING"
        return hbc_pid, hbc_state, hbc_srcName, hbc_time
    connection_db = postgres_db_open(user_name,password,ip_address,port_num,db_name)
    cur = connection_db.cursor()
    query_value = "SELECT process_id,source_name,last_accessed_time,current_state FROM hb_common;"
    cur.execute(query_value)
    rows = cur.fetchall()
    print("HB_Notif::hb_common contents - ", rows)
    hbc_pid = rows[0][0]
    hbc_srcName = rows[0][1]
    hbc_time = rows[0][2]
    hbc_state = rows[0][3]
    commit_and_close_db(connection_db)
    cur.close()
    return hbc_pid, hbc_state, hbc_srcName, hbc_time

def update_hb_common(update_flg, process_id, state, user_name,password,ip_address,port_num,db_name):
    current_time = int(round(time.time()))
    source_name = socket.gethostname()
    source_name = source_name + "-" + str(os.getenv('SERVICE_NAME', ""))
    envPytest = os.getenv('pytest', "")
    if (envPytest == 'test'):
        return True
    connection_db = postgres_db_open(user_name,password,ip_address,port_num,db_name)
    cur = connection_db.cursor()
    query_value = "UPDATE hb_common SET PROCESS_ID='%d',SOURCE_NAME='%s', LAST_ACCESSED_TIME='%d',CURRENT_STATE='%s'" %(process_id, source_name, current_time, state)
    cur.execute(query_value)
    commit_and_close_db(connection_db)
    cur.close()
    return True

def fetch_json_file():
    if get_cbs_config():
        current_runtime_config_file_name = "../etc/download1.json"
        envPytest = os.getenv('pytest', "")
        if (envPytest == 'test'):
            jsfile = "../etc/config.json"
            return jsfile
        print("Config_N:current config logged to : %s" % current_runtime_config_file_name)
        with open(current_runtime_config_file_name, 'w') as outfile:
            json.dump(tds.c_config, outfile)
        jsfile = current_runtime_config_file_name
    else:
        print("MSHBD:CBS Config not available, using local config")
        jsfile = "../etc/config.json"
    print("Config_N: The json file is - %s", jsfile)
    return jsfile

#if __name__ == "__main__":
def config_notif_run():
   jsfile = fetch_json_file()
   ip_address, port_num, user_name, password, db_name, cbs_polling_required, cbs_polling_interval = read_hb_properties(jsfile)
   envPytest = os.getenv('pytest', "")
   if (envPytest == 'test'):
        return True
   connection_db = postgres_db_open(user_name,password,ip_address,port_num,db_name)
   cur = connection_db.cursor()
   if(db_table_creation_check(connection_db,"hb_common") == False):
      print("HB_Notif::ERROR::hb_common table not exists - No config download")
      connection_db.close()
   else: #pragma: no cover
      hbc_pid, hbc_state, hbc_srcName, hbc_time = read_hb_common(user_name,password,ip_address,port_num,db_name)
      state = "RECONFIGURATION"
      update_flg = 1
      ret = update_hb_common(update_flg, hbc_pid, state, user_name,password,ip_address,port_num,db_name)
      if (ret == True):
         print("HB_Notif::hb_common table updated with RECONFIGURATION state")
         commit_and_close_db(connection_db)
         return True
      else:
         print("HB_Notif::Failure updating hb_common table")
         commit_and_close_db(connection_db)
         return False
      
   cur.close()