+static container_t findChild (container_t root, container_t a1)
+{
+ if (root == a1) return root;
+
+ xbt_dict_cursor_t cursor = NULL;
+ container_t child;
+ char *child_name;
+ xbt_dict_foreach(root->children, cursor, child_name, child) {
+ if (findChild (child, a1)) return child;
+ }
+ return NULL;
+}
+
+static container_t findCommonFather (container_t root, container_t a1, container_t a2)
+{
+ if (a1->father == a2->father) return a1->father;
+
+ xbt_dict_cursor_t cursor = NULL;
+ container_t child;
+ char *child_name;
+ container_t a1_try = NULL;
+ container_t a2_try = NULL;
+ xbt_dict_foreach(root->children, cursor, child_name, child) {
+ a1_try = findChild (child, a1);
+ a2_try = findChild (child, a2);
+ if (a1_try && a2_try) return child;
+ }
+ return NULL;
+}
+
+static void linkContainers (const char *a1, const char *a2)
+{
+ //ignore loopback
+ if (strcmp (a1, "__loopback__") == 0 || strcmp (a2, "__loopback__") == 0)
+ return;
+
+ container_t a1_container = ((container_t)xbt_dict_get (allContainers, a1));
+ type_t a1_type = a1_container->type;
+
+ container_t a2_container = ((container_t)xbt_dict_get (allContainers, a2));
+ type_t a2_type = a2_container->type;
+
+ container_t container = findCommonFather (rootContainer, a1_container, a2_container);
+ xbt_assert0 (container != NULL, "common father not found");
+
+ //declare type
+ char link_typename[INSTR_DEFAULT_STR_SIZE];
+ snprintf (link_typename, INSTR_DEFAULT_STR_SIZE, "%s-%s", a1_type->name, a2_type->name);
+ type_t link_type = getLinkType (link_typename, container->type, a1_type, a2_type);
+
+ //create the link
+ static long long counter = 0;
+ char key[INSTR_DEFAULT_STR_SIZE];
+ snprintf (key, INSTR_DEFAULT_STR_SIZE, "%lld", counter++);
+ pajeStartLink(SIMIX_get_clock(), link_type->id, container->id, "G", a1_container->id, key);
+ pajeEndLink(SIMIX_get_clock(), link_type->id, container->id, "G", a2_container->id, key);
+}
+
+static void recursiveGraphExtraction (container_t container)
+{
+ if (xbt_dict_length(container->children)){
+ xbt_dict_cursor_t cursor = NULL;
+ container_t child;
+ char *child_name;
+ //bottom-up recursion
+ xbt_dict_foreach(container->children, cursor, child_name, child) {
+ recursiveGraphExtraction (child);
+ }
+
+ //let's get routes
+ xbt_dict_cursor_t cursor1 = NULL, cursor2 = NULL;
+ container_t child1, child2;
+ const char *child_name1, *child_name2;
+
+ xbt_dict_t filter = xbt_dict_new ();
+
+ xbt_dict_foreach(container->children, cursor1, child_name1, child1) {
+ xbt_dict_foreach(container->children, cursor2, child_name2, child2) {
+ //check if we already register this pair (we only need one direction)
+ char aux1[INSTR_DEFAULT_STR_SIZE], aux2[INSTR_DEFAULT_STR_SIZE];
+ snprintf (aux1, INSTR_DEFAULT_STR_SIZE, "%s%s", child_name1, child_name2);
+ snprintf (aux2, INSTR_DEFAULT_STR_SIZE, "%s%s", child_name2, child_name1);
+ if (xbt_dict_get_or_null (filter, aux1)) continue;
+ if (xbt_dict_get_or_null (filter, aux2)) continue;
+
+ //ok, not found, register it
+ xbt_dict_set (filter, aux1, xbt_strdup ("1"), xbt_free);
+ xbt_dict_set (filter, aux2, xbt_strdup ("1"), xbt_free);
+
+ if ((child1->kind == INSTR_HOST || child1->kind == INSTR_ROUTER) &&
+ (child2->kind == INSTR_HOST || child2->kind == INSTR_ROUTER)){
+
+ //getting route
+ xbt_dynar_t route;
+ xbt_ex_t exception;
+ TRY {
+ route = global_routing->get_route (child_name1, child_name2);
+ }CATCH(exception) {
+ //no route between them, that's possible
+ continue;
+ }
+
+ //link the route members
+ unsigned int cpt;
+ void *link;
+ char *previous_entity_name = (char*)child_name1;
+ xbt_dynar_foreach (route, cpt, link) {
+ char *link_name = ((link_CM02_t)link)->lmm_resource.generic_resource.name;
+ linkContainers (previous_entity_name, link_name);
+ previous_entity_name = link_name;
+ }
+ linkContainers (previous_entity_name, child_name2);
+ }else if (child1->kind == INSTR_AS &&
+ child2->kind == INSTR_AS &&
+ strcmp(child_name1, child_name2) != 0){
+
+ //getting route
+ routing_component_t root = global_routing->root;
+ route_extended_t route;
+ xbt_ex_t exception;
+ TRY {
+ route = root->get_route (root, child_name1, child_name2);
+ }CATCH(exception) {
+ //no route between them, that's possible
+ continue;
+ }
+ xbt_assert2(route!=NULL,
+ "there is no ASroute between %s and %s", child_name1, child_name2);
+ unsigned int cpt;
+ void *link;
+ char *previous_entity_name = route->src_gateway;
+ xbt_dynar_foreach (route->generic_route.link_list, cpt, link) {
+ char *link_name = ((link_CM02_t)link)->lmm_resource.generic_resource.name;
+ linkContainers (previous_entity_name, link_name);
+ previous_entity_name = link_name;
+ }
+ linkContainers (previous_entity_name, route->dst_gateway);
+ }
+ }
+ }
+ xbt_dict_free(&filter);
+ }
+}
+