Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
9161f01a12b3b07c7488e10f3100c30a818f757d
[simgrid.git] / src / surf / surf_routing.c
1 /* Copyright (c) 2009, 2010. The SimGrid Team. 
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7
8
9 #include <float.h>
10 #include "gras_config.h"
11
12 #ifdef HAVE_PCRE_LIB
13 #include <pcre.h>               /* regular expresion library */
14 #endif
15 #include "surf_private.h"
16 #include "xbt/dynar.h"
17 #include "xbt/str.h"
18 #include "xbt/config.h"
19 #include "xbt/graph.h"
20 #include "xbt/set.h"
21 #include "surf/surfxml_parse.h"
22
23 xbt_dict_t patterns = NULL;
24 xbt_dict_t random_value = NULL;
25
26 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route, surf, "Routing part of surf");
27
28 /* Global vars */
29 routing_global_t global_routing = NULL;
30 routing_component_t current_routing = NULL;
31 model_type_t current_routing_model = NULL;
32 static double_f_cpvoid_t get_link_latency = NULL;
33
34 /* Prototypes of each model */
35 static void *model_full_create(void);   /* create structures for full routing model */
36 static void model_full_load(void);      /* load parse functions for full routing model */
37 static void model_full_unload(void);    /* unload parse functions for full routing model */
38 static void model_full_end(void);       /* finalize the creation of full routing model */
39 static void model_full_set_route(               /* Set the route and ASroute between src and dst */
40                 routing_component_t rc, const char *src, const char *dst, name_route_extended_t route);
41
42 static void *model_floyd_create(void);  /* create structures for floyd routing model */
43 static void model_floyd_load(void);     /* load parse functions for floyd routing model */
44 static void model_floyd_unload(void);   /* unload parse functions for floyd routing model */
45 static void model_floyd_end(void);      /* finalize the creation of floyd routing model */
46 static void model_floyd_set_route(routing_component_t rc, const char *src,
47         const char *dst, name_route_extended_t route);
48
49 static void *model_dijkstra_both_create(int cached);    /* create by calling dijkstra or dijkstracache */
50 static void *model_dijkstra_create(void);       /* create structures for dijkstra routing model */
51 static void *model_dijkstracache_create(void);  /* create structures for dijkstracache routing model */
52 static void model_dijkstra_both_load(void);     /* load parse functions for dijkstra routing model */
53 static void model_dijkstra_both_unload(void);   /* unload parse functions for dijkstra routing model */
54 static void model_dijkstra_both_end(void);      /* finalize the creation of dijkstra routing model */
55 static void model_dijkstra_both_set_route (routing_component_t rc, const char *src,
56                      const char *dst, name_route_extended_t route);
57
58 static void *model_rulebased_create(void);      /* create structures for rulebased routing model */
59 static void model_rulebased_load(void); /* load parse functions for rulebased routing model */
60 static void model_rulebased_unload(void);       /* unload parse functions for rulebased routing model */
61 static void model_rulebased_end(void);  /* finalize the creation of rulebased routing model */
62
63 static void *model_none_create(void);   /* none routing model */
64 static void model_none_load(void);      /* none routing model */
65 static void model_none_unload(void);    /* none routing model */
66 static void model_none_end(void);       /* none routing model */
67
68 static void routing_parse_Scluster(void);  /*cluster bypass */
69 static void routing_parse_Speer(void);          /*peer bypass */
70 static void routing_parse_Srandom(void);        /*random bypass */
71 static void routing_parse_Erandom(void);        /*random bypass */
72
73 static void routing_parse_Sconfig(void);        /*config Tag */
74 static void routing_parse_Econfig(void);        /*config Tag */
75
76 static char* replace_random_parameter(char * chaine);
77 static void clean_dict_random(void);
78
79 /* this lines are only for replace use like index in the model table */
80 typedef enum {
81   SURF_MODEL_FULL = 0,
82   SURF_MODEL_FLOYD,
83   SURF_MODEL_DIJKSTRA,
84   SURF_MODEL_DIJKSTRACACHE,
85   SURF_MODEL_NONE,
86 #ifdef HAVE_PCRE_LIB
87   SURF_MODEL_RULEBASED
88 #endif
89 } e_routing_types;
90
91
92 /* must be finish with null and carefull if change de order */
93 struct s_model_type routing_models[] = { {"Full",
94                                           "Full routing data (fast, large memory requirements, fully expressive)",
95                                           model_full_create,
96                                           model_full_load,
97                                           model_full_unload,
98                                           model_full_end},
99 {"Floyd",
100  "Floyd routing data (slow initialization, fast lookup, lesser memory requirements, shortest path routing only)",
101  model_floyd_create, model_floyd_load, model_floyd_unload,
102  model_floyd_end},
103 {"Dijkstra",
104  "Dijkstra routing data (fast initialization, slow lookup, small memory requirements, shortest path routing only)",
105  model_dijkstra_create, model_dijkstra_both_load,
106  model_dijkstra_both_unload, model_dijkstra_both_end},
107 {"DijkstraCache",
108  "Dijkstra routing data (fast initialization, fast lookup, small memory requirements, shortest path routing only)",
109  model_dijkstracache_create, model_dijkstra_both_load,
110  model_dijkstra_both_unload, model_dijkstra_both_end},
111 {"none", "No routing (usable with Constant network only)",
112  model_none_create, model_none_load, model_none_unload, model_none_end},
113 #ifdef HAVE_PCRE_LIB
114 {"RuleBased", "Rule-Based routing data (...)", model_rulebased_create,
115  model_rulebased_load, model_rulebased_unload, model_rulebased_end},
116 {"Vivaldi", "Vivaldi routing", model_rulebased_create,
117   model_rulebased_load, model_rulebased_unload, model_rulebased_end},
118 #endif
119 {NULL, NULL, NULL, NULL, NULL, NULL}
120 };
121
122 /* ************************************************************************** */
123 /* ***************** GENERIC PARSE FUNCTIONS (declarations) ***************** */
124
125 static void generic_set_processing_unit(routing_component_t rc,
126                                         const char *name);
127 static void generic_set_autonomous_system(routing_component_t rc,
128                                           const char *name);
129 static void generic_set_bypassroute(routing_component_t rc,
130                                     const char *src, const char *dst,
131                                     route_extended_t e_route);
132
133 static int surf_link_resource_cmp(const void *a, const void *b);
134 static int surf_pointer_resource_cmp(const void *a, const void *b);
135
136 /* ************************************************************************** */
137 /* *************** GENERIC BUSINESS METHODS (declarations) ****************** */
138
139 static double generic_get_link_latency(routing_component_t rc, const char *src, const char *dst);
140 static xbt_dynar_t generic_get_onelink_routes(routing_component_t rc);
141 static route_extended_t generic_get_bypassroute(routing_component_t rc,
142                                                 const char *src,
143                                                 const char *dst);
144
145 /* ************************************************************************** */
146 /* ****************** GENERIC AUX FUNCTIONS (declarations) ****************** */
147
148 static route_extended_t
149 generic_new_extended_route(e_surf_routing_hierarchy_t hierarchy,
150                            void *data, int order);
151 static route_t
152 generic_new_route(e_surf_routing_hierarchy_t hierarchy,
153                            void *data, int order);
154 static void generic_free_route(route_t route);
155 static void generic_free_extended_route(route_extended_t e_route);
156 static routing_component_t
157 generic_autonomous_system_exist(routing_component_t rc, char *element);
158 static routing_component_t
159 generic_processing_units_exist(routing_component_t rc, char *element);
160 static void generic_src_dst_check(routing_component_t rc, const char *src,
161                                   const char *dst);
162
163 /* ************************************************************************** */
164 /* **************************** GLOBAL FUNCTIONS **************************** */
165
166 /* global parse functions */
167 static const char *src = NULL;        /* temporary store the source name of a route */
168 static const char *dst = NULL;        /* temporary store the destination name of a route */
169 static char *gw_src = NULL;     /* temporary store the gateway source name of a route */
170 static char *gw_dst = NULL;     /* temporary store the gateway destination name of a route */
171 static xbt_dynar_t link_list = NULL;    /* temporary store of current list link of a route */
172
173 static xbt_dict_t coordinates = NULL;
174
175
176 static double eculidean_dist_comp(int index, xbt_dynar_t src, xbt_dynar_t dst)
177 {
178         double src_coord, dst_coord;
179
180         src_coord = atof(xbt_dynar_get_as(src, index, char *));
181         dst_coord = atof(xbt_dynar_get_as(dst, index, char *));
182
183         return (src_coord-dst_coord)*(src_coord-dst_coord);
184
185 }
186
187 static double vivaldi_get_link_latency (routing_component_t rc,const char *src, const char *dst)
188 {
189   double euclidean_dist;
190   xbt_dynar_t src_ctn, dst_ctn;
191   src_ctn = xbt_dict_get(coordinates, src);
192   dst_ctn = xbt_dict_get(coordinates, dst);
193
194   euclidean_dist = sqrt (eculidean_dist_comp(0,src_ctn,dst_ctn)+eculidean_dist_comp(1,src_ctn,dst_ctn))
195                                                                 +fabs(atof(xbt_dynar_get_as(src_ctn, 2, char *)))+fabs(atof(xbt_dynar_get_as(dst_ctn, 2, char *)));
196
197   xbt_assert2(euclidean_dist>=0, "Euclidean Dist is less than 0\"%s\" and \"%.2f\"", src, euclidean_dist);
198
199   return euclidean_dist;
200
201   /*
202   x = atof(xbt_dynar_get_as(src_ctn, 0, char *))-atof(xbt_dynar_get_as(dst_ctn, 0, char *));
203   y = atof(xbt_dynar_get_as(src_ctn, 1, char *));
204   h = atof(xbt_dynar_get_as(ctn, 2, char *));
205   sqrt((c1->x - c2->x) * (c1->x - c2->x) + (c1->y - c2->y) * (c1->y - c2->y)) + fabs(c1->h) + fabs(c2->h);
206
207           if (strcmp(coord,"")) {
208         xbt_dynar_t ctn = xbt_str_split_str(coord, " ");
209         xbt_dynar_shrink(ctn,0);
210         xbt_dict_set (coordinates,host_id,ctn,NULL);
211   }
212         */
213 }
214
215 /**
216  * \brief Add a "host" to the network element list
217  */
218 static void parse_S_host(const char *host_id, const char* coord)
219 {
220   network_element_info_t info = NULL;
221   if (current_routing->hierarchy == SURF_ROUTING_NULL)
222     current_routing->hierarchy = SURF_ROUTING_BASE;
223   xbt_assert1(!xbt_dict_get_or_null
224               (global_routing->where_network_elements, host_id),
225               "Reading a host, processing unit \"%s\" already exists",
226               host_id);
227   xbt_assert1(current_routing->set_processing_unit,
228               "no defined method \"set_processing_unit\" in \"%s\"",
229               current_routing->name);
230   (*(current_routing->set_processing_unit)) (current_routing, host_id);
231   info = xbt_new0(s_network_element_info_t, 1);
232   info->rc_component = current_routing;
233   info->rc_type = SURF_NETWORK_ELEMENT_HOST;
234   xbt_dict_set(global_routing->where_network_elements, host_id,
235                (void *) info, xbt_free);
236   if (strcmp(coord,"")) {
237     xbt_dynar_t ctn = xbt_str_split_str(coord, " ");
238     xbt_dynar_shrink(ctn, 0);
239     xbt_dict_set(coordinates, host_id, ctn, xbt_dynar_free_voidp);
240   }
241 }
242
243 static void parse_E_host(void)
244 {
245          xbt_dict_cursor_t cursor = NULL;
246           char *key;
247           char *elem;
248
249           xbt_dict_foreach(current_property_set, cursor, key, elem) {
250                   XBT_DEBUG("property : %s = %s",key,elem);
251                 }
252 }
253
254 /*
255  * \brief Add a host to the network element list from XML
256  */
257 static void parse_S_host_XML(void)
258 {
259         parse_S_host(A_surfxml_host_id, A_surfxml_host_coordinates);
260 }
261 static void parse_E_host_XML(void)
262 {
263         parse_E_host();
264 }
265
266 /*
267  * \brief Add a host to the network element list from lua script
268  */
269 static void parse_S_host_lua(const char *host_id, const char *coord)
270 {
271   parse_S_host(host_id, coord);
272 }
273
274
275 /**
276  * \brief Add a "router" to the network element list
277  */
278 static void parse_S_router(const char *router_id)
279 {
280   network_element_info_t info = NULL;
281
282   if (current_routing->hierarchy == SURF_ROUTING_NULL)
283     current_routing->hierarchy = SURF_ROUTING_BASE;
284   xbt_assert1(!xbt_dict_get_or_null
285               (global_routing->where_network_elements,
286                A_surfxml_router_id),
287               "Reading a router, processing unit \"%s\" already exists",
288               router_id);
289   xbt_assert1(current_routing->set_processing_unit,
290               "no defined method \"set_processing_unit\" in \"%s\"",
291               current_routing->name);
292   (*(current_routing->set_processing_unit)) (current_routing,
293                                              router_id);
294   info = xbt_new0(s_network_element_info_t, 1);
295   info->rc_component = current_routing;
296   info->rc_type = SURF_NETWORK_ELEMENT_ROUTER;
297   xbt_dict_set(global_routing->where_network_elements, router_id,
298                (void *) info, xbt_free);
299   if (strcmp(A_surfxml_router_coordinates,"")) {
300     xbt_dynar_t ctn = xbt_str_split_str(A_surfxml_router_coordinates, " ");
301     xbt_dynar_shrink(ctn, 0);
302     xbt_dict_set(coordinates, router_id, ctn, xbt_dynar_free_voidp);
303   }
304 }
305
306 /**
307  * brief Add a "router" to the network element list from XML description
308  */
309 static void parse_S_router_XML(void)
310 {
311         return parse_S_router(A_surfxml_router_id);
312 }
313
314 /**
315  * brief Add a "router" to the network element list from XML description
316  */
317 static void parse_S_router_lua(const char* router_id)
318 {
319         return parse_S_router(router_id);
320 }
321
322 /**
323  * \brief Set the endponints for a route
324  */
325 static void parse_S_route_new_and_endpoints(const char *src_id, const char *dst_id)
326 {
327   if (src != NULL && dst != NULL && link_list != NULL)
328     THROW2(arg_error, 0, "Route between %s to %s can not be defined",
329            src_id, dst_id);
330   src = src_id;
331   dst = dst_id;
332   xbt_assert2(strlen(src) > 0 || strlen(dst) > 0,
333               "Some limits are null in the route between \"%s\" and \"%s\"",
334               src, dst);
335   link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
336 }
337
338 /**
339  * \brief Set the endpoints for a route from XML
340  */
341 static void parse_S_route_new_and_endpoints_XML(void)
342 {
343   parse_S_route_new_and_endpoints(A_surfxml_route_src,
344                                   A_surfxml_route_dst);
345 }
346
347 /**
348  * \brief Set the endpoints for a route from lua
349  */
350 static void parse_S_route_new_and_endpoints_lua(const char *id_src, const char *id_dst)
351 {
352   parse_S_route_new_and_endpoints(id_src, id_dst);
353 }
354
355 /**
356  * \brief Set the endponints and gateways for a ASroute
357  */
358 static void parse_S_ASroute_new_and_endpoints(void)
359 {
360   if (src != NULL && dst != NULL && link_list != NULL)
361     THROW2(arg_error, 0, "Route between %s to %s can not be defined",
362            A_surfxml_ASroute_src, A_surfxml_ASroute_dst);
363   src = A_surfxml_ASroute_src;
364   dst = A_surfxml_ASroute_dst;
365   gw_src = A_surfxml_ASroute_gw_src;
366   gw_dst = A_surfxml_ASroute_gw_dst;
367   xbt_assert2(strlen(src) > 0 || strlen(dst) > 0 || strlen(gw_src) > 0
368               || strlen(gw_dst) > 0,
369               "Some limits are null in the route between \"%s\" and \"%s\"",
370               src, dst);
371   link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
372 }
373
374 /**
375  * \brief Set the endponints for a bypassRoute
376  */
377 static void parse_S_bypassRoute_new_and_endpoints(void)
378 {
379   if (src != NULL && dst != NULL && link_list != NULL)
380     THROW2(arg_error, 0,
381            "Bypass Route between %s to %s can not be defined",
382            A_surfxml_bypassRoute_src, A_surfxml_bypassRoute_dst);
383   src = A_surfxml_bypassRoute_src;
384   dst = A_surfxml_bypassRoute_dst;
385   gw_src = A_surfxml_bypassRoute_gw_src;
386   gw_dst = A_surfxml_bypassRoute_gw_dst;
387   xbt_assert2(strlen(src) > 0 || strlen(dst) > 0 || strlen(gw_src) > 0
388               || strlen(gw_dst) > 0,
389               "Some limits are null in the route between \"%s\" and \"%s\"",
390               src, dst);
391   link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
392 }
393
394 /**
395  * \brief Set a new link on the actual list of link for a route or ASroute
396  */
397 static void parse_E_link_ctn_new_elem(const char *link_id)
398 {
399   char *val;
400   val = xbt_strdup(link_id);
401   xbt_dynar_push(link_list, &val);
402 }
403
404 /**
405  * \brief Set a new link on the actual list of link for a route or ASroute from XML
406  */
407
408 static void parse_E_link_ctn_new_elem_XML(void)
409 {
410   if (A_surfxml_link_ctn_direction == A_surfxml_link_ctn_direction_NONE)
411     parse_E_link_ctn_new_elem(A_surfxml_link_ctn_id);
412   if (A_surfxml_link_ctn_direction == A_surfxml_link_ctn_direction_UP) {
413     char *link_id = bprintf("%s_UP", A_surfxml_link_ctn_id);
414     parse_E_link_ctn_new_elem(link_id);
415     free(link_id);
416   }
417   if (A_surfxml_link_ctn_direction == A_surfxml_link_ctn_direction_DOWN) {
418     char *link_id = bprintf("%s_DOWN", A_surfxml_link_ctn_id);
419     parse_E_link_ctn_new_elem(link_id);
420     free(link_id);
421   }
422 }
423
424 /**
425  * \brief Set a new link on the actual list of link for a route or ASroute from lua
426  */
427 static void parse_E_link_c_ctn_new_elem_lua(const char *link_id)
428 {
429   parse_E_link_ctn_new_elem(link_id);
430 }
431
432 /**
433  * \brief Store the route by calling the set_route function of the current routing component
434  */
435 static void parse_E_route_store_route(void)
436 {
437   name_route_extended_t route = xbt_new0(s_name_route_extended_t, 1);
438   route->generic_route.link_list = link_list;
439   xbt_assert1(current_routing->set_route,
440               "no defined method \"set_route\" in \"%s\"",
441               current_routing->name);
442   (*(current_routing->set_route)) (current_routing, src, dst, route);
443   link_list = NULL;
444   src = NULL;
445   dst = NULL;
446 }
447
448 /**
449  * \brief Store the ASroute by calling the set_ASroute function of the current routing component
450  */
451 static void parse_E_ASroute_store_route(void)
452 {
453   name_route_extended_t e_route = xbt_new0(s_name_route_extended_t, 1);
454   e_route->generic_route.link_list = link_list;
455   e_route->src_gateway = xbt_strdup(gw_src);
456   e_route->dst_gateway = xbt_strdup(gw_dst);
457   xbt_assert1(current_routing->set_ASroute,
458               "no defined method \"set_ASroute\" in \"%s\"",
459               current_routing->name);
460   (*(current_routing->set_ASroute)) (current_routing, src, dst, e_route);
461   link_list = NULL;
462   src = NULL;
463   dst = NULL;
464   gw_src = NULL;
465   gw_dst = NULL;
466 }
467
468 /**
469  * \brief Store the bypass route by calling the set_bypassroute function of the current routing component
470  */
471 static void parse_E_bypassRoute_store_route(void)
472 {
473   route_extended_t e_route = xbt_new0(s_route_extended_t, 1);
474   e_route->generic_route.link_list = link_list;
475   e_route->src_gateway = xbt_strdup(gw_src);
476   e_route->dst_gateway = xbt_strdup(gw_dst);
477   xbt_assert1(current_routing->set_bypassroute,
478               "no defined method \"set_bypassroute\" in \"%s\"",
479               current_routing->name);
480   (*(current_routing->set_bypassroute)) (current_routing, src, dst,
481                                          e_route);
482   link_list = NULL;
483   src = NULL;
484   dst = NULL;
485   gw_src = NULL;
486   gw_dst = NULL;
487 }
488
489 /**
490  * \brief Make a new routing component
491  *
492  * make the new structure and
493  * set the parsing functions to allows parsing the part of the routing tree
494  */
495 static void parse_S_AS(char *AS_id, char *AS_routing)
496 {
497   routing_component_t new_routing;
498   model_type_t model = NULL;
499   char *wanted = AS_routing;
500   int cpt;
501   /* seach the routing model */
502   for (cpt = 0; routing_models[cpt].name; cpt++)
503     if (!strcmp(wanted, routing_models[cpt].name))
504       model = &routing_models[cpt];
505   /* if its not exist, error */
506   if (model == NULL) {
507     fprintf(stderr, "Routing model %s not found. Existing models:\n",
508             wanted);
509     for (cpt = 0; routing_models[cpt].name; cpt++)
510       if (!strcmp(wanted, routing_models[cpt].name))
511         fprintf(stderr, "   %s: %s\n", routing_models[cpt].name,
512                 routing_models[cpt].desc);
513     xbt_die(NULL);
514   }
515
516   /* make a new routing component */
517   new_routing = (routing_component_t) (*(model->create)) ();
518   new_routing->routing = model;
519   new_routing->hierarchy = SURF_ROUTING_NULL;
520   new_routing->name = xbt_strdup(AS_id);
521   new_routing->routing_sons = xbt_dict_new();
522
523   /* Hack for Vivaldi */
524   if(!strcmp(model->name,"Vivaldi"))
525         new_routing->get_latency = vivaldi_get_link_latency;
526
527   if (current_routing == NULL && global_routing->root == NULL) {
528
529     /* it is the first one */
530     new_routing->routing_father = NULL;
531     global_routing->root = new_routing;
532
533   } else if (current_routing != NULL && global_routing->root != NULL) {
534
535     xbt_assert1(!xbt_dict_get_or_null
536                 (current_routing->routing_sons, AS_id),
537                 "The AS \"%s\" already exists", AS_id);
538     /* it is a part of the tree */
539     new_routing->routing_father = current_routing;
540     /* set the father behavior */
541     if (current_routing->hierarchy == SURF_ROUTING_NULL)
542       current_routing->hierarchy = SURF_ROUTING_RECURSIVE;
543     /* add to the sons dictionary */
544     xbt_dict_set(current_routing->routing_sons, AS_id,
545                  (void *) new_routing, NULL);
546     /* add to the father element list */
547     (*(current_routing->set_autonomous_system)) (current_routing, AS_id);
548     /* unload the prev parse rules */
549     (*(current_routing->routing->unload)) ();
550
551   } else {
552     THROW0(arg_error, 0, "All defined components must be belong to a AS");
553   }
554   /* set the new parse rules */
555   (*(new_routing->routing->load)) ();
556   /* set the new current component of the tree */
557   current_routing = new_routing;
558 }
559
560 /*
561  * Detect the routing model type of the routing component from XML platforms
562  */
563 static void parse_S_AS_XML(void)
564 {
565   parse_S_AS(A_surfxml_AS_id, A_surfxml_AS_routing);
566
567   if (strcmp(A_surfxml_AS_coordinates,"")) {
568     XBT_DEBUG("%s coordinates : %s", A_surfxml_AS_id, A_surfxml_AS_coordinates);
569     xbt_dynar_t ctn = xbt_str_split_str(A_surfxml_AS_coordinates, " ");
570     xbt_dynar_shrink(ctn, 0);
571     xbt_dict_set(coordinates, A_surfxml_AS_id, ctn, xbt_dynar_free_voidp);
572   }
573 }
574
575 /*
576  * define the routing model type of routing component from lua script
577  */
578 static void parse_S_AS_lua(char *id, char *mode)
579 {
580   parse_S_AS(id, mode);
581 }
582
583
584 /**
585  * \brief Finish the creation of a new routing component
586  *
587  * When you finish to read the routing component, other structures must be created. 
588  * the "end" method allow to do that for any routing model type
589  */
590 static void parse_E_AS(const char *AS_id)
591 {
592
593   if (current_routing == NULL) {
594     THROW1(arg_error, 0, "Close AS(%s), that never open", AS_id);
595   } else {
596     network_element_info_t info = NULL;
597     xbt_assert1(!xbt_dict_get_or_null
598                 (global_routing->where_network_elements,
599                  current_routing->name), "The AS \"%s\" already exists",
600                 current_routing->name);
601     info = xbt_new0(s_network_element_info_t, 1);
602     info->rc_component = current_routing->routing_father;
603     info->rc_type = SURF_NETWORK_ELEMENT_AS;
604     xbt_dict_set(global_routing->where_network_elements,
605                  current_routing->name, info, xbt_free);
606     (*(current_routing->routing->unload)) ();
607     (*(current_routing->routing->end)) ();
608     current_routing = current_routing->routing_father;
609     if (current_routing != NULL)
610       (*(current_routing->routing->load)) ();
611   }
612 }
613
614 /*
615  * \brief Finish the creation of a new routing component from XML
616  */
617 static void parse_E_AS_XML(void)
618 {
619   parse_E_AS(A_surfxml_AS_id);
620 }
621
622 /*
623  * \brief Finish the creation of a new routing component from lua
624  */
625 static void parse_E_AS_lua(const char *id)
626 {
627   parse_E_AS(id);
628 }
629
630 /* Aux Business methods */
631
632 /**
633  * \brief Get the AS name of the element
634  *
635  * \param name the host name
636  *
637  */
638 static char* elements_As_name(const char *name)
639 {
640   routing_component_t as_comp;
641
642   /* (1) find the as where the host is located */
643   as_comp = ((network_element_info_t)
644             xbt_dict_get_or_null(global_routing->where_network_elements,
645                                  name))->rc_component;
646   return as_comp->name;
647 }
648
649
650 /**
651  * \brief Get the AS father and the first elements of the chain
652  *
653  * \param src the source host name 
654  * \param dst the destination host name
655  * 
656  * Get the common father of the to processing units, and the first different 
657  * father in the chain
658  */
659 static xbt_dynar_t elements_father(const char *src, const char *dst)
660 {
661
662   xbt_assert0(src && dst, "bad parameters for \"elements_father\" method");
663
664   xbt_dynar_t result = xbt_dynar_new(sizeof(char *), NULL);
665
666   routing_component_t src_as, dst_as;
667   int index_src, index_dst, index_father_src, index_father_dst;
668   xbt_dynar_t path_src = NULL;
669   xbt_dynar_t path_dst = NULL;
670   routing_component_t current = NULL;
671   routing_component_t *current_src = NULL;
672   routing_component_t *current_dst = NULL;
673   routing_component_t *father = NULL;
674
675   /* (1) find the as where the src and dst are located */
676   src_as = ((network_element_info_t)
677             xbt_dict_get_or_null(global_routing->where_network_elements,
678                                  src))->rc_component;
679   dst_as = ((network_element_info_t)
680             xbt_dict_get_or_null(global_routing->where_network_elements,
681                                  dst))->rc_component;
682   xbt_assert2(src_as
683               && dst_as,
684               "Ask for route \"from\"(%s) or \"to\"(%s) no found", src,
685               dst);
686
687   /* (2) find the path to the root routing component */
688   path_src = xbt_dynar_new(sizeof(routing_component_t), NULL);
689   current = src_as;
690   while (current != NULL) {
691     xbt_dynar_push(path_src, &current);
692     current = current->routing_father;
693   }
694   path_dst = xbt_dynar_new(sizeof(routing_component_t), NULL);
695   current = dst_as;
696   while (current != NULL) {
697     xbt_dynar_push(path_dst, &current);
698     current = current->routing_father;
699   }
700
701   /* (3) find the common father */
702   index_src = path_src->used - 1;
703   index_dst = path_dst->used - 1;
704   current_src = xbt_dynar_get_ptr(path_src, index_src);
705   current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
706   while (index_src >= 0 && index_dst >= 0 && *current_src == *current_dst) {
707     current_src = xbt_dynar_get_ptr(path_src, index_src);
708     current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
709     index_src--;
710     index_dst--;
711   }
712   index_src++;
713   index_dst++;
714   current_src = xbt_dynar_get_ptr(path_src, index_src);
715   current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
716
717   /* (4) they are not in the same routing component, make the path */
718   index_father_src = index_src + 1;
719   index_father_dst = index_dst + 1;
720
721   if (*current_src == *current_dst)
722     father = current_src;
723   else
724     father = xbt_dynar_get_ptr(path_src, index_father_src);
725
726   /* (5) result generation */
727   xbt_dynar_push(result, father);       /* first same the father of src and dst */
728   xbt_dynar_push(result, current_src);  /* second the first different father of src */
729   xbt_dynar_push(result, current_dst);  /* three  the first different father of dst */
730
731   xbt_dynar_free(&path_src);
732   xbt_dynar_free(&path_dst);
733
734   return result;
735 }
736
737 /* Global Business methods */
738
739 /**
740  * \brief Recursive function for get_route
741  *
742  * \param src the source host name 
743  * \param dst the destination host name
744  * 
745  * This fuction is call by "get_route". It allow to walk through the 
746  * routing components tree.
747  */
748 static route_extended_t _get_route(const char *src, const char *dst)
749 {
750
751   void *link;
752   unsigned int cpt = 0;
753
754   XBT_DEBUG("Solve route  \"%s\" to \"%s\"", src, dst);
755
756   xbt_assert0(src && dst, "bad parameters for \"_get_route\" method");
757
758   route_extended_t e_route, e_route_cnt, e_route_src = NULL, e_route_dst =
759       NULL;
760
761   xbt_dynar_t elem_father_list = elements_father(src, dst);
762
763   routing_component_t common_father =
764       xbt_dynar_get_as(elem_father_list, 0, routing_component_t);
765   routing_component_t src_father =
766       xbt_dynar_get_as(elem_father_list, 1, routing_component_t);
767   routing_component_t dst_father =
768       xbt_dynar_get_as(elem_father_list, 2, routing_component_t);
769
770   e_route = xbt_new0(s_route_extended_t, 1);
771   e_route->src_gateway = NULL;
772   e_route->dst_gateway = NULL;
773   e_route->generic_route.link_list =
774       xbt_dynar_new(global_routing->size_of_link, NULL);
775
776   if (src_father == dst_father) {       /* SURF_ROUTING_BASE */
777
778     if (strcmp(src, dst)) {
779       e_route_cnt =
780           (*(common_father->get_route)) (common_father, src, dst);
781       xbt_assert2(e_route_cnt, "no route between \"%s\" and \"%s\"", src,
782                   dst);
783       xbt_dynar_foreach(e_route_cnt->generic_route.link_list, cpt, link) {
784         xbt_dynar_push(e_route->generic_route.link_list, &link);
785       }
786       generic_free_extended_route(e_route_cnt);
787     }
788
789   } else {                      /* SURF_ROUTING_RECURSIVE */
790
791     route_extended_t e_route_bypass = NULL;
792
793     if (common_father->get_bypass_route)
794       e_route_bypass =
795           (*(common_father->get_bypass_route)) (common_father, src, dst);
796
797     if (e_route_bypass)
798       e_route_cnt = e_route_bypass;
799     else
800       e_route_cnt =
801           (*(common_father->get_route)) (common_father, src_father->name,
802                                          dst_father->name);
803
804     xbt_assert2(e_route_cnt, "no route between \"%s\" and \"%s\"",
805                 src_father->name, dst_father->name);
806
807     xbt_assert2((e_route_cnt->src_gateway == NULL) ==
808                 (e_route_cnt->dst_gateway == NULL),
809                 "bad gateway for route between \"%s\" and \"%s\"", src,
810                 dst);
811
812     if (strcmp(src, e_route_cnt->src_gateway)) {
813       e_route_src = _get_route(src, e_route_cnt->src_gateway);
814       xbt_assert2(e_route_src, "no route between \"%s\" and \"%s\"", src,
815                   e_route_cnt->src_gateway);
816       xbt_dynar_foreach(e_route_src->generic_route.link_list, cpt, link) {
817         xbt_dynar_push(e_route->generic_route.link_list, &link);
818       }
819     }
820
821     xbt_dynar_foreach(e_route_cnt->generic_route.link_list, cpt, link) {
822       xbt_dynar_push(e_route->generic_route.link_list, &link);
823     }
824
825     if (strcmp(e_route_cnt->dst_gateway, dst)) {
826       e_route_dst = _get_route(e_route_cnt->dst_gateway, dst);
827       xbt_assert2(e_route_dst, "no route between \"%s\" and \"%s\"",
828                   e_route_cnt->dst_gateway, dst);
829       xbt_dynar_foreach(e_route_dst->generic_route.link_list, cpt, link) {
830         xbt_dynar_push(e_route->generic_route.link_list, &link);
831       }
832     }
833
834     e_route->src_gateway = xbt_strdup(e_route_cnt->src_gateway);
835     e_route->dst_gateway = xbt_strdup(e_route_cnt->dst_gateway);
836
837     generic_free_extended_route(e_route_src);
838     generic_free_extended_route(e_route_cnt);
839     generic_free_extended_route(e_route_dst);
840   }
841
842   xbt_dynar_free(&elem_father_list);
843
844   return e_route;
845 }
846
847 static double _get_latency(const char *src, const char *dst)
848 {
849   double latency, latency_src, latency_dst = 0.0;
850
851   XBT_DEBUG("Solve route  \"%s\" to \"%s\"", src, dst);
852   xbt_assert0(src && dst, "bad parameters for \"_get_route\" method");
853
854   route_extended_t e_route_cnt;
855
856   xbt_dynar_t elem_father_list = elements_father(src, dst);
857
858   routing_component_t common_father =
859       xbt_dynar_get_as(elem_father_list, 0, routing_component_t);
860   routing_component_t src_father =
861       xbt_dynar_get_as(elem_father_list, 1, routing_component_t);
862   routing_component_t dst_father =
863       xbt_dynar_get_as(elem_father_list, 2, routing_component_t);
864
865   if (src_father == dst_father) {       /* SURF_ROUTING_BASE */
866
867     if (strcmp(src, dst)) {
868       latency =
869           (*(common_father->get_latency)) (common_father, src, dst);
870       xbt_assert2(latency>=0, "no route between \"%s\" and \"%s\"", src,
871                   dst);
872      } else latency = 0;
873   } else {                      /* SURF_ROUTING_RECURSIVE */
874      route_extended_t e_route_bypass = NULL;
875     if (common_father->get_bypass_route)
876       e_route_bypass =
877           (*(common_father->get_bypass_route)) (common_father, src, dst);
878
879     xbt_assert0(!e_route_bypass,"Bypass cannot work yet with get_latency"); 
880                                                  
881     e_route_cnt =
882           (*(common_father->get_route)) (common_father, src_father->name,
883                                          dst_father->name);
884
885     xbt_assert2(e_route_cnt, "no route between \"%s\" and \"%s\"",
886                 src_father->name, dst_father->name);
887
888     xbt_assert2((e_route_cnt->src_gateway == NULL) ==
889                 (e_route_cnt->dst_gateway == NULL),
890                 "bad gateway for route between \"%s\" and \"%s\"", src,
891                 dst);            
892     latency =
893           (*(common_father->get_latency)) (common_father, elements_As_name(src),
894                           elements_As_name(dst));
895
896     xbt_assert2(latency>=0, "no route between \"%s\" and \"%s\"",
897                 src_father->name, dst_father->name);
898     
899
900     if (src != e_route_cnt->src_gateway) {
901
902       latency_src = _get_latency(src, e_route_cnt->src_gateway);
903       xbt_assert2(latency_src>=0, "no route between \"%s\" and \"%s\"", src,
904                   e_route_cnt->src_gateway);
905       latency += latency_src;
906     }
907
908     if (e_route_cnt->dst_gateway != dst) {
909     
910       latency_dst = _get_latency(e_route_cnt->dst_gateway, dst);
911       xbt_assert2(latency_dst>=0, "no route between \"%s\" and \"%s\"",
912                   e_route_cnt->dst_gateway, dst);
913       latency += latency_dst;
914     }
915         
916   }
917
918   xbt_dynar_free(&elem_father_list);
919
920   return latency;
921 }
922
923 /**
924  * \brief Generic method: find a route between hosts
925  *
926  * \param src the source host name 
927  * \param dst the destination host name
928  * 
929  * walk through the routing components tree and find a route between hosts
930  * by calling the differents "get_route" functions in each routing component.
931  * No need to free the returned dynar. It will be freed at the next call.
932  */
933 static xbt_dynar_t get_route(const char *src, const char *dst)
934 {
935
936   route_extended_t e_route;
937   xbt_dynar_t elem_father_list = NULL;
938   routing_component_t common_father = NULL;
939
940   if (strcmp(src, dst))
941     e_route = _get_route(src, dst);
942   else {
943     elem_father_list = elements_father(src, dst);
944     common_father =
945         xbt_dynar_get_as(elem_father_list, 0, routing_component_t);
946
947     e_route = (*(common_father->get_route)) (common_father, src, dst);
948     xbt_dynar_free(&elem_father_list);
949   }
950
951   xbt_assert2(e_route, "no route between \"%s\" and \"%s\"", src, dst);
952
953   if (global_routing->last_route)
954     xbt_dynar_free(&(global_routing->last_route));
955   global_routing->last_route = e_route->generic_route.link_list;
956
957   if (e_route->src_gateway)
958     xbt_free(e_route->src_gateway);
959   if (e_route->dst_gateway)
960     xbt_free(e_route->dst_gateway);
961
962   xbt_free(e_route);
963
964 /*
965   if (xbt_dynar_length(global_routing->last_route) == 0)
966     return NULL;
967   else
968 */
969     return global_routing->last_route;
970 }
971
972 /**
973  * \brief Generic method: find a route between hosts
974  *
975  * \param src the source host name
976  * \param dst the destination host name
977  *
978  * walk through the routing components tree and find a route between hosts
979  * by calling the differents "get_route" functions in each routing component.
980  * Leaves the caller the responsability to clean the returned dynar.
981  */
982 static xbt_dynar_t get_route_no_cleanup(const char *src, const char *dst)
983 {
984         xbt_dynar_t d = get_route(src,dst);
985         global_routing->last_route = NULL;
986         return d;
987 }
988
989 /*Get Latency*/
990 static double get_latency(const char *src, const char *dst)
991 {
992
993   double latency = -1.0;
994   xbt_dynar_t elem_father_list = elements_father(src, dst);
995   routing_component_t common_father =
996       xbt_dynar_get_as(elem_father_list, 0, routing_component_t);
997
998   if (strcmp(src, dst))
999     latency = _get_latency(src, dst);
1000   else
1001     latency = (*(common_father->get_latency)) (common_father, src, dst);
1002
1003   xbt_assert2(latency>=0.0, "no route between \"%s\" and \"%s\"", src, dst);
1004   xbt_dynar_free(&elem_father_list);
1005
1006   return latency;
1007 }
1008
1009 /**
1010  * \brief Recursive function for finalize
1011  *
1012  * \param rc the source host name 
1013  * 
1014  * This fuction is call by "finalize". It allow to finalize the 
1015  * AS or routing components. It delete all the structures.
1016  */
1017 static void _finalize(routing_component_t rc)
1018 {
1019   if (rc) {
1020     xbt_dict_cursor_t cursor = NULL;
1021     char *key;
1022     routing_component_t elem;
1023     xbt_dict_foreach(rc->routing_sons, cursor, key, elem) {
1024       _finalize(elem);
1025     }
1026     xbt_dict_t tmp_sons = rc->routing_sons;
1027     char *tmp_name = rc->name;
1028     xbt_dict_free(&tmp_sons);
1029     xbt_free(tmp_name);
1030     xbt_assert1(rc->finalize, "no defined method \"finalize\" in \"%s\"",
1031                 current_routing->name);
1032     (*(rc->finalize)) (rc);
1033   }
1034 }
1035
1036 /**
1037  * \brief Generic method: delete all the routing structures
1038  * 
1039  * walk through the routing components tree and delete the structures
1040  * by calling the differents "finalize" functions in each routing component
1041  */
1042 static void finalize(void)
1043 {
1044   /* delete recursibly all the tree */
1045   _finalize(global_routing->root);
1046   /* delete "where" dict */
1047   xbt_dict_free(&(global_routing->where_network_elements));
1048   xbt_dict_free(&(coordinates));
1049   /* delete last_route */
1050   xbt_dynar_free(&(global_routing->last_route));
1051   /* delete global routing structure */
1052   xbt_free(global_routing);
1053 }
1054
1055 static xbt_dynar_t recursive_get_onelink_routes(routing_component_t rc)
1056 {
1057   xbt_dynar_t ret = xbt_dynar_new(sizeof(onelink_t), xbt_free);
1058
1059   //adding my one link routes
1060   unsigned int cpt;
1061   void *link;
1062   xbt_dynar_t onelink_mine = rc->get_onelink_routes(rc);
1063   if (onelink_mine) {
1064     xbt_dynar_foreach(onelink_mine, cpt, link) {
1065       xbt_dynar_push(ret, &link);
1066     }
1067   }
1068   //recursing
1069   char *key;
1070   xbt_dict_cursor_t cursor = NULL;
1071   routing_component_t rc_child;
1072   xbt_dict_foreach(rc->routing_sons, cursor, key, rc_child) {
1073     xbt_dynar_t onelink_child = recursive_get_onelink_routes(rc_child);
1074     if (onelink_child) {
1075       xbt_dynar_foreach(onelink_child, cpt, link) {
1076         xbt_dynar_push(ret, &link);
1077       }
1078     }
1079   }
1080   return ret;
1081 }
1082
1083 static xbt_dynar_t get_onelink_routes(void)
1084 {
1085   return recursive_get_onelink_routes(global_routing->root);
1086 }
1087
1088 e_surf_network_element_type_t get_network_element_type(const char
1089                                                               *name)
1090 {
1091   network_element_info_t rc = NULL;
1092   rc = xbt_dict_get_or_null(global_routing->where_network_elements, name);
1093   if(!rc) return SURF_NETWORK_ELEMENT_NULL;
1094   return rc->rc_type;
1095 }
1096
1097 /**
1098  * \brief Generic method: create the global routing schema
1099  * 
1100  * Make a global routing structure and set all the parsing functions.
1101  */
1102 void routing_model_create(size_t size_of_links, void *loopback, double_f_cpvoid_t get_link_latency_fun)
1103 {
1104   /* config the uniq global routing */
1105   global_routing = xbt_new0(s_routing_global_t, 1);
1106   global_routing->where_network_elements = xbt_dict_new();
1107   global_routing->root = NULL;
1108   global_routing->get_route = get_route;
1109   global_routing->get_latency = get_latency;
1110   global_routing->get_route_no_cleanup = get_route_no_cleanup;
1111   global_routing->get_onelink_routes = get_onelink_routes;
1112   global_routing->get_network_element_type = get_network_element_type;
1113   global_routing->finalize = finalize;
1114   global_routing->loopback = loopback;
1115   global_routing->size_of_link = size_of_links;
1116   global_routing->last_route = NULL;
1117   get_link_latency = get_link_latency_fun;
1118   /* no current routing at moment */
1119   current_routing = NULL;
1120
1121   coordinates = xbt_dict_new();
1122
1123   /* parse generic elements */
1124   surfxml_add_callback(STag_surfxml_host_cb_list, &parse_S_host_XML);
1125   surfxml_add_callback(ETag_surfxml_host_cb_list, &parse_E_host_XML);
1126   surfxml_add_callback(STag_surfxml_router_cb_list, &parse_S_router_XML);
1127
1128   surfxml_add_callback(STag_surfxml_route_cb_list,
1129                        &parse_S_route_new_and_endpoints_XML);
1130   surfxml_add_callback(STag_surfxml_ASroute_cb_list,
1131                        &parse_S_ASroute_new_and_endpoints);
1132   surfxml_add_callback(STag_surfxml_bypassRoute_cb_list,
1133                        &parse_S_bypassRoute_new_and_endpoints);
1134
1135   surfxml_add_callback(ETag_surfxml_link_ctn_cb_list,
1136                        &parse_E_link_ctn_new_elem_XML);
1137
1138   surfxml_add_callback(ETag_surfxml_route_cb_list,
1139                        &parse_E_route_store_route);
1140   surfxml_add_callback(ETag_surfxml_ASroute_cb_list,
1141                        &parse_E_ASroute_store_route);
1142   surfxml_add_callback(ETag_surfxml_bypassRoute_cb_list,
1143                        &parse_E_bypassRoute_store_route);
1144
1145   surfxml_add_callback(STag_surfxml_AS_cb_list, &parse_S_AS_XML);
1146   surfxml_add_callback(ETag_surfxml_AS_cb_list, &parse_E_AS_XML);
1147
1148   surfxml_add_callback(STag_surfxml_cluster_cb_list,
1149                        &routing_parse_Scluster);
1150
1151   surfxml_add_callback(STag_surfxml_peer_cb_list,
1152                          &routing_parse_Speer);
1153
1154   surfxml_add_callback(ETag_surfxml_platform_cb_list,
1155                                                   &clean_dict_random);
1156
1157 #ifdef HAVE_TRACING
1158   instr_routing_define_callbacks();
1159 #endif
1160 }
1161
1162 void surf_parse_add_callback_config(void)
1163 {
1164         surfxml_add_callback(STag_surfxml_config_cb_list, &routing_parse_Sconfig);
1165         surfxml_add_callback(ETag_surfxml_config_cb_list, &routing_parse_Econfig);
1166         surfxml_add_callback(STag_surfxml_prop_cb_list, &parse_properties);
1167         surfxml_add_callback(STag_surfxml_AS_cb_list, &surf_parse_models_setup);
1168         surfxml_add_callback(STag_surfxml_random_cb_list, &routing_parse_Srandom);
1169 }
1170
1171 void surf_parse_models_setup()
1172 {
1173         routing_parse_Erandom();
1174         surfxml_del_callback(STag_surfxml_AS_cb_list, surf_parse_models_setup);
1175         surf_config_models_setup(platform_filename);
1176         free(platform_filename);
1177 }
1178
1179 /* ************************************************************************** */
1180 /* *************************** FULL ROUTING ********************************* */
1181
1182 #define TO_ROUTE_FULL(i,j) routing->routing_table[(i)+(j)*table_size]
1183
1184 /* Routing model structure */
1185
1186 typedef struct {
1187   s_routing_component_t generic_routing;
1188   route_extended_t *routing_table;
1189 } s_routing_component_full_t, *routing_component_full_t;
1190
1191 /* Business methods */
1192 static xbt_dynar_t full_get_onelink_routes(routing_component_t rc)
1193 {
1194   xbt_dynar_t ret = xbt_dynar_new(sizeof(onelink_t), xbt_free);
1195
1196   routing_component_full_t routing = (routing_component_full_t) rc;
1197   size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
1198   xbt_dict_cursor_t c1 = NULL, c2 = NULL;
1199   char *k1, *d1, *k2, *d2;
1200   xbt_dict_foreach(routing->generic_routing.to_index, c1, k1, d1) {
1201     xbt_dict_foreach(routing->generic_routing.to_index, c2, k2, d2) {
1202       int *src_id = xbt_dict_get_or_null(routing->generic_routing.to_index, k1);
1203       int *dst_id = xbt_dict_get_or_null(routing->generic_routing.to_index, k2);
1204       xbt_assert2(src_id
1205                   && dst_id,
1206                   "Ask for route \"from\"(%s)  or \"to\"(%s) no found in the local table",
1207                   src, dst);
1208       route_extended_t route = TO_ROUTE_FULL(*src_id, *dst_id);
1209       if (route) {
1210         if (xbt_dynar_length(route->generic_route.link_list) == 1) {
1211           void *link =
1212               *(void **) xbt_dynar_get_ptr(route->generic_route.link_list,
1213                                            0);
1214           onelink_t onelink = xbt_new0(s_onelink_t, 1);
1215           onelink->link_ptr = link;
1216           if (routing->generic_routing.hierarchy == SURF_ROUTING_BASE) {
1217             onelink->src = xbt_strdup(k1);
1218             onelink->dst = xbt_strdup(k2);
1219           } else if (routing->generic_routing.hierarchy ==
1220                      SURF_ROUTING_RECURSIVE) {
1221             onelink->src = xbt_strdup(route->src_gateway);
1222             onelink->dst = xbt_strdup(route->dst_gateway);
1223           }
1224           xbt_dynar_push(ret, &onelink);
1225         }
1226       }
1227     }
1228   }
1229   return ret;
1230 }
1231
1232 static route_extended_t full_get_route(routing_component_t rc,
1233                                        const char *src, const char *dst)
1234 {
1235   xbt_assert1(rc && src
1236               && dst,
1237               "Invalid params for \"get_route\" function at AS \"%s\"",
1238               rc->name);
1239
1240   /* set utils vars */
1241   routing_component_full_t routing = (routing_component_full_t) rc;
1242   size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
1243
1244   generic_src_dst_check(rc, src, dst);
1245   int *src_id = xbt_dict_get_or_null(routing->generic_routing.to_index, src);
1246   int *dst_id = xbt_dict_get_or_null(routing->generic_routing.to_index, dst);
1247   xbt_assert2(src_id
1248               && dst_id,
1249               "Ask for route \"from\"(%s)  or \"to\"(%s) no found in the local table",
1250               src, dst);
1251
1252   route_extended_t e_route = NULL;
1253   route_extended_t new_e_route = NULL;
1254   void *link;
1255   unsigned int cpt = 0;
1256
1257   e_route = TO_ROUTE_FULL(*src_id, *dst_id);
1258
1259   if (e_route) {
1260     new_e_route = xbt_new0(s_route_extended_t, 1);
1261     new_e_route->src_gateway = xbt_strdup(e_route->src_gateway);
1262     new_e_route->dst_gateway = xbt_strdup(e_route->dst_gateway);
1263     new_e_route->generic_route.link_list =
1264         xbt_dynar_new(global_routing->size_of_link, NULL);
1265     xbt_dynar_foreach(e_route->generic_route.link_list, cpt, link) {
1266       xbt_dynar_push(new_e_route->generic_route.link_list, &link);
1267     }
1268   }
1269   return new_e_route;
1270 }
1271
1272 static void full_finalize(routing_component_t rc)
1273 {
1274   routing_component_full_t routing = (routing_component_full_t) rc;
1275   size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
1276   int i, j;
1277   if (routing) {
1278     /* Delete routing table */
1279     for (i = 0; i < table_size; i++)
1280       for (j = 0; j < table_size; j++)
1281         generic_free_extended_route(TO_ROUTE_FULL(i, j));
1282     xbt_free(routing->routing_table);
1283     /* Delete bypass dict */
1284     xbt_dict_free(&rc->bypassRoutes);
1285     /* Delete index dict */
1286     xbt_dict_free(&rc->to_index);
1287     /* Delete structure */
1288     xbt_free(rc);
1289   }
1290 }
1291
1292 /* Creation routing model functions */
1293
1294 static void *model_full_create(void)
1295 {
1296   routing_component_full_t new_component =
1297       xbt_new0(s_routing_component_full_t, 1);
1298   new_component->generic_routing.set_processing_unit =
1299       generic_set_processing_unit;
1300   new_component->generic_routing.set_autonomous_system =
1301       generic_set_autonomous_system;
1302   new_component->generic_routing.set_route = model_full_set_route;
1303   new_component->generic_routing.set_ASroute = model_full_set_route;
1304   new_component->generic_routing.set_bypassroute = generic_set_bypassroute;
1305   new_component->generic_routing.get_route = full_get_route;
1306   new_component->generic_routing.get_latency = generic_get_link_latency;
1307   new_component->generic_routing.get_onelink_routes =
1308       full_get_onelink_routes;
1309   new_component->generic_routing.get_bypass_route =
1310       generic_get_bypassroute;
1311   new_component->generic_routing.finalize = full_finalize;
1312   new_component->generic_routing.to_index = xbt_dict_new();
1313   new_component->generic_routing.bypassRoutes = xbt_dict_new();
1314   new_component->generic_routing.get_network_element_type = get_network_element_type;
1315   return new_component;
1316 }
1317
1318 static void model_full_load(void)
1319 {
1320   /* use "surfxml_add_callback" to add a parse function call */
1321 }
1322
1323 static void model_full_unload(void)
1324 {
1325   /* use "surfxml_del_callback" to remove a parse function call */
1326 }
1327
1328 static void model_full_end(void)
1329 {
1330   unsigned int i;
1331   route_extended_t e_route;
1332
1333   /* set utils vars */
1334   routing_component_full_t routing =
1335       ((routing_component_full_t) current_routing);
1336   size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
1337
1338   /* Create table if necessary */
1339   if(!routing->routing_table)
1340           routing->routing_table = xbt_new0(route_extended_t, table_size * table_size);
1341
1342   /* Add the loopback if needed */
1343   if (current_routing->hierarchy == SURF_ROUTING_BASE) {
1344     for (i = 0; i < table_size; i++) {
1345       e_route = TO_ROUTE_FULL(i, i);
1346       if (!e_route) {
1347         e_route = xbt_new0(s_route_extended_t, 1);
1348         e_route->src_gateway = NULL;
1349         e_route->dst_gateway = NULL;
1350         e_route->generic_route.link_list =
1351             xbt_dynar_new(global_routing->size_of_link, NULL);
1352         xbt_dynar_push(e_route->generic_route.link_list,
1353                        &global_routing->loopback);
1354         TO_ROUTE_FULL(i, i) = e_route;
1355       }
1356     }
1357   }
1358 }
1359
1360 static void model_full_set_route(routing_component_t rc, const char *src,
1361                 const char *dst, name_route_extended_t route)
1362 {
1363         int *src_id, *dst_id;
1364         src_id = xbt_dict_get_or_null(rc->to_index, src);
1365         dst_id = xbt_dict_get_or_null(rc->to_index, dst);
1366         routing_component_full_t routing = ((routing_component_full_t) rc);
1367         size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
1368
1369         xbt_assert2(src_id
1370                           && dst_id, "Network elements %s or %s not found", src, dst);
1371
1372         xbt_assert2(xbt_dynar_length(route->generic_route.link_list) > 0,
1373                           "Invalid count of links, must be greater than zero (%s,%s)",
1374                           src, dst);
1375
1376         if(!routing->routing_table)
1377                 routing->routing_table = xbt_new0(route_extended_t, table_size * table_size);
1378
1379         if(TO_ROUTE_FULL(*src_id, *dst_id))
1380         {
1381                 char * link_name;
1382                 unsigned int i;
1383                 xbt_dynar_t link_route_to_test = xbt_dynar_new(global_routing->size_of_link, NULL);
1384                 xbt_dynar_foreach(route->generic_route.link_list,i,link_name)
1385                 {
1386                         void *link = xbt_dict_get_or_null(surf_network_model->resource_set, link_name);
1387                         xbt_assert1(link,"Link : '%s' doesn't exists.",link_name);
1388                         xbt_dynar_push(link_route_to_test,&link);
1389                 }
1390                 xbt_assert2(!xbt_dynar_compare(
1391                           (void*)TO_ROUTE_FULL(*src_id, *dst_id)->generic_route.link_list,
1392                           (void*)link_route_to_test,
1393                           (int_f_cpvoid_cpvoid_t) surf_pointer_resource_cmp),
1394                           "The route between \"%s\" and \"%s\" already exists. If you are trying to define a reverse route, you must set the symmetrical=no attribute to your routes tags.", src,dst);
1395         }
1396         else
1397         {
1398                   if(!route->dst_gateway && !route->src_gateway)
1399                           XBT_DEBUG("Load Route from \"%s\" to \"%s\"", src, dst);
1400                   else{
1401                           XBT_DEBUG("Load ASroute from \"%s(%s)\" to \"%s(%s)\"", src,
1402                                  route->src_gateway, dst, route->dst_gateway);
1403                           if(global_routing->get_network_element_type((const char*)route->dst_gateway) == SURF_NETWORK_ELEMENT_NULL)
1404                                   xbt_die("The dst_gateway '%s' does not exist!",route->dst_gateway);
1405                           if(global_routing->get_network_element_type((const char*)route->src_gateway) == SURF_NETWORK_ELEMENT_NULL)
1406                                   xbt_die("The src_gateway '%s' does not exist!",route->src_gateway);
1407                   }
1408               TO_ROUTE_FULL(*src_id, *dst_id) = generic_new_extended_route(rc->hierarchy,route,1);
1409               xbt_dynar_shrink(TO_ROUTE_FULL(*src_id, *dst_id)->generic_route.link_list, 0);
1410         }
1411
1412         if( A_surfxml_route_symmetrical == A_surfxml_route_symmetrical_YES
1413                 || A_surfxml_ASroute_symmetrical == A_surfxml_ASroute_symmetrical_YES )
1414         {
1415                 if(route->dst_gateway && route->src_gateway)
1416                 {
1417                   char *gw_src = xbt_strdup(route->src_gateway);
1418                   char *gw_dst = xbt_strdup(route->dst_gateway);
1419                   route->src_gateway = gw_dst;
1420                   route->dst_gateway = gw_src;
1421                 }
1422                 if(TO_ROUTE_FULL(*dst_id, *src_id))
1423                 {
1424                         char * link_name;
1425                         unsigned int i;
1426                         xbt_dynar_t link_route_to_test = xbt_dynar_new(global_routing->size_of_link, NULL);
1427                         for(i=xbt_dynar_length(route->generic_route.link_list) ;i>0 ;i--)
1428                         {
1429                                 link_name = xbt_dynar_get_as(route->generic_route.link_list,i-1,void *);
1430                                 void *link = xbt_dict_get_or_null(surf_network_model->resource_set, link_name);
1431                                 xbt_assert1(link,"Link : '%s' doesn't exists.",link_name);
1432                                 xbt_dynar_push(link_route_to_test,&link);
1433                         }
1434                         xbt_assert2(!xbt_dynar_compare(
1435                                   (void*)TO_ROUTE_FULL(*dst_id, *src_id)->generic_route.link_list,
1436                               (void*)link_route_to_test,
1437                                   (int_f_cpvoid_cpvoid_t) surf_pointer_resource_cmp),
1438                                   "The route between \"%s\" and \"%s\" already exists", src,dst);
1439                 }
1440                 else
1441                 {
1442                           if(!route->dst_gateway && !route->src_gateway)
1443                                   XBT_DEBUG("Load Route from \"%s\" to \"%s\"", dst, src);
1444                           else
1445                                   XBT_DEBUG("Load ASroute from \"%s(%s)\" to \"%s(%s)\"", dst,
1446                                          route->src_gateway, src, route->dst_gateway);
1447                       TO_ROUTE_FULL(*dst_id, *src_id) = generic_new_extended_route(rc->hierarchy,route,0);
1448                       xbt_dynar_shrink(TO_ROUTE_FULL(*dst_id, *src_id)->generic_route.link_list, 0);
1449                 }
1450
1451         }
1452 }
1453
1454 /* ************************************************************************** */
1455 /* *************************** FLOYD ROUTING ******************************** */
1456
1457 #define TO_FLOYD_COST(i,j) (routing->cost_table)[(i)+(j)*table_size]
1458 #define TO_FLOYD_PRED(i,j) (routing->predecessor_table)[(i)+(j)*table_size]
1459 #define TO_FLOYD_LINK(i,j) (routing->link_table)[(i)+(j)*table_size]
1460
1461 /* Routing model structure */
1462
1463 typedef struct {
1464   s_routing_component_t generic_routing;
1465   /* vars for calculate the floyd algorith. */
1466   int *predecessor_table;
1467   double *cost_table;
1468   route_extended_t *link_table; /* char* -> int* */
1469 } s_routing_component_floyd_t, *routing_component_floyd_t;
1470
1471 static route_extended_t floyd_get_route(routing_component_t rc,
1472                                         const char *src, const char *dst);
1473
1474 /* Business methods */
1475 static xbt_dynar_t floyd_get_onelink_routes(routing_component_t rc)
1476 {
1477   xbt_dynar_t ret = xbt_dynar_new(sizeof(onelink_t), xbt_free);
1478
1479   routing_component_floyd_t routing = (routing_component_floyd_t) rc;
1480   //size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
1481   xbt_dict_cursor_t c1 = NULL, c2 = NULL;
1482   char *k1, *d1, *k2, *d2;
1483   xbt_dict_foreach(routing->generic_routing.to_index, c1, k1, d1) {
1484     xbt_dict_foreach(routing->generic_routing.to_index, c2, k2, d2) {
1485       route_extended_t route = floyd_get_route(rc, k1, k2);
1486       if (route) {
1487         if (xbt_dynar_length(route->generic_route.link_list) == 1) {
1488           void *link =
1489               *(void **) xbt_dynar_get_ptr(route->generic_route.link_list,
1490                                            0);
1491           onelink_t onelink = xbt_new0(s_onelink_t, 1);
1492           onelink->link_ptr = link;
1493           if (routing->generic_routing.hierarchy == SURF_ROUTING_BASE) {
1494             onelink->src = xbt_strdup(k1);
1495             onelink->dst = xbt_strdup(k2);
1496           } else if (routing->generic_routing.hierarchy ==
1497                      SURF_ROUTING_RECURSIVE) {
1498             onelink->src = xbt_strdup(route->src_gateway);
1499             onelink->dst = xbt_strdup(route->dst_gateway);
1500           }
1501           xbt_dynar_push(ret, &onelink);
1502         }
1503       }
1504     }
1505   }
1506   return ret;
1507 }
1508
1509 static route_extended_t floyd_get_route(routing_component_t rc,
1510                                         const char *src, const char *dst)
1511 {
1512   xbt_assert1(rc && src
1513               && dst,
1514               "Invalid params for \"get_route\" function at AS \"%s\"",
1515               rc->name);
1516
1517   /* set utils vars */
1518   routing_component_floyd_t routing = (routing_component_floyd_t) rc;
1519   size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
1520
1521   generic_src_dst_check(rc, src, dst);
1522   int *src_id = xbt_dict_get_or_null(routing->generic_routing.to_index, src);
1523   int *dst_id = xbt_dict_get_or_null(routing->generic_routing.to_index, dst);
1524   xbt_assert2(src_id
1525               && dst_id,
1526               "Ask for route \"from\"(%s)  or \"to\"(%s) no found in the local table",
1527               src, dst);
1528
1529   /* create a result route */
1530   route_extended_t new_e_route = xbt_new0(s_route_extended_t, 1);
1531   new_e_route->generic_route.link_list =
1532       xbt_dynar_new(global_routing->size_of_link, NULL);
1533   new_e_route->src_gateway = NULL;
1534   new_e_route->dst_gateway = NULL;
1535
1536   int first = 1;
1537   int pred = *dst_id;
1538   int prev_pred = 0;
1539   char *gw_src = NULL, *gw_dst =
1540       NULL, *prev_gw_src, *prev_gw_dst, *first_gw = NULL;
1541   unsigned int cpt;
1542   void *link;
1543   xbt_dynar_t links;
1544
1545   do {
1546     prev_pred = pred;
1547     pred = TO_FLOYD_PRED(*src_id, pred);
1548     if (pred == -1)             /* if no pred in route -> no route to host */
1549       break;
1550     xbt_assert2(TO_FLOYD_LINK(pred, prev_pred),
1551                 "Invalid link for the route between \"%s\" or \"%s\"", src,
1552                 dst);
1553
1554     prev_gw_src = gw_src;
1555     prev_gw_dst = gw_dst;
1556
1557     route_extended_t e_route = TO_FLOYD_LINK(pred, prev_pred);
1558     gw_src = e_route->src_gateway;
1559     gw_dst = e_route->dst_gateway;
1560
1561     if (first)
1562       first_gw = gw_dst;
1563
1564     if (rc->hierarchy == SURF_ROUTING_RECURSIVE && !first
1565         && strcmp(gw_dst, prev_gw_src)) {
1566       xbt_dynar_t e_route_as_to_as =
1567           (*(global_routing->get_route)) (gw_dst, prev_gw_src);
1568       xbt_assert2(e_route_as_to_as, "no route between \"%s\" and \"%s\"",
1569                   gw_dst, prev_gw_src);
1570       links = e_route_as_to_as;
1571       int pos = 0;
1572       xbt_dynar_foreach(links, cpt, link) {
1573         xbt_dynar_insert_at(new_e_route->generic_route.link_list, pos,
1574                             &link);
1575         pos++;
1576       }
1577     }
1578
1579     links = e_route->generic_route.link_list;
1580     xbt_dynar_foreach(links, cpt, link) {
1581       xbt_dynar_unshift(new_e_route->generic_route.link_list, &link);
1582     }
1583     first = 0;
1584
1585   } while (pred != *src_id);
1586   xbt_assert4(pred != -1, "no route from host %d to %d (\"%s\" to \"%s\")",
1587               *src_id, *dst_id, src, dst);
1588
1589   if (rc->hierarchy == SURF_ROUTING_RECURSIVE) {
1590     new_e_route->src_gateway = xbt_strdup(gw_src);
1591     new_e_route->dst_gateway = xbt_strdup(first_gw);
1592   }
1593
1594   return new_e_route;
1595 }
1596
1597 static void floyd_finalize(routing_component_t rc)
1598 {
1599   routing_component_floyd_t routing = (routing_component_floyd_t) rc;
1600   int i, j;
1601   size_t table_size;
1602   if (routing) {
1603     table_size = xbt_dict_length(routing->generic_routing.to_index);
1604     /* Delete link_table */
1605     for (i = 0; i < table_size; i++)
1606       for (j = 0; j < table_size; j++)
1607         generic_free_extended_route(TO_FLOYD_LINK(i, j));
1608     xbt_free(routing->link_table);
1609     /* Delete bypass dict */
1610     xbt_dict_free(&routing->generic_routing.bypassRoutes);
1611     /* Delete index dict */
1612     xbt_dict_free(&(routing->generic_routing.to_index));
1613     /* Delete dictionary index dict, predecessor and links table */
1614     xbt_free(routing->predecessor_table);
1615     /* Delete structure */
1616     xbt_free(rc);
1617   }
1618 }
1619
1620 static void *model_floyd_create(void)
1621 {
1622   routing_component_floyd_t new_component =
1623       xbt_new0(s_routing_component_floyd_t, 1);
1624   new_component->generic_routing.set_processing_unit =
1625       generic_set_processing_unit;
1626   new_component->generic_routing.set_autonomous_system =
1627       generic_set_autonomous_system;
1628   new_component->generic_routing.set_route = model_floyd_set_route;
1629   new_component->generic_routing.set_ASroute = model_floyd_set_route;
1630   new_component->generic_routing.set_bypassroute = generic_set_bypassroute;
1631   new_component->generic_routing.get_route = floyd_get_route;
1632   new_component->generic_routing.get_latency = generic_get_link_latency;
1633   new_component->generic_routing.get_onelink_routes =
1634       floyd_get_onelink_routes;
1635   new_component->generic_routing.get_bypass_route =
1636       generic_get_bypassroute;
1637   new_component->generic_routing.finalize = floyd_finalize;
1638   new_component->generic_routing.to_index = xbt_dict_new();
1639   new_component->generic_routing.bypassRoutes = xbt_dict_new();
1640   new_component->generic_routing.get_network_element_type = get_network_element_type;
1641   return new_component;
1642 }
1643
1644 static void model_floyd_load(void)
1645 {
1646   /* use "surfxml_add_callback" to add a parse function call */
1647 }
1648
1649 static void model_floyd_unload(void)
1650 {
1651   /* use "surfxml_del_callback" to remove a parse function call */
1652 }
1653
1654 static void model_floyd_end(void)
1655 {
1656
1657         routing_component_floyd_t routing =
1658           ((routing_component_floyd_t) current_routing);
1659
1660         unsigned int i, j, a, b, c;
1661
1662         /* set the size of table routing */
1663         size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
1664
1665         if(!routing->link_table)
1666         {
1667                 /* Create Cost, Predecessor and Link tables */
1668                 routing->cost_table = xbt_new0(double, table_size * table_size);       /* link cost from host to host */
1669                 routing->predecessor_table = xbt_new0(int, table_size * table_size);  /* predecessor host numbers */
1670                 routing->link_table = xbt_new0(route_extended_t, table_size * table_size);    /* actual link between src and dst */
1671
1672                 /* Initialize costs and predecessors */
1673                 for (i = 0; i < table_size; i++)
1674                 for (j = 0; j < table_size; j++) {
1675                   TO_FLOYD_COST(i, j) = DBL_MAX;
1676                   TO_FLOYD_PRED(i, j) = -1;
1677                   TO_FLOYD_LINK(i, j) = NULL;       /* fixed, missing in the previous version */
1678                 }
1679         }
1680
1681         /* Add the loopback if needed */
1682         if (current_routing->hierarchy == SURF_ROUTING_BASE) {
1683                 for (i = 0; i < table_size; i++) {
1684                   route_extended_t e_route = TO_FLOYD_LINK(i, i);
1685                   if (!e_route) {
1686                         e_route = xbt_new0(s_route_extended_t, 1);
1687                         e_route->src_gateway = NULL;
1688                         e_route->dst_gateway = NULL;
1689                         e_route->generic_route.link_list =
1690                                 xbt_dynar_new(global_routing->size_of_link, NULL);
1691                         xbt_dynar_push(e_route->generic_route.link_list,
1692                                                    &global_routing->loopback);
1693                         TO_FLOYD_LINK(i, i) = e_route;
1694                         TO_FLOYD_PRED(i, i) = i;
1695                         TO_FLOYD_COST(i, i) = 1;
1696                   }
1697                 }
1698         }
1699         /* Calculate path costs */
1700         for (c = 0; c < table_size; c++) {
1701                 for (a = 0; a < table_size; a++) {
1702                   for (b = 0; b < table_size; b++) {
1703                         if (TO_FLOYD_COST(a, c) < DBL_MAX && TO_FLOYD_COST(c, b) < DBL_MAX) {
1704                           if (TO_FLOYD_COST(a, b) == DBL_MAX ||
1705                                   (TO_FLOYD_COST(a, c) + TO_FLOYD_COST(c, b) <
1706                                    TO_FLOYD_COST(a, b))) {
1707                                 TO_FLOYD_COST(a, b) =
1708                                         TO_FLOYD_COST(a, c) + TO_FLOYD_COST(c, b);
1709                                 TO_FLOYD_PRED(a, b) = TO_FLOYD_PRED(c, b);
1710                           }
1711                         }
1712                   }
1713                 }
1714         }
1715 }
1716
1717 static void model_floyd_set_route(routing_component_t rc, const char *src,
1718         const char *dst, name_route_extended_t route)
1719 {
1720         routing_component_floyd_t routing = (routing_component_floyd_t) rc;
1721
1722         /* set the size of table routing */
1723         size_t table_size = xbt_dict_length(rc->to_index);
1724         int *src_id, *dst_id;
1725         int i,j;
1726
1727         src_id = xbt_dict_get_or_null(rc->to_index, src);
1728         dst_id = xbt_dict_get_or_null(rc->to_index, dst);
1729
1730         if(!routing->link_table)
1731         {
1732                 /* Create Cost, Predecessor and Link tables */
1733                 routing->cost_table = xbt_new0(double, table_size * table_size);       /* link cost from host to host */
1734                 routing->predecessor_table = xbt_new0(int, table_size * table_size);  /* predecessor host numbers */
1735                 routing->link_table = xbt_new0(route_extended_t, table_size * table_size);    /* actual link between src and dst */
1736
1737                 /* Initialize costs and predecessors */
1738                 for (i = 0; i < table_size; i++)
1739                 for (j = 0; j < table_size; j++) {
1740                   TO_FLOYD_COST(i, j) = DBL_MAX;
1741                   TO_FLOYD_PRED(i, j) = -1;
1742                   TO_FLOYD_LINK(i, j) = NULL;       /* fixed, missing in the previous version */
1743                 }
1744         }
1745
1746         if(TO_FLOYD_LINK(*src_id, *dst_id))
1747         {
1748                 if(!route->dst_gateway && !route->src_gateway)
1749                         XBT_DEBUG("See Route from \"%s\" to \"%s\"", src, dst);
1750                 else
1751                         XBT_DEBUG("See ASroute from \"%s(%s)\" to \"%s(%s)\"", src,
1752                                  route->src_gateway, dst, route->dst_gateway);
1753                 char * link_name;
1754                 unsigned int cpt;
1755                 xbt_dynar_t link_route_to_test = xbt_dynar_new(global_routing->size_of_link, NULL);
1756                 xbt_dynar_foreach(route->generic_route.link_list,cpt,link_name)
1757                 {
1758                         void *link = xbt_dict_get_or_null(surf_network_model->resource_set, link_name);
1759                         xbt_assert1(link,"Link : '%s' doesn't exists.",link_name);
1760                         xbt_dynar_push(link_route_to_test,&link);
1761                 }
1762                 xbt_assert2(!xbt_dynar_compare(
1763                           (void*)TO_FLOYD_LINK(*src_id, *dst_id)->generic_route.link_list,
1764                           (void*)link_route_to_test,
1765                           (int_f_cpvoid_cpvoid_t) surf_pointer_resource_cmp),
1766                           "The route between \"%s\" and \"%s\" already exists", src,dst);
1767         }
1768         else
1769         {
1770                 if(!route->dst_gateway && !route->src_gateway)
1771                   XBT_DEBUG("Load Route from \"%s\" to \"%s\"", src, dst);
1772                 else{
1773                         XBT_DEBUG("Load ASroute from \"%s(%s)\" to \"%s(%s)\"", src,
1774                                  route->src_gateway, dst, route->dst_gateway);
1775                         if(global_routing->get_network_element_type((const char*)route->dst_gateway) == SURF_NETWORK_ELEMENT_NULL)
1776                                 xbt_die("The dst_gateway '%s' does not exist!",route->dst_gateway);
1777                         if(global_routing->get_network_element_type((const char*)route->src_gateway) == SURF_NETWORK_ELEMENT_NULL)
1778                                 xbt_die("The src_gateway '%s' does not exist!",route->src_gateway);
1779                 }
1780             TO_FLOYD_LINK(*src_id, *dst_id) =
1781                         generic_new_extended_route(rc->hierarchy, route, 1);
1782             TO_FLOYD_PRED(*src_id, *dst_id) = *src_id;
1783             TO_FLOYD_COST(*src_id, *dst_id) =
1784                         ((TO_FLOYD_LINK(*src_id, *dst_id))->generic_route.link_list)->used;   /* count of links, old model assume 1 */
1785         }
1786
1787         if( A_surfxml_route_symmetrical == A_surfxml_route_symmetrical_YES
1788                 || A_surfxml_ASroute_symmetrical == A_surfxml_ASroute_symmetrical_YES )
1789         {
1790                 if(TO_FLOYD_LINK(*dst_id, *src_id))
1791                 {
1792                         if(!route->dst_gateway && !route->src_gateway)
1793                           XBT_DEBUG("See Route from \"%s\" to \"%s\"", dst, src);
1794                         else
1795                           XBT_DEBUG("See ASroute from \"%s(%s)\" to \"%s(%s)\"", dst,
1796                                          route->src_gateway, src, route->dst_gateway);
1797                         char * link_name;
1798                         unsigned int i;
1799                         xbt_dynar_t link_route_to_test = xbt_dynar_new(global_routing->size_of_link, NULL);
1800                         for(i=xbt_dynar_length(route->generic_route.link_list) ;i>0 ;i--)
1801                         {
1802                                 link_name = xbt_dynar_get_as(route->generic_route.link_list,i-1,void *);
1803                                 void *link = xbt_dict_get_or_null(surf_network_model->resource_set, link_name);
1804                                 xbt_assert1(link,"Link : '%s' doesn't exists.",link_name);
1805                                 xbt_dynar_push(link_route_to_test,&link);
1806                         }
1807                         xbt_assert2(!xbt_dynar_compare(
1808                                   (void*)TO_FLOYD_LINK(*dst_id, *src_id)->generic_route.link_list,
1809                               (void*)link_route_to_test,
1810                                   (int_f_cpvoid_cpvoid_t) surf_pointer_resource_cmp),
1811                                   "The route between \"%s\" and \"%s\" already exists", src,dst);
1812                 }
1813                 else
1814                 {
1815                         if(route->dst_gateway && route->src_gateway)
1816                         {
1817                           char *gw_src = xbt_strdup(route->src_gateway);
1818                           char *gw_dst = xbt_strdup(route->dst_gateway);
1819                           route->src_gateway = gw_dst;
1820                           route->dst_gateway = gw_src;
1821                         }
1822
1823                         if(!route->dst_gateway && !route->src_gateway)
1824                           XBT_DEBUG("Load Route from \"%s\" to \"%s\"", dst, src);
1825                         else
1826                           XBT_DEBUG("Load ASroute from \"%s(%s)\" to \"%s(%s)\"", dst,
1827                                          route->src_gateway, src, route->dst_gateway);
1828
1829                     TO_FLOYD_LINK(*dst_id, *src_id) =
1830                                 generic_new_extended_route(rc->hierarchy, route, 0);
1831                     TO_FLOYD_PRED(*dst_id, *src_id) = *dst_id;
1832                     TO_FLOYD_COST(*dst_id, *src_id) =
1833                                 ((TO_FLOYD_LINK(*dst_id, *src_id))->generic_route.link_list)->used;   /* count of links, old model assume 1 */
1834                 }
1835         }
1836 }
1837
1838 /* ************************************************************************** */
1839 /* ********** Dijkstra & Dijkstra Cached ROUTING **************************** */
1840
1841 typedef struct {
1842   s_routing_component_t generic_routing;
1843   xbt_graph_t route_graph;      /* xbt_graph */
1844   xbt_dict_t graph_node_map;    /* map */
1845   xbt_dict_t route_cache;       /* use in cache mode */
1846   int cached;
1847 } s_routing_component_dijkstra_t, *routing_component_dijkstra_t;
1848
1849
1850 typedef struct graph_node_data {
1851   int id;
1852   int graph_id;                 /* used for caching internal graph id's */
1853 } s_graph_node_data_t, *graph_node_data_t;
1854
1855 typedef struct graph_node_map_element {
1856   xbt_node_t node;
1857 } s_graph_node_map_element_t, *graph_node_map_element_t;
1858
1859 typedef struct route_cache_element {
1860   int *pred_arr;
1861   int size;
1862 } s_route_cache_element_t, *route_cache_element_t;
1863
1864 /* Free functions */
1865
1866 static void route_cache_elem_free(void *e)
1867 {
1868   route_cache_element_t elm = (route_cache_element_t) e;
1869   if (elm) {
1870     xbt_free(elm->pred_arr);
1871     xbt_free(elm);
1872   }
1873 }
1874
1875 static void graph_node_map_elem_free(void *e)
1876 {
1877   graph_node_map_element_t elm = (graph_node_map_element_t) e;
1878   if (elm) {
1879     xbt_free(elm);
1880   }
1881 }
1882
1883 static void graph_edge_data_free(void *e)
1884 {
1885   route_extended_t e_route = (route_extended_t) e;
1886   if (e_route) {
1887     xbt_dynar_free(&(e_route->generic_route.link_list));
1888     if (e_route->src_gateway)
1889       xbt_free(e_route->src_gateway);
1890     if (e_route->dst_gateway)
1891       xbt_free(e_route->dst_gateway);
1892     xbt_free(e_route);
1893   }
1894 }
1895
1896 /* Utility functions */
1897
1898 static xbt_node_t route_graph_new_node(routing_component_dijkstra_t rc,
1899                                        int id, int graph_id)
1900 {
1901   routing_component_dijkstra_t routing = (routing_component_dijkstra_t) rc;
1902   xbt_node_t node = NULL;
1903   graph_node_data_t data = NULL;
1904   graph_node_map_element_t elm = NULL;
1905
1906   data = xbt_new0(struct graph_node_data, 1);
1907   data->id = id;
1908   data->graph_id = graph_id;
1909   node = xbt_graph_new_node(routing->route_graph, data);
1910
1911   elm = xbt_new0(struct graph_node_map_element, 1);
1912   elm->node = node;
1913   xbt_dict_set_ext(routing->graph_node_map, (char *) (&id), sizeof(int),
1914                    (xbt_set_elm_t) elm, &graph_node_map_elem_free);
1915
1916   return node;
1917 }
1918
1919 static graph_node_map_element_t
1920 graph_node_map_search(routing_component_dijkstra_t rc, int id)
1921 {
1922   routing_component_dijkstra_t routing = (routing_component_dijkstra_t) rc;
1923   graph_node_map_element_t elm = (graph_node_map_element_t)
1924       xbt_dict_get_or_null_ext(routing->graph_node_map,
1925                                (char *) (&id),
1926                                sizeof(int));
1927   return elm;
1928 }
1929
1930 /* Parsing */
1931
1932 static void route_new_dijkstra(routing_component_dijkstra_t rc, int src_id,
1933                                int dst_id, route_extended_t e_route)
1934 {
1935   routing_component_dijkstra_t routing = (routing_component_dijkstra_t) rc;
1936   XBT_DEBUG("Load Route from \"%d\" to \"%d\"", src_id, dst_id);
1937   xbt_node_t src = NULL;
1938   xbt_node_t dst = NULL;
1939
1940   graph_node_map_element_t src_elm = (graph_node_map_element_t)
1941       xbt_dict_get_or_null_ext(routing->graph_node_map,
1942                                (char *) (&src_id),
1943                                sizeof(int));
1944   graph_node_map_element_t dst_elm = (graph_node_map_element_t)
1945       xbt_dict_get_or_null_ext(routing->graph_node_map,
1946                                (char *) (&dst_id),
1947                                sizeof(int));
1948
1949
1950   if (src_elm)
1951     src = src_elm->node;
1952
1953   if (dst_elm)
1954     dst = dst_elm->node;
1955
1956   /* add nodes if they don't exist in the graph */
1957   if (src_id == dst_id && src == NULL && dst == NULL) {
1958     src = route_graph_new_node(rc, src_id, -1);
1959     dst = src;
1960   } else {
1961     if (src == NULL) {
1962       src = route_graph_new_node(rc, src_id, -1);
1963     }
1964     if (dst == NULL) {
1965       dst = route_graph_new_node(rc, dst_id, -1);
1966     }
1967   }
1968
1969   /* add link as edge to graph */
1970   xbt_graph_new_edge(routing->route_graph, src, dst, e_route);
1971 }
1972
1973 static void add_loopback_dijkstra(routing_component_dijkstra_t rc)
1974 {
1975   routing_component_dijkstra_t routing = (routing_component_dijkstra_t) rc;
1976
1977   xbt_dynar_t nodes = xbt_graph_get_nodes(routing->route_graph);
1978
1979   xbt_node_t node = NULL;
1980   unsigned int cursor2;
1981   xbt_dynar_foreach(nodes, cursor2, node) {
1982     xbt_dynar_t out_edges = xbt_graph_node_get_outedges(node);
1983     xbt_edge_t edge = NULL;
1984     unsigned int cursor;
1985
1986     int found = 0;
1987     xbt_dynar_foreach(out_edges, cursor, edge) {
1988       xbt_node_t other_node = xbt_graph_edge_get_target(edge);
1989       if (other_node == node) {
1990         found = 1;
1991         break;
1992       }
1993     }
1994
1995     if (!found) {
1996       route_extended_t e_route = xbt_new0(s_route_extended_t, 1);
1997       e_route->src_gateway = NULL;
1998       e_route->dst_gateway = NULL;
1999       e_route->generic_route.link_list =
2000           xbt_dynar_new(global_routing->size_of_link, NULL);
2001       xbt_dynar_push(e_route->generic_route.link_list,
2002                      &global_routing->loopback);
2003       xbt_graph_new_edge(routing->route_graph, node, node, e_route);
2004     }
2005   }
2006 }
2007
2008 /* Business methods */
2009 static xbt_dynar_t dijkstra_get_onelink_routes(routing_component_t rc)
2010 {
2011   xbt_die("\"dijkstra_get_onelink_routes\" function not implemented yet");
2012 }
2013
2014 static route_extended_t dijkstra_get_route(routing_component_t rc,
2015                                            const char *src,
2016                                            const char *dst)
2017 {
2018   xbt_assert1(rc && src
2019               && dst,
2020               "Invalid params for \"get_route\" function at AS \"%s\"",
2021               rc->name);
2022
2023   /* set utils vars */
2024   routing_component_dijkstra_t routing = (routing_component_dijkstra_t) rc;
2025
2026   generic_src_dst_check(rc, src, dst);
2027   int *src_id = xbt_dict_get_or_null(routing->generic_routing.to_index, src);
2028   int *dst_id = xbt_dict_get_or_null(routing->generic_routing.to_index, dst);
2029   xbt_assert2(src_id
2030               && dst_id,
2031               "Ask for route \"from\"(%s)  or \"to\"(%s) no found in the local table",
2032               src, dst);
2033
2034   /* create a result route */
2035   route_extended_t new_e_route = xbt_new0(s_route_extended_t, 1);
2036   new_e_route->generic_route.link_list =
2037       xbt_dynar_new(global_routing->size_of_link, NULL);
2038   new_e_route->src_gateway = NULL;
2039   new_e_route->dst_gateway = NULL;
2040
2041   int *pred_arr = NULL;
2042   int src_node_id = 0;
2043   int dst_node_id = 0;
2044   int *nodeid = NULL;
2045   int v;
2046   route_extended_t e_route;
2047   int size = 0;
2048   unsigned int cpt;
2049   void *link;
2050   xbt_dynar_t links = NULL;
2051   route_cache_element_t elm = NULL;
2052   xbt_dynar_t nodes = xbt_graph_get_nodes(routing->route_graph);
2053
2054   /* Use the graph_node id mapping set to quickly find the nodes */
2055   graph_node_map_element_t src_elm =
2056       graph_node_map_search(routing, *src_id);
2057   graph_node_map_element_t dst_elm =
2058       graph_node_map_search(routing, *dst_id);
2059   xbt_assert2(src_elm != NULL
2060               && dst_elm != NULL, "src %d or dst %d does not exist",
2061               *src_id, *dst_id);
2062   src_node_id = ((graph_node_data_t)
2063                  xbt_graph_node_get_data(src_elm->node))->graph_id;
2064   dst_node_id = ((graph_node_data_t)
2065                  xbt_graph_node_get_data(dst_elm->node))->graph_id;
2066
2067   /* if the src and dst are the same *//* fixed, missing in the previous version */
2068   if (src_node_id == dst_node_id) {
2069
2070     xbt_node_t node_s_v = xbt_dynar_get_as(nodes, src_node_id, xbt_node_t);
2071     xbt_node_t node_e_v = xbt_dynar_get_as(nodes, dst_node_id, xbt_node_t);
2072     xbt_edge_t edge =
2073         xbt_graph_get_edge(routing->route_graph, node_s_v, node_e_v);
2074
2075     xbt_assert2(edge != NULL, "no route between host %d and %d", *src_id,
2076                 *dst_id);
2077
2078     e_route = (route_extended_t) xbt_graph_edge_get_data(edge);
2079
2080     links = e_route->generic_route.link_list;
2081     xbt_dynar_foreach(links, cpt, link) {
2082       xbt_dynar_unshift(new_e_route->generic_route.link_list, &link);
2083     }
2084
2085     return new_e_route;
2086   }
2087
2088   if (routing->cached) {
2089     /*check if there is a cached predecessor list avail */
2090     elm = (route_cache_element_t)
2091         xbt_dict_get_or_null_ext(routing->route_cache, (char *) (&src_id),
2092                                  sizeof(int));
2093   }
2094
2095   if (elm) {                    /* cached mode and cache hit */
2096     pred_arr = elm->pred_arr;
2097   } else {                      /* not cached mode or cache miss */
2098     double *cost_arr = NULL;
2099     xbt_heap_t pqueue = NULL;
2100     int i = 0;
2101
2102     int nr_nodes = xbt_dynar_length(nodes);
2103     cost_arr = xbt_new0(double, nr_nodes);      /* link cost from src to other hosts */
2104     pred_arr = xbt_new0(int, nr_nodes); /* predecessors in path from src */
2105     pqueue = xbt_heap_new(nr_nodes, xbt_free);
2106
2107     /* initialize */
2108     cost_arr[src_node_id] = 0.0;
2109
2110     for (i = 0; i < nr_nodes; i++) {
2111       if (i != src_node_id) {
2112         cost_arr[i] = DBL_MAX;
2113       }
2114
2115       pred_arr[i] = 0;
2116
2117       /* initialize priority queue */
2118       nodeid = xbt_new0(int, 1);
2119       *nodeid = i;
2120       xbt_heap_push(pqueue, nodeid, cost_arr[i]);
2121
2122     }
2123
2124     /* apply dijkstra using the indexes from the graph's node array */
2125     while (xbt_heap_size(pqueue) > 0) {
2126       int *v_id = xbt_heap_pop(pqueue);
2127       xbt_node_t v_node = xbt_dynar_get_as(nodes, *v_id, xbt_node_t);
2128       xbt_dynar_t out_edges = xbt_graph_node_get_outedges(v_node);
2129       xbt_edge_t edge = NULL;
2130       unsigned int cursor;
2131
2132       xbt_dynar_foreach(out_edges, cursor, edge) {
2133         xbt_node_t u_node = xbt_graph_edge_get_target(edge);
2134         graph_node_data_t data = xbt_graph_node_get_data(u_node);
2135         int u_id = data->graph_id;
2136         route_extended_t tmp_e_route =
2137             (route_extended_t) xbt_graph_edge_get_data(edge);
2138         int cost_v_u = (tmp_e_route->generic_route.link_list)->used;    /* count of links, old model assume 1 */
2139
2140         if (cost_v_u + cost_arr[*v_id] < cost_arr[u_id]) {
2141           pred_arr[u_id] = *v_id;
2142           cost_arr[u_id] = cost_v_u + cost_arr[*v_id];
2143           nodeid = xbt_new0(int, 1);
2144           *nodeid = u_id;
2145           xbt_heap_push(pqueue, nodeid, cost_arr[u_id]);
2146         }
2147       }
2148
2149       /* free item popped from pqueue */
2150       xbt_free(v_id);
2151     }
2152
2153     xbt_free(cost_arr);
2154     xbt_heap_free(pqueue);
2155   }
2156
2157   /* compose route path with links */
2158   char *gw_src = NULL, *gw_dst =
2159       NULL, *prev_gw_src, *prev_gw_dst, *first_gw = NULL;
2160
2161   for (v = dst_node_id; v != src_node_id; v = pred_arr[v]) {
2162     xbt_node_t node_pred_v =
2163         xbt_dynar_get_as(nodes, pred_arr[v], xbt_node_t);
2164     xbt_node_t node_v = xbt_dynar_get_as(nodes, v, xbt_node_t);
2165     xbt_edge_t edge =
2166         xbt_graph_get_edge(routing->route_graph, node_pred_v, node_v);
2167
2168     xbt_assert2(edge != NULL, "no route between host %d and %d", *src_id,
2169                 *dst_id);
2170
2171     prev_gw_src = gw_src;
2172     prev_gw_dst = gw_dst;
2173
2174     e_route = (route_extended_t) xbt_graph_edge_get_data(edge);
2175     gw_src = e_route->src_gateway;
2176     gw_dst = e_route->dst_gateway;
2177
2178     if (v == dst_node_id)
2179       first_gw = gw_dst;
2180
2181     if (rc->hierarchy == SURF_ROUTING_RECURSIVE && v != dst_node_id
2182         && strcmp(gw_dst, prev_gw_src)) {
2183       xbt_dynar_t e_route_as_to_as =
2184           (*(global_routing->get_route)) (gw_dst, prev_gw_src);
2185       xbt_assert2(e_route_as_to_as, "no route between \"%s\" and \"%s\"",
2186                   gw_dst, prev_gw_src);
2187       links = e_route_as_to_as;
2188       int pos = 0;
2189       xbt_dynar_foreach(links, cpt, link) {
2190         xbt_dynar_insert_at(new_e_route->generic_route.link_list, pos,
2191                             &link);
2192         pos++;
2193       }
2194     }
2195
2196     links = e_route->generic_route.link_list;
2197     xbt_dynar_foreach(links, cpt, link) {
2198       xbt_dynar_unshift(new_e_route->generic_route.link_list, &link);
2199     }
2200     size++;
2201   }
2202
2203   if (rc->hierarchy == SURF_ROUTING_RECURSIVE) {
2204     new_e_route->src_gateway = xbt_strdup(gw_src);
2205     new_e_route->dst_gateway = xbt_strdup(first_gw);
2206   }
2207
2208   if (routing->cached && elm == NULL) {
2209     /* add to predecessor list of the current src-host to cache */
2210     elm = xbt_new0(struct route_cache_element, 1);
2211     elm->pred_arr = pred_arr;
2212     elm->size = size;
2213     xbt_dict_set_ext(routing->route_cache, (char *) (&src_id), sizeof(int),
2214                      (xbt_set_elm_t) elm, &route_cache_elem_free);
2215   }
2216
2217   if (!routing->cached)
2218     xbt_free(pred_arr);
2219
2220   return new_e_route;
2221 }
2222
2223 static void dijkstra_finalize(routing_component_t rc)
2224 {
2225   routing_component_dijkstra_t routing = (routing_component_dijkstra_t) rc;
2226
2227   if (routing) {
2228     xbt_graph_free_graph(routing->route_graph, &xbt_free,
2229                          &graph_edge_data_free, &xbt_free);
2230     xbt_dict_free(&routing->graph_node_map);
2231     if (routing->cached)
2232       xbt_dict_free(&routing->route_cache);
2233     /* Delete bypass dict */
2234     xbt_dict_free(&routing->generic_routing.bypassRoutes);
2235     /* Delete index dict */
2236     xbt_dict_free(&(routing->generic_routing.to_index));
2237     /* Delete structure */
2238     xbt_free(routing);
2239   }
2240 }
2241
2242 /* Creation routing model functions */
2243
2244 static void *model_dijkstra_both_create(int cached)
2245 {
2246   routing_component_dijkstra_t new_component =
2247       xbt_new0(s_routing_component_dijkstra_t, 1);
2248   new_component->generic_routing.set_processing_unit =
2249       generic_set_processing_unit;
2250   new_component->generic_routing.set_autonomous_system =
2251       generic_set_autonomous_system;
2252   new_component->generic_routing.set_route = model_dijkstra_both_set_route;
2253   new_component->generic_routing.set_ASroute = model_dijkstra_both_set_route;
2254   new_component->generic_routing.set_bypassroute = generic_set_bypassroute;
2255   new_component->generic_routing.get_route = dijkstra_get_route;
2256   new_component->generic_routing.get_latency = generic_get_link_latency;
2257   new_component->generic_routing.get_onelink_routes =
2258       dijkstra_get_onelink_routes;
2259   new_component->generic_routing.get_bypass_route =
2260       generic_get_bypassroute;
2261   new_component->generic_routing.finalize = dijkstra_finalize;
2262   new_component->cached = cached;
2263   new_component->generic_routing.to_index = xbt_dict_new();
2264   new_component->generic_routing.bypassRoutes = xbt_dict_new();
2265   new_component->generic_routing.get_network_element_type = get_network_element_type;
2266   return new_component;
2267 }
2268
2269 static void *model_dijkstra_create(void)
2270 {
2271   return model_dijkstra_both_create(0);
2272 }
2273
2274 static void *model_dijkstracache_create(void)
2275 {
2276   return model_dijkstra_both_create(1);
2277 }
2278
2279 static void model_dijkstra_both_load(void)
2280 {
2281   /* use "surfxml_add_callback" to add a parse function call */
2282 }
2283
2284 static void model_dijkstra_both_unload(void)
2285 {
2286   /* use "surfxml_del_callback" to remove a parse function call */
2287 }
2288
2289 static void model_dijkstra_both_end(void)
2290 {
2291   routing_component_dijkstra_t routing =
2292       (routing_component_dijkstra_t) current_routing;
2293
2294   xbt_node_t node = NULL;
2295   unsigned int cursor2;
2296   xbt_dynar_t nodes = NULL;
2297
2298   /* Create the topology graph */
2299   if(!routing->route_graph)
2300   routing->route_graph = xbt_graph_new_graph(1, NULL);
2301   if(!routing->graph_node_map)
2302   routing->graph_node_map = xbt_dict_new();
2303
2304   if (routing->cached && !routing->route_cache)
2305   routing->route_cache = xbt_dict_new();
2306
2307   /* Add the loopback if needed */
2308   if (current_routing->hierarchy == SURF_ROUTING_BASE)
2309     add_loopback_dijkstra(routing);
2310
2311   /* initialize graph indexes in nodes after graph has been built */
2312   nodes = xbt_graph_get_nodes(routing->route_graph);
2313
2314   xbt_dynar_foreach(nodes, cursor2, node) {
2315     graph_node_data_t data = xbt_graph_node_get_data(node);
2316     data->graph_id = cursor2;
2317   }
2318
2319 }
2320 static void model_dijkstra_both_set_route (routing_component_t rc, const char *src,
2321                      const char *dst, name_route_extended_t route)
2322 {
2323         routing_component_dijkstra_t routing = (routing_component_dijkstra_t) rc;
2324         int *src_id, *dst_id;
2325         src_id = xbt_dict_get_or_null(rc->to_index, src);
2326         dst_id = xbt_dict_get_or_null(rc->to_index, dst);
2327
2328     /* Create the topology graph */
2329         if(!routing->route_graph)
2330         routing->route_graph = xbt_graph_new_graph(1, NULL);
2331         if(!routing->graph_node_map)
2332         routing->graph_node_map = xbt_dict_new();
2333
2334         if (routing->cached && !routing->route_cache)
2335         routing->route_cache = xbt_dict_new();
2336
2337         if( A_surfxml_route_symmetrical == A_surfxml_route_symmetrical_YES
2338                 || A_surfxml_ASroute_symmetrical == A_surfxml_ASroute_symmetrical_YES )
2339                 xbt_die("Route symmetrical not supported on model dijkstra");
2340
2341         if(!route->dst_gateway && !route->src_gateway)
2342           XBT_DEBUG("Load Route from \"%s\" to \"%s\"", src, dst);
2343         else{
2344           XBT_DEBUG("Load ASroute from \"%s(%s)\" to \"%s(%s)\"", src,
2345                          route->src_gateway, dst, route->dst_gateway);
2346           if(global_routing->get_network_element_type((const char*)route->dst_gateway) == SURF_NETWORK_ELEMENT_NULL)
2347                   xbt_die("The dst_gateway '%s' does not exist!",route->dst_gateway);
2348           if(global_routing->get_network_element_type((const char*)route->src_gateway) == SURF_NETWORK_ELEMENT_NULL)
2349                   xbt_die("The src_gateway '%s' does not exist!",route->src_gateway);
2350         }
2351
2352         route_extended_t e_route =
2353                 generic_new_extended_route(current_routing->hierarchy, route, 1);
2354         route_new_dijkstra(routing, *src_id, *dst_id, e_route);
2355 }
2356
2357 #ifdef HAVE_PCRE_LIB
2358 /* ************************************************** */
2359 /* ************** RULE-BASED ROUTING **************** */
2360
2361 /* Routing model structure */
2362
2363 typedef struct {
2364   s_routing_component_t generic_routing;
2365   xbt_dict_t dict_processing_units;
2366   xbt_dict_t dict_autonomous_systems;
2367   xbt_dynar_t list_route;
2368   xbt_dynar_t list_ASroute;
2369 } s_routing_component_rulebased_t, *routing_component_rulebased_t;
2370
2371 typedef struct s_rule_route s_rule_route_t, *rule_route_t;
2372 typedef struct s_rule_route_extended s_rule_route_extended_t,
2373     *rule_route_extended_t;
2374
2375 struct s_rule_route {
2376   xbt_dynar_t re_str_link;      // dynar of char*
2377   pcre *re_src;
2378   pcre *re_dst;
2379 };
2380
2381 struct s_rule_route_extended {
2382   s_rule_route_t generic_rule_route;
2383   char *re_src_gateway;
2384   char *re_dst_gateway;
2385 };
2386
2387 static void rule_route_free(void *e)
2388 {
2389   rule_route_t *elem = (rule_route_t *) (e);
2390   if (*elem) {
2391     xbt_dynar_free(&(*elem)->re_str_link);
2392     pcre_free((*elem)->re_src);
2393     pcre_free((*elem)->re_dst);
2394     xbt_free(*elem);
2395   }
2396   *elem = NULL;
2397 }
2398
2399 static void rule_route_extended_free(void *e)
2400 {
2401   rule_route_extended_t *elem = (rule_route_extended_t *) e;
2402   if (*elem) {
2403     xbt_dynar_free(&(*elem)->generic_rule_route.re_str_link);
2404     pcre_free((*elem)->generic_rule_route.re_src);
2405     pcre_free((*elem)->generic_rule_route.re_dst);
2406     xbt_free((*elem)->re_src_gateway);
2407     xbt_free((*elem)->re_dst_gateway);
2408     xbt_free(*elem);
2409   }
2410 }
2411
2412 /* Parse routing model functions */
2413
2414 static void model_rulebased_set_processing_unit(routing_component_t rc,
2415                                                 const char *name)
2416 {
2417   routing_component_rulebased_t routing =
2418       (routing_component_rulebased_t) rc;
2419   xbt_dict_set(routing->dict_processing_units, name, (void *) (-1), NULL);
2420 }
2421
2422 static void model_rulebased_set_autonomous_system(routing_component_t rc,
2423                                                   const char *name)
2424 {
2425   routing_component_rulebased_t routing =
2426       (routing_component_rulebased_t) rc;
2427   xbt_dict_set(routing->dict_autonomous_systems, name, (void *) (-1),
2428                NULL);
2429 }
2430
2431 static void model_rulebased_set_route(routing_component_t rc,
2432                                       const char *src, const char *dst,
2433                                       name_route_extended_t route)
2434 {
2435   routing_component_rulebased_t routing =
2436       (routing_component_rulebased_t) rc;
2437   rule_route_t ruleroute = xbt_new0(s_rule_route_t, 1);
2438   const char *error;
2439   int erroffset;
2440   ruleroute->re_src = pcre_compile(src, 0, &error, &erroffset, NULL);
2441   xbt_assert3(ruleroute->re_src,
2442               "PCRE compilation failed at offset %d (\"%s\"): %s\n",
2443               erroffset, src, error);
2444   ruleroute->re_dst = pcre_compile(dst, 0, &error, &erroffset, NULL);
2445   xbt_assert3(ruleroute->re_src,
2446               "PCRE compilation failed at offset %d (\"%s\"): %s\n",
2447               erroffset, dst, error);
2448   ruleroute->re_str_link = route->generic_route.link_list;
2449   xbt_dynar_push(routing->list_route, &ruleroute);
2450   xbt_free(route);
2451 }
2452
2453 static void model_rulebased_set_ASroute(routing_component_t rc,
2454                                         const char *src, const char *dst,
2455                                         name_route_extended_t route)
2456 {
2457   routing_component_rulebased_t routing =
2458       (routing_component_rulebased_t) rc;
2459   rule_route_extended_t ruleroute_e = xbt_new0(s_rule_route_extended_t, 1);
2460   const char *error;
2461   int erroffset;
2462   ruleroute_e->generic_rule_route.re_src =
2463       pcre_compile(src, 0, &error, &erroffset, NULL);
2464   xbt_assert3(ruleroute_e->generic_rule_route.re_src,
2465               "PCRE compilation failed at offset %d (\"%s\"): %s\n",
2466               erroffset, src, error);
2467   ruleroute_e->generic_rule_route.re_dst =
2468       pcre_compile(dst, 0, &error, &erroffset, NULL);
2469   xbt_assert3(ruleroute_e->generic_rule_route.re_src,
2470               "PCRE compilation failed at offset %d (\"%s\"): %s\n",
2471               erroffset, dst, error);
2472   ruleroute_e->generic_rule_route.re_str_link =
2473       route->generic_route.link_list;
2474   ruleroute_e->re_src_gateway = route->src_gateway;
2475   ruleroute_e->re_dst_gateway = route->dst_gateway;
2476   xbt_dynar_push(routing->list_ASroute, &ruleroute_e);
2477 //  xbt_free(route->src_gateway);
2478 //  xbt_free(route->dst_gateway);
2479   xbt_free(route);
2480 }
2481
2482 static void model_rulebased_set_bypassroute(routing_component_t rc,
2483                                             const char *src,
2484                                             const char *dst,
2485                                             route_extended_t e_route)
2486 {
2487   xbt_die("bypass routing not supported for Route-Based model");
2488 }
2489
2490 #define BUFFER_SIZE 4096        /* result buffer size */
2491 #define OVECCOUNT 30            /* should be a multiple of 3 */
2492
2493 static char *remplace(char *value, const char **src_list, int src_size,
2494                       const char **dst_list, int dst_size)
2495 {
2496
2497   char result_result[BUFFER_SIZE];
2498   int i_result_buffer;
2499   int value_length = (int) strlen(value);
2500   int number = 0;
2501
2502   int i = 0;
2503   i_result_buffer = 0;
2504   do {
2505     if (value[i] == '$') {
2506       i++;                      // skip $
2507
2508       // find the number      
2509       int number_length = 0;
2510       while ('0' <= value[i + number_length]
2511              && value[i + number_length] <= '9') {
2512         number_length++;
2513       }
2514       xbt_assert2(number_length != 0,
2515                   "bad string parameter, no number indication, at offset: %d (\"%s\")",
2516                   i, value);
2517
2518       // solve number
2519       number = atoi(value + i);
2520       i = i + number_length;
2521       xbt_assert2(i + 2 < value_length,
2522                   "bad string parameter, too few chars, at offset: %d (\"%s\")",
2523                   i, value);
2524
2525       // solve the indication
2526       const char **param_list;
2527       int param_size;
2528       if (value[i] == 's' && value[i + 1] == 'r' && value[i + 2] == 'c') {
2529         param_list = src_list;
2530         param_size = src_size;
2531       } else if (value[i] == 'd' && value[i + 1] == 's'
2532                  && value[i + 2] == 't') {
2533         param_list = dst_list;
2534         param_size = dst_size;
2535       } else {
2536         xbt_die("bad string parameter, support only \"src\" and \"dst\", "
2537                 "at offset: %d (\"%s\")", i, value);
2538       }
2539       i = i + 3;
2540
2541       xbt_assert4(param_size >= number,
2542                   "bad string parameter, not enough length param_size, at offset: %d (\"%s\") %d %d",
2543                   i, value, param_size, number);
2544
2545       const char *param = param_list[number];
2546       int size = strlen(param);
2547       int cp;
2548       for (cp = 0; cp < size; cp++) {
2549         result_result[i_result_buffer] = param[cp];
2550         i_result_buffer++;
2551         if (i_result_buffer >= BUFFER_SIZE)
2552           break;
2553       }
2554     } else {
2555       result_result[i_result_buffer] = value[i];
2556       i_result_buffer++;
2557       i++;                      // next char
2558     }
2559
2560   } while (i < value_length && i_result_buffer < BUFFER_SIZE);
2561
2562   xbt_assert2(i_result_buffer < BUFFER_SIZE,
2563               "solving string \"%s\", small buffer size (%d)", value,
2564               BUFFER_SIZE);
2565   result_result[i_result_buffer] = 0;
2566   return xbt_strdup(result_result);
2567 }
2568
2569 static route_extended_t rulebased_get_route(routing_component_t rc,
2570                                             const char *src,
2571                                             const char *dst);
2572 static xbt_dynar_t rulebased_get_onelink_routes(routing_component_t rc)
2573 {
2574   xbt_dynar_t ret = xbt_dynar_new (sizeof(onelink_t), xbt_free);
2575   routing_component_rulebased_t routing = (routing_component_rulebased_t)rc;
2576
2577   xbt_dict_cursor_t c1 = NULL;
2578   char *k1, *d1;
2579
2580   //find router
2581   char *router = NULL;
2582   xbt_dict_foreach(routing->dict_processing_units, c1, k1, d1) {
2583     if (strstr (k1, "router")){
2584       router = k1;
2585     }
2586   }
2587   if (!router){
2588     xbt_die ("rulebased_get_onelink_routes works only if the AS is a cluster, sorry.");
2589   }
2590
2591   xbt_dict_foreach(routing->dict_processing_units, c1, k1, d1) {
2592     route_extended_t route = rulebased_get_route (rc, router, k1);
2593
2594     int number_of_links = xbt_dynar_length(route->generic_route.link_list);
2595     if (number_of_links != 3) {
2596       xbt_die ("rulebased_get_onelink_routes works only if the AS is a cluster, sorry.");
2597     }
2598
2599     void *link_ptr;
2600     xbt_dynar_get_cpy (route->generic_route.link_list, 2, &link_ptr);
2601     onelink_t onelink = xbt_new0 (s_onelink_t, 1);
2602     onelink->src = xbt_strdup (k1);
2603     onelink->dst = xbt_strdup (router);
2604     onelink->link_ptr = link_ptr;
2605     xbt_dynar_push (ret, &onelink);
2606   }
2607   return ret;
2608 }
2609
2610 /* Business methods */
2611 static route_extended_t rulebased_get_route(routing_component_t rc,
2612                                             const char *src,
2613                                             const char *dst)
2614 {
2615   xbt_assert1(rc && src
2616               && dst,
2617               "Invalid params for \"get_route\" function at AS \"%s\"",
2618               rc->name);
2619
2620   /* set utils vars */
2621   routing_component_rulebased_t routing =
2622       (routing_component_rulebased_t) rc;
2623
2624   int are_processing_units=0;
2625   xbt_dynar_t rule_list;
2626   if (xbt_dict_get_or_null(routing->dict_processing_units, src)
2627       && xbt_dict_get_or_null(routing->dict_processing_units, dst)) {
2628     are_processing_units = 1;
2629     rule_list = routing->list_route;
2630   } else if (xbt_dict_get_or_null(routing->dict_autonomous_systems, src)
2631              && xbt_dict_get_or_null(routing->dict_autonomous_systems,
2632                                      dst)) {
2633     are_processing_units = 0;
2634     rule_list = routing->list_ASroute;
2635   } else
2636     xbt_die("Ask for route \"from\"(%s)  or \"to\"(%s) no found in "
2637             "the local table", src, dst);
2638
2639   int rc_src = -1;
2640   int rc_dst = -1;
2641   int src_length = (int) strlen(src);
2642   int dst_length = (int) strlen(dst);
2643
2644   xbt_dynar_t links_list =
2645       xbt_dynar_new(global_routing->size_of_link, NULL);
2646
2647   rule_route_t ruleroute;
2648   unsigned int cpt;
2649   int ovector_src[OVECCOUNT];
2650   int ovector_dst[OVECCOUNT];
2651   const char **list_src = NULL;
2652   const char **list_dst = NULL;
2653   int res;
2654   xbt_dynar_foreach(rule_list, cpt, ruleroute) {
2655     rc_src =
2656         pcre_exec(ruleroute->re_src, NULL, src, src_length, 0, 0,
2657                   ovector_src, OVECCOUNT);
2658     if (rc_src >= 0) {
2659       rc_dst =
2660           pcre_exec(ruleroute->re_dst, NULL, dst, dst_length, 0, 0,
2661                     ovector_dst, OVECCOUNT);
2662       if (rc_dst >= 0) {
2663         res = pcre_get_substring_list(src, ovector_src, rc_src, &list_src);
2664         xbt_assert1(!res, "error solving substring list for src \"%s\"", src);
2665         res = pcre_get_substring_list(dst, ovector_dst, rc_dst, &list_dst);
2666         xbt_assert1(!res, "error solving substring list for src \"%s\"", dst);
2667         char *link_name;
2668         xbt_dynar_foreach(ruleroute->re_str_link, cpt, link_name) {
2669           char *new_link_name =
2670               remplace(link_name, list_src, rc_src, list_dst, rc_dst);
2671           void *link =
2672               xbt_dict_get_or_null(surf_network_model->resource_set,
2673                                    new_link_name);
2674           if (link)
2675             xbt_dynar_push(links_list, &link);
2676           else
2677             THROW1(mismatch_error, 0, "Link %s not found", new_link_name);
2678           xbt_free(new_link_name);
2679         }
2680       }
2681     }
2682     if (rc_src >= 0 && rc_dst >= 0)
2683       break;
2684   }
2685
2686   route_extended_t new_e_route = NULL;
2687   if (rc_src >= 0 && rc_dst >= 0) {
2688     new_e_route = xbt_new0(s_route_extended_t, 1);
2689     new_e_route->generic_route.link_list = links_list;
2690   } else if (!strcmp(src, dst) && are_processing_units) {
2691     new_e_route = xbt_new0(s_route_extended_t, 1);
2692     xbt_dynar_push(links_list, &(global_routing->loopback));
2693     new_e_route->generic_route.link_list = links_list;
2694   } else {
2695     xbt_dynar_free(&link_list);
2696   }
2697
2698   if (!are_processing_units && new_e_route) {
2699     rule_route_extended_t ruleroute_extended =
2700         (rule_route_extended_t) ruleroute;
2701     new_e_route->src_gateway =
2702         remplace(ruleroute_extended->re_src_gateway, list_src, rc_src,
2703                  list_dst, rc_dst);
2704     new_e_route->dst_gateway =
2705         remplace(ruleroute_extended->re_dst_gateway, list_src, rc_src,
2706                  list_dst, rc_dst);
2707   }
2708
2709   if (list_src)
2710     pcre_free_substring_list(list_src);
2711   if (list_dst)
2712     pcre_free_substring_list(list_dst);
2713
2714   return new_e_route;
2715 }
2716
2717 static route_extended_t rulebased_get_bypass_route(routing_component_t rc,
2718                                                    const char *src,
2719                                                    const char *dst)
2720 {
2721   return NULL;
2722 }
2723
2724 static void rulebased_finalize(routing_component_t rc)
2725 {
2726   routing_component_rulebased_t routing =
2727       (routing_component_rulebased_t) rc;
2728   if (routing) {
2729     xbt_dict_free(&routing->dict_processing_units);
2730     xbt_dict_free(&routing->dict_autonomous_systems);
2731     xbt_dynar_free(&routing->list_route);
2732     xbt_dynar_free(&routing->list_ASroute);
2733     /* Delete structure */
2734     xbt_free(routing);
2735   }
2736 }
2737
2738 /* Creation routing model functions */
2739 static void *model_rulebased_create(void)
2740 {
2741   routing_component_rulebased_t new_component =
2742       xbt_new0(s_routing_component_rulebased_t, 1);
2743   new_component->generic_routing.set_processing_unit =
2744       model_rulebased_set_processing_unit;
2745   new_component->generic_routing.set_autonomous_system =
2746       model_rulebased_set_autonomous_system;
2747   new_component->generic_routing.set_route = model_rulebased_set_route;
2748   new_component->generic_routing.set_ASroute = model_rulebased_set_ASroute;
2749   new_component->generic_routing.set_bypassroute = model_rulebased_set_bypassroute;
2750   new_component->generic_routing.get_onelink_routes = rulebased_get_onelink_routes;
2751   new_component->generic_routing.get_route = rulebased_get_route;
2752   new_component->generic_routing.get_latency = generic_get_link_latency;
2753   new_component->generic_routing.get_bypass_route = rulebased_get_bypass_route;
2754   new_component->generic_routing.finalize = rulebased_finalize;
2755   new_component->generic_routing.get_network_element_type = get_network_element_type;
2756   /* initialization of internal structures */
2757   new_component->dict_processing_units = xbt_dict_new();
2758   new_component->dict_autonomous_systems = xbt_dict_new();
2759   new_component->list_route = xbt_dynar_new(sizeof(rule_route_t), &rule_route_free);
2760   new_component->list_ASroute =
2761       xbt_dynar_new(sizeof(rule_route_extended_t),
2762                     &rule_route_extended_free);
2763   return new_component;
2764 }
2765
2766 static void model_rulebased_load(void)
2767 {
2768   /* use "surfxml_add_callback" to add a parse function call */
2769 }
2770
2771 static void model_rulebased_unload(void)
2772 {
2773   /* use "surfxml_del_callback" to remove a parse function call */
2774 }
2775
2776 static void model_rulebased_end(void)
2777 {
2778 }
2779
2780 #endif                          /* HAVE_PCRE_LIB */
2781
2782 /* ************************************************************************** */
2783 /* ******************************* NO ROUTING ******************************* */
2784
2785 /* Routing model structure */
2786 typedef struct {
2787   s_routing_component_t generic_routing;
2788 } s_routing_component_none_t, *routing_component_none_t;
2789
2790 /* Business methods */
2791 static xbt_dynar_t none_get_onelink_routes(routing_component_t rc)
2792 {
2793   return NULL;
2794 }
2795
2796 static route_extended_t none_get_route(routing_component_t rc,
2797                                        const char *src, const char *dst)
2798 {
2799   return NULL;
2800 }
2801
2802 static route_extended_t none_get_bypass_route(routing_component_t rc,
2803                                               const char *src,
2804                                               const char *dst)
2805 {
2806   return NULL;
2807 }
2808
2809 static void none_finalize(routing_component_t rc)
2810 {
2811   xbt_free(rc);
2812 }
2813
2814 static void none_set_processing_unit(routing_component_t rc,
2815                                      const char *name)
2816 {
2817 }
2818
2819 static void none_set_autonomous_system(routing_component_t rc,
2820                                        const char *name)
2821 {
2822 }
2823
2824 /* Creation routing model functions */
2825 static void *model_none_create(void)
2826 {
2827   routing_component_none_t new_component =
2828       xbt_new0(s_routing_component_none_t, 1);
2829   new_component->generic_routing.set_processing_unit =
2830       none_set_processing_unit;
2831   new_component->generic_routing.set_autonomous_system =
2832       none_set_autonomous_system;
2833   new_component->generic_routing.set_route = NULL;
2834   new_component->generic_routing.set_ASroute = NULL;
2835   new_component->generic_routing.set_bypassroute = NULL;
2836   new_component->generic_routing.get_route = none_get_route;
2837   new_component->generic_routing.get_onelink_routes =
2838       none_get_onelink_routes;
2839   new_component->generic_routing.get_bypass_route = none_get_bypass_route;
2840   new_component->generic_routing.finalize = none_finalize;
2841   return new_component;
2842 }
2843
2844 static void model_none_load(void)
2845 {
2846 }
2847
2848 static void model_none_unload(void)
2849 {
2850 }
2851
2852 static void model_none_end(void)
2853 {
2854 }
2855
2856 /* ************************************************** */
2857 /* ********** PATERN FOR NEW ROUTING **************** */
2858
2859 /* The minimal configuration of a new routing model need the next functions,
2860  * also you need to set at the start of the file, the new model in the model
2861  * list. Remember keep the null ending of the list.
2862  */
2863 /*** Routing model structure ***/
2864 // typedef struct {
2865 //   s_routing_component_t generic_routing;
2866 //   /* things that your routing model need */
2867 // } s_routing_component_NEW_t,*routing_component_NEW_t;
2868
2869 /*** Parse routing model functions ***/
2870 // static void model_NEW_set_processing_unit(routing_component_t rc, const char* name) {}
2871 // static void model_NEW_set_autonomous_system(routing_component_t rc, const char* name) {}
2872 // static void model_NEW_set_route(routing_component_t rc, const char* src, const char* dst, route_t route) {}
2873 // static void model_NEW_set_ASroute(routing_component_t rc, const char* src, const char* dst, route_extended_t route) {}
2874 // static void model_NEW_set_bypassroute(routing_component_t rc, const char* src, const char* dst, route_extended_t e_route) {}
2875
2876 /*** Business methods ***/
2877 // static route_extended_t NEW_get_route(routing_component_t rc, const char* src,const char* dst) {return NULL;}
2878 // static route_extended_t NEW_get_bypass_route(routing_component_t rc, const char* src,const char* dst) {return NULL;}
2879 // static void NEW_finalize(routing_component_t rc) { xbt_free(rc);}
2880
2881 /*** Creation routing model functions ***/
2882 // static void* model_NEW_create(void) {
2883 //   routing_component_NEW_t new_component =  xbt_new0(s_routing_component_NEW_t,1);
2884 //   new_component->generic_routing.set_processing_unit = model_NEW_set_processing_unit;
2885 //   new_component->generic_routing.set_autonomous_system = model_NEW_set_autonomous_system;
2886 //   new_component->generic_routing.set_route = model_NEW_set_route;
2887 //   new_component->generic_routing.set_ASroute = model_NEW_set_ASroute;
2888 //   new_component->generic_routing.set_bypassroute = model_NEW_set_bypassroute;
2889 //   new_component->generic_routing.get_route = NEW_get_route;
2890 //   new_component->generic_routing.get_bypass_route = NEW_get_bypass_route;
2891 //   new_component->generic_routing.finalize = NEW_finalize;
2892 //   /* initialization of internal structures */
2893 //   return new_component;
2894 // } /* mandatory */
2895 // static void  model_NEW_load(void) {}   /* mandatory */
2896 // static void  model_NEW_unload(void) {} /* mandatory */
2897 // static void  model_NEW_end(void) {}    /* mandatory */
2898
2899 /* ************************************************************************** */
2900 /* ************************* GENERIC PARSE FUNCTIONS ************************ */
2901
2902 static void generic_set_processing_unit(routing_component_t rc,
2903                                         const char *name)
2904 {
2905   XBT_DEBUG("Load process unit \"%s\"", name);
2906   int *id = xbt_new0(int, 1);
2907   xbt_dict_t _to_index;
2908   _to_index = current_routing->to_index;
2909   *id = xbt_dict_length(_to_index);
2910   xbt_dict_set(_to_index, name, id, xbt_free);
2911 }
2912
2913 static void generic_set_autonomous_system(routing_component_t rc,
2914                                           const char *name)
2915 {
2916   XBT_DEBUG("Load Autonomous system \"%s\"", name);
2917   int *id = xbt_new0(int, 1);
2918   xbt_dict_t _to_index;
2919   _to_index = current_routing->to_index;
2920   *id = xbt_dict_length(_to_index);
2921   xbt_dict_set(_to_index, name, id, xbt_free);
2922 }
2923
2924 static int surf_pointer_resource_cmp(const void *a, const void *b)
2925 {
2926   return a != b;
2927 }
2928
2929 static int surf_link_resource_cmp(const void *a, const void *b)
2930 {
2931   return !!memcmp(a,b,global_routing->size_of_link);
2932 }
2933
2934 static void generic_set_bypassroute(routing_component_t rc,
2935                                     const char *src, const char *dst,
2936                                     route_extended_t e_route)
2937 {
2938   XBT_DEBUG("Load bypassRoute from \"%s\" to \"%s\"", src, dst);
2939   xbt_dict_t dict_bypassRoutes = rc->bypassRoutes;
2940   char *route_name;
2941
2942   route_name = bprintf("%s#%s", src, dst);
2943   xbt_assert2(xbt_dynar_length(e_route->generic_route.link_list) > 0,
2944               "Invalid count of links, must be greater than zero (%s,%s)",
2945               src, dst);
2946   xbt_assert4(!xbt_dict_get_or_null(dict_bypassRoutes, route_name),
2947               "The bypass route between \"%s\"(\"%s\") and \"%s\"(\"%s\") already exists",
2948               src, e_route->src_gateway, dst, e_route->dst_gateway);
2949
2950   route_extended_t new_e_route =
2951       generic_new_extended_route(SURF_ROUTING_RECURSIVE, e_route, 0);
2952   xbt_dynar_free(&(e_route->generic_route.link_list));
2953   xbt_free(e_route);
2954
2955   xbt_dict_set(dict_bypassRoutes, route_name, new_e_route,
2956                (void (*)(void *)) generic_free_extended_route);
2957   xbt_free(route_name);
2958 }
2959
2960 /* ************************************************************************** */
2961 /* *********************** GENERIC BUSINESS METHODS ************************* */
2962
2963 static double generic_get_link_latency(routing_component_t rc,
2964                                        const char *src, const char *dst)
2965 {
2966         route_extended_t route = rc->get_route(rc,src,dst);
2967         void * link;
2968         unsigned int i;
2969         double latency = 0.0;
2970
2971         xbt_dynar_foreach(route->generic_route.link_list,i,link) {
2972                 latency += get_link_latency(link);
2973         }
2974         generic_free_extended_route(route);
2975   return latency;
2976 }
2977
2978 static xbt_dynar_t generic_get_onelink_routes(routing_component_t rc)
2979 {
2980   xbt_die("\"generic_get_onelink_routes\" not implemented yet");
2981 }
2982
2983 static route_extended_t generic_get_bypassroute(routing_component_t rc,
2984                                                 const char *src,
2985                                                 const char *dst)
2986 {
2987   xbt_dict_t dict_bypassRoutes = rc->bypassRoutes;
2988   routing_component_t src_as, dst_as;
2989   int index_src, index_dst;
2990   xbt_dynar_t path_src = NULL;
2991   xbt_dynar_t path_dst = NULL;
2992   routing_component_t current = NULL;
2993   routing_component_t *current_src = NULL;
2994   routing_component_t *current_dst = NULL;
2995
2996   /* (1) find the as where the src and dst are located */
2997   src_as = ((network_element_info_t)
2998             xbt_dict_get_or_null(global_routing->where_network_elements,
2999                                  src))->rc_component;
3000   dst_as = ((network_element_info_t)
3001             xbt_dict_get_or_null(global_routing->where_network_elements,
3002                                  dst))->rc_component;
3003   xbt_assert2(src_as
3004               && dst_as,
3005               "Ask for route \"from\"(%s) or \"to\"(%s) no found", src,
3006               dst);
3007
3008   /* (2) find the path to the root routing component */
3009   path_src = xbt_dynar_new(sizeof(routing_component_t), NULL);
3010   current = src_as;
3011   while (current != NULL) {
3012     xbt_dynar_push(path_src, &current);
3013     current = current->routing_father;
3014   }
3015   path_dst = xbt_dynar_new(sizeof(routing_component_t), NULL);
3016   current = dst_as;
3017   while (current != NULL) {
3018     xbt_dynar_push(path_dst, &current);
3019     current = current->routing_father;
3020   }
3021
3022   /* (3) find the common father */
3023   index_src = path_src->used - 1;
3024   index_dst = path_dst->used - 1;
3025   current_src = xbt_dynar_get_ptr(path_src, index_src);
3026   current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
3027   while (index_src >= 0 && index_dst >= 0 && *current_src == *current_dst) {
3028     routing_component_t *tmp_src, *tmp_dst;
3029     tmp_src = xbt_dynar_pop_ptr(path_src);
3030     tmp_dst = xbt_dynar_pop_ptr(path_dst);
3031     index_src--;
3032     index_dst--;
3033     current_src = xbt_dynar_get_ptr(path_src, index_src);
3034     current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
3035   }
3036
3037   int max_index_src = path_src->used - 1;
3038   int max_index_dst = path_dst->used - 1;
3039
3040   int max_index = max(max_index_src, max_index_dst);
3041   int i, max;
3042
3043   route_extended_t e_route_bypass = NULL;
3044
3045   for (max = 0; max <= max_index; max++) {
3046     for (i = 0; i < max; i++) {
3047       if (i <= max_index_src && max <= max_index_dst) {
3048         char *route_name = bprintf("%s#%s",
3049                                    (*(routing_component_t *)
3050                                     (xbt_dynar_get_ptr
3051                                      (path_src, i)))->name,
3052                                    (*(routing_component_t *)
3053                                     (xbt_dynar_get_ptr
3054                                      (path_dst, max)))->name);
3055         e_route_bypass =
3056             xbt_dict_get_or_null(dict_bypassRoutes, route_name);
3057         xbt_free(route_name);
3058       }
3059       if (e_route_bypass)
3060         break;
3061       if (max <= max_index_src && i <= max_index_dst) {
3062         char *route_name = bprintf("%s#%s",
3063                                    (*(routing_component_t *)
3064                                     (xbt_dynar_get_ptr
3065                                      (path_src, max)))->name,
3066                                    (*(routing_component_t *)
3067                                     (xbt_dynar_get_ptr
3068                                      (path_dst, i)))->name);
3069         e_route_bypass =
3070             xbt_dict_get_or_null(dict_bypassRoutes, route_name);
3071         xbt_free(route_name);
3072       }
3073       if (e_route_bypass)
3074         break;
3075     }
3076
3077     if (e_route_bypass)
3078       break;
3079
3080     if (max <= max_index_src && max <= max_index_dst) {
3081       char *route_name = bprintf("%s#%s",
3082                                  (*(routing_component_t *)
3083                                   (xbt_dynar_get_ptr
3084                                    (path_src, max)))->name,
3085                                  (*(routing_component_t *)
3086                                   (xbt_dynar_get_ptr
3087                                    (path_dst, max)))->name);
3088       e_route_bypass = xbt_dict_get_or_null(dict_bypassRoutes, route_name);
3089       xbt_free(route_name);
3090     }
3091     if (e_route_bypass)
3092       break;
3093   }
3094
3095   xbt_dynar_free(&path_src);
3096   xbt_dynar_free(&path_dst);
3097
3098   route_extended_t new_e_route = NULL;
3099
3100   if (e_route_bypass) {
3101     void *link;
3102     unsigned int cpt = 0;
3103     new_e_route = xbt_new0(s_route_extended_t, 1);
3104     new_e_route->src_gateway = xbt_strdup(e_route_bypass->src_gateway);
3105     new_e_route->dst_gateway = xbt_strdup(e_route_bypass->dst_gateway);
3106     new_e_route->generic_route.link_list =
3107         xbt_dynar_new(global_routing->size_of_link, NULL);
3108     xbt_dynar_foreach(e_route_bypass->generic_route.link_list, cpt, link) {
3109       xbt_dynar_push(new_e_route->generic_route.link_list, &link);
3110     }
3111   }
3112
3113   return new_e_route;
3114 }
3115
3116 /* ************************************************************************** */
3117 /* ************************* GENERIC AUX FUNCTIONS ************************** */
3118
3119 static route_t
3120 generic_new_route(e_surf_routing_hierarchy_t hierarchy,
3121                            void *data, int order)
3122 {
3123
3124   char *link_name;
3125   route_t new_route;
3126   unsigned int cpt;
3127   xbt_dynar_t links = NULL, links_id = NULL;
3128
3129   new_route = xbt_new0(s_route_t, 1);
3130   new_route->link_list =
3131       xbt_dynar_new(global_routing->size_of_link, NULL);
3132
3133   xbt_assert0(hierarchy == SURF_ROUTING_BASE,
3134               "the hierarchy type is not SURF_ROUTING_BASE");
3135
3136   links = ((route_t) data)->link_list;
3137
3138
3139   links_id = new_route->link_list;
3140
3141   xbt_dynar_foreach(links, cpt, link_name) {
3142
3143     void *link =
3144         xbt_dict_get_or_null(surf_network_model->resource_set, link_name);
3145     if (link) {
3146       if (order)
3147         xbt_dynar_push(links_id, &link);
3148       else
3149         xbt_dynar_unshift(links_id, &link);
3150     } else
3151       THROW1(mismatch_error, 0, "Link %s not found", link_name);
3152   }
3153
3154   return new_route;
3155 }
3156
3157 static route_extended_t
3158 generic_new_extended_route(e_surf_routing_hierarchy_t hierarchy,
3159                            void *data, int order)
3160 {
3161
3162   char *link_name;
3163   route_extended_t e_route, new_e_route;
3164   route_t route;
3165   unsigned int cpt;
3166   xbt_dynar_t links = NULL, links_id = NULL;
3167
3168   new_e_route = xbt_new0(s_route_extended_t, 1);
3169   new_e_route->generic_route.link_list =
3170       xbt_dynar_new(global_routing->size_of_link, NULL);
3171   new_e_route->src_gateway = NULL;
3172   new_e_route->dst_gateway = NULL;
3173
3174   xbt_assert0(hierarchy == SURF_ROUTING_BASE
3175               || hierarchy == SURF_ROUTING_RECURSIVE,
3176               "the hierarchy type is not defined");
3177
3178   if (hierarchy == SURF_ROUTING_BASE) {
3179
3180     route = (route_t) data;
3181     links = route->link_list;
3182
3183   } else if (hierarchy == SURF_ROUTING_RECURSIVE) {
3184
3185     e_route = (route_extended_t) data;
3186     xbt_assert0(e_route->src_gateway
3187                 && e_route->dst_gateway, "bad gateway, is null");
3188     links = e_route->generic_route.link_list;
3189
3190     /* remeber not erase the gateway names */
3191     new_e_route->src_gateway = e_route->src_gateway;
3192     new_e_route->dst_gateway = e_route->dst_gateway;
3193   }
3194
3195   links_id = new_e_route->generic_route.link_list;
3196
3197   xbt_dynar_foreach(links, cpt, link_name) {
3198
3199     void *link =
3200         xbt_dict_get_or_null(surf_network_model->resource_set, link_name);
3201     if (link) {
3202       if (order)
3203         xbt_dynar_push(links_id, &link);
3204       else
3205         xbt_dynar_unshift(links_id, &link);
3206     } else
3207       THROW1(mismatch_error, 0, "Link %s not found", link_name);
3208   }
3209
3210   return new_e_route;
3211 }
3212
3213 static void generic_free_route(route_t route)
3214 {
3215   if (route) {
3216     xbt_dynar_free(&(route->link_list));
3217     xbt_free(route);
3218   }
3219 }
3220
3221 static void generic_free_extended_route(route_extended_t e_route)
3222 {
3223   if (e_route) {
3224     xbt_dynar_free(&(e_route->generic_route.link_list));
3225     if (e_route->src_gateway)
3226       xbt_free(e_route->src_gateway);
3227     if (e_route->dst_gateway)
3228       xbt_free(e_route->dst_gateway);
3229     xbt_free(e_route);
3230   }
3231 }
3232
3233 static routing_component_t generic_as_exist(routing_component_t find_from,
3234                                             routing_component_t to_find)
3235 {
3236   //return to_find; // FIXME: BYPASSERROR OF FOREACH WITH BREAK
3237   xbt_dict_cursor_t cursor = NULL;
3238   char *key;
3239   int found = 0;
3240   routing_component_t elem;
3241   xbt_dict_foreach(find_from->routing_sons, cursor, key, elem) {
3242     if (to_find == elem || generic_as_exist(elem, to_find)) {
3243       found = 1;
3244       break;
3245     }
3246   }
3247   if (found)
3248     return to_find;
3249   return NULL;
3250 }
3251
3252 static routing_component_t
3253 generic_autonomous_system_exist(routing_component_t rc, char *element)
3254 {
3255   //return rc; // FIXME: BYPASSERROR OF FOREACH WITH BREAK
3256   routing_component_t element_as, result, elem;
3257   xbt_dict_cursor_t cursor = NULL;
3258   char *key;
3259   element_as = ((network_element_info_t)
3260                 xbt_dict_get_or_null
3261                 (global_routing->where_network_elements,
3262                  element))->rc_component;
3263   result = ((routing_component_t) - 1);
3264   if (element_as != rc)
3265     result = generic_as_exist(rc, element_as);
3266
3267   int found = 0;
3268   if (result) {
3269     xbt_dict_foreach(element_as->routing_sons, cursor, key, elem) {
3270       found = !strcmp(elem->name, element);
3271       if (found)
3272         break;
3273     }
3274     if (found)
3275       return element_as;
3276   }
3277   return NULL;
3278 }
3279
3280 static routing_component_t
3281 generic_processing_units_exist(routing_component_t rc, char *element)
3282 {
3283   routing_component_t element_as;
3284   element_as = ((network_element_info_t)
3285                 xbt_dict_get_or_null
3286                 (global_routing->where_network_elements,
3287                  element))->rc_component;
3288   if (element_as == rc)
3289     return element_as;
3290   return generic_as_exist(rc, element_as);
3291 }
3292
3293 static void generic_src_dst_check(routing_component_t rc, const char *src,
3294                                   const char *dst)
3295 {
3296   routing_component_t src_as = ((network_element_info_t)
3297                                 xbt_dict_get_or_null
3298                                 (global_routing->where_network_elements,
3299                                  src))->rc_component;
3300   routing_component_t dst_as = ((network_element_info_t)
3301                                 xbt_dict_get_or_null
3302                                 (global_routing->where_network_elements,
3303                                  dst))->rc_component;
3304   if(src_as == NULL || dst_as == NULL)
3305           xbt_die("Ask for route \"from\"(%s) or \"to\"(%s) no found at AS \"%s\"",
3306               src, dst, rc->name);
3307   if(src_as != dst_as)
3308           xbt_die("The src(%s in %s) and dst(%s in %s) are in differents AS",
3309               src, src_as->name, dst, dst_as->name);
3310   if(rc != dst_as)
3311          xbt_die("The routing component of src and dst is not the same as the network elements belong (%s==%s)",
3312      rc->name, dst_as->name);
3313 }
3314
3315 static void routing_parse_Sconfig(void)
3316 {
3317   XBT_DEBUG("START configuration name = %s",A_surfxml_config_id);
3318 }
3319
3320 static void routing_parse_Econfig(void)
3321 {
3322   xbt_dict_cursor_t cursor = NULL;
3323   char *key;
3324   char *elem;
3325   char *cfg;
3326   xbt_dict_foreach(current_property_set, cursor, key, elem) {
3327           cfg = bprintf("%s:%s",key,elem);
3328           if(xbt_cfg_is_default_value(_surf_cfg_set, key))
3329                   xbt_cfg_set_parse(_surf_cfg_set, cfg);
3330           else
3331                   XBT_INFO("The custom configuration '%s' is already define by user!",key);
3332           free(cfg);
3333   }
3334   XBT_DEBUG("End configuration name = %s",A_surfxml_config_id);
3335 }
3336
3337 static void routing_parse_Scluster(void)
3338 {
3339   static int AX_ptr = 0;
3340
3341   char *cluster_id = A_surfxml_cluster_id;
3342   char *cluster_prefix = A_surfxml_cluster_prefix;
3343   char *cluster_suffix = A_surfxml_cluster_suffix;
3344   char *cluster_radical = A_surfxml_cluster_radical;
3345   char *cluster_power = A_surfxml_cluster_power;
3346   char *cluster_core = A_surfxml_cluster_core;
3347   char *cluster_bw = A_surfxml_cluster_bw;
3348   char *cluster_lat = A_surfxml_cluster_lat;
3349   char *temp_cluster_bw = NULL;
3350   char *temp_cluster_lat = NULL;
3351   char *temp_cluster_power = NULL;
3352   char *cluster_bb_bw = A_surfxml_cluster_bb_bw;
3353   char *cluster_bb_lat = A_surfxml_cluster_bb_lat;
3354   char *cluster_availability_file = A_surfxml_cluster_availability_file;
3355   char *cluster_state_file = A_surfxml_cluster_state_file;
3356   char *host_id, *groups, *link_id = NULL;
3357   char *router_id, *link_router, *link_backbone;
3358   char *availability_file = bprintf("%s",cluster_availability_file);
3359   char *state_file = bprintf("%s",cluster_state_file);
3360
3361   if(xbt_dict_size(patterns)==0)
3362           patterns = xbt_dict_new();
3363
3364   xbt_dict_set(patterns,"id",cluster_id,NULL);
3365   xbt_dict_set(patterns,"prefix",cluster_prefix,NULL);
3366   xbt_dict_set(patterns,"suffix",cluster_suffix,NULL);
3367
3368 #ifdef HAVE_PCRE_LIB
3369   char *route_src_dst;
3370 #endif
3371   unsigned int iter;
3372   int start, end, i;
3373   xbt_dynar_t radical_elements;
3374   xbt_dynar_t radical_ends;
3375   int cluster_sharing_policy = AX_surfxml_cluster_sharing_policy;
3376   int cluster_bb_sharing_policy = AX_surfxml_cluster_bb_sharing_policy;
3377
3378 #ifndef HAVE_PCRE_LIB
3379   xbt_dynar_t tab_elements_num = xbt_dynar_new(sizeof(int), NULL);
3380   char *route_src, *route_dst;
3381   int j;
3382 #endif
3383
3384   static unsigned int surfxml_buffer_stack_stack_ptr = 1;
3385   static unsigned int surfxml_buffer_stack_stack[1024];
3386
3387   surfxml_buffer_stack_stack[0] = 0;
3388
3389   surfxml_bufferstack_push(1);
3390
3391   SURFXML_BUFFER_SET(AS_id, cluster_id);
3392 #ifdef HAVE_PCRE_LIB
3393   SURFXML_BUFFER_SET(AS_routing, "RuleBased");
3394   XBT_DEBUG("<AS id=\"%s\"\trouting=\"RuleBased\">", cluster_id);
3395 #else
3396   SURFXML_BUFFER_SET(AS_routing, "Full");
3397   XBT_DEBUG("<AS id=\"%s\"\trouting=\"Full\">", cluster_id);
3398 #endif
3399   SURFXML_START_TAG(AS);
3400
3401   radical_elements = xbt_str_split(cluster_radical, ",");
3402   xbt_dynar_foreach(radical_elements, iter, groups) {
3403     radical_ends = xbt_str_split(groups, "-");
3404     switch (xbt_dynar_length(radical_ends)) {
3405     case 1:
3406       surf_parse_get_int(&start,
3407                          xbt_dynar_get_as(radical_ends, 0, char *));
3408       host_id = bprintf("%s%d%s", cluster_prefix, start, cluster_suffix);
3409 #ifndef HAVE_PCRE_LIB
3410       xbt_dynar_push_as(tab_elements_num, int, start);
3411 #endif
3412       link_id = bprintf("%s_link_%d", cluster_id, start);
3413
3414       xbt_dict_set(patterns, "radical", bprintf("%d", start), xbt_free);
3415       temp_cluster_power = bprintf("%s",cluster_power);
3416       temp_cluster_power = replace_random_parameter(temp_cluster_power);
3417       XBT_DEBUG("<host\tid=\"%s\"\tpower=\"%s\">", host_id, temp_cluster_power);
3418       A_surfxml_host_state = A_surfxml_host_state_ON;
3419       SURFXML_BUFFER_SET(host_id, host_id);
3420       SURFXML_BUFFER_SET(host_power, temp_cluster_power);
3421       SURFXML_BUFFER_SET(host_core, cluster_core);
3422       SURFXML_BUFFER_SET(host_availability, "1.0");
3423       SURFXML_BUFFER_SET(host_coordinates, "");
3424       xbt_free(availability_file);
3425       availability_file = bprintf("%s",cluster_availability_file);
3426       xbt_free(state_file);
3427       state_file = bprintf("%s",cluster_state_file);
3428       XBT_DEBUG("\tavailability_file=\"%s\"",xbt_str_varsubst(availability_file,patterns));
3429       XBT_DEBUG("\tstate_file=\"%s\"",xbt_str_varsubst(state_file,patterns));
3430       SURFXML_BUFFER_SET(host_availability_file, xbt_str_varsubst(availability_file,patterns));
3431       SURFXML_BUFFER_SET(host_state_file, xbt_str_varsubst(state_file,patterns));
3432       XBT_DEBUG("</host>");
3433       SURFXML_START_TAG(host);
3434       SURFXML_END_TAG(host);
3435
3436
3437       temp_cluster_bw = bprintf("%s",cluster_bw);
3438       temp_cluster_bw = replace_random_parameter(temp_cluster_bw);
3439       temp_cluster_lat = bprintf("%s",cluster_lat);
3440       temp_cluster_lat = replace_random_parameter(temp_cluster_lat);
3441       XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%s\"\tlat=\"%s\"/>", link_id,temp_cluster_bw, cluster_lat);
3442       A_surfxml_link_state = A_surfxml_link_state_ON;
3443       A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
3444       if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
3445           {A_surfxml_link_sharing_policy =  A_surfxml_link_sharing_policy_FULLDUPLEX;}
3446       if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FATPIPE)
3447           {A_surfxml_link_sharing_policy =  A_surfxml_link_sharing_policy_FATPIPE;}
3448       SURFXML_BUFFER_SET(link_id, link_id);
3449       SURFXML_BUFFER_SET(link_bandwidth, temp_cluster_bw);
3450       SURFXML_BUFFER_SET(link_latency, temp_cluster_lat);
3451       SURFXML_BUFFER_SET(link_bandwidth_file, "");
3452       SURFXML_BUFFER_SET(link_latency_file, "");
3453       SURFXML_BUFFER_SET(link_state_file, "");
3454       SURFXML_START_TAG(link);
3455       SURFXML_END_TAG(link);
3456
3457       xbt_free(temp_cluster_bw);
3458       xbt_free(temp_cluster_lat);
3459       xbt_free(temp_cluster_power);
3460       free(link_id);
3461       free(host_id);
3462       break;
3463
3464     case 2:
3465
3466       surf_parse_get_int(&start,
3467                          xbt_dynar_get_as(radical_ends, 0, char *));
3468       surf_parse_get_int(&end, xbt_dynar_get_as(radical_ends, 1, char *));
3469       for (i = start; i <= end; i++) {
3470         host_id = bprintf("%s%d%s", cluster_prefix, i, cluster_suffix);
3471 #ifndef HAVE_PCRE_LIB
3472         xbt_dynar_push_as(tab_elements_num, int, i);
3473 #endif
3474         link_id = bprintf("%s_link_%d", cluster_id, i);
3475
3476         xbt_dict_set(patterns, "radical", bprintf("%d", i), xbt_free);
3477         temp_cluster_power = bprintf("%s",cluster_power);
3478         temp_cluster_power = replace_random_parameter(temp_cluster_power);
3479         XBT_DEBUG("<host\tid=\"%s\"\tpower=\"%s\">", host_id, temp_cluster_power);
3480         A_surfxml_host_state = A_surfxml_host_state_ON;
3481         SURFXML_BUFFER_SET(host_id, host_id);
3482         SURFXML_BUFFER_SET(host_power, temp_cluster_power);
3483         SURFXML_BUFFER_SET(host_core, cluster_core);
3484         SURFXML_BUFFER_SET(host_availability, "1.0");
3485         SURFXML_BUFFER_SET(host_coordinates, "");
3486         xbt_free(availability_file);
3487         availability_file = bprintf("%s",cluster_availability_file);
3488         xbt_free(state_file);
3489         state_file = bprintf("%s",cluster_state_file);
3490         XBT_DEBUG("\tavailability_file=\"%s\"",xbt_str_varsubst(availability_file,patterns));
3491         XBT_DEBUG("\tstate_file=\"%s\"",xbt_str_varsubst(state_file,patterns));
3492         SURFXML_BUFFER_SET(host_availability_file, xbt_str_varsubst(availability_file,patterns));
3493         SURFXML_BUFFER_SET(host_state_file, xbt_str_varsubst(state_file,patterns));
3494         XBT_DEBUG("</host>");
3495         SURFXML_START_TAG(host);
3496         SURFXML_END_TAG(host);
3497
3498         xbt_free(temp_cluster_power);
3499
3500         temp_cluster_bw = bprintf("%s",cluster_bw);
3501         temp_cluster_bw = replace_random_parameter(temp_cluster_bw);
3502         temp_cluster_lat = bprintf("%s",cluster_lat);
3503         temp_cluster_lat = replace_random_parameter(temp_cluster_lat);
3504         XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%s\"\tlat=\"%s\"/>", link_id,temp_cluster_bw, cluster_lat);
3505         A_surfxml_link_state = A_surfxml_link_state_ON;
3506         A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
3507         if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
3508             {A_surfxml_link_sharing_policy =  A_surfxml_link_sharing_policy_FULLDUPLEX;}
3509         if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FATPIPE)
3510             {A_surfxml_link_sharing_policy =  A_surfxml_link_sharing_policy_FATPIPE;}
3511         SURFXML_BUFFER_SET(link_id, link_id);
3512         SURFXML_BUFFER_SET(link_bandwidth, temp_cluster_bw);
3513         SURFXML_BUFFER_SET(link_latency, temp_cluster_lat);
3514         SURFXML_BUFFER_SET(link_bandwidth_file, "");
3515         SURFXML_BUFFER_SET(link_latency_file, "");
3516         SURFXML_BUFFER_SET(link_state_file, "");
3517         SURFXML_START_TAG(link);
3518         SURFXML_END_TAG(link);
3519
3520         xbt_free(temp_cluster_bw);
3521         xbt_free(temp_cluster_lat);
3522         free(link_id);
3523         free(host_id);
3524       }
3525       break;
3526
3527     default:
3528       XBT_DEBUG("Malformed radical");
3529     }
3530
3531     xbt_dynar_free(&radical_ends);
3532   }
3533   xbt_dynar_free(&radical_elements);
3534
3535   XBT_DEBUG(" ");
3536   router_id =
3537       bprintf("%s%s_router%s", cluster_prefix, cluster_id,
3538               cluster_suffix);
3539   link_router = bprintf("%s_link_%s_router", cluster_id, cluster_id);
3540   link_backbone = bprintf("%s_backbone", cluster_id);
3541
3542   XBT_DEBUG("<router id=\"%s\"/>", router_id);
3543   SURFXML_BUFFER_SET(router_id, router_id);
3544   SURFXML_START_TAG(router);
3545   SURFXML_END_TAG(router);
3546
3547   //TODO
3548   xbt_dict_set(patterns, "radical", bprintf("_router"), xbt_free);
3549   temp_cluster_bw = bprintf("%s",cluster_bw);
3550   temp_cluster_bw = replace_random_parameter(temp_cluster_bw);
3551   temp_cluster_lat = bprintf("%s",cluster_lat);
3552   temp_cluster_lat = replace_random_parameter(temp_cluster_lat);
3553   XBT_DEBUG("<link\tid=\"%s\" bw=\"%s\" lat=\"%s\"/>", link_router,temp_cluster_bw, temp_cluster_lat);
3554   A_surfxml_link_state = A_surfxml_link_state_ON;
3555   A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
3556   if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
3557   {A_surfxml_link_sharing_policy =  A_surfxml_link_sharing_policy_FULLDUPLEX;}
3558   if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FATPIPE)
3559   {A_surfxml_link_sharing_policy =  A_surfxml_link_sharing_policy_FATPIPE;}
3560   SURFXML_BUFFER_SET(link_id, link_router);
3561   SURFXML_BUFFER_SET(link_bandwidth, temp_cluster_bw);
3562   SURFXML_BUFFER_SET(link_latency, temp_cluster_lat);
3563   SURFXML_BUFFER_SET(link_bandwidth_file, "");
3564   SURFXML_BUFFER_SET(link_latency_file, "");
3565   SURFXML_BUFFER_SET(link_state_file, "");
3566   SURFXML_START_TAG(link);
3567   SURFXML_END_TAG(link);
3568
3569   xbt_free(temp_cluster_bw);
3570   xbt_free(temp_cluster_lat);
3571
3572   XBT_DEBUG("<link\tid=\"%s\" bw=\"%s\" lat=\"%s\"/>", link_backbone,cluster_bb_bw, cluster_bb_lat);
3573   A_surfxml_link_state = A_surfxml_link_state_ON;
3574   A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
3575   if(cluster_bb_sharing_policy == A_surfxml_cluster_bb_sharing_policy_FATPIPE)
3576   {A_surfxml_link_sharing_policy =  A_surfxml_link_sharing_policy_FATPIPE;}
3577   SURFXML_BUFFER_SET(link_id, link_backbone);
3578   SURFXML_BUFFER_SET(link_bandwidth, cluster_bb_bw);
3579   SURFXML_BUFFER_SET(link_latency, cluster_bb_lat);
3580   SURFXML_BUFFER_SET(link_bandwidth_file, "");
3581   SURFXML_BUFFER_SET(link_latency_file, "");
3582   SURFXML_BUFFER_SET(link_state_file, "");
3583   SURFXML_START_TAG(link);
3584   SURFXML_END_TAG(link);
3585
3586   XBT_DEBUG(" ");
3587
3588 #ifdef HAVE_PCRE_LIB
3589   char *new_suffix = xbt_strdup("");
3590
3591   radical_elements = xbt_str_split(cluster_suffix, ".");
3592   xbt_dynar_foreach(radical_elements, iter, groups) {
3593     if (strcmp(groups, "")) {
3594       char *old_suffix = new_suffix;
3595       new_suffix = bprintf("%s\\.%s", old_suffix, groups);
3596       free(old_suffix);
3597     }
3598   }
3599   route_src_dst = bprintf("%s(.*)%s", cluster_prefix, new_suffix);
3600   xbt_dynar_free(&radical_elements);
3601   free(new_suffix);
3602
3603   char *pcre_link_src = bprintf("%s_link_$1src", cluster_id);
3604   char *pcre_link_backbone = bprintf("%s_backbone", cluster_id);
3605   char *pcre_link_dst = bprintf("%s_link_$1dst", cluster_id);
3606
3607   XBT_DEBUG("<route\tsrc=\"%s\"\tdst=\"%s\"", route_src_dst, route_src_dst);
3608   XBT_DEBUG("symmetrical=\"NO\">");
3609   SURFXML_BUFFER_SET(route_src, route_src_dst);
3610   SURFXML_BUFFER_SET(route_dst, route_src_dst);
3611   A_surfxml_route_symmetrical = A_surfxml_route_symmetrical_NO;
3612   SURFXML_START_TAG(route);
3613
3614   XBT_DEBUG("<link_ctn\tid=\"%s\"/>", pcre_link_src);
3615   SURFXML_BUFFER_SET(link_ctn_id, pcre_link_src);
3616   A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
3617   if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
3618   {A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_UP;}
3619   SURFXML_START_TAG(link_ctn);
3620   SURFXML_END_TAG(link_ctn);
3621
3622   XBT_DEBUG("<link_ctn\tid=\"%s\"/>", pcre_link_backbone);
3623   SURFXML_BUFFER_SET(link_ctn_id, pcre_link_backbone);
3624   A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
3625   SURFXML_START_TAG(link_ctn);
3626   SURFXML_END_TAG(link_ctn);
3627
3628   XBT_DEBUG("<link_ctn\tid=\"%s\"/>", pcre_link_dst);
3629   SURFXML_BUFFER_SET(link_ctn_id, pcre_link_dst);
3630   A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
3631   if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
3632   {A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_DOWN;}
3633   SURFXML_START_TAG(link_ctn);
3634   SURFXML_END_TAG(link_ctn);
3635
3636   XBT_DEBUG("</route>");
3637   SURFXML_END_TAG(route);
3638
3639   free(pcre_link_dst);
3640   free(pcre_link_backbone);
3641   free(pcre_link_src);
3642   free(route_src_dst);
3643 #else
3644   for (i = 0; i <= xbt_dynar_length(tab_elements_num); i++) {
3645     for (j = 0; j <= xbt_dynar_length(tab_elements_num); j++) {
3646       if (i == xbt_dynar_length(tab_elements_num)) {
3647         route_src = router_id;
3648       } else {
3649         route_src =
3650             bprintf("%s%d%s", cluster_prefix,
3651                     xbt_dynar_get_as(tab_elements_num, i, int),
3652                     cluster_suffix);
3653       }
3654
3655       if (j == xbt_dynar_length(tab_elements_num)) {
3656         route_dst = router_id;
3657       } else {
3658         route_dst =
3659             bprintf("%s%d%s", cluster_prefix,
3660                     xbt_dynar_get_as(tab_elements_num, j, int),
3661                     cluster_suffix);
3662       }
3663
3664       XBT_DEBUG("<route\tsrc=\"%s\"\tdst=\"%s\"", route_src, route_dst);
3665       XBT_DEBUG("symmetrical=\"NO\">");
3666       SURFXML_BUFFER_SET(route_src, route_src);
3667       SURFXML_BUFFER_SET(route_dst, route_dst);
3668       A_surfxml_route_symmetrical = A_surfxml_route_symmetrical_NO;
3669       SURFXML_START_TAG(route);
3670
3671       if (i == xbt_dynar_length(tab_elements_num)) {
3672         route_src = link_router;
3673       } else {
3674         route_src =
3675             bprintf("%s_link_%d", cluster_id,
3676                     xbt_dynar_get_as(tab_elements_num, i, int));
3677       }
3678
3679       if (j == xbt_dynar_length(tab_elements_num)) {
3680         route_dst = link_router;
3681       } else {
3682         route_dst =
3683             bprintf("%s_link_%d", cluster_id,
3684                     xbt_dynar_get_as(tab_elements_num, j, int));
3685       }
3686
3687       XBT_DEBUG("<link_ctn\tid=\"%s\"/>", route_src);
3688       SURFXML_BUFFER_SET(link_ctn_id, route_src);
3689       A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
3690       if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
3691       {A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_UP;}
3692       SURFXML_START_TAG(link_ctn);
3693       SURFXML_END_TAG(link_ctn);
3694
3695       XBT_DEBUG("<link_ctn\tid=\"%s_backbone\"/>", cluster_id);
3696       SURFXML_BUFFER_SET(link_ctn_id, bprintf("%s_backbone", cluster_id));
3697       A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
3698       SURFXML_START_TAG(link_ctn);
3699       SURFXML_END_TAG(link_ctn);
3700
3701       XBT_DEBUG("<link_ctn\tid=\"%s\"/>", route_dst);
3702       SURFXML_BUFFER_SET(link_ctn_id, route_dst);
3703       A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
3704       if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
3705       {A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_DOWN;}
3706       SURFXML_START_TAG(link_ctn);
3707       SURFXML_END_TAG(link_ctn);
3708
3709       XBT_DEBUG("</route>");
3710       SURFXML_END_TAG(route);
3711     }
3712   }
3713   xbt_dynar_free(&tab_elements_num);
3714
3715 #endif
3716
3717   free(router_id);
3718   free(link_backbone);
3719   free(link_router);
3720   xbt_dict_free(&patterns);
3721   free(availability_file);
3722   free(state_file);
3723
3724   XBT_DEBUG("</AS>");
3725   SURFXML_END_TAG(AS);
3726   XBT_DEBUG(" ");
3727
3728   surfxml_bufferstack_pop(1);
3729 }
3730 /*
3731  * This function take a string and replace parameters from patterns dict.
3732  * It returns the new value.
3733  */
3734 static char* replace_random_parameter(char * string)
3735 {
3736   char *test_string = NULL;
3737
3738   if(xbt_dict_size(random_value)==0)
3739     return string;
3740
3741   string = xbt_str_varsubst(string, patterns); // for patterns of cluster
3742   test_string = bprintf("${%s}", string);
3743   test_string = xbt_str_varsubst(test_string,random_value); //Add ${xxxxx} for random Generator
3744
3745   if (strcmp(test_string,"")) { //if not empty, keep this value.
3746     xbt_free(string);
3747     string = test_string;
3748   } //In other case take old value (without ${})
3749   else
3750         free(test_string);
3751   return string;
3752 }
3753
3754 static void clean_dict_random(void)
3755 {
3756         XBT_DEBUG("Clean dict for random");
3757         xbt_dict_free(&random_value);
3758         xbt_dict_free(&patterns);
3759 }
3760
3761 static void routing_parse_Speer(void)
3762 {
3763   static int AX_ptr = 0;
3764
3765   char *peer_id = A_surfxml_peer_id;
3766   char *peer_power = A_surfxml_peer_power;
3767   char *peer_bw_in = A_surfxml_peer_bw_in;
3768   char *peer_bw_out = A_surfxml_peer_bw_out;
3769   char *peer_lat = A_surfxml_peer_lat;
3770   char *peer_coord = A_surfxml_peer_coordinates;
3771   char *peer_state_file = A_surfxml_peer_state_file;
3772   char *peer_availability_file = A_surfxml_peer_availability_file;
3773
3774   char *host_id = NULL;
3775   char *router_id, *link_router, *link_backbone, *link_id_up, *link_id_down;
3776
3777 #ifdef HAVE_PCRE_LIB
3778
3779 #endif
3780
3781   int peer_sharing_policy = AX_surfxml_peer_sharing_policy;
3782
3783 #ifndef HAVE_PCRE_LIB
3784   //xbt_dynar_t tab_elements_num = xbt_dynar_new(sizeof(int), NULL);
3785   //char *route_src, *route_dst;
3786   //int j;
3787 #endif
3788
3789   static unsigned int surfxml_buffer_stack_stack_ptr = 1;
3790   static unsigned int surfxml_buffer_stack_stack[1024];
3791
3792   surfxml_buffer_stack_stack[0] = 0;
3793
3794   surfxml_bufferstack_push(1);
3795
3796   SURFXML_BUFFER_SET(AS_id, peer_id);
3797   SURFXML_BUFFER_SET(AS_coordinates, peer_coord);
3798 #ifdef HAVE_PCRE_LIB
3799   SURFXML_BUFFER_SET(AS_routing, "RuleBased");
3800   XBT_DEBUG("<AS id=\"%s\"\trouting=\"RuleBased\">", peer_id);
3801 #else
3802   SURFXML_BUFFER_SET(AS_routing, "Full");
3803   XBT_DEBUG("<AS id=\"%s\"\trouting=\"Full\">", peer_id);
3804 #endif
3805   SURFXML_START_TAG(AS);
3806
3807   XBT_DEBUG(" ");
3808   host_id = bprintf("peer_%s", peer_id);
3809   router_id = bprintf("router_%s", peer_id);
3810   link_id_up = bprintf("link_%s_up", peer_id);
3811   link_id_down = bprintf("link_%s_down", peer_id);
3812
3813   link_router = bprintf("%s_link_router", peer_id);
3814   link_backbone = bprintf("%s_backbone", peer_id);
3815
3816   XBT_DEBUG("<host\tid=\"%s\"\tpower=\"%s\"/>", host_id, peer_power);
3817   A_surfxml_host_state = A_surfxml_host_state_ON;
3818   SURFXML_BUFFER_SET(host_id, host_id);
3819   SURFXML_BUFFER_SET(host_power, peer_power);
3820   SURFXML_BUFFER_SET(host_availability, "1.0");
3821   SURFXML_BUFFER_SET(host_availability_file, peer_availability_file);
3822   SURFXML_BUFFER_SET(host_state_file, peer_state_file);
3823   SURFXML_BUFFER_SET(host_coordinates, "");
3824   SURFXML_START_TAG(host);
3825   SURFXML_END_TAG(host);
3826
3827   XBT_DEBUG("<router id=\"%s\"\tcoordinates=\"%s\"/>", router_id, peer_coord);
3828   SURFXML_BUFFER_SET(router_id, router_id);
3829   SURFXML_BUFFER_SET(router_coordinates, peer_coord);
3830   SURFXML_START_TAG(router);
3831   SURFXML_END_TAG(router);
3832
3833   XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%s\"\tlat=\"%s\"/>", link_id_up, peer_bw_in, peer_lat);
3834   A_surfxml_link_state = A_surfxml_link_state_ON;
3835   A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
3836   if(peer_sharing_policy == A_surfxml_peer_sharing_policy_FULLDUPLEX)
3837 {A_surfxml_link_sharing_policy =  A_surfxml_link_sharing_policy_FULLDUPLEX;}
3838   SURFXML_BUFFER_SET(link_id, link_id_up);
3839   SURFXML_BUFFER_SET(link_bandwidth, peer_bw_in);
3840   SURFXML_BUFFER_SET(link_latency, peer_lat);
3841   SURFXML_BUFFER_SET(link_bandwidth_file, "");
3842   SURFXML_BUFFER_SET(link_latency_file, "");
3843   SURFXML_BUFFER_SET(link_state_file, "");
3844   SURFXML_START_TAG(link);
3845   SURFXML_END_TAG(link);
3846
3847   XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%s\"\tlat=\"%s\"/>", link_id_down, peer_bw_out, peer_lat);
3848   A_surfxml_link_state = A_surfxml_link_state_ON;
3849   A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
3850   if(peer_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
3851 {A_surfxml_link_sharing_policy =  A_surfxml_link_sharing_policy_FULLDUPLEX;}
3852   SURFXML_BUFFER_SET(link_id, link_id_down);
3853   SURFXML_BUFFER_SET(link_bandwidth, peer_bw_out);
3854   SURFXML_BUFFER_SET(link_latency, peer_lat);
3855   SURFXML_BUFFER_SET(link_bandwidth_file, "");
3856   SURFXML_BUFFER_SET(link_latency_file, "");
3857   SURFXML_BUFFER_SET(link_state_file, "");
3858   SURFXML_START_TAG(link);
3859   SURFXML_END_TAG(link);
3860
3861   XBT_DEBUG(" ");
3862
3863   // begin here
3864   XBT_DEBUG("<route\tsrc=\"%s\"\tdst=\"%s\"", peer_id, router_id);
3865   XBT_DEBUG("symmetrical=\"NO\">");
3866   SURFXML_BUFFER_SET(route_src, peer_id);
3867   SURFXML_BUFFER_SET(route_dst, router_id);
3868   A_surfxml_route_symmetrical = A_surfxml_route_symmetrical_NO;
3869   SURFXML_START_TAG(route);
3870
3871   XBT_DEBUG("<link_ctn\tid=\"%s\"/>", link_id_up);
3872   SURFXML_BUFFER_SET(link_ctn_id, link_id_up);
3873   A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
3874   if(peer_sharing_policy == A_surfxml_peer_sharing_policy_FULLDUPLEX)
3875   {A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_UP;}
3876   SURFXML_START_TAG(link_ctn);
3877   SURFXML_END_TAG(link_ctn);
3878
3879   XBT_DEBUG("</route>");
3880   SURFXML_END_TAG(route);
3881
3882   //Opposite Route
3883   XBT_DEBUG("<route\tsrc=\"%s\"\tdst=\"%s\"", router_id, peer_id);
3884   XBT_DEBUG("symmetrical=\"NO\">");
3885   SURFXML_BUFFER_SET(route_src, router_id);
3886   SURFXML_BUFFER_SET(route_dst, peer_id);
3887   A_surfxml_route_symmetrical = A_surfxml_route_symmetrical_NO;
3888   SURFXML_START_TAG(route);
3889
3890   XBT_DEBUG("<link_ctn\tid=\"%s\"/>", link_id_down);
3891   SURFXML_BUFFER_SET(link_ctn_id, link_id_down);
3892   A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
3893   if(peer_sharing_policy == A_surfxml_peer_sharing_policy_FULLDUPLEX)
3894   {A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_DOWN;}
3895   SURFXML_START_TAG(link_ctn);
3896   SURFXML_END_TAG(link_ctn);
3897
3898   XBT_DEBUG("</route>");
3899   SURFXML_END_TAG(route);
3900
3901   XBT_DEBUG("</AS>");
3902   SURFXML_END_TAG(AS);
3903   XBT_DEBUG(" ");
3904
3905   //xbt_dynar_free(&tab_elements_num);
3906
3907   surfxml_bufferstack_pop(1);
3908 }
3909
3910 static void routing_parse_Srandom(void)
3911 {
3912           double mean, std, min, max, seed;
3913           char *random_id = A_surfxml_random_id;
3914           char *random_radical = A_surfxml_random_radical;
3915           char *rd_name = NULL;
3916           char *rd_value;
3917           surf_parse_get_double(&mean,A_surfxml_random_mean);
3918           surf_parse_get_double(&std,A_surfxml_random_std_deviation);
3919           surf_parse_get_double(&min,A_surfxml_random_min);
3920           surf_parse_get_double(&max,A_surfxml_random_max);
3921           surf_parse_get_double(&seed,A_surfxml_random_seed);
3922
3923           double res = 0;
3924           int i = 0;
3925           random_data_t random = xbt_new0(s_random_data_t, 1);
3926           char *tmpbuf;
3927
3928           xbt_dynar_t radical_elements;
3929           unsigned int iter;
3930           char *groups;
3931           int start, end;
3932           xbt_dynar_t radical_ends;
3933
3934           random->generator = A_surfxml_random_generator;
3935           random->seed = seed;
3936           random->min = min;
3937           random->max = max;
3938
3939           /* Check user stupidities */
3940           if (max < min)
3941             THROW2(arg_error, 0, "random->max < random->min (%f < %f)", max, min);
3942           if (mean < min)
3943             THROW2(arg_error, 0, "random->mean < random->min (%f < %f)", mean,
3944                    min);
3945           if (mean > max)
3946             THROW2(arg_error, 0, "random->mean > random->max (%f > %f)", mean,
3947                    max);
3948
3949           /* normalize the mean and standard deviation before storing */
3950           random->mean = (mean - min) / (max - min);
3951           random->std = std / (max - min);
3952
3953           if (random->mean * (1 - random->mean) < random->std * random->std)
3954             THROW2(arg_error, 0, "Invalid mean and standard deviation (%f and %f)",
3955                    random->mean, random->std);
3956
3957           XBT_DEBUG("id = '%s' min = '%f' max = '%f' mean = '%f' std_deviatinon = '%f' generator = '%d' seed = '%ld' radical = '%s'",
3958           random_id,
3959           random->min,
3960           random->max,
3961           random->mean,
3962           random->std,
3963           random->generator,
3964           random->seed,
3965           random_radical);
3966
3967           if(xbt_dict_size(random_value)==0)
3968                   random_value = xbt_dict_new();
3969
3970           if(!strcmp(random_radical,""))
3971           {
3972                   res = random_generate(random);
3973                   rd_value = bprintf("%f",res);
3974                   xbt_dict_set(random_value, random_id, rd_value, free);
3975           }
3976           else
3977           {
3978                   radical_elements = xbt_str_split(random_radical, ",");
3979                   xbt_dynar_foreach(radical_elements, iter, groups) {
3980                         radical_ends = xbt_str_split(groups, "-");
3981                         switch (xbt_dynar_length(radical_ends)) {
3982                         case 1:
3983                                           xbt_assert1(!xbt_dict_get_or_null(random_value,random_id),"Custom Random '%s' already exists !",random_id);
3984                                           res = random_generate(random);
3985                                           tmpbuf = bprintf("%s%d",random_id,atoi(xbt_dynar_getfirst_as(radical_ends,char *)));
3986                                           xbt_dict_set(random_value, tmpbuf, bprintf("%f",res), free);
3987                                           xbt_free(tmpbuf);
3988                                           break;
3989
3990                         case 2:   surf_parse_get_int(&start,
3991                                                                                  xbt_dynar_get_as(radical_ends, 0, char *));
3992                                           surf_parse_get_int(&end, xbt_dynar_get_as(radical_ends, 1, char *));
3993                                           for (i = start; i <= end; i++) {
3994                                                   xbt_assert1(!xbt_dict_get_or_null(random_value,random_id),"Custom Random '%s' already exists !",bprintf("%s%d",random_id,i));
3995                                                   res = random_generate(random);
3996                           tmpbuf = bprintf("%s%d",random_id,i);
3997                                                   xbt_dict_set(random_value, tmpbuf, bprintf("%f",res), free);
3998                           xbt_free(tmpbuf);
3999                                           }
4000                                           break;
4001                         default:
4002                                 XBT_INFO("Malformed radical");
4003                         }
4004                         res = random_generate(random);
4005                         rd_name  = bprintf("%s_router",random_id);
4006                         rd_value = bprintf("%f",res);
4007                         xbt_dict_set(random_value, rd_name, rd_value, free);
4008
4009                         xbt_dynar_free(&radical_ends);
4010                   }
4011                   free(rd_name);
4012                   xbt_dynar_free(&radical_elements);
4013           }
4014 }
4015
4016 static void routing_parse_Erandom(void)
4017 {
4018         xbt_dict_cursor_t cursor = NULL;
4019         char *key;
4020         char *elem;
4021
4022         xbt_dict_foreach(random_value, cursor, key, elem) {
4023           XBT_DEBUG("%s = %s",key,elem);
4024         }
4025
4026 }
4027
4028
4029 /*
4030  * New methods to init the routing model component from the lua script
4031  */
4032
4033 /*
4034  * calling parse_S_AS_lua with lua values
4035  */
4036 void routing_AS_init(const char *AS_id, const char *AS_routing)
4037 {
4038   parse_S_AS_lua((char *) AS_id, (char *) AS_routing);
4039 }
4040
4041 /*
4042  * calling parse_E_AS_lua to fisnish the creation of routing component
4043  */
4044 void routing_AS_end(const char *AS_id)
4045 {
4046   parse_E_AS_lua((char *) AS_id);
4047 }
4048
4049 /*
4050  * add a host to the network element list
4051  */
4052
4053 void routing_add_host(const char *host_id)
4054 {
4055   parse_S_host_lua((char *) host_id, (char*)""); // FIXME propagate coordinate system to lua
4056 }
4057
4058 /*
4059  * Set a new link on the actual list of link for a route or ASroute
4060  */
4061 void routing_add_link(const char *link_id)
4062 {
4063   parse_E_link_c_ctn_new_elem_lua((char *) link_id);
4064 }
4065
4066 /*
4067  *Set the endpoints for a route
4068  */
4069 void routing_set_route(const char *src_id, const char *dst_id)
4070 {
4071   parse_S_route_new_and_endpoints_lua(src_id, dst_id);
4072 }
4073
4074 /*
4075  * Store the route by calling parse_E_route_store_route
4076  */
4077 void routing_store_route(void)
4078 {
4079   parse_E_route_store_route();
4080 }