+/*** Routing model structure ***/
+// typedef struct {
+// s_routing_component_t generic_routing;
+// /* things that your routing model need */
+// } s_routing_component_NEW_t,*routing_component_NEW_t;
+
+/*** Parse routing model functions ***/
+// static void model_NEW_set_processing_unit(routing_component_t rc, const char* name) {}
+// static void model_NEW_set_autonomous_system(routing_component_t rc, const char* name) {}
+// static void model_NEW_set_route(routing_component_t rc, const char* src, const char* dst, route_t route) {}
+// static void model_NEW_set_ASroute(routing_component_t rc, const char* src, const char* dst, route_extended_t route) {}
+// static void model_NEW_set_bypassroute(routing_component_t rc, const char* src, const char* dst, route_extended_t e_route) {}
+
+/*** Business methods ***/
+// static route_extended_t NEW_get_route(routing_component_t rc, const char* src,const char* dst) {return NULL;}
+// static route_extended_t NEW_get_bypass_route(routing_component_t rc, const char* src,const char* dst) {return NULL;}
+// static void NEW_finalize(routing_component_t rc) { xbt_free(rc);}
+
+/*** Creation routing model functions ***/
+// static void* model_NEW_create(void) {
+// routing_component_NEW_t new_component = xbt_new0(s_routing_component_NEW_t,1);
+// new_component->generic_routing.set_processing_unit = model_NEW_set_processing_unit;
+// new_component->generic_routing.set_autonomous_system = model_NEW_set_autonomous_system;
+// new_component->generic_routing.set_route = model_NEW_set_route;
+// new_component->generic_routing.set_ASroute = model_NEW_set_ASroute;
+// new_component->generic_routing.set_bypassroute = model_NEW_set_bypassroute;
+// new_component->generic_routing.get_route = NEW_get_route;
+// new_component->generic_routing.get_bypass_route = NEW_get_bypass_route;
+// new_component->generic_routing.finalize = NEW_finalize;
+// /* initialization of internal structures */
+// return new_component;
+// } /* mandatory */
+// static void model_NEW_load(void) {} /* mandatory */
+// static void model_NEW_unload(void) {} /* mandatory */
+// static void model_NEW_end(void) {} /* mandatory */
+
+/* ************************************************************************** */
+/* ************************* GENERIC PARSE FUNCTIONS ************************ */
+
+static void generic_set_processing_unit(routing_component_t rc,
+ const char *name)
+{
+ DEBUG1("Load process unit \"%s\"", name);
+ int *id = xbt_new0(int, 1);
+ xbt_dict_t _to_index;
+ _to_index = current_routing->to_index;
+ *id = xbt_dict_length(_to_index);
+ xbt_dict_set(_to_index, name, id, xbt_free);
+}
+
+static void generic_set_autonomous_system(routing_component_t rc,
+ const char *name)
+{
+ DEBUG1("Load Autonomous system \"%s\"", name);
+ int *id = xbt_new0(int, 1);
+ xbt_dict_t _to_index;
+ _to_index = current_routing->to_index;
+ *id = xbt_dict_length(_to_index);
+ xbt_dict_set(_to_index, name, id, xbt_free);
+}
+
+static void generic_set_route(routing_component_t rc, const char *src,
+ const char *dst, route_t route)
+{
+ DEBUG2("Load Route from \"%s\" to \"%s\"", src, dst);
+ xbt_dict_t _parse_routes;
+ xbt_dict_t _to_index;
+ char *route_name;
+ int *src_id, *dst_id;
+ unsigned long nb_links = xbt_dynar_length(route->link_list);
+
+ _to_index = current_routing->to_index;
+ //TODO
+ _parse_routes = current_routing->parse_routes;
+
+ src_id = xbt_dict_get_or_null(_to_index, src);
+ dst_id = xbt_dict_get_or_null(_to_index, dst);
+
+ xbt_assert2(src_id
+ && dst_id, "Network elements %s or %s not found", src, dst);
+ route_name = bprintf("%d#%d", *src_id, *dst_id);
+
+ xbt_assert2(xbt_dynar_length(route->link_list) > 0,
+ "Invalid count of links, must be greater than zero (%s,%s)",
+ src, dst);
+
+ route_t route_to_test = xbt_dict_get_or_null(_parse_routes, route_name);
+ if(route_to_test)
+ xbt_assert2(!xbt_dynar_compare(
+ (void*)route->link_list,
+ (void*)route_to_test->link_list,
+ (int_f_cpvoid_cpvoid_t) strcmp),
+ "The route between \"%s\" and \"%s\" already exist", src,dst);
+
+ xbt_dict_set(_parse_routes, route_name, route, NULL);
+ xbt_free(route_name);
+
+ if(A_surfxml_route_symetrical == A_surfxml_route_symetrical_YES)
+ {
+ int i;
+ route_t route_sym = xbt_new0(s_route_t, 1);
+ route_sym->link_list = xbt_dynar_new(sizeof(char *),NULL);
+ for(i=nb_links ; i>0 ; i--)
+ {
+ char *link_name = xbt_new0(char,strlen(xbt_dynar_get_as(route->link_list, i-1, char *)));
+ link_name = xbt_strdup(xbt_dynar_get_as(route->link_list, i-1, char *));
+ xbt_dynar_push_as(route_sym->link_list ,char *, link_name);
+ }
+ DEBUG2("Load Route from \"%s\" to \"%s\"", dst, src);
+ route_to_test = xbt_dict_get_or_null(_parse_routes, bprintf("%d#%d",*dst_id, *src_id));
+ if(route_to_test)
+ xbt_assert2(!xbt_dynar_compare(
+ (void*)route_sym->link_list,
+ (void*)route_to_test->link_list,
+ (int_f_cpvoid_cpvoid_t) strcmp),
+ "The route between \"%s\" and \"%s\" already exist", dst,src);
+ xbt_dict_set(_parse_routes, bprintf("%d#%d",*dst_id, *src_id), route_sym, NULL);
+ }
+}
+
+static void generic_set_ASroute(routing_component_t rc, const char *src,
+ const char *dst, route_extended_t e_route)
+{
+ DEBUG4("Load ASroute from \"%s(%s)\" to \"%s(%s)\"", src,
+ e_route->src_gateway, dst, e_route->dst_gateway);
+
+ xbt_dict_t _parse_routes;
+ xbt_dict_t _to_index;
+ char *route_name;
+ int *src_id, *dst_id;
+ _to_index = current_routing->to_index;
+ _parse_routes = current_routing->parse_routes;
+
+
+ src_id = xbt_dict_get_or_null(_to_index, src);
+ dst_id = xbt_dict_get_or_null(_to_index, dst);
+
+ xbt_assert2(src_id
+ && dst_id, "Network elements %s or %s not found", src, dst);
+ route_name = bprintf("%d#%d", *src_id, *dst_id);
+
+ xbt_assert2(xbt_dynar_length(e_route->generic_route.link_list) > 0,
+ "Invalid count of links, must be greater than zero (%s,%s)",
+ src, dst);
+
+ route_t route_to_test = xbt_dict_get_or_null(_parse_routes, route_name);
+ if(route_to_test)
+ xbt_assert4(!xbt_dynar_compare(
+ (void*) (&e_route->generic_route)->link_list,
+ (void*) route_to_test->link_list,
+ (int_f_cpvoid_cpvoid_t) strcmp),
+ "The route between \"%s\"(\"%s\") and \"%s\"(\"%s\") already exist",
+ src, e_route->src_gateway, dst, e_route->dst_gateway);
+
+ xbt_dict_set(_parse_routes, route_name, e_route, NULL);
+ xbt_free(route_name);
+
+ unsigned long nb_links = xbt_dynar_length(e_route->generic_route.link_list);
+ if(A_surfxml_ASroute_symetrical == A_surfxml_ASroute_symetrical_YES)
+ {
+ int i;
+ route_extended_t route_sym = xbt_new0(s_route_extended_t, 1);
+ route_sym->generic_route.link_list = xbt_dynar_new(sizeof(char *),NULL);
+ for(i=nb_links ; i>0 ; i--)
+ {
+ char *link_name = xbt_new0(char,strlen(xbt_dynar_get_as(e_route->generic_route.link_list, i-1, char *)));
+ link_name = bprintf("%s",xbt_dynar_get_as(e_route->generic_route.link_list, i-1, char *));
+ xbt_dynar_push_as(route_sym->generic_route.link_list ,char *, link_name);
+ }
+ route_sym->src_gateway = xbt_new0( char,strlen(e_route->dst_gateway) );
+ route_sym->src_gateway = bprintf("%s",e_route->dst_gateway);
+ route_sym->dst_gateway = xbt_new0( char,strlen(e_route->src_gateway) );
+ route_sym->dst_gateway = bprintf("%s",e_route->src_gateway);
+ DEBUG4("Load ASroute from \"%s(%s)\" to \"%s(%s)\"",dst, route_sym->src_gateway,src,route_sym->dst_gateway);
+
+ route_to_test = xbt_dict_get_or_null(_parse_routes, bprintf("%d#%d", *dst_id, *src_id));
+ if(route_to_test)
+ xbt_assert4(!xbt_dynar_compare(
+ (void*) (&route_sym->generic_route)->link_list,
+ (void*) route_to_test->link_list,
+ (int_f_cpvoid_cpvoid_t) strcmp),
+ "The route between \"%s\"(\"%s\") and \"%s\"(\"%s\") already exist",
+ dst, route_sym->src_gateway, src, route_sym->dst_gateway);
+
+ xbt_dict_set(_parse_routes, bprintf("%d#%d", *dst_id, *src_id), route_sym, NULL);
+ }
+}
+
+static void generic_set_bypassroute(routing_component_t rc,
+ const char *src, const char *dst,
+ route_extended_t e_route)
+{
+ DEBUG2("Load bypassRoute from \"%s\" to \"%s\"", src, dst);
+ xbt_dict_t dict_bypassRoutes = rc->bypassRoutes;
+ char *route_name;
+
+ route_name = bprintf("%s#%s", src, dst);
+ xbt_assert2(xbt_dynar_length(e_route->generic_route.link_list) > 0,
+ "Invalid count of links, must be greater than zero (%s,%s)",
+ src, dst);
+ xbt_assert4(!xbt_dict_get_or_null(dict_bypassRoutes, route_name),
+ "The bypass route between \"%s\"(\"%s\") and \"%s\"(\"%s\") already exist",
+ src, e_route->src_gateway, dst, e_route->dst_gateway);
+
+ route_extended_t new_e_route =
+ generic_new_extended_route(SURF_ROUTING_RECURSIVE, e_route, 0);
+ xbt_dynar_free(&(e_route->generic_route.link_list));
+ xbt_free(e_route);
+
+ xbt_dict_set(dict_bypassRoutes, route_name, new_e_route,
+ (void (*)(void *)) generic_free_extended_route);
+ xbt_free(route_name);
+}
+
+/* ************************************************************************** */
+/* *********************** GENERIC BUSINESS METHODS ************************* */
+
+static xbt_dynar_t generic_get_onelink_routes(routing_component_t rc)
+{
+ xbt_die("\"generic_get_onelink_routes\" not implemented yet");
+}
+
+static route_extended_t generic_get_bypassroute(routing_component_t rc,
+ const char *src,
+ const char *dst)
+{
+ xbt_dict_t dict_bypassRoutes = rc->bypassRoutes;
+ routing_component_t src_as, dst_as;
+ int index_src, index_dst;
+ xbt_dynar_t path_src = NULL;
+ xbt_dynar_t path_dst = NULL;
+ routing_component_t current = NULL;
+ routing_component_t *current_src = NULL;
+ routing_component_t *current_dst = NULL;
+
+ /* (1) find the as where the src and dst are located */
+ src_as = ((network_element_info_t)
+ xbt_dict_get_or_null(global_routing->where_network_elements,
+ src))->rc_component;
+ dst_as = ((network_element_info_t)
+ xbt_dict_get_or_null(global_routing->where_network_elements,
+ dst))->rc_component;
+ xbt_assert2(src_as
+ && dst_as,
+ "Ask for route \"from\"(%s) or \"to\"(%s) no found", src,
+ dst);
+
+ /* (2) find the path to the root routing component */
+ path_src = xbt_dynar_new(sizeof(routing_component_t), NULL);
+ current = src_as;
+ while (current != NULL) {
+ xbt_dynar_push(path_src, ¤t);
+ current = current->routing_father;
+ }
+ path_dst = xbt_dynar_new(sizeof(routing_component_t), NULL);
+ current = dst_as;
+ while (current != NULL) {
+ xbt_dynar_push(path_dst, ¤t);
+ current = current->routing_father;
+ }
+
+ /* (3) find the common father */
+ index_src = (path_src->used) - 1;
+ index_dst = (path_dst->used) - 1;
+ current_src = xbt_dynar_get_ptr(path_src, index_src);
+ current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
+ while (index_src >= 0 && index_dst >= 0 && *current_src == *current_dst) {
+ routing_component_t *tmp_src, *tmp_dst;
+ tmp_src = xbt_dynar_pop_ptr(path_src);
+ tmp_dst = xbt_dynar_pop_ptr(path_dst);
+ index_src--;
+ index_dst--;
+ current_src = xbt_dynar_get_ptr(path_src, index_src);
+ current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
+ }
+
+ int max_index_src = (path_src->used) - 1;
+ int max_index_dst = (path_dst->used) - 1;
+
+ int max_index = max(max_index_src, max_index_dst);
+ int i, max;
+
+ route_extended_t e_route_bypass = NULL;
+
+ for (max = 0; max <= max_index; max++) {
+ for (i = 0; i < max; i++) {
+ if (i <= max_index_src && max <= max_index_dst) {
+ char *route_name = bprintf("%s#%s",
+ (*(routing_component_t *)
+ (xbt_dynar_get_ptr
+ (path_src, i)))->name,
+ (*(routing_component_t *)
+ (xbt_dynar_get_ptr
+ (path_dst, max)))->name);
+ e_route_bypass =
+ xbt_dict_get_or_null(dict_bypassRoutes, route_name);
+ xbt_free(route_name);
+ }
+ if (e_route_bypass)
+ break;
+ if (max <= max_index_src && i <= max_index_dst) {
+ char *route_name = bprintf("%s#%s",
+ (*(routing_component_t *)
+ (xbt_dynar_get_ptr
+ (path_src, max)))->name,
+ (*(routing_component_t *)
+ (xbt_dynar_get_ptr
+ (path_dst, i)))->name);
+ e_route_bypass =
+ xbt_dict_get_or_null(dict_bypassRoutes, route_name);
+ xbt_free(route_name);
+ }
+ if (e_route_bypass)
+ break;
+ }
+
+ if (e_route_bypass)
+ break;
+
+ if (max <= max_index_src && max <= max_index_dst) {
+ char *route_name = bprintf("%s#%s",
+ (*(routing_component_t *)
+ (xbt_dynar_get_ptr
+ (path_src, max)))->name,
+ (*(routing_component_t *)
+ (xbt_dynar_get_ptr
+ (path_dst, max)))->name);
+ e_route_bypass = xbt_dict_get_or_null(dict_bypassRoutes, route_name);
+ xbt_free(route_name);
+ }
+ if (e_route_bypass)
+ break;
+ }
+
+ xbt_dynar_free(&path_src);
+ xbt_dynar_free(&path_dst);
+
+ route_extended_t new_e_route = NULL;
+
+ if (e_route_bypass) {
+ void *link;
+ unsigned int cpt = 0;
+ new_e_route = xbt_new0(s_route_extended_t, 1);
+ new_e_route->src_gateway = xbt_strdup(e_route_bypass->src_gateway);
+ new_e_route->dst_gateway = xbt_strdup(e_route_bypass->dst_gateway);
+ new_e_route->generic_route.link_list =
+ xbt_dynar_new(global_routing->size_of_link, NULL);
+ xbt_dynar_foreach(e_route_bypass->generic_route.link_list, cpt, link) {
+ xbt_dynar_push(new_e_route->generic_route.link_list, &link);
+ }
+ }
+
+ return new_e_route;
+}
+
+/* ************************************************************************** */
+/* ************************* GENERIC AUX FUNCTIONS ************************** */
+
+static route_extended_t
+generic_new_extended_route(e_surf_routing_hierarchy_t hierarchy,
+ void *data, int order)
+{
+
+ char *link_name;
+ route_extended_t e_route, new_e_route;
+ route_t route;
+ unsigned int cpt;
+ xbt_dynar_t links = NULL, links_id = NULL;
+
+ new_e_route = xbt_new0(s_route_extended_t, 1);
+ new_e_route->generic_route.link_list =
+ xbt_dynar_new(global_routing->size_of_link, NULL);
+ new_e_route->src_gateway = NULL;
+ new_e_route->dst_gateway = NULL;
+
+ xbt_assert0(hierarchy == SURF_ROUTING_BASE
+ || hierarchy == SURF_ROUTING_RECURSIVE,
+ "the hierarchy type is not defined");
+
+ if (hierarchy == SURF_ROUTING_BASE) {
+
+ route = (route_t) data;
+ links = route->link_list;
+
+ } else if (hierarchy == SURF_ROUTING_RECURSIVE) {
+
+ e_route = (route_extended_t) data;
+ xbt_assert0(e_route->src_gateway
+ && e_route->dst_gateway, "bad gateway, is null");
+ links = e_route->generic_route.link_list;
+
+ /* remeber not erase the gateway names */
+ new_e_route->src_gateway = e_route->src_gateway;
+ new_e_route->dst_gateway = e_route->dst_gateway;
+ }
+
+ links_id = new_e_route->generic_route.link_list;
+
+ xbt_dynar_foreach(links, cpt, link_name) {
+
+ void *link =
+ xbt_dict_get_or_null(surf_network_model->resource_set, link_name);
+ if (link) {
+ if (order)
+ xbt_dynar_push(links_id, &link);
+ else
+ xbt_dynar_unshift(links_id, &link);
+ } else
+ THROW1(mismatch_error, 0, "Link %s not found", link_name);
+ }
+
+ return new_e_route;
+}
+
+static void generic_free_route(route_t route)
+{
+ if (route) {
+ xbt_dynar_free(&(route->link_list));
+ xbt_free(route);
+ }
+}
+
+static void generic_free_extended_route(route_extended_t e_route)
+{
+ if (e_route) {
+ xbt_dynar_free(&(e_route->generic_route.link_list));
+ if (e_route->src_gateway)
+ xbt_free(e_route->src_gateway);
+ if (e_route->dst_gateway)
+ xbt_free(e_route->dst_gateway);
+ xbt_free(e_route);
+ }
+}
+
+static routing_component_t generic_as_exist(routing_component_t find_from,
+ routing_component_t to_find)
+{
+ //return to_find; // FIXME: BYPASSERROR OF FOREACH WITH BREAK
+ xbt_dict_cursor_t cursor = NULL;
+ char *key;
+ int found = 0;
+ routing_component_t elem;
+ xbt_dict_foreach(find_from->routing_sons, cursor, key, elem) {
+ if (to_find == elem || generic_as_exist(elem, to_find)) {
+ found = 1;
+ break;
+ }
+ }
+ if (found)
+ return to_find;
+ return NULL;
+}
+
+static routing_component_t
+generic_autonomous_system_exist(routing_component_t rc, char *element)
+{
+ //return rc; // FIXME: BYPASSERROR OF FOREACH WITH BREAK
+ routing_component_t element_as, result, elem;
+ xbt_dict_cursor_t cursor = NULL;
+ char *key;
+ element_as = ((network_element_info_t)
+ xbt_dict_get_or_null
+ (global_routing->where_network_elements,
+ element))->rc_component;
+ result = ((routing_component_t) - 1);
+ if (element_as != rc)
+ result = generic_as_exist(rc, element_as);
+
+ int found = 0;
+ if (result) {
+ xbt_dict_foreach(element_as->routing_sons, cursor, key, elem) {
+ found = !strcmp(elem->name, element);
+ if (found)
+ break;
+ }
+ if (found)
+ return element_as;
+ }
+ return NULL;
+}
+
+static routing_component_t
+generic_processing_units_exist(routing_component_t rc, char *element)
+{
+ routing_component_t element_as;
+ element_as = ((network_element_info_t)
+ xbt_dict_get_or_null
+ (global_routing->where_network_elements,
+ element))->rc_component;
+ if (element_as == rc)
+ return element_as;
+ return generic_as_exist(rc, element_as);
+}
+
+static void generic_src_dst_check(routing_component_t rc, const char *src,
+ const char *dst)
+{
+
+ routing_component_t src_as = ((network_element_info_t)
+ xbt_dict_get_or_null
+ (global_routing->where_network_elements,
+ src))->rc_component;
+ routing_component_t dst_as = ((network_element_info_t)
+ xbt_dict_get_or_null
+ (global_routing->where_network_elements,
+ dst))->rc_component;
+
+ xbt_assert3(src_as != NULL && dst_as != NULL,
+ "Ask for route \"from\"(%s) or \"to\"(%s) no found at AS \"%s\"",
+ src, dst, rc->name);
+ xbt_assert4(src_as == dst_as,
+ "The src(%s in %s) and dst(%s in %s) are in differents AS",
+ src, src_as->name, dst, dst_as->name);
+ xbt_assert2(rc == dst_as,
+ "The routing component of src and dst is not the same as the network elements belong (%s==%s)",
+ rc->name, dst_as->name);
+}
+
+static void routing_parse_Sconfig(void)
+{
+ //TODO
+ DEBUG0("WARNING tag config not yet implemented.");
+ DEBUG1("Configuration name = %s",A_surfxml_config_id);
+}
+
+static void routing_parse_Econfig(void)
+{
+ //TODO
+ xbt_dict_cursor_t cursor = NULL;
+ char *key;
+ char *elem;
+ xbt_dict_foreach(current_property_set, cursor, key, elem) {
+ DEBUG2("property : %s = %s",key,elem);