def update_SHARING_MODEL(id, max_apps, battery_limit): LOG.debug( '[usermgnt.data.standalone.db] [update_SHARING_MODEL] Updating record ...' ) try: record = get_from_SHARING_MODEL_by_id(id) if record is not None: # 'id', 'user_id', 'device_id', 'max_apps', 'battery_limit' DB_SHARING_MODEL.update(record, max_apps=max_apps, battery_limit=battery_limit) DB_SHARING_MODEL.commit() # save changes on disk # debug DB __print_records(DB_SHARING_MODEL) return "updated" else: LOG.warning( '[usermgnt.data.standalone.db] [update_SHARING_MODEL] Sharing-Model not found' ) return None except: LOG.exception( '[usermgnt.data.standalone.db] [update_SHARING_MODEL] Exception') return None
def __thr_create_user_profile(data): time.sleep(70) try: LOG.info("[usermgnt.init_config] [__thr_create_user_profile] << User Profile Creation Thread >> Creating USER-PROFILE [" + str(data) + "] in current device ...") created = False while not created: LOG.log(TRACE, '[usermgnt.init_config] [__thr_create_user_profile] << User Profile Creation Thread >> executing ...') # get device_id device_id = data_adapter.get_current_device_id() if device_id == -1: LOG.log(TRACE, '[usermgnt.init_config] [__thr_create_user_profile] << User Profile Creation Thread >> trying again in 45s ...') time.sleep(45) else: # create user-profile data['device_id'] = device_id up = um_profiling.create_user_profile(data) if up is not None: LOG.info('[usermgnt.init_config] [__thr_create_user_profile] << User Profile Creation Thread >> user-profile created! ...') created = True else: LOG.error('[usermgnt.init_config] [__thr_create_user_profile] << User Profile Creation Thread >> user-profile not created! Trying again in 60s ...') time.sleep(30) LOG.info('[usermgnt.init_config] [__thr_create_user_profile] << User Profile Creation Thread >> thread finishes') except: LOG.exception('[usermgnt.init_config] [__thr_create_user_profile] << User Profile Creation Thread >> Exception: Error while initializing application')
def save_to_SHARING_MODEL(user_id, device_id, max_apps, battery_limit): LOG.debug( '[usermgnt.data.standalone.db] [save_to_SHARING_MODEL] Saving record ...' ) # 1. there is only one record [sharing-model] per device: delete all del_all_from_SHARING_MODEL() # 2. add record try: record = get_from_SHARING_MODEL(user_id, device_id) if record is None: id = user_id + "_" + str(uuid.uuid4()) # 'id', 'user_id', 'device_id', 'max_apps', 'battery_limit' DB_SHARING_MODEL.insert(id=id, user_id=user_id, device_id=device_id, max_apps=max_apps, battery_limit=battery_limit) DB_SHARING_MODEL.commit() # save changes on disk # debug DB __print_records(DB_SHARING_MODEL) return "saved" else: LOG.warning( '[usermgnt.data.standalone.db] [save_to_SHARING_MODEL] Sharing-Model already added to DB' ) return None except: LOG.exception( '[usermgnt.data.standalone.db] [save_to_SHARING_MODEL] Exception') return None
def add_resource(resource_name, content): try: LOG.debug( "[usermgnt.data.mF2C.cimi] [add_resource] Adding new resource to [" + resource_name + "] with content [" + str(content) + "] ... ") # complete map and update resource content.update(common_new_map_fields()) #content.pop("user_id", None) res = requests.post(config.dic['CIMI_URL'] + '/' + resource_name, headers=CIMI_HEADER, verify=False, json=content) LOG.log( TRACE, "[usermgnt.data.mF2C.cimi] [add_resource] response: " + str(res) + ", " + str(res.json())) if res.status_code == 201: return get_resource_by_id(res.json()['resource-id']) LOG.error("[usermgnt.data.mF2C.cimi] [add_resource] Request failed: " + str(res.status_code) + "; Returning None ...") except: LOG.exception( "[usermgnt.data.mF2C.cimi] [add_resource] Exception; Returning None ..." ) return None
def __thr_create_sharing_model(data): time.sleep(65) try: LOG.info("[usermgnt.init_config] [__thr_create_sharing_model] << Sharing Model Creation Thread >> Creating SHARING-MODEL [" + str(data) + "] in current device ...") created = False while not created: LOG.log(TRACE, '[usermgnt.init_config] [__thr_create_sharing_model] << Sharing Model Creation Thread >> executing ...') # get device_id device_id = data_adapter.get_current_device_id() if device_id == -1: LOG.log(TRACE, '[usermgnt.init_config] [__thr_create_sharing_model] << Sharing Model Creation Thread >> trying again in 45s ...') time.sleep(45) else: # create sharing-model data['device_id'] = device_id up = um_sharing_model.init_sharing_model(data) if up is not None: LOG.info('[usermgnt.init_config] [__thr_create_sharing_model] << Sharing Model Creation Thread >> sharing-model created! ...') created = True else: LOG.error('[usermgnt.init_config] [__thr_create_sharing_model] << Sharing Model Creation Thread >> sharing-model not created! Trying again in 60s ...') time.sleep(30) LOG.info('[usermgnt.init_config] [__thr_create_sharing_model] << Sharing Model Creation Thread >> thread finishes') except: LOG.exception('[usermgnt.init_config] [__thr_create_sharing_model] << Sharing Model Creation Thread >> Exception: Error while initializing application')
def delete_resource_by_owner(resource, resources, owner_id): try: res = requests.get(config.dic['CIMI_URL'] + "/" + resource + "?$filter=acl/owner/principal='" + owner_id + "'", headers=CIMI_HEADER, verify=False) LOG.log( TRACE, "[usermgnt.data.mF2C.cimi] [delete_resource_by_owner] response: " + str(res)) # + ", " + str(res.json())) if res.status_code == 200 and len(res.json()[resources]) > 0: for r in res.json()[resources]: res2 = requests.delete(config.dic['CIMI_URL'] + "/" + r["id"], headers=CIMI_HEADER, verify=False) LOG.debug( "[usermgnt.data.mF2C.cimi] [delete_resource_by_owner] response: " + str(res2) + ", " + str(res2.json())) if res2.status_code == 200: LOG.debug( "[usermgnt.data.mF2C.cimi] [delete_resource_by_owner] Resource " + r["id"] + " deleted!") else: LOG.warning( "[usermgnt.data.mF2C.cimi] [delete_resource_by_owner] No " + resources + " found.") except: LOG.exception( "[usermgnt.data.mF2C.cimi] [delete_resource_by_owner] Exception.")
def update_resource(resource_id, content): try: LOG.log( TRACE, "[usermgnt.data.mF2C.cimi] [update_resource] (1) Updating resource [" + resource_id + "] with content [" + str(content) + "] ... ") # complete map and update resource content.update(common_update_map_fields()) LOG.debug( "[usermgnt.data.mF2C.cimi] [update_resource] (2) Updating resource [" + resource_id + "] with content [" + str(content) + "] ... ") res = requests.put(config.dic['CIMI_URL'] + '/' + resource_id, headers=CIMI_HEADER, verify=False, json=content) LOG.log( TRACE, "[usermgnt.data.mF2C.cimi] [update_resource] response: " + str(res) + ", " + str(res.json())) if res.status_code == 200: return get_resource_by_id(resource_id) LOG.error( "[usermgnt.data.mF2C.cimi] [update_resource] Request failed: " + str(res.status_code) + "; Returning None ...") except: LOG.exception( "[usermgnt.data.mF2C.cimi] [update_resource] Exception; Returning None ..." ) return None
def get_user_profile(device_id): try: device_id = device_id.replace('device/', '') res = requests.get(config.dic['CIMI_URL'] + "/user-profile?$filter=device_id=\"device/" + device_id + "\"", headers=CIMI_HEADER, verify=False) LOG.log( TRACE, "[usermgnt.data.mF2C.cimi] [get_user_profile] response: " + str(res) + ", " + str(res.json())) if res.status_code == 200 and len(res.json()['userProfiles']) > 0: return res.json()['userProfiles'][0] else: LOG.warning( "[usermgnt.data.mF2C.cimi] [get_user_profile] User's profile not found [device_id=" + device_id + "]; Returning -1 ...") return -1 except: LOG.exception( "[usermgnt.data.mF2C.cimi] [get_user_profile] Exception; Returning None ..." ) return None
def save_to_USER_PROFILE(user_id, device_id, service_consumer, resource_contributor): LOG.debug( '[usermgnt.data.standalone.db] [save_to_USER_PROFILE] Saving record ...' ) # 1. there is only one record [user-profile] per device: delete all del_all_from_USER_PROFILE() # 2. add record try: record = get_from_USER_PROFILE(user_id, device_id) if record is None: id = user_id + "_" + str(uuid.uuid4()) DB_USER_PROFILE.insert(id=id, user_id=user_id, device_id=device_id, service_consumer=service_consumer, resource_contributor=resource_contributor) DB_USER_PROFILE.commit() # save changes on disk # debug DB __print_records(DB_USER_PROFILE) return "saved" else: LOG.warning( '[usermgnt.data.standalone.db] [save_to_USER_PROFILE] User-Profile already added to DB' ) return None except: LOG.exception( '[usermgnt.data.standalone.db] [save_to_USER_PROFILE] Exception') return None
def get_power(device_id): try: device_id = device_id.replace('device/', '') res = requests.get(config.dic['CIMI_URL'] + "/device-dynamic?$filter=device/href='device/" + device_id + "'", headers=CIMI_HEADER, verify=False) LOG.log( TRACE, "[usermgnt.data.mF2C.cimi] [get_power] response: " + str(res) + ", " + str(res.json())) if res.status_code == 200 and res.json()['count'] > 0: power_value = res.json( )['deviceDynamics'][0]['powerRemainingStatus'] if str(power_value).lower() == "unlimited": return 100 elif __is_number(power_value): return int(float(power_value)) else: return int(power_value) else: LOG.warning( "[usermgnt.data.mF2C.cimi] [get_power] 'device-dynamic' not found [device_id=" + device_id + "]; Returning -1 ...") return -1 except: LOG.exception( "[usermgnt.data.mF2C.cimi] [get_power] Exception; Returning 100 ..." ) return 100
def init(): global DB_SHARING_MODEL global DB_USER_PROFILE try: # DB_SHARING_MODEL: LOG.info( "[usermgnt.data.standalone.db] [init] Initializing DB_SHARING_MODEL [" + config.dic['UM_WORKING_DIR_VOLUME'] + config.dic['DB_SHARING_MODEL'] + "] ...") DB_SHARING_MODEL = Base(config.dic['UM_WORKING_DIR_VOLUME'] + config.dic['DB_SHARING_MODEL']) if not DB_SHARING_MODEL.exists(): # create new base with field names DB_SHARING_MODEL.create('id', 'user_id', 'device_id', 'max_apps', 'battery_limit') else: DB_SHARING_MODEL.open() # DB_USER_PROFILE: LOG.info( "[usermgnt.data.standalone.db] [init] Initializing DB_USER_PROFILE [" + config.dic['UM_WORKING_DIR_VOLUME'] + config.dic['DB_USER_PROFILE'] + "] ...") DB_USER_PROFILE = Base(config.dic['UM_WORKING_DIR_VOLUME'] + config.dic['DB_USER_PROFILE']) if not DB_USER_PROFILE.exists(): # create new base with field names DB_USER_PROFILE.create('id', 'user_id', 'device_id', 'service_consumer', 'resource_contributor') else: DB_USER_PROFILE.open() except: LOG.exception( '[usermgnt.data.standalone.db] [init] Exception: Error while initializing db components' )
def __up_policies(): global message try: LOG.log( TRACE, "[usermgnt.modules.policies] [__up_policies] Checking policies ..." ) # get current profile user_profile = data_adapter.get_current_user_profile() if user_profile is None: LOG.error( '[usermgnt.modules.policies] [__up_policies] user_profile not found / error' ) elif user_profile == -1: LOG.warning( '[usermgnt.modules.policies] [__up_policies] user_profile not found' ) else: LOG.log( TRACE, '[usermgnt.modules.policies] [__up_policies] user_profile found. checking values ...' ) if user_profile['resource_contributor']: message = message + "ALLOWED: resource_contributor is set to TRUE; " return True message = message + "NOT ALLOWED: resource_contributor is set to FALSE; " except: LOG.exception('[usermgnt.modules.policies] [__up_policies] Exception') return False
def __stop(): LOG.info("[usermgnt.modules.um_assessment] [__stop] stop process") try: p_status = process.stop() return common.gen_response_ok('Assessment process stopped', 'status', p_status) except: LOG.exception('[usermgnt.modules.um_assessment] [__stop] Exception') return common.gen_response( 500, 'Exception when stopping the assessment process', 'status', '')
def status(): LOG.info("[usermgnt.modules.um_assessment] [status] get process status") try: p_status = process.get_status() return common.gen_response_ok('Assessment process status', 'status', p_status) except: LOG.exception('[usermgnt.modules.um_assessment] [status] Exception') return common.gen_response( 500, 'Exception getting the assessment process status', 'status', '')
def __sm_policies(): global message try: LOG.log( TRACE, "[usermgnt.modules.policies] [__sm_policies] Checking policies ..." ) # get current sharing model sharing_model = data_adapter.get_current_sharing_model() if sharing_model is None: LOG.error( '[usermgnt.modules.policies] [__sm_policies] sharing_model not found / error' ) elif sharing_model == -1: LOG.warning( '[usermgnt.modules.policies] [__sm_policies] sharing_model not found' ) else: LOG.log( TRACE, '[usermgnt.modules.policies] [__sm_policies] sharing_model found. checking values ...' ) # 1. battery_level battery_level = data_adapter.get_power() LOG.log( TRACE, "[usermgnt.modules.policies] [__sm_policies] 1. [battery_level=" + str(battery_level) + "] ... [sharing_model('battery_limit')=" + str(sharing_model['battery_limit']) + "]") if battery_level is None or battery_level == -1 or battery_level > sharing_model[ 'battery_limit']: # 2. total services running apps_running = data_adapter.get_total_services_running() LOG.log( TRACE, "[usermgnt.modules.policies] [__sm_policies] 2. [apps_running=" + str(apps_running) + "] ... [sharing_model('max_apps')=" + str(sharing_model['max_apps']) + "]") if apps_running >= sharing_model['max_apps']: message = message + "NOT ALLOWED: apps_running >= max_apps; " return False return True else: message = message + "NOT ALLOWED: battery_level < battery_limit; " return False except: LOG.exception('[usermgnt.modules.policies] [__sm_policies] Exception') return False
def del_all_from_USER_PROFILE(): try: records = DB_USER_PROFILE() if len(records) > 0: for r in records: DB_USER_PROFILE.delete(r) return True else: LOG.warning( '[usermgnt.data.standalone.db] [del_all_from_USER_PROFILE] No records [User-Profile] found' ) return False except: LOG.exception( '[usermgnt.data.standalone.db] [del_all_from_USER_PROFILE] Exception' ) return False
def create_user_profile(): if config.dic['UM_MODE'] == "MF2C": try: LOG.log(TRACE, '[usermgnt.init_config] [create_user_profile] Creating USER-PROFILE [MF2C] in current device ...') data = { "service_consumer": config.dic['SERVICE_CONSUMER'], "resource_contributor": config.dic['RESOURCE_CONTRIBUTOR'], "device_id": "" } d = threading.Thread(target=__thr_create_user_profile, args=(data,)) d.setDaemon(True) d.start() except: LOG.exception('[usermgnt.init_config] [create_user_profile] Exception: Error while initializing application') else: LOG.info('[usermgnt.init_config] [create_user_profile] Creating USER-PROFILE [STANDALONE] in current device ...') db.save_to_SHARING_MODEL(config.dic['DEVICE_USER'], "localhost", 5, 50)
def del_all_from_SHARING_MODEL(): try: records = DB_SHARING_MODEL() if len(records) > 0: for r in records: DB_SHARING_MODEL.delete(r) return True else: LOG.warning( '[usermgnt.data.standalone.db] [del_all_from_SHARING_MODEL] No records [Sharing-Model] found' ) return False except: LOG.exception( '[usermgnt.data.standalone.db] [del_all_from_SHARING_MODEL] Exception' ) return False
def del_from_USER_PROFILE(user_id, device_id): try: record = get_from_USER_PROFILE(user_id, device_id) if record is not None: LOG.debug( "[usermgnt.data.standalone.db] [del_from_USER_PROFILE] deleted records: " + str(DB_USER_PROFILE.delete(record))) DB_USER_PROFILE.commit() # save changes on disk return "deleted" else: LOG.warning( '[usermgnt.data.standalone.db] [del_from_USER_PROFILE] User-Profile not found' ) return None except: LOG.exception( '[usermgnt.data.standalone.db] [del_from_USER_PROFILE] Exception') return None
def delete_resource(resource_id): try: res = requests.delete(config.dic['CIMI_URL'] + '/' + resource_id, headers=CIMI_HEADER, verify=False) LOG.log( TRACE, "[usermgnt.data.mF2C.cimi] [delete_resource] response: " + str(res) + ", " + str(res.json())) if res.status_code == 200: return res.json() except: LOG.exception( "[usermgnt.data.mF2C.cimi] [delete_resource] Exception; Returning None ..." ) LOG.warning( "[usermgnt.data.mF2C.cimi] [delete_resource] Returning None ...") return None
def del_from_SHARING_MODEL(user_id, device_id): try: record = get_from_SHARING_MODEL(user_id, device_id) if record is not None: LOG.debug( "[usermgnt.data.standalone.db] [del_from_SHARING_MODEL] deleted records: " + str(DB_SHARING_MODEL.delete(record))) DB_SHARING_MODEL.commit() # save changes on disk return "deleted" else: LOG.warning( '[usermgnt.data.standalone.db] [del_from_SHARING_MODEL] Sharing-Model not found' ) return None except: LOG.exception( '[usermgnt.data.standalone.db] [del_from_SHARING_MODEL] Exception') return None
def get_current_SHARING_MODEL(): try: records = DB_SHARING_MODEL() LOG.debug( "[usermgnt.data.standalone.db] [get_current_SHARING_MODEL] records: " + str(records)) if len(records) >= 1: return list(records)[0] else: LOG.warning( '[usermgnt.data.standalone.db] [get_current_SHARING_MODEL] No records found' ) except: LOG.exception( '[usermgnt.data.standalone.db] [get_current_SHARING_MODEL] Exception' ) return None
def get_from_SHARING_MODEL_by_id(id): try: # print_records(DB_DOCKER_PORTS) # debug DB records = [r for r in DB_SHARING_MODEL if r['id'] == id] LOG.debug( "[usermgnt.data.standalone.db] [get_from_SHARING_MODEL_by_id] records: " + str(records)) if len(records) >= 1: return list(records)[0] else: LOG.warning( '[usermgnt.data.standalone.db] [get_from_SHARING_MODEL_by_id] No records found' ) except: LOG.exception( '[usermgnt.data.standalone.db] [get_from_SHARING_MODEL_by_id] Exception' ) return None
def get_from_USER_PROFILE_by_device_id(device_id): try: # print_records(DB_DOCKER_PORTS) # debug DB records = [r for r in DB_USER_PROFILE if r['device_id'] == device_id] LOG.debug( "[usermgnt.data.standalone.db] [get_from_USER_PROFILE_by_device_id] records: " + str(records)) if len(records) >= 1: return list(records)[0] else: LOG.warning( '[usermgnt.data.standalone.db] [get_from_USER_PROFILE_by_device_id] No records found' ) except: LOG.exception( '[usermgnt.data.standalone.db] [get_from_USER_PROFILE_by_device_id] Exception' ) return None
def get_from_SHARING_MODEL(user_id, device_id): try: # print_records(DB_SHARING_MODEL) # debug DB records = [ r for r in DB_SHARING_MODEL if r['user_id'] == user_id and r['device_id'] == device_id ] LOG.debug( "[usermgnt.data.standalone.db] [get_from_SHARING_MODEL] records: " + str(records)) if len(records) >= 1: return list(records)[0] else: LOG.warning( '[usermgnt.data.standalone.db] [get_from_SHARING_MODEL] No records found' ) except: LOG.exception( '[usermgnt.data.standalone.db] [get_from_SHARING_MODEL] Exception') return None
def get_resource_by_id(resource_id): try: res = requests.get(config.dic['CIMI_URL'] + "/" + resource_id, headers=CIMI_HEADER, verify=False) LOG.log( TRACE, "[usermgnt.data.mF2C.cimi] [get_resource_by_id] response: " + str(res) + ", " + str(res.json())) if res.status_code == 200 and 'id' in res.json(): return res.json() LOG.error( "[usermgnt.data.mF2C.cimi] [get_resource_by_id] Request failed: " + res.status_code + "; Returning None ...") except: LOG.exception( "[usermgnt.data.mF2C.cimi] [get_resource_by_id] Exception; Returning None ..." ) return None
def create_sharing_model(): if config.dic['UM_MODE'] == "MF2C": try: LOG.log(TRACE, '[usermgnt.init_config] [create_sharing_model] Creating SHARING-MODEL in current device ...') data = { "gps_allowed": config.dic['GPS_ALLOWED'], "max_cpu_usage": config.dic['MAX_CPU_USAGE'], "max_memory_usage": config.dic['MAX_MEM_USAGE'], "max_storage_usage": config.dic['MAX_STO_USAGE'], "max_bandwidth_usage": config.dic['MAX_BANDWITH_USAGE'], "battery_limit": config.dic['BATTERY_LIMIT'], "max_apps": config.dic['MAX_APPS'], "device_id": "" } d = threading.Thread(target=__thr_create_sharing_model, args=(data,)) d.setDaemon(True) d.start() except: LOG.exception('[usermgnt.init_config] [create_sharing_model] Exception: Error while initializing application') else: LOG.info('[usermgnt.init_config] [create_sharing_model] Creating USER-PROFILE [STANDALONE] in current device ...') db.save_to_USER_PROFILE(config.dic['DEVICE_USER'], "localhost", True, True)
def get_id_from_device(deviceID): try: res = requests.get(config.dic['CIMI_URL'] + "/device?$filter=deviceID=\"" + deviceID + "\"", headers=CIMI_HEADER, verify=False) LOG.log(TRACE, "[usermgnt.data.mF2C.cimi] [get_id_from_device] response: " + str(res)) # + ", " + str(res.json())) if res.status_code == 200 and len(res.json()['devices']) > 0: return res.json()['devices'][0]['id'] else: LOG.warning( "[usermgnt.data.mF2C.cimi] [get_id_from_device] No device found; Returning -1 ..." ) return -1 except: LOG.exception( "[usermgnt.data.mF2C.cimi] [get_id_from_device] Exception; Returning None ..." ) return None
def __check_resources_used(user_profile, sharing_model, battery_level, total_services): try: LOG.log( TRACE, "[usermgnt.modules.assessment] [__check_resources_used] << Assessment Process >> [battery_level=" + str(battery_level) + "], " "[total_services=" + str(total_services) + "]") result = {} if battery_level <= sharing_model['battery_limit']: result['battery_limit_violation'] = True if not user_profile['resource_contributor'] and total_services > 0: result['resource_contributor_violation'] = True if total_services > sharing_model['max_apps']: result['max_apps_violation'] = True except: LOG.exception( '[usermgnt.modules.assessment] [__check_resources_used] << Assessment Process >> check_resources_used >> Exception' ) return result # TODO check if empty
def update_USER_PROFILE(id, service_consumer, resource_contributor): LOG.debug( '[usermgnt.data.standalone.db] [update_USER_PROFILE] Updating record ...' ) try: record = get_from_USER_PROFILE_by_id(id) if record is not None: DB_USER_PROFILE.update(record, service_consumer=service_consumer, resource_contributor=resource_contributor) DB_USER_PROFILE.commit() # save changes on disk # debug DB __print_records(DB_USER_PROFILE) return "updated" else: LOG.warning( '[usermgnt.data.standalone.db] [update_USER_PROFILE] User-Profile not found' ) return None except: LOG.exception( '[usermgnt.data.standalone.db] [update_USER_PROFILE] Exception') return None