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 get_current_sharing_model(): LOG.info("[usermgnt.modules.um_sharing_model] [get_current_sharing_model] Getting current sharing model ...") sharing_model = data_adapter.get_current_sharing_model() if sharing_model is None: return common.gen_response(500, 'Error', 'sharing_model', 'not found / error', 'sharing_model', {}) elif sharing_model == -1: return common.gen_response_ko('Warning: Sharing-model not found', 'sharing_model', 'not found / error', 'sharing_model', {}) else: return common.gen_response_ok('Sharing-model found', 'sharing_model', sharing_model)
def delete_user_profile_by_id(profile_id): LOG.info( "[usermgnt.modules.um_profiling] [delete_user_profile_by_id] profile_id=" + profile_id) # delete profile if data_adapter.delete_user_profile_by_id(profile_id) is None: return common.gen_response(500, 'Error', 'profile_id', profile_id) else: return common.gen_response_ok('Profile deleted', 'profile_id', profile_id)
def get_sharing_model_by_id(sharing_model_id): LOG.info("[usermgnt.modules.um_sharing_model] [get_sharing_model_by_id] sharing_model_id=" + sharing_model_id) # get sharing_model sharing_model = data_adapter.get_sharing_model_by_id(sharing_model_id) if sharing_model is None: return common.gen_response(500, 'Exception', 'sharing_model_id', sharing_model_id, 'sharing_model', {}) elif sharing_model == -1: return common.gen_response_ko('Warning: Sharing-model not found', 'sharing_model_id', sharing_model_id, 'sharing_model', {}) else: return common.gen_response_ok('Sharing model found', 'sharing_model_id', sharing_model_id, 'sharing_model', sharing_model)
def update_sharing_model_by_id(sharing_model_id, data): LOG.info("[usermgnt.modules.um_sharing_model] [update_sharing_model_by_id] sharing_model_id=" + sharing_model_id + ", data=" + str(data)) # updates sharing_model sharing_model = data_adapter.update_sharing_model_by_id(sharing_model_id, data) if sharing_model is None: return common.gen_response(500, 'Exception', 'data', str(data), 'sharing_model', {}) elif sharing_model == -1: return common.gen_response_ko('Warning: Sharing model not found', 'sharing_model_id', sharing_model_id, 'sharing_model', {}) else: return common.gen_response_ok('Sharing-model updated', 'data', str(data), 'sharing_model', sharing_model)
def delete_sharing_model_by_id(sharing_model_id): LOG.info("[usermgnt.modules.um_sharing_model] [delete_sharing_model_by_id] sharing_model_id=" + sharing_model_id) # deletes sharing_model res = data_adapter.delete_sharing_model_by_id(sharing_model_id) if res is None: return common.gen_response(500, 'Exception', 'sharing_model_id', sharing_model_id) elif res == -1: return common.gen_response_ko('Warning: Sharing-model not found', 'sharing_model_id', sharing_model_id) else: return common.gen_response_ok('Sharing-model deleted', 'sharing_model_id', sharing_model_id)
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 __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 delete_user(user_id): user_id = user_id.replace('user/', '') LOG.info("[usermgnt.data.mF2C.data] [delete_user] Removing user [" + user_id + "] from mF2C ...") cimi.delete_resource_by_owner("sharing-model", "sharingModels", user_id) cimi.delete_resource_by_owner("user-profile", "userProfiles", user_id) cimi.delete_resource_by_owner("service", "services", user_id) cimi.delete_resource_by_owner("service-instance", "serviceInstances", user_id) return cimi.delete_resource("user/" + user_id)
def getCurrent(val): LOG.info("[usermgnt.modules.current] [getCurrent] Getting current " + val + " ...") if val == "user": return __getCurrentUser() elif val == "device": return __getCurrentDevice() elif val == "all": return __getCurrentAll() else: return common.gen_response(404, "Error", "parameter", val, "message", "Parameter '" + val + "' not allowed")
def get_current_device_id(): LOG.info( "[usermgnt.data.standalone.data] [get_current_device_id] Getting 'my' device ID ..." ) # get from local volume device_id = vol.read_device_id() if device_id is not None and not device_id == "" and len(device_id) > 0: LOG.debug( "[usermgnt.data.standalone.data] [get_current_device_id] (LOCAL VOLUME) device_id = " + device_id) return device_id # get from config.dic['HOST_IP'] else: return config.dic['HOST_IP']
def __getCurrentUser(): LOG.info( "[usermgnt.modules.current] [__getCurrentUser] Getting current user ..." ) user_profile = data_adapter.get_current_user_profile() if user_profile is None: return common.gen_response(500, 'Error', 'cause', 'not found / error', 'user', '') elif user_profile == -1: return common.gen_response_ko('Warning: User profile not found', 'cause', 'not found / error', 'user', '') else: return common.gen_response_ok( 'User found', 'user_profile', user_profile, 'user_id', user_profile['acl']['owner']['principal'])
def get_current_user_profile(): LOG.info( "[usermgnt.modules.um_profiling] [get_current_user_profile] Getting current user-profile value ..." ) user_profile = data_adapter.get_current_user_profile() if user_profile is None: return common.gen_response(500, 'Error', 'user_profile', 'not found / error', 'profile', {}) elif user_profile == -1: return common.gen_response_ko('Warning: User profile not found', 'user_profile', 'not found / error', 'profile', {}) else: return common.gen_response_ok('User found', 'user_profile', user_profile)
def __getCurrentDevice(): LOG.info( "[usermgnt.modules.current] [__getCurrentDevice] Getting current device ..." ) user_profile = data_adapter.get_current_user_profile() if user_profile is None: return common.gen_response(500, 'Error', 'cause', 'not found / error', 'device', '') elif user_profile == -1: return common.gen_response_ko('Warning: User profile not found', 'cause', 'not found / error', 'device', '') else: return common.gen_response_ok('User found', 'user_profile', user_profile, 'device', user_profile['device_id'])
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 init_sharing_model(data): LOG.info("[usermgnt.modules.um_sharing_model] [init_sharing_model] data=" + str(data)) # check if sharing_model exists device_id = data['device_id'] sharing_model = data_adapter.get_sharing_model(device_id) data_adapter.save_device_id(device_id) if sharing_model == -1 or sharing_model is None: # initializes sharing_model sharing_model = data_adapter.init_sharing_model(data) if sharing_model is None: return None else: return sharing_model else: return sharing_model
def create_user_profile(data): LOG.info("[usermgnt.modules.um_profiling] [create_user_profile] data=" + str(data)) # check if profile exists device_id = data['device_id'] user_profile = data_adapter.get_user_profile(device_id) data_adapter.save_device_id(device_id) if user_profile == -1 or user_profile is None: # register user/profile user_profile = data_adapter.register_user(data) if user_profile is None: return None else: return user_profile else: return user_profile
def __getCurrentAll(): LOG.info( "[usermgnt.modules.current] [__getCurrentAll] Getting current device ..." ) user_profile = data_adapter.get_current_user_profile() sharing_model = data_adapter.get_current_sharing_model() agent = data_adapter.get_agent_info() if user_profile is None: return common.gen_response(500, 'Error', 'cause', 'not found / error', 'info', {}) elif user_profile == -1: return common.gen_response_ko( 'Warning: user_profile / sharing_model / agent not found', 'cause', 'not found / error', 'info', {}) else: return common.gen_response_ok( 'User found', 'info', { "user_profile": user_profile, "sharing_model": sharing_model, "agent": agent })
def operation(data): LOG.info( "[usermgnt.modules.um_assessment] [operation] Execute operation: " + str(data)) if 'operation' not in data: LOG.error( '[usermgnt.modules.um_assessment] [operation] Exception - parameter not found' ) return common.gen_response(406, 'parameter not found: operation', 'data', str(data)) if data['operation'] == 'start': return __start() elif data['operation'] == 'stop': return __stop() else: LOG.error('[usermgnt.modules.um_assessment] [operation] Operation ' + data['operation'] + ' not defined / implemented') return common.gen_response(500, 'operation not defined / implemented', 'operation', data['operation'])
def get_current_device_id(): LOG.log( TRACE, "[usermgnt.data.mF2C.data] [get_current_device_id] Getting 'my' device ID from 'agent' resource ..." ) # get from local volume device_id = vol.read_device_id() if device_id is not None and not device_id == "" and len(device_id) > 0: LOG.log( TRACE, "[usermgnt.data.mF2C.data] [get_current_device_id] (LOCAL VOLUME) device_id = " + device_id) return device_id # get from AGENT resource else: agent = cimi.get_agent_info() LOG.log( TRACE, "[usermgnt.data.mF2C.data] [get_current_device_id] agent=" + str(agent)) if not agent is None and agent != -1: LOG.log( TRACE, "[usermgnt.data.mF2C.data] [get_current_device_id] Getting device 'id' by 'deviceID'=" + agent['device_id']) id = cimi.get_id_from_device(agent['device_id']) if not id is None and id != -1: LOG.info( "[usermgnt.data.mF2C.data] [get_current_device_id] Returning 'my' device ID = " + id) return id else: LOG.warning( "[usermgnt.data.mF2C.data] [get_current_device_id] Device information not found. Returning -1 ..." ) return -1 else: LOG.warning( "[usermgnt.data.mF2C.data] [get_current_device_id] Agent information not found. Returning -1 ..." ) return -1
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 __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 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 init(um_mode): global adapter LOG.info('[usermgnt.data.data_adapter] [init] Setting data adapter...') if um_mode == "MF2C": LOG.info('[usermgnt.data.data_adapter] [init] UM_MODE = MF2C') adapter = mf2c_data_adapter.Mf2cDataAdapter() else: LOG.info('[usermgnt.data.data_adapter] [init] UM_MODE = STANDALONE') adapter = default_data_adapter.StandaloneDataAdapter() adapter.init() # init local database
def main(): LOG.info("[app] Starting User Management application [version=" + str(cfg.dic['VERSION']) + "] ...") LOG.info("[app] Swagger running on http://" + cfg.dic['HOST_IP'] + ":" + str(cfg.dic['SERVER_PORT']) + cfg.dic['API_DOC_URL'] + ".html") LOG.info("[app] REST API running on http://" + cfg.dic['HOST_IP'] + ":" + str(cfg.dic['SERVER_PORT']) + cfg.dic['API_DOC_URL']) # START (SSL) SERVER # context = ('PATH_TO_CERT_CRT'], 'PATH_TO_CERT_KEY']) # app.run(host='0.0.0.0', port=config.dic['SERVER_PORT'], ssl_context=context, threaded=True, debug=False) # START SERVER app.run(host='0.0.0.0', port=cfg.dic['SERVER_PORT'], threaded=True, debug=False)
def update_sharing_model_by_id(sharing_model_id, data): LOG.info("[usermgnt.data.standalone.data] [update_sharing_model_by_id] " + sharing_model_id + ", " + str(data)) return db.update_SHARING_MODEL(sharing_model_id, data['max_apps'], data['battery_limit'])
def delete_sharing_model_by_id(sharing_model_id): LOG.info("[usermgnt.data.standalone.data] [delete_sharing_model_by_id] " + sharing_model_id) return db.del_from_SHARING_MODEL_by_id(sharing_model_id)
def updateUM(data): LOG.info("[usermgnt.modules.um_sharing_model] [updateUM] data=" + str(data)) if 'apps_running' in data: data_adapter.setAPPS_RUNNING(data['apps_running'])
def init(): try: # get CIMI from environment values: LOG.info('[usermgnt.init_config] [init] Reading values from ENVIRONMENT...') # UM_MODE common.set_value_env('UM_MODE') common.set_value_env('HOST_IP') common.set_value_env('DEVICE_USER') common.set_value_env('UM_WORKING_DIR_VOLUME') # UM_WORKING_DIR_VOLUME from environment values: common.set_value_env('CIMI_URL') LOG.info('[usermgnt.init_config] [init] Reading User-Profile and Sharing-Model values from ENVIRONMENT...') common.set_value_env_bool('SERVICE_CONSUMER', config.dic['SERVICE_CONSUMER']) common.set_value_env_bool('RESOURCE_CONTRIBUTOR', config.dic['RESOURCE_CONTRIBUTOR']) common.set_value_env_int('MAX_APPS', config.dic['MAX_APPS']) common.set_value_env_int('BATTERY_LIMIT', config.dic['BATTERY_LIMIT']) common.set_value_env_bool('GPS_ALLOWED', config.dic['GPS_ALLOWED']) common.set_value_env_int('MAX_CPU_USAGE', config.dic['MAX_CPU_USAGE']) common.set_value_env_int('MAX_MEM_USAGE', config.dic['MAX_MEM_USAGE']) common.set_value_env_int('MAX_STO_USAGE', config.dic['MAX_STO_USAGE']) common.set_value_env_int('MAX_BANDWITH_USAGE', config.dic['MAX_BANDWITH_USAGE']) LOG.info('[usermgnt.init_config] [init] Checking configuration...') LOG.info('[usermgnt.init_config] [init] [UM_MODE=' + config.dic['UM_MODE'] + ']') # CIMI URL if "/api" not in config.dic['CIMI_URL'] and not config.dic['CIMI_URL'].endswith("/api"): LOG.debug("[usermgnt.init_config] [init] Adding '/api' to CIMI_URL ...") if config.dic['CIMI_URL'].endswith("/"): config.dic['CIMI_URL'] = config.dic['CIMI_URL'] + "api" else: config.dic['CIMI_URL'] = config.dic['CIMI_URL'] + "/api" LOG.debug('[usermgnt.init_config] [init] [CIMI_URL=' + config.dic['CIMI_URL'] + ']') else: LOG.debug("[usermgnt.init_config] [init] CIMI_URL ... " + config.dic['CIMI_URL']) LOG.info('[usermgnt.init_config] [init] [CIMI_URL=' + config.dic['CIMI_URL'] + ']') LOG.info('[usermgnt.init_config] [init] [DEVICE_USER='******'DEVICE_USER'] + ']') LOG.info('[usermgnt.init_config] [init] [UM_WORKING_DIR_VOLUME=' + config.dic['UM_WORKING_DIR_VOLUME'] + ']') LOG.info('[usermgnt.init_config] [init] [DB_SHARING_MODEL=' + config.dic['DB_SHARING_MODEL'] + ']') LOG.info('[usermgnt.init_config] [init] [DB_USER_PROFILE=' + config.dic['DB_USER_PROFILE'] + ']') LOG.info('[usermgnt.init_config] [init] [SERVER_PORT=' + str(config.dic['SERVER_PORT']) + ']') LOG.info('[usermgnt.init_config] [init] [API_DOC_URL=' + config.dic['API_DOC_URL'] + ']') LOG.info('[usermgnt.init_config] [init] [HOST_IP=' + config.dic['HOST_IP'] + ']') LOG.info('[usermgnt.init_config] [init] User-Profile and Sharing-Model configuration...') LOG.info('[usermgnt.init_config] [init] [SERVICE_CONSUMER=' + str(config.dic['SERVICE_CONSUMER']) + ']') LOG.info('[usermgnt.init_config] [init] [RESOURCE_CONTRIBUTOR=' + str(config.dic['RESOURCE_CONTRIBUTOR']) + ']') LOG.info('[usermgnt.init_config] [init] [MAX_APPS=' + str(config.dic['MAX_APPS']) + ']') LOG.info('[usermgnt.init_config] [init] [BATTERY_LIMIT=' + str(config.dic['BATTERY_LIMIT']) + ']') LOG.info('[usermgnt.init_config] [init] [GPS_ALLOWED=' + str(config.dic['GPS_ALLOWED']) + ']') LOG.info('[usermgnt.init_config] [init] [MAX_CPU_USAGE=' + str(config.dic['MAX_CPU_USAGE']) + ']') LOG.info('[usermgnt.init_config] [init] [MAX_MEM_USAGE=' + str(config.dic['MAX_MEM_USAGE']) + ']') LOG.info('[usermgnt.init_config] [init] [MAX_STO_USAGE=' + str(config.dic['MAX_STO_USAGE']) + ']') LOG.info('[usermgnt.init_config] [init] [MAX_BANDWITH_USAGE=' + str(config.dic['MAX_BANDWITH_USAGE']) + ']') LOG.info('[usermgnt.init_config] [init] Checking volume files ...') if os.path.exists(config.dic['UM_WORKING_DIR_VOLUME'] + "device_id.txt"): os.remove(config.dic['UM_WORKING_DIR_VOLUME'] + "device_id.txt") LOG.info("[usermgnt.init_config] [init] File deleted: " + config.dic['UM_WORKING_DIR_VOLUME'] + "device_id.txt") else: LOG.info("[usermgnt.init_config] [init] The file does not exist: " + config.dic['UM_WORKING_DIR_VOLUME'] + "device_id.txt. Creating new path ...") os.makedirs(config.dic['UM_WORKING_DIR_VOLUME'], exist_ok = True) if os.path.exists(config.dic['UM_WORKING_DIR_VOLUME'] + "user_id.txt"): os.remove(config.dic['UM_WORKING_DIR_VOLUME'] + "user_id.txt") LOG.info("[usermgnt.init_config] [init] File deleted: " + config.dic['UM_WORKING_DIR_VOLUME'] + "user_id.txt") else: LOG.info("[usermgnt.init_config] [init] The file does not exist: " + config.dic['UM_WORKING_DIR_VOLUME'] + "user_id.txt. Creating new path ...") os.makedirs(config.dic['UM_WORKING_DIR_VOLUME'], exist_ok = True) data_adapter.init(config.dic['UM_MODE']) except: LOG.exception('[usermgnt.init_config] [init] Exception: Error while initializing application')
def init_sharing_model(data): LOG.info("[usermgnt.data.standalone.data] [init_sharing_model] " + str(data)) return db.save_to_SHARING_MODEL(config.dic['DEVICE_USER'], config.dic['HOST_IP'], data['max_apps'], data['battery_limit'])