def append_topic(single_topic: str) -> bool: try: CacheRedisAdapter.dictionary_update_value( label_info=UtilityCatalogCached.LABEL_DICTIONARY_TOPICS, key=single_topic, value=1) except Exception as ex: logger.info( 'UtilityCatalogCached append_topic Exception: {}'.format(ex)) return False
def configure_catalog_observable_backup(label_list_types: List[str]): try: CacheRedisAdapter.dictionary_create( label_info=UtilityCatalogCached. LABEL_DICTIONARY_OBSERVABLE_TO_BACKUP) for type_observable in label_list_types: label_store = UtilityCatalogCached.LABEL_DICTIONARY_OBSERVABLE_TO_BACKUP + type_observable CacheRedisAdapter.dictionary_update_value( label_info=UtilityCatalogCached. LABEL_DICTIONARY_OBSERVABLE_TO_BACKUP, key=type_observable, value=label_store) except Exception as ex: logger.error( 'configure_catalog_observable_backup Exception: {}'.format(ex))
def append_new_observable(label_type_observable: str, observable: ObservableGeneric) -> bool: try: if not observable: return False return CacheRedisAdapter.dictionary_update_value( label_info=UtilityCatalogCached.get_dictionary_name( label_type_observable), key=observable.get_label_cache(), value=observable) except Exception as ex: logger.error( 'UtilityCatalogCache append_new_observable Exception: {}'. format(ex)) return False
def add_device_registration( device_registration: DeviceRegistration) -> bool: try: if not device_registration: return False CachedComponents.checkandset_maxiotid( observable_type=device_registration.get_obs_type(), iot_id=device_registration.get_datastream_id()) return CacheRedisAdapter.dictionary_update_value( label_info=UtilityCatalogCached. LABEL_DICTIONARY_DEVICE_REGISTRATION, key=device_registration.get_datastream_id(), value=device_registration) except Exception as ex: logger.error( 'UtilityCatalogCached add_device_registration Exception: {}'. format(ex)) return False
def test_method(): try: LABEL_DICTIONARY_OBSERVABLE = 'OBSERVABLE' LABEL_COUNTER = 'TEST_COUNTER' LABEL_TEST_LIST = 'TEST_LIST' LABEL_TEST_LISTSTRING = 'TEST_LIST_STRING' STRING_TRY = 'PROVA' LABEL_LIST_OBSERVABLE = 'TEST_LIST_OBSERVABLE' LABEL_DICT_OBSERVABLE = 'TEST_DICT_OBSERVABLES' LABEL_GOST_FILTER = 'GOST_LARGE_SCALE_TEST' max_counter = 3 timestamp = datetime.datetime.now(tz=pytz.utc) logger.info( 'STARTED TEST DEMO {} Observable CatalogCache Write Simulated'. format(max_counter)) start_value = 4 increase_time = 10 CacheRedisAdapter.set_cache_info(label_info='NEW', data=0) byte_array = CacheRecord.dumps(STRING_TRY) string_return = CacheRecord.loads(byte_array=byte_array, type_object=str) byte_array = CacheRecord.dumps(start_value) compare_value = CacheRecord.loads(byte_array=byte_array, type_object=int) logger.info('String pass: {0}, string get: {1}'.format( STRING_TRY, string_return)) observable = UnitTestsUtilityObservables.test_method_create_observable( label_gost=LABEL_GOST_FILTER, index_element=1, device_index=1, timestamp=timestamp) byte_array = CacheRecord.dumps(record=observable) observable_test = CacheRecord.loads(byte_array=byte_array, type_object=Localization) CacheRedisAdapter.counter_create(label_info=LABEL_COUNTER, start_value=start_value) for counter in range(0, increase_time): CacheRedisAdapter.counter_increase(label_info=LABEL_COUNTER) value = CacheRedisAdapter.counter_get(label_info=LABEL_COUNTER) if value != start_value + increase_time: logger.error( 'Unexpected counter value: {0} rather than {1}'.format( value, counter + increase_time)) list_elements = list() dictionary_test = dict() for index_element in range(0, max_counter): observable = UnitTestsUtilityObservables.test_method_create_observable( label_gost=LABEL_GOST_FILTER, index_element=index_element, device_index=index_element, timestamp=timestamp) list_elements.append(observable) dictionary_test[observable.get_label_cache()] = observable CacheRedisAdapter.set_cache_info(label_info=LABEL_LIST_OBSERVABLE, data=list_elements) CacheRedisAdapter.set_cache_info(label_info=LABEL_DICT_OBSERVABLE, data=dictionary_test) list_return = CacheRedisAdapter.get_cached_info( label_info=LABEL_LIST_OBSERVABLE, type_data=list) dict_return = CacheRedisAdapter.get_cached_info( label_info=LABEL_DICT_OBSERVABLE, type_data=dict) CacheRedisAdapter.dictionary_create( label_info=LABEL_DICTIONARY_OBSERVABLE) CacheRedisAdapter.list_create(label_info=LABEL_TEST_LISTSTRING) CacheRedisAdapter.list_append_singleelement( label_info=LABEL_TEST_LISTSTRING, elem_to_append=STRING_TRY) string_get = CacheRedisAdapter.list_extractfirstelement( label_info=LABEL_TEST_LISTSTRING, type_element=str) logger.info('Test Set String: {0}, Extracted: {1}'.format( STRING_TRY, string_get)) for index_element in range(0, max_counter): if (index_element % 500) == 0: logger.info( 'TEST EXAMPLE COUNTER: {}'.format(index_element)) localization_test = UnitTestsUtilityObservables.test_method_create_observable( label_gost=LABEL_GOST_FILTER, index_element=index_element, device_index=index_element, timestamp=timestamp) CacheRedisAdapter.dictionary_update_value( label_info=LABEL_DICTIONARY_OBSERVABLE, key=localization_test.get_label_cache(), value=localization_test) logger.info( 'STARTED TEST DEMO {} Observable CatalogCache Read Simulated'. format(max_counter)) dictionary_imported = CacheRedisAdapter.dictionary_get_all( label_info=LABEL_DICTIONARY_OBSERVABLE, type_value=ObservableGeneric) for index_element in range(0, max_counter): label_cache = UnitTestsUtilityObservables.\ test_method_get_label_cache(label_gostfilter=LABEL_GOST_FILTER, index_element=index_element) if label_cache not in dictionary_imported or not dictionary_imported[ label_cache]: return False localization_read = dictionary_imported[label_cache] if not UnitTestsUtilityObservables.test_method_check_obs_validity( observable=localization_read, index_element=index_element, timestamp=timestamp): return False if (index_element % 500) == 0: logger.info( 'TEST EXAMPLE COUNTER READ: {}'.format(index_element)) logger.info( 'END TEST DEMO {} Observable CatalogCache Read Simulated'. format(max_counter)) CacheRedisAdapter.list_create(label_info=LABEL_TEST_LIST) for index_element in range(0, max_counter): localiz_to_append = UnitTestsUtilityObservables.test_method_create_observable( label_gost=LABEL_GOST_FILTER, index_element=index_element, device_index=index_element, timestamp=timestamp) CacheRedisAdapter.list_append_singleelement( label_info=LABEL_TEST_LIST, elem_to_append=localiz_to_append) index_element = 0 while CacheRedisAdapter.list_getcounterelements( label_info=LABEL_TEST_LIST) > 0: localiz_extracted = CacheRedisAdapter.list_extractfirstelement( label_info=LABEL_TEST_LIST, type_element=Localization) if not UnitTestsUtilityObservables.test_method_check_obs_validity( observable=localiz_extracted, index_element=index_element, timestamp=timestamp): return False index_element += 1 logger.info( 'END TEST DEMO {} Observable CatalogCache List Extract'.format( max_counter)) return True except Exception as ex: logger.error( 'UnitTestCacheRedis test_method Exception: {}'.format(ex)) return False