Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Kill the XML-level callbacks to AS_open/close and create some at sg_platf level instead
[simgrid.git] / src / surf / surf_routing.c
1 /* Copyright (c) 2009, 2010, 2011. 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 #include <pcre.h>               /* regular expression library */
8
9 #include "simgrid/platf_interface.h" // platform creation API internal interface
10
11 #include "surf_routing_private.h"
12 #include "surf/surf_routing.h"
13 #include "surf/surfxml_parse_values.h"
14
15 xbt_lib_t host_lib;
16 int ROUTING_HOST_LEVEL; //Routing level
17 int     SURF_CPU_LEVEL;         //Surf cpu level
18 int SURF_WKS_LEVEL;             //Surf workstation level
19 int SIMIX_HOST_LEVEL;   //Simix level
20 int     MSG_HOST_LEVEL;         //Msg level
21 int     SD_HOST_LEVEL;          //Simdag level
22 int     COORD_HOST_LEVEL;       //Coordinates level
23 int NS3_HOST_LEVEL;             //host node for ns3
24
25 xbt_lib_t link_lib;
26 int SD_LINK_LEVEL;              //Simdag level
27 int SURF_LINK_LEVEL;    //Surf level
28 int NS3_LINK_LEVEL;             //link for ns3
29
30 xbt_lib_t as_router_lib;
31 int ROUTING_ASR_LEVEL;  //Routing level
32 int COORD_ASR_LEVEL;    //Coordinates level
33 int NS3_ASR_LEVEL;              //host node for ns3
34
35 static xbt_dict_t patterns = NULL;
36 static xbt_dict_t random_value = NULL;
37
38 /* Global vars */
39 routing_global_t global_routing = NULL;
40 routing_component_t current_routing = NULL;
41 model_type_t current_routing_model = NULL;
42
43 /* global parse functions */
44 xbt_dynar_t link_list = NULL;    /* temporary store of current list link of a route */
45 static const char *src = NULL;        /* temporary store the source name of a route */
46 static const char *dst = NULL;        /* temporary store the destination name of a route */
47 static char *gw_src = NULL;     /* temporary store the gateway source name of a route */
48 static char *gw_dst = NULL;     /* temporary store the gateway destination name of a route */
49 static double_f_cpvoid_t get_link_latency = NULL;
50
51 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route, surf, "Routing part of surf");
52
53 static void routing_parse_Speer(sg_platf_peer_cbarg_t peer);          /* peer bypass */
54 static void routing_parse_Srandom(void);        /* random bypass */
55
56 static char* replace_random_parameter(char * chaine);
57 static void clean_routing_after_parse(void);
58
59 /* this lines are only for replace use like index in the model table */
60 typedef enum {
61   SURF_MODEL_FULL = 0,
62   SURF_MODEL_FLOYD,
63   SURF_MODEL_DIJKSTRA,
64   SURF_MODEL_DIJKSTRACACHE,
65   SURF_MODEL_NONE,
66   SURF_MODEL_RULEBASED,
67   SURF_MODEL_VIVALDI,
68   SURF_MODEL_CLUSTER
69 } e_routing_types;
70
71 struct s_model_type routing_models[] = { {"Full",
72                                           "Full routing data (fast, large memory requirements, fully expressive)",
73                                           model_full_create,
74                                           model_full_load,
75                                           model_full_unload,
76                                           model_full_end},
77 {"Floyd",
78  "Floyd routing data (slow initialization, fast lookup, lesser memory requirements, shortest path routing only)",
79  model_floyd_create, model_floyd_load, model_floyd_unload,
80  model_floyd_end},
81 {"Dijkstra",
82  "Dijkstra routing data (fast initialization, slow lookup, small memory requirements, shortest path routing only)",
83  model_dijkstra_create, model_dijkstra_both_load,
84  model_dijkstra_both_unload, model_dijkstra_both_end},
85 {"DijkstraCache",
86  "Dijkstra routing data (fast initialization, fast lookup, small memory requirements, shortest path routing only)",
87  model_dijkstracache_create, model_dijkstra_both_load,
88  model_dijkstra_both_unload, model_dijkstra_both_end},
89 {"none", "No routing (usable with Constant network only)",
90  model_none_create, model_none_load, model_none_unload, model_none_end},
91 {"RuleBased", "Rule-Based routing data (...)", model_rulebased_create,
92  model_none_load, model_none_unload, model_none_end},
93 {"Vivaldi", "Vivaldi routing", model_vivaldi_create,
94   model_none_load, model_none_unload, model_none_end},
95 {"Cluster", "Cluster routing", model_cluster_create,
96   model_none_load, model_cluster_unload, model_none_end},
97 {NULL, NULL, NULL, NULL, NULL, NULL}
98 };
99
100 /**
101  * \brief Add a "host" to the network element list
102  */
103 static void parse_S_host(sg_platf_host_cbarg_t host) {
104   network_element_info_t info = NULL;
105   if (current_routing->hierarchy == SURF_ROUTING_NULL)
106     current_routing->hierarchy = SURF_ROUTING_BASE;
107   xbt_assert(!xbt_lib_get_or_null(host_lib, host->id,ROUTING_HOST_LEVEL),
108               "Reading a host, processing unit \"%s\" already exists",
109               host->id);
110   xbt_assert(current_routing->set_processing_unit,
111               "no defined method \"set_processing_unit\" in \"%s\"",
112               current_routing->name);
113   (*(current_routing->set_processing_unit)) (current_routing, host->id);
114   info = xbt_new0(s_network_element_info_t, 1);
115   info->rc_component = current_routing;
116   info->rc_type = SURF_NETWORK_ELEMENT_HOST;
117   xbt_lib_set(host_lib,host->id,ROUTING_HOST_LEVEL,(void *) info);
118   if (host->coord && strcmp(host->coord,"")) {
119     if(!COORD_HOST_LEVEL) xbt_die("To use coordinates, you must set configuration 'coordinates' to 'yes'");
120     xbt_dynar_t ctn = xbt_str_split_str(host->coord, " ");
121     xbt_dynar_shrink(ctn, 0);
122     xbt_lib_set(host_lib,host->id,COORD_HOST_LEVEL,(void *) ctn);
123   }
124 }
125
126 /**
127  * \brief Add a "router" to the network element list
128  */
129 static void parse_S_router(sg_platf_router_cbarg_t router)
130 {
131   network_element_info_t info = NULL;
132   if (current_routing->hierarchy == SURF_ROUTING_NULL)
133     current_routing->hierarchy = SURF_ROUTING_BASE;
134   xbt_assert(!xbt_lib_get_or_null(as_router_lib,router->id, ROUTING_ASR_LEVEL),
135               "Reading a router, processing unit \"%s\" already exists",
136               router->id);
137   xbt_assert(current_routing->set_processing_unit,
138               "no defined method \"set_processing_unit\" in \"%s\"",
139               current_routing->name);
140   (*(current_routing->set_processing_unit)) (current_routing, router->id);
141   info = xbt_new0(s_network_element_info_t, 1);
142   info->rc_component = current_routing;
143   info->rc_type = SURF_NETWORK_ELEMENT_ROUTER;
144
145   xbt_lib_set(as_router_lib,router->id,ROUTING_ASR_LEVEL,(void *) info);
146   if (strcmp(router->coord,"")) {
147         if(!COORD_ASR_LEVEL) xbt_die("To use coordinates, you must set configuration 'coordinates' to 'yes'");
148     xbt_dynar_t ctn = xbt_str_split_str(router->coord, " ");
149     xbt_dynar_shrink(ctn, 0);
150     xbt_lib_set(as_router_lib,router->id,COORD_ASR_LEVEL,(void *) ctn);
151   }
152 }
153
154 /**
155  * \brief Set the endponints for a route
156  */
157 static void parse_S_route_new_and_endpoints(const char *src_id, const char *dst_id)
158 {
159   if (src != NULL && dst != NULL && link_list != NULL)
160     THROWF(arg_error, 0, "Route between %s to %s can not be defined",
161            src_id, dst_id);
162   src = src_id;
163   dst = dst_id;
164   xbt_assert(strlen(src) > 0 || strlen(dst) > 0,
165               "Some limits are null in the route between \"%s\" and \"%s\"",
166               src, dst);
167   link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
168 }
169
170 /**
171  * \brief Set the endpoints for a route from XML
172  */
173 static void parse_S_route_new_and_endpoints_XML(void)
174 {
175   parse_S_route_new_and_endpoints(A_surfxml_route_src,
176                                   A_surfxml_route_dst);
177 }
178
179 /**
180  * \brief Set the endponints and gateways for a ASroute
181  */
182 static void parse_S_ASroute_new_and_endpoints(void)
183 {
184   if (src != NULL && dst != NULL && link_list != NULL)
185     THROWF(arg_error, 0, "Route between %s to %s can not be defined",
186            A_surfxml_ASroute_src, A_surfxml_ASroute_dst);
187   src = A_surfxml_ASroute_src;
188   dst = A_surfxml_ASroute_dst;
189   gw_src = A_surfxml_ASroute_gw_src;
190   gw_dst = A_surfxml_ASroute_gw_dst;
191   xbt_assert(strlen(src) > 0 || strlen(dst) > 0 || strlen(gw_src) > 0
192               || strlen(gw_dst) > 0,
193               "Some limits are null in the route between \"%s\" and \"%s\"",
194               src, dst);
195   link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
196 }
197
198 /**
199  * \brief Set the endponints for a bypassRoute
200  */
201 static void parse_S_bypassRoute_new_and_endpoints(void)
202 {
203   if (src != NULL && dst != NULL && link_list != NULL)
204     THROWF(arg_error, 0,
205            "Bypass Route between %s to %s can not be defined",
206            A_surfxml_bypassRoute_src, A_surfxml_bypassRoute_dst);
207   src = A_surfxml_bypassRoute_src;
208   dst = A_surfxml_bypassRoute_dst;
209   gw_src = A_surfxml_bypassRoute_gw_src;
210   gw_dst = A_surfxml_bypassRoute_gw_dst;
211   xbt_assert(strlen(src) > 0 || strlen(dst) > 0 || strlen(gw_src) > 0
212               || strlen(gw_dst) > 0,
213               "Some limits are null in the route between \"%s\" and \"%s\"",
214               src, dst);
215   link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
216 }
217
218 /**
219  * \brief Set a new link on the actual list of link for a route or ASroute
220  */
221 static void parse_E_link_ctn_new_elem(const char *link_id)
222 {
223   char *val;
224   val = xbt_strdup(link_id);
225   xbt_dynar_push(link_list, &val);
226 }
227
228 /**
229  * \brief Set a new link on the actual list of link for a route or ASroute from XML
230  */
231
232 static void parse_E_link_ctn_new_elem_XML(void)
233 {
234   if (A_surfxml_link_ctn_direction == A_surfxml_link_ctn_direction_NONE)
235     parse_E_link_ctn_new_elem(A_surfxml_link_ctn_id);
236   if (A_surfxml_link_ctn_direction == A_surfxml_link_ctn_direction_UP) {
237     char *link_id = bprintf("%s_UP", A_surfxml_link_ctn_id);
238     parse_E_link_ctn_new_elem(link_id);
239     free(link_id);
240   }
241   if (A_surfxml_link_ctn_direction == A_surfxml_link_ctn_direction_DOWN) {
242     char *link_id = bprintf("%s_DOWN", A_surfxml_link_ctn_id);
243     parse_E_link_ctn_new_elem(link_id);
244     free(link_id);
245   }
246 }
247
248 /**
249  * \brief Store the route by calling the set_route function of the current routing component
250  */
251 static void parse_E_route_store_route(void)
252 {
253   name_route_extended_t route = xbt_new0(s_name_route_extended_t, 1);
254   route->generic_route.link_list = link_list;
255   xbt_assert(current_routing->set_route,
256               "no defined method \"set_route\" in \"%s\"",
257               current_routing->name);
258   (*(current_routing->set_route)) (current_routing, src, dst, route);
259   link_list = NULL;
260   src = NULL;
261   dst = NULL;
262 }
263
264 /**
265  * \brief Store the ASroute by calling the set_ASroute function of the current routing component
266  */
267 static void parse_E_ASroute_store_route(void)
268 {
269   name_route_extended_t e_route = xbt_new0(s_name_route_extended_t, 1);
270   e_route->generic_route.link_list = link_list;
271   e_route->src_gateway = xbt_strdup(gw_src);
272   e_route->dst_gateway = xbt_strdup(gw_dst);
273   xbt_assert(current_routing->set_ASroute,
274               "no defined method \"set_ASroute\" in \"%s\"",
275               current_routing->name);
276   (*(current_routing->set_ASroute)) (current_routing, src, dst, e_route);
277   link_list = NULL;
278   src = NULL;
279   dst = NULL;
280   gw_src = NULL;
281   gw_dst = NULL;
282 }
283
284 /**
285  * \brief Store the bypass route by calling the set_bypassroute function of the current routing component
286  */
287 static void parse_E_bypassRoute_store_route(void)
288 {
289   route_extended_t e_route = xbt_new0(s_route_extended_t, 1);
290   e_route->generic_route.link_list = link_list;
291   e_route->src_gateway = xbt_strdup(gw_src);
292   e_route->dst_gateway = xbt_strdup(gw_dst);
293   xbt_assert(current_routing->set_bypassroute,
294               "no defined method \"set_bypassroute\" in \"%s\"",
295               current_routing->name);
296   (*(current_routing->set_bypassroute)) (current_routing, src, dst,
297                                          e_route);
298   link_list = NULL;
299   src = NULL;
300   dst = NULL;
301   gw_src = NULL;
302   gw_dst = NULL;
303 }
304
305 /**
306  * \brief Make a new routing component to the platform
307  *
308  * Add a new autonomous system to the platform. Any elements (such as host,
309  * router or sub-AS) added after this call and before the corresponding call
310  * to sg_platf_new_AS_close() will be added to this AS.
311  *
312  * Once this function was called, the configuration concerning the used
313  * models cannot be changed anymore.
314  *
315  * @param AS_id name of this autonomous system. Must be unique in the platform
316  * @param wanted_routing_type one of Full, Floyd, Dijkstra or similar. Full list in the variable routing_models, in src/surf/surf_routing.c
317  */
318 void routing_AS_open(const char *AS_id, const char *wanted_routing_type)
319 {
320   routing_component_t new_routing;
321   model_type_t model = NULL;
322   int cpt;
323
324   surf_parse_models_setup(); /* ensure that the models are created after the last <config> tag and before the first <AS>-like */
325
326   /* search the routing model */
327   for (cpt = 0; routing_models[cpt].name; cpt++)
328     if (!strcmp(wanted_routing_type, routing_models[cpt].name))
329       model = &routing_models[cpt];
330   /* if its not exist, error */
331   if (model == NULL) {
332     fprintf(stderr, "Routing model %s not found. Existing models:\n",
333             wanted_routing_type);
334     for (cpt = 0; routing_models[cpt].name; cpt++)
335         fprintf(stderr, "   %s: %s\n", routing_models[cpt].name,
336                 routing_models[cpt].desc);
337     xbt_die(NULL);
338   }
339
340   /* make a new routing component */
341   new_routing = (routing_component_t) (*(model->create)) ();
342   new_routing->routing = model;
343   new_routing->hierarchy = SURF_ROUTING_NULL;
344   new_routing->name = xbt_strdup(AS_id);
345   new_routing->routing_sons = xbt_dict_new();
346
347   if (current_routing == NULL && global_routing->root == NULL) {
348
349     /* it is the first one */
350     new_routing->routing_father = NULL;
351     global_routing->root = new_routing;
352
353   } else if (current_routing != NULL && global_routing->root != NULL) {
354
355     xbt_assert(!xbt_dict_get_or_null
356                 (current_routing->routing_sons, AS_id),
357                 "The AS \"%s\" already exists", AS_id);
358     /* it is a part of the tree */
359     new_routing->routing_father = current_routing;
360     /* set the father behavior */
361     if (current_routing->hierarchy == SURF_ROUTING_NULL)
362       current_routing->hierarchy = SURF_ROUTING_RECURSIVE;
363     /* add to the sons dictionary */
364     xbt_dict_set(current_routing->routing_sons, AS_id,
365                  (void *) new_routing, NULL);
366     /* add to the father element list */
367     (*(current_routing->set_autonomous_system)) (current_routing, AS_id);
368     /* unload the prev parse rules */
369     (*(current_routing->routing->unload)) ();
370
371   } else {
372     THROWF(arg_error, 0, "All defined components must be belong to a AS");
373   }
374   /* set the new parse rules */
375   (*(new_routing->routing->load)) ();
376   /* set the new current component of the tree */
377   current_routing = new_routing;
378 }
379
380 /**
381  * \brief Specify that the current description of AS is finished
382  *
383  * Once you've declared all the content of your AS, you have to close
384  * it with this call. Your AS is not usable until you call this function.
385  *
386  * @fixme: this call is not as robust as wanted: bad things WILL happen
387  * if you call it twice for the same AS, or if you forget calling it, or
388  * even if you add stuff to a closed AS
389  *
390  */
391 void routing_AS_close() {
392
393   if (current_routing == NULL) {
394     THROWF(arg_error, 0, "Close an AS, but none was under construction");
395   } else {
396     network_element_info_t info = NULL;
397     xbt_assert(!xbt_lib_get_or_null(as_router_lib,current_routing->name, ROUTING_ASR_LEVEL),
398                 "The AS \"%s\" already exists",current_routing->name);
399     info = xbt_new0(s_network_element_info_t, 1);
400     info->rc_component = current_routing->routing_father;
401     info->rc_type = SURF_NETWORK_ELEMENT_AS;
402     xbt_lib_set(as_router_lib,current_routing->name,ROUTING_ASR_LEVEL,(void *) info);
403
404     (*(current_routing->routing->unload)) ();
405     (*(current_routing->routing->end)) ();
406     current_routing = current_routing->routing_father;
407     if (current_routing != NULL)
408       (*(current_routing->routing->load)) ();
409   }
410 }
411
412 /* Aux Business methods */
413
414 /**
415  * \brief Get the AS name of the element
416  *
417  * \param name the host name
418  *
419  */
420 static char* elements_As_name(const char *name)
421 {
422   routing_component_t as_comp;
423
424   /* (1) find the as where the host is located */
425   as_comp = ((network_element_info_t)
426             xbt_lib_get_or_null(host_lib,name, ROUTING_HOST_LEVEL))->rc_component;
427   return as_comp->name;
428 }
429
430
431 /**
432  * \brief Get the AS father and the first elements of the chain
433  *
434  * \param src the source host name 
435  * \param dst the destination host name
436  * 
437  * Get the common father of the to processing units, and the first different 
438  * father in the chain
439  */
440 static void elements_father(const char *src, const char *dst,
441                             routing_component_t *res_father,
442                             routing_component_t *res_src,
443                             routing_component_t *res_dst)
444 {
445   xbt_assert(src && dst, "bad parameters for \"elements_father\" method");
446 #define ELEMENTS_FATHER_MAXDEPTH 16 /* increase if it is not enough */
447   routing_component_t src_as, dst_as;
448   routing_component_t path_src[ELEMENTS_FATHER_MAXDEPTH];
449   routing_component_t path_dst[ELEMENTS_FATHER_MAXDEPTH];
450   int index_src = 0;
451   int index_dst = 0;
452   routing_component_t current;
453   routing_component_t current_src;
454   routing_component_t current_dst;
455   routing_component_t father;
456
457   /* (1) find the as where the src and dst are located */
458   network_element_info_t src_data = xbt_lib_get_or_null(host_lib, src,
459                                                         ROUTING_HOST_LEVEL);
460   network_element_info_t dst_data = xbt_lib_get_or_null(host_lib, dst,
461                                                         ROUTING_HOST_LEVEL);
462   if (!src_data)
463     src_data = xbt_lib_get_or_null(as_router_lib, src, ROUTING_ASR_LEVEL);
464   if (!dst_data)
465     dst_data = xbt_lib_get_or_null(as_router_lib, dst, ROUTING_ASR_LEVEL);
466   src_as = src_data->rc_component;
467   dst_as = dst_data->rc_component;
468
469   xbt_assert(src_as && dst_as,
470              "Ask for route \"from\"(%s) or \"to\"(%s) no found", src, dst);
471
472   /* (2) find the path to the root routing component */
473   for (current = src_as ; current != NULL ; current = current->routing_father) {
474     if (index_src >= ELEMENTS_FATHER_MAXDEPTH)
475       xbt_die("ELEMENTS_FATHER_MAXDEPTH should be increased for path_src");
476     path_src[index_src++] = current;
477   }
478   for (current = dst_as ; current != NULL ; current = current->routing_father) {
479     if (index_dst >= ELEMENTS_FATHER_MAXDEPTH)
480       xbt_die("ELEMENTS_FATHER_MAXDEPTH should be increased for path_dst");
481     path_dst[index_dst++] = current;
482   }
483
484   /* (3) find the common father */
485   do {
486     current_src = path_src[--index_src];
487     current_dst = path_dst[--index_dst];
488   } while (index_src > 0 && index_dst > 0 && current_src == current_dst);
489
490   /* (4) they are not in the same routing component, make the path */
491   if (current_src == current_dst)
492     father = current_src;
493   else
494     father = path_src[index_src + 1];
495
496   /* (5) result generation */
497   *res_father = father;         /* first the common father of src and dst */
498   *res_src = current_src;       /* second the first different father of src */
499   *res_dst = current_dst;       /* three  the first different father of dst */
500
501 #undef ELEMENTS_FATHER_MAXDEPTH
502 }
503
504 /* Global Business methods */
505
506 /**
507  * \brief Recursive function for get_route_latency
508  *
509  * \param src the source host name 
510  * \param dst the destination host name
511  * \param *e_route the route where the links are stored
512  * \param *latency the latency, if needed
513  * 
514  * This function is called by "get_route" and "get_latency". It allows to walk
515  * recursively through the routing components tree.
516  */
517 static void _get_route_latency(const char *src, const char *dst,
518                                xbt_dynar_t *route, double *latency)
519 {
520   XBT_DEBUG("Solve route/latency  \"%s\" to \"%s\"", src, dst);
521   xbt_assert(src && dst, "bad parameters for \"_get_route_latency\" method");
522
523   routing_component_t common_father;
524   routing_component_t src_father;
525   routing_component_t dst_father;
526   elements_father(src, dst, &common_father, &src_father, &dst_father);
527
528   if (src_father == dst_father) { /* SURF_ROUTING_BASE */
529
530     route_extended_t e_route = NULL;
531     if (route) {
532       e_route = common_father->get_route(common_father, src, dst);
533       xbt_assert(e_route, "no route between \"%s\" and \"%s\"", src, dst);
534       *route = e_route->generic_route.link_list;
535     }
536     if (latency) {
537       *latency = common_father->get_latency(common_father, src, dst, e_route);
538       xbt_assert(*latency >= 0.0,
539                  "latency error on route between \"%s\" and \"%s\"", src, dst);
540     }
541     if (e_route) {
542       xbt_free(e_route->src_gateway);
543       xbt_free(e_route->dst_gateway);
544       xbt_free(e_route);
545     }
546
547   } else {                      /* SURF_ROUTING_RECURSIVE */
548
549     route_extended_t e_route_bypass = NULL;
550     if (common_father->get_bypass_route)
551       e_route_bypass = common_father->get_bypass_route(common_father, src, dst);
552
553     xbt_assert(!latency || !e_route_bypass,
554                "Bypass cannot work yet with get_latency");
555
556     route_extended_t e_route_cnt = e_route_bypass
557       ? e_route_bypass
558       : common_father->get_route(common_father,
559                                src_father->name, dst_father->name);
560
561     xbt_assert(e_route_cnt, "no route between \"%s\" and \"%s\"",
562                src_father->name, dst_father->name);
563
564     xbt_assert((e_route_cnt->src_gateway == NULL) ==
565                (e_route_cnt->dst_gateway == NULL),
566                "bad gateway for route between \"%s\" and \"%s\"", src, dst);
567
568     if (route) {
569       *route = xbt_dynar_new(global_routing->size_of_link, NULL);
570     }
571     if (latency) {
572       *latency = common_father->get_latency(common_father,
573                                             src_father->name, dst_father->name,
574                                             e_route_cnt);
575       xbt_assert(*latency >= 0.0,
576                  "latency error on route between \"%s\" and \"%s\"",
577                  src_father->name, dst_father->name);
578     }
579
580     void *link;
581     unsigned int cpt;
582
583     if (strcmp(src, e_route_cnt->src_gateway)) {
584       double latency_src;
585       xbt_dynar_t route_src;
586
587       _get_route_latency(src, e_route_cnt->src_gateway,
588                          (route ? &route_src : NULL),
589                          (latency ? &latency_src : NULL));
590       if (route) {
591         xbt_assert(route_src, "no route between \"%s\" and \"%s\"",
592                    src, e_route_cnt->src_gateway);
593         xbt_dynar_foreach(route_src, cpt, link) {
594           xbt_dynar_push(*route, &link);
595         }
596         xbt_dynar_free(&route_src);
597       }
598       if (latency) {
599         xbt_assert(latency_src >= 0.0,
600                    "latency error on route between \"%s\" and \"%s\"",
601                    src, e_route_cnt->src_gateway);
602         *latency += latency_src;
603       }
604     }
605
606     if (route) {
607       xbt_dynar_foreach(e_route_cnt->generic_route.link_list, cpt, link) {
608         xbt_dynar_push(*route, &link);
609       }
610     }
611
612     if (strcmp(e_route_cnt->dst_gateway, dst)) {
613       double latency_dst;
614       xbt_dynar_t route_dst;
615
616       _get_route_latency(e_route_cnt->dst_gateway, dst,
617                          (route ? &route_dst : NULL),
618                          (latency ? &latency_dst : NULL));
619       if (route) {
620         xbt_assert(route_dst, "no route between \"%s\" and \"%s\"",
621                    e_route_cnt->dst_gateway, dst);
622         xbt_dynar_foreach(route_dst, cpt, link) {
623           xbt_dynar_push(*route, &link);
624         }
625         xbt_dynar_free(&route_dst);
626       }
627       if (latency) {
628         xbt_assert(latency_dst >= 0.0,
629                    "latency error on route between \"%s\" and \"%s\"",
630                    e_route_cnt->dst_gateway, dst);
631         *latency += latency_dst;
632       }
633     }
634
635     generic_free_extended_route(e_route_cnt);
636   }
637 }
638
639 /**
640  * \brief Generic function for get_route, get_route_no_cleanup, and get_latency
641  */
642 static void get_route_latency(const char *src, const char *dst,
643                               xbt_dynar_t *route, double *latency, int cleanup)
644 {
645   _get_route_latency(src, dst, route, latency);
646   xbt_assert(!route || *route, "no route between \"%s\" and \"%s\"", src, dst);
647   xbt_assert(!latency || *latency >= 0.0,
648              "latency error on route between \"%s\" and \"%s\"", src, dst);
649   if (route) {
650     xbt_dynar_free(&global_routing->last_route);
651     global_routing->last_route = cleanup ? *route : NULL;
652   }
653 }
654
655 /**
656  * \brief Generic method: find a route between hosts
657  *
658  * \param src the source host name 
659  * \param dst the destination host name
660  * 
661  * walk through the routing components tree and find a route between hosts
662  * by calling the differents "get_route" functions in each routing component.
663  * No need to free the returned dynar. It will be freed at the next call.
664  */
665 static xbt_dynar_t get_route(const char *src, const char *dst)
666 {
667   xbt_dynar_t route = NULL;
668   get_route_latency(src, dst, &route, NULL, 1);
669   return route;
670 }
671
672 /**
673  * \brief Generic method: find a route between hosts
674  *
675  * \param src the source host name
676  * \param dst the destination host name
677  *
678  * same as get_route, but return NULL if any exception is raised.
679  */
680 static xbt_dynar_t get_route_or_null(const char *src, const char *dst)
681 {
682   xbt_dynar_t route = NULL;
683   xbt_ex_t exception;
684   TRY {
685     get_route_latency(src, dst, &route, NULL, 1);
686   }CATCH(exception) {
687     xbt_ex_free(exception);
688     return NULL;
689   }
690   return route;
691 }
692
693 /**
694  * \brief Generic method: find a route between hosts
695  *
696  * \param src the source host name
697  * \param dst the destination host name
698  *
699  * walk through the routing components tree and find a route between hosts
700  * by calling the differents "get_route" functions in each routing component.
701  * Leaves the caller the responsability to clean the returned dynar.
702  */
703 static xbt_dynar_t get_route_no_cleanup(const char *src, const char *dst)
704 {
705   xbt_dynar_t route = NULL;
706   get_route_latency(src, dst, &route, NULL, 0);
707   return route;
708 }
709
710 /*Get Latency*/
711 static double get_latency(const char *src, const char *dst)
712 {
713   double latency = -1.0;
714   get_route_latency(src, dst, NULL, &latency, 0);
715   return latency;
716 }
717
718 static int surf_parse_models_setup_already_called=0;
719 /* Call the last initialization functions, that must be called after the
720  * <config> tag, if any, and before the first of cluster|peer|AS|trace|trace_connect
721  */
722 void surf_parse_models_setup()
723 {
724   if (surf_parse_models_setup_already_called)
725     return;
726   surf_parse_models_setup_already_called=1;
727   surf_config_models_setup();
728 }
729
730
731 /**
732  * \brief Recursive function for finalize
733  *
734  * \param rc the source host name 
735  * 
736  * This fuction is call by "finalize". It allow to finalize the 
737  * AS or routing components. It delete all the structures.
738  */
739 static void _finalize(routing_component_t rc)
740 {
741   if (rc) {
742     xbt_dict_cursor_t cursor = NULL;
743     char *key;
744     routing_component_t elem;
745     xbt_dict_foreach(rc->routing_sons, cursor, key, elem) {
746       _finalize(elem);
747     }
748     xbt_dict_t tmp_sons = rc->routing_sons;
749     char *tmp_name = rc->name;
750     xbt_dict_free(&tmp_sons);
751     xbt_free(tmp_name);
752     xbt_assert(rc->finalize, "no defined method \"finalize\" in \"%s\"",
753                 current_routing->name);
754     (*(rc->finalize)) (rc);
755   }
756 }
757
758 /**
759  * \brief Generic method: delete all the routing structures
760  * 
761  * walk through the routing components tree and delete the structures
762  * by calling the differents "finalize" functions in each routing component
763  */
764 static void finalize(void)
765 {
766   /* delete recursively all the tree */
767   _finalize(global_routing->root);
768   /* delete last_route */
769   xbt_dynar_free(&(global_routing->last_route));
770   /* delete global routing structure */
771   xbt_free(global_routing);
772   /* make sure that we will reinit the models while loading the platf once reinited -- HACK but there is no proper surf_routing_init() */
773   surf_parse_models_setup_already_called = 0;
774 }
775
776 static xbt_dynar_t recursive_get_onelink_routes(routing_component_t rc)
777 {
778   xbt_dynar_t ret = xbt_dynar_new(sizeof(onelink_t), xbt_free);
779
780   //adding my one link routes
781   unsigned int cpt;
782   void *link;
783   xbt_dynar_t onelink_mine = rc->get_onelink_routes(rc);
784   if (onelink_mine) {
785     xbt_dynar_foreach(onelink_mine, cpt, link) {
786       xbt_dynar_push(ret, &link);
787     }
788   }
789   //recursing
790   char *key;
791   xbt_dict_cursor_t cursor = NULL;
792   routing_component_t rc_child;
793   xbt_dict_foreach(rc->routing_sons, cursor, key, rc_child) {
794     xbt_dynar_t onelink_child = recursive_get_onelink_routes(rc_child);
795     if (onelink_child) {
796       xbt_dynar_foreach(onelink_child, cpt, link) {
797         xbt_dynar_push(ret, &link);
798       }
799     }
800   }
801   return ret;
802 }
803
804 static xbt_dynar_t get_onelink_routes(void)
805 {
806   return recursive_get_onelink_routes(global_routing->root);
807 }
808
809 e_surf_network_element_type_t get_network_element_type(const char
810                                                               *name)
811 {
812   network_element_info_t rc = NULL;
813
814   rc = xbt_lib_get_or_null(host_lib, name, ROUTING_HOST_LEVEL);
815   if(rc) return rc->rc_type;
816
817   rc = xbt_lib_get_or_null(as_router_lib, name, ROUTING_ASR_LEVEL);
818   if(rc) return rc->rc_type;
819
820   return SURF_NETWORK_ELEMENT_NULL;
821 }
822
823 /**
824  * \brief Generic method: create the global routing schema
825  * 
826  * Make a global routing structure and set all the parsing functions.
827  */
828 void routing_model_create(size_t size_of_links, void *loopback, double_f_cpvoid_t get_link_latency_fun)
829 {
830   /* config the uniq global routing */
831   global_routing = xbt_new0(s_routing_global_t, 1);
832   global_routing->root = NULL;
833   global_routing->get_route = get_route;
834   global_routing->get_route_or_null = get_route_or_null;
835   global_routing->get_latency = get_latency;
836   global_routing->get_route_no_cleanup = get_route_no_cleanup;
837   global_routing->get_onelink_routes = get_onelink_routes;
838   global_routing->get_route_latency = get_route_latency;
839   global_routing->get_network_element_type = get_network_element_type;
840   global_routing->finalize = finalize;
841   global_routing->loopback = loopback;
842   global_routing->size_of_link = size_of_links;
843   global_routing->last_route = NULL;
844   get_link_latency = get_link_latency_fun;
845   /* no current routing at moment */
846   current_routing = NULL;
847
848   /* parse generic elements */
849   sg_platf_host_add_cb(parse_S_host);
850   sg_platf_router_add_cb(parse_S_router);
851
852   surfxml_add_callback(STag_surfxml_route_cb_list,
853                        &parse_S_route_new_and_endpoints_XML);
854   surfxml_add_callback(STag_surfxml_ASroute_cb_list,
855                        &parse_S_ASroute_new_and_endpoints);
856   surfxml_add_callback(STag_surfxml_bypassRoute_cb_list,
857                        &parse_S_bypassRoute_new_and_endpoints);
858
859   surfxml_add_callback(ETag_surfxml_link_ctn_cb_list,
860                        &parse_E_link_ctn_new_elem_XML);
861
862   surfxml_add_callback(ETag_surfxml_route_cb_list,
863                        &parse_E_route_store_route);
864   surfxml_add_callback(ETag_surfxml_ASroute_cb_list,
865                        &parse_E_ASroute_store_route);
866   surfxml_add_callback(ETag_surfxml_bypassRoute_cb_list,
867                        &parse_E_bypassRoute_store_route);
868
869   surfxml_add_callback(STag_surfxml_cluster_cb_list,
870                        &routing_parse_Scluster);
871
872   sg_platf_peer_add_cb(routing_parse_Speer); // FIXME: inline in the sg_platf_new_peer instead
873   sg_platf_postparse_add_cb(clean_routing_after_parse);
874
875 #ifdef HAVE_TRACING
876   instr_routing_define_callbacks();
877 #endif
878 }
879
880 void surf_parse_add_callback_config(void)
881 {
882   surfxml_add_callback(STag_surfxml_random_cb_list, &routing_parse_Srandom);
883 }
884
885
886 /* ************************************************** */
887 /* ********** PATERN FOR NEW ROUTING **************** */
888
889 /* The minimal configuration of a new routing model need the next functions,
890  * also you need to set at the start of the file, the new model in the model
891  * list. Remember keep the null ending of the list.
892  */
893 /*** Routing model structure ***/
894 // typedef struct {
895 //   s_routing_component_t generic_routing;
896 //   /* things that your routing model need */
897 // } s_routing_component_NEW_t,*routing_component_NEW_t;
898
899 /*** Parse routing model functions ***/
900 // static void model_NEW_set_processing_unit(routing_component_t rc, const char* name) {}
901 // static void model_NEW_set_autonomous_system(routing_component_t rc, const char* name) {}
902 // static void model_NEW_set_route(routing_component_t rc, const char* src, const char* dst, route_t route) {}
903 // static void model_NEW_set_ASroute(routing_component_t rc, const char* src, const char* dst, route_extended_t route) {}
904 // static void model_NEW_set_bypassroute(routing_component_t rc, const char* src, const char* dst, route_extended_t e_route) {}
905
906 /*** Business methods ***/
907 // static route_extended_t NEW_get_route(routing_component_t rc, const char* src,const char* dst) {return NULL;}
908 // static route_extended_t NEW_get_bypass_route(routing_component_t rc, const char* src,const char* dst) {return NULL;}
909 // static void NEW_finalize(routing_component_t rc) { xbt_free(rc);}
910
911 /*** Creation routing model functions ***/
912 // static void* model_NEW_create(void) {
913 //   routing_component_NEW_t new_component =  xbt_new0(s_routing_component_NEW_t,1);
914 //   new_component->generic_routing.set_processing_unit = model_NEW_set_processing_unit;
915 //   new_component->generic_routing.set_autonomous_system = model_NEW_set_autonomous_system;
916 //   new_component->generic_routing.set_route = model_NEW_set_route;
917 //   new_component->generic_routing.set_ASroute = model_NEW_set_ASroute;
918 //   new_component->generic_routing.set_bypassroute = model_NEW_set_bypassroute;
919 //   new_component->generic_routing.get_route = NEW_get_route;
920 //   new_component->generic_routing.get_bypass_route = NEW_get_bypass_route;
921 //   new_component->generic_routing.finalize = NEW_finalize;
922 //   /* initialization of internal structures */
923 //   return new_component;
924 // } /* mandatory */
925 // static void  model_NEW_load(void) {}   /* mandatory */
926 // static void  model_NEW_unload(void) {} /* mandatory */
927 // static void  model_NEW_end(void) {}    /* mandatory */
928
929 /* ************************************************************************** */
930 /* ************************* GENERIC PARSE FUNCTIONS ************************ */
931
932 void generic_set_processing_unit(routing_component_t rc,
933                                         const char *name)
934 {
935   XBT_DEBUG("Load process unit \"%s\"", name);
936   int *id = xbt_new0(int, 1);
937   xbt_dict_t _to_index;
938   _to_index = current_routing->to_index;
939   *id = xbt_dict_length(_to_index);
940   xbt_dict_set(_to_index, name, id, xbt_free);
941 }
942
943 void generic_set_autonomous_system(routing_component_t rc,
944                                           const char *name)
945 {
946   XBT_DEBUG("Load Autonomous system \"%s\"", name);
947   int *id = xbt_new0(int, 1);
948   xbt_dict_t _to_index;
949   _to_index = current_routing->to_index;
950   *id = xbt_dict_length(_to_index);
951   xbt_dict_set(_to_index, name, id, xbt_free);
952 }
953
954 int surf_pointer_resource_cmp(const void *a, const void *b)
955 {
956   return a != b;
957 }
958
959 int surf_link_resource_cmp(const void *a, const void *b)
960 {
961   return !!memcmp(a,b,global_routing->size_of_link);
962 }
963
964 void generic_set_bypassroute(routing_component_t rc,
965                                     const char *src, const char *dst,
966                                     route_extended_t e_route)
967 {
968   XBT_DEBUG("Load bypassRoute from \"%s\" to \"%s\"", src, dst);
969   xbt_dict_t dict_bypassRoutes = rc->bypassRoutes;
970   char *route_name;
971
972   route_name = bprintf("%s#%s", src, dst);
973   xbt_assert(xbt_dynar_length(e_route->generic_route.link_list) > 0,
974               "Invalid count of links, must be greater than zero (%s,%s)",
975               src, dst);
976   xbt_assert(!xbt_dict_get_or_null(dict_bypassRoutes, route_name),
977               "The bypass route between \"%s\"(\"%s\") and \"%s\"(\"%s\") already exists",
978               src, e_route->src_gateway, dst, e_route->dst_gateway);
979
980   route_extended_t new_e_route =
981       generic_new_extended_route(SURF_ROUTING_RECURSIVE, e_route, 0);
982   xbt_dynar_free(&(e_route->generic_route.link_list));
983   xbt_free(e_route);
984
985   xbt_dict_set(dict_bypassRoutes, route_name, new_e_route,
986                (void (*)(void *)) generic_free_extended_route);
987   xbt_free(route_name);
988 }
989
990 /* ************************************************************************** */
991 /* *********************** GENERIC BUSINESS METHODS ************************* */
992
993 double generic_get_link_latency(routing_component_t rc,
994                                        const char *src, const char *dst,
995                                        route_extended_t route)
996 {
997         int need_to_clean = route?0:1;
998         void * link;
999         unsigned int i;
1000         double latency = 0.0;
1001
1002         route = route?route:rc->get_route(rc,src,dst);
1003
1004         xbt_dynar_foreach(route->generic_route.link_list,i,link) {
1005                 latency += get_link_latency(link);
1006         }
1007         if(need_to_clean) generic_free_extended_route(route);
1008   return latency;
1009 }
1010
1011 xbt_dynar_t generic_get_onelink_routes(routing_component_t rc)
1012 {
1013   xbt_die("\"generic_get_onelink_routes\" not implemented yet");
1014 }
1015
1016 route_extended_t generic_get_bypassroute(routing_component_t rc,
1017                                                 const char *src,
1018                                                 const char *dst)
1019 {
1020   xbt_dict_t dict_bypassRoutes = rc->bypassRoutes;
1021   routing_component_t src_as, dst_as;
1022   int index_src, index_dst;
1023   xbt_dynar_t path_src = NULL;
1024   xbt_dynar_t path_dst = NULL;
1025   routing_component_t current = NULL;
1026   routing_component_t *current_src = NULL;
1027   routing_component_t *current_dst = NULL;
1028
1029   /* (1) find the as where the src and dst are located */
1030   void * src_data = xbt_lib_get_or_null(host_lib,src, ROUTING_HOST_LEVEL);
1031   void * dst_data = xbt_lib_get_or_null(host_lib,dst, ROUTING_HOST_LEVEL);
1032   if(!src_data) src_data = xbt_lib_get_or_null(as_router_lib,src, ROUTING_ASR_LEVEL);
1033   if(!dst_data) dst_data = xbt_lib_get_or_null(as_router_lib,dst, ROUTING_ASR_LEVEL);
1034
1035   if(src_data == NULL || dst_data == NULL)
1036           xbt_die("Ask for route \"from\"(%s) or \"to\"(%s) no found at AS \"%s\"",
1037                      src, dst, rc->name);
1038
1039   src_as = ((network_element_info_t)src_data)->rc_component;
1040   dst_as = ((network_element_info_t)dst_data)->rc_component;
1041
1042   /* (2) find the path to the root routing component */
1043   path_src = xbt_dynar_new(sizeof(routing_component_t), NULL);
1044   current = src_as;
1045   while (current != NULL) {
1046     xbt_dynar_push(path_src, &current);
1047     current = current->routing_father;
1048   }
1049   path_dst = xbt_dynar_new(sizeof(routing_component_t), NULL);
1050   current = dst_as;
1051   while (current != NULL) {
1052     xbt_dynar_push(path_dst, &current);
1053     current = current->routing_father;
1054   }
1055
1056   /* (3) find the common father */
1057   index_src = path_src->used - 1;
1058   index_dst = path_dst->used - 1;
1059   current_src = xbt_dynar_get_ptr(path_src, index_src);
1060   current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
1061   while (index_src >= 0 && index_dst >= 0 && *current_src == *current_dst) {
1062     xbt_dynar_pop_ptr(path_src);
1063     xbt_dynar_pop_ptr(path_dst);
1064     index_src--;
1065     index_dst--;
1066     current_src = xbt_dynar_get_ptr(path_src, index_src);
1067     current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
1068   }
1069
1070   int max_index_src = path_src->used - 1;
1071   int max_index_dst = path_dst->used - 1;
1072
1073   int max_index = max(max_index_src, max_index_dst);
1074   int i, max;
1075
1076   route_extended_t e_route_bypass = NULL;
1077
1078   for (max = 0; max <= max_index; max++) {
1079     for (i = 0; i < max; i++) {
1080       if (i <= max_index_src && max <= max_index_dst) {
1081         char *route_name = bprintf("%s#%s",
1082                                    (*(routing_component_t *)
1083                                     (xbt_dynar_get_ptr
1084                                      (path_src, i)))->name,
1085                                    (*(routing_component_t *)
1086                                     (xbt_dynar_get_ptr
1087                                      (path_dst, max)))->name);
1088         e_route_bypass =
1089             xbt_dict_get_or_null(dict_bypassRoutes, route_name);
1090         xbt_free(route_name);
1091       }
1092       if (e_route_bypass)
1093         break;
1094       if (max <= max_index_src && i <= max_index_dst) {
1095         char *route_name = bprintf("%s#%s",
1096                                    (*(routing_component_t *)
1097                                     (xbt_dynar_get_ptr
1098                                      (path_src, max)))->name,
1099                                    (*(routing_component_t *)
1100                                     (xbt_dynar_get_ptr
1101                                      (path_dst, i)))->name);
1102         e_route_bypass =
1103             xbt_dict_get_or_null(dict_bypassRoutes, route_name);
1104         xbt_free(route_name);
1105       }
1106       if (e_route_bypass)
1107         break;
1108     }
1109
1110     if (e_route_bypass)
1111       break;
1112
1113     if (max <= max_index_src && max <= max_index_dst) {
1114       char *route_name = bprintf("%s#%s",
1115                                  (*(routing_component_t *)
1116                                   (xbt_dynar_get_ptr
1117                                    (path_src, max)))->name,
1118                                  (*(routing_component_t *)
1119                                   (xbt_dynar_get_ptr
1120                                    (path_dst, max)))->name);
1121       e_route_bypass = xbt_dict_get_or_null(dict_bypassRoutes, route_name);
1122       xbt_free(route_name);
1123     }
1124     if (e_route_bypass)
1125       break;
1126   }
1127
1128   xbt_dynar_free(&path_src);
1129   xbt_dynar_free(&path_dst);
1130
1131   route_extended_t new_e_route = NULL;
1132
1133   if (e_route_bypass) {
1134     void *link;
1135     unsigned int cpt = 0;
1136     new_e_route = xbt_new0(s_route_extended_t, 1);
1137     new_e_route->src_gateway = xbt_strdup(e_route_bypass->src_gateway);
1138     new_e_route->dst_gateway = xbt_strdup(e_route_bypass->dst_gateway);
1139     new_e_route->generic_route.link_list =
1140         xbt_dynar_new(global_routing->size_of_link, NULL);
1141     xbt_dynar_foreach(e_route_bypass->generic_route.link_list, cpt, link) {
1142       xbt_dynar_push(new_e_route->generic_route.link_list, &link);
1143     }
1144   }
1145
1146   return new_e_route;
1147 }
1148
1149 /* ************************************************************************** */
1150 /* ************************* GENERIC AUX FUNCTIONS ************************** */
1151
1152 route_t
1153 generic_new_route(e_surf_routing_hierarchy_t hierarchy,
1154                            void *data, int order)
1155 {
1156
1157   char *link_name;
1158   route_t new_route;
1159   unsigned int cpt;
1160   xbt_dynar_t links = NULL, links_id = NULL;
1161
1162   new_route = xbt_new0(s_route_t, 1);
1163   new_route->link_list =
1164       xbt_dynar_new(global_routing->size_of_link, NULL);
1165
1166   xbt_assert(hierarchy == SURF_ROUTING_BASE,
1167               "the hierarchy type is not SURF_ROUTING_BASE");
1168
1169   links = ((route_t) data)->link_list;
1170
1171
1172   links_id = new_route->link_list;
1173
1174   xbt_dynar_foreach(links, cpt, link_name) {
1175
1176     void *link =
1177                 xbt_lib_get_or_null(link_lib, link_name, SURF_LINK_LEVEL);
1178     if (link) {
1179       if (order)
1180         xbt_dynar_push(links_id, &link);
1181       else
1182         xbt_dynar_unshift(links_id, &link);
1183     } else
1184       THROWF(mismatch_error, 0, "Link %s not found", link_name);
1185   }
1186
1187   return new_route;
1188 }
1189
1190 route_extended_t
1191 generic_new_extended_route(e_surf_routing_hierarchy_t hierarchy,
1192                            void *data, int order)
1193 {
1194
1195   char *link_name;
1196   route_extended_t e_route, new_e_route;
1197   route_t route;
1198   unsigned int cpt;
1199   xbt_dynar_t links = NULL, links_id = NULL;
1200
1201   new_e_route = xbt_new0(s_route_extended_t, 1);
1202   new_e_route->generic_route.link_list =
1203       xbt_dynar_new(global_routing->size_of_link, NULL);
1204   new_e_route->src_gateway = NULL;
1205   new_e_route->dst_gateway = NULL;
1206
1207   xbt_assert(hierarchy == SURF_ROUTING_BASE
1208               || hierarchy == SURF_ROUTING_RECURSIVE,
1209               "the hierarchy type is not defined");
1210
1211   if (hierarchy == SURF_ROUTING_BASE) {
1212
1213     route = (route_t) data;
1214     links = route->link_list;
1215
1216   } else if (hierarchy == SURF_ROUTING_RECURSIVE) {
1217
1218     e_route = (route_extended_t) data;
1219     xbt_assert(e_route->src_gateway
1220                 && e_route->dst_gateway, "bad gateway, is null");
1221     links = e_route->generic_route.link_list;
1222
1223     /* remeber not erase the gateway names */
1224     new_e_route->src_gateway = strdup(e_route->src_gateway);
1225     new_e_route->dst_gateway = strdup(e_route->dst_gateway);
1226   }
1227
1228   links_id = new_e_route->generic_route.link_list;
1229
1230   xbt_dynar_foreach(links, cpt, link_name) {
1231
1232     void *link =
1233                 xbt_lib_get_or_null(link_lib, link_name, SURF_LINK_LEVEL);
1234     if (link) {
1235       if (order)
1236         xbt_dynar_push(links_id, &link);
1237       else
1238         xbt_dynar_unshift(links_id, &link);
1239     } else
1240       THROWF(mismatch_error, 0, "Link %s not found", link_name);
1241   }
1242
1243   return new_e_route;
1244 }
1245
1246 void generic_free_route(route_t route)
1247 {
1248   if (route) {
1249     xbt_dynar_free(&(route->link_list));
1250     xbt_free(route);
1251   }
1252 }
1253
1254 void generic_free_extended_route(route_extended_t e_route)
1255 {
1256   if (e_route) {
1257     xbt_dynar_free(&(e_route->generic_route.link_list));
1258     if (e_route->src_gateway)
1259       xbt_free(e_route->src_gateway);
1260     if (e_route->dst_gateway)
1261       xbt_free(e_route->dst_gateway);
1262     xbt_free(e_route);
1263   }
1264 }
1265
1266 static routing_component_t generic_as_exist(routing_component_t find_from,
1267                                             routing_component_t to_find)
1268 {
1269   //return to_find; // FIXME: BYPASSERROR OF FOREACH WITH BREAK
1270   xbt_dict_cursor_t cursor = NULL;
1271   char *key;
1272   int found = 0;
1273   routing_component_t elem;
1274   xbt_dict_foreach(find_from->routing_sons, cursor, key, elem) {
1275     if (to_find == elem || generic_as_exist(elem, to_find)) {
1276       found = 1;
1277       break;
1278     }
1279   }
1280   if (found)
1281     return to_find;
1282   return NULL;
1283 }
1284
1285 routing_component_t
1286 generic_autonomous_system_exist(routing_component_t rc, char *element)
1287 {
1288   //return rc; // FIXME: BYPASSERROR OF FOREACH WITH BREAK
1289   routing_component_t element_as, result, elem;
1290   xbt_dict_cursor_t cursor = NULL;
1291   char *key;
1292   element_as = ((network_element_info_t)
1293                 xbt_lib_get_or_null(as_router_lib, element, ROUTING_ASR_LEVEL))->rc_component;
1294   result = ((routing_component_t) - 1);
1295   if (element_as != rc)
1296     result = generic_as_exist(rc, element_as);
1297
1298   int found = 0;
1299   if (result) {
1300     xbt_dict_foreach(element_as->routing_sons, cursor, key, elem) {
1301       found = !strcmp(elem->name, element);
1302       if (found)
1303         break;
1304     }
1305     if (found)
1306       return element_as;
1307   }
1308   return NULL;
1309 }
1310
1311 routing_component_t
1312 generic_processing_units_exist(routing_component_t rc, char *element)
1313 {
1314   routing_component_t element_as;
1315   element_as = ((network_element_info_t)
1316                 xbt_lib_get_or_null(host_lib,
1317                  element, ROUTING_HOST_LEVEL))->rc_component;
1318   if (element_as == rc)
1319     return element_as;
1320   return generic_as_exist(rc, element_as);
1321 }
1322
1323 void generic_src_dst_check(routing_component_t rc, const char *src,
1324                                   const char *dst)
1325 {
1326
1327   void * src_data = xbt_lib_get_or_null(host_lib,src, ROUTING_HOST_LEVEL);
1328   void * dst_data = xbt_lib_get_or_null(host_lib,dst, ROUTING_HOST_LEVEL);
1329   if(!src_data) src_data = xbt_lib_get_or_null(as_router_lib,src, ROUTING_ASR_LEVEL);
1330   if(!dst_data) dst_data = xbt_lib_get_or_null(as_router_lib,dst, ROUTING_ASR_LEVEL);
1331
1332   if(src_data == NULL || dst_data == NULL)
1333           xbt_die("Ask for route \"from\"(%s) or \"to\"(%s) no found at AS \"%s\"",
1334                      src, dst, rc->name);
1335
1336   routing_component_t src_as = ((network_element_info_t)src_data)->rc_component;
1337   routing_component_t dst_as = ((network_element_info_t)dst_data)->rc_component;
1338
1339   if(src_as != dst_as)
1340           xbt_die("The src(%s in %s) and dst(%s in %s) are in differents AS",
1341               src, src_as->name, dst, dst_as->name);
1342   if(rc != dst_as)
1343          xbt_die("The routing component of src'%s' and dst'%s' is not the same as the network elements belong (%s?=%s?=%s)",
1344      src,dst,src_as->name, dst_as->name,rc->name);
1345 }
1346
1347 void routing_parse_Scluster(void)
1348 {
1349   static int AX_ptr = 0;
1350   char *host_id, *groups, *link_id = NULL;
1351
1352   s_sg_platf_host_cbarg_t host;
1353   s_sg_platf_link_cbarg_t link;
1354
1355   if( strcmp(struct_cluster->availability_trace,"")
1356           || strcmp(struct_cluster->state_trace,"") )
1357   {
1358           if(xbt_dict_size(patterns)==0)
1359                   patterns = xbt_dict_new();
1360           xbt_dict_set(patterns,"id",xbt_strdup(struct_cluster->id),free);
1361           xbt_dict_set(patterns,"prefix",xbt_strdup(struct_cluster->prefix),free);
1362           xbt_dict_set(patterns,"suffix",xbt_strdup(struct_cluster->suffix),free);
1363   }
1364
1365   unsigned int iter;
1366   int start, end, i;
1367   xbt_dynar_t radical_elements;
1368   xbt_dynar_t radical_ends;
1369
1370   static unsigned int surfxml_buffer_stack_stack_ptr = 1;
1371   static unsigned int surfxml_buffer_stack_stack[1024];
1372
1373   surfxml_buffer_stack_stack[0] = 0;
1374   surfxml_bufferstack_push(1);
1375
1376   SURFXML_BUFFER_SET(AS_id, struct_cluster->id);
1377   SURFXML_BUFFER_SET(AS_routing, "Cluster");
1378   XBT_DEBUG("<AS id=\"%s\"\trouting=\"Cluster\">", struct_cluster->id);
1379   SURFXML_START_TAG(AS);
1380
1381   //Make all hosts
1382   radical_elements = xbt_str_split(struct_cluster->radical, ",");
1383   xbt_dynar_foreach(radical_elements, iter, groups) {
1384     memset(&host,0,sizeof(host));
1385
1386     radical_ends = xbt_str_split(groups, "-");
1387     switch (xbt_dynar_length(radical_ends)) {
1388     case 1:
1389                 start=surf_parse_get_int(xbt_dynar_get_as(radical_ends, 0, char *));
1390                 host_id = bprintf("%s%d%s", struct_cluster->prefix, start, struct_cluster->suffix);
1391                 link_id = bprintf("%s_link_%d", struct_cluster->id, start);
1392
1393                 XBT_DEBUG("<host\tid=\"%s\"\tpower=\"%f\">", host_id, struct_cluster->power);
1394                 host.id = host_id;
1395                 if(strcmp(struct_cluster->availability_trace,"")){
1396                   xbt_dict_set(patterns, "radical", bprintf("%d", start), xbt_free);
1397                   char* tmp_availability_file = xbt_strdup(struct_cluster->availability_trace);
1398                   xbt_str_varsubst(tmp_availability_file,patterns);
1399                   XBT_DEBUG("\tavailability_file=\"%s\"",tmp_availability_file);
1400                   host.power_trace = tmgr_trace_new(tmp_availability_file);
1401                   xbt_free(tmp_availability_file);
1402                 }
1403                 else
1404                 {
1405                   XBT_DEBUG("\tavailability_file=\"\"");
1406                 }
1407                 if(strcmp(struct_cluster->state_trace,"")){
1408                   char *tmp_state_file = xbt_strdup(struct_cluster->state_trace);
1409                   xbt_str_varsubst(tmp_state_file,patterns);
1410                   XBT_DEBUG("\tstate_file=\"%s\"",tmp_state_file);
1411                   host.state_trace = tmgr_trace_new(tmp_state_file);
1412                   xbt_free(tmp_state_file);
1413                 }
1414                 else
1415                 {
1416                   XBT_DEBUG("\tstate_file=\"\"");
1417                 }
1418
1419                 host.power_peak = struct_cluster->power;
1420                 host.power_scale = 1.0;
1421                 host.core_amount = struct_cluster->core_amount;
1422                 host.initial_state = SURF_RESOURCE_ON;
1423                 host.coord = "";
1424                 sg_platf_new_host(&host);
1425                 XBT_DEBUG("</host>");
1426
1427
1428                 XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%f\"\tlat=\"%f\"/>", link_id,struct_cluster->bw, struct_cluster->lat);
1429
1430                 memset(&link,0,sizeof(link));
1431                 link.id = link_id;
1432                 link.bandwidth = struct_cluster->bw;
1433                 link.latency = struct_cluster->lat;
1434                 link.state = SURF_RESOURCE_ON;
1435
1436                 switch (struct_cluster->sharing_policy) {
1437                 case A_surfxml_cluster_sharing_policy_FATPIPE:
1438                   link.policy = SURF_LINK_FATPIPE;
1439                   break;
1440                 case A_surfxml_cluster_sharing_policy_FULLDUPLEX:
1441                   link.policy = SURF_LINK_FULLDUPLEX;
1442                   break;
1443                 default:
1444       link.policy = SURF_LINK_SHARED;
1445                 }
1446
1447                 sg_platf_new_link(&link);
1448
1449                 surf_parsing_link_up_down_t info = xbt_new0(s_surf_parsing_link_up_down_t, 1);
1450                 if (struct_cluster->sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX){
1451                         char* tmp_link =  bprintf("%s_UP",link_id);
1452                         info->link_up   = xbt_lib_get_or_null(link_lib, tmp_link, SURF_LINK_LEVEL);
1453                         free(tmp_link);
1454                         tmp_link =  bprintf("%s_DOWN",link_id);
1455                         info->link_down = xbt_lib_get_or_null(link_lib, tmp_link, SURF_LINK_LEVEL);
1456                         free(tmp_link);
1457                 }
1458                 else{
1459                         info->link_up   = xbt_lib_get_or_null(link_lib, link_id, SURF_LINK_LEVEL);
1460                         info->link_down = info->link_up;
1461                 }
1462                 surf_routing_cluster_add_link(host_id,info);
1463                 xbt_free(link_id);
1464                 xbt_free(host_id);
1465
1466                 break;
1467
1468     case 2:
1469
1470       start=surf_parse_get_int(xbt_dynar_get_as(radical_ends, 0, char *));
1471       end=  surf_parse_get_int(xbt_dynar_get_as(radical_ends, 1, char *));
1472       for (i = start; i <= end; i++) {
1473                 host_id = bprintf("%s%d%s", struct_cluster->prefix, i, struct_cluster->suffix);
1474                 link_id = bprintf("%s_link_%d", struct_cluster->id, i);
1475
1476                 A_surfxml_host_state = A_surfxml_host_state_ON;
1477
1478                 XBT_DEBUG("<host\tid=\"%s\"\tpower=\"%f\">", host_id, struct_cluster->power);
1479                 host.id = host_id;
1480                 if(strcmp(struct_cluster->availability_trace,"")){
1481                   xbt_dict_set(patterns, "radical", bprintf("%d", i), xbt_free);
1482                   char* tmp_availability_file = xbt_strdup(struct_cluster->availability_trace);
1483                   xbt_str_varsubst(tmp_availability_file,patterns);
1484                   XBT_DEBUG("\tavailability_file=\"%s\"",tmp_availability_file);
1485                   host.power_trace = tmgr_trace_new(tmp_availability_file);
1486                   xbt_free(tmp_availability_file);
1487                 }
1488                 else
1489                 {
1490                   XBT_DEBUG("\tavailability_file=\"\"");
1491                 }
1492                 if(strcmp(struct_cluster->state_trace,"")){
1493                   char *tmp_state_file = xbt_strdup(struct_cluster->state_trace);
1494                   xbt_str_varsubst(tmp_state_file,patterns);
1495                   XBT_DEBUG("\tstate_file=\"%s\"",tmp_state_file);
1496                   host.state_trace = tmgr_trace_new(tmp_state_file);
1497                   xbt_free(tmp_state_file);
1498                 }
1499                 else
1500                 {
1501                   XBT_DEBUG("\tstate_file=\"\"");
1502                 }
1503
1504                 host.power_peak = struct_cluster->power;
1505                 host.power_scale = 1.0;
1506                 host.core_amount = struct_cluster->core_amount;
1507                 host.initial_state = SURF_RESOURCE_ON;
1508                 host.coord = "";
1509                 sg_platf_new_host(&host);
1510                 XBT_DEBUG("</host>");
1511
1512                 XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%f\"\tlat=\"%f\"/>", link_id,struct_cluster->bw, struct_cluster->lat);
1513
1514                 memset(&link,0,sizeof(link));
1515                 link.id = link_id;
1516                 link.bandwidth = struct_cluster->bw;
1517                 link.latency = struct_cluster->lat;
1518                 link.state = SURF_RESOURCE_ON;
1519
1520                 switch (struct_cluster->sharing_policy) {
1521                 case A_surfxml_cluster_sharing_policy_SHARED:
1522       link.policy = SURF_LINK_SHARED;
1523       break;
1524                 case A_surfxml_cluster_sharing_policy_FULLDUPLEX:
1525                   link.policy = SURF_LINK_FULLDUPLEX;
1526                   break;
1527                 case A_surfxml_cluster_sharing_policy_FATPIPE:
1528                   link.policy = SURF_LINK_FATPIPE;
1529                   break;
1530                 default:
1531                   surf_parse_error(bprintf("Invalid cluster sharing policy for cluster %s",struct_cluster->id));
1532                 }
1533                 sg_platf_new_link(&link);
1534
1535                 surf_parsing_link_up_down_t info = xbt_new0(s_surf_parsing_link_up_down_t, 1);
1536                 if (link.policy == SURF_LINK_FULLDUPLEX) {
1537                         char* tmp_link =  bprintf("%s_UP",link_id);
1538                         info->link_up   = xbt_lib_get_or_null(link_lib, tmp_link, SURF_LINK_LEVEL);
1539                         free(tmp_link);
1540                         tmp_link =  bprintf("%s_DOWN",link_id);
1541                         info->link_down = xbt_lib_get_or_null(link_lib, tmp_link, SURF_LINK_LEVEL);
1542                         free(tmp_link);
1543                 }
1544                 else{
1545                         info->link_up   = xbt_lib_get_or_null(link_lib, link_id, SURF_LINK_LEVEL);
1546                         info->link_down = info->link_up;
1547                 }
1548                 surf_routing_cluster_add_link(host_id,info);
1549
1550                 xbt_free(link_id);
1551                 xbt_free(host_id);
1552
1553       }
1554       break;
1555
1556     default:
1557       XBT_DEBUG("Malformed radical");
1558       break;
1559     }
1560
1561     xbt_dynar_free(&radical_ends);
1562   }
1563   xbt_dynar_free(&radical_elements);
1564
1565   // Add a router. It is magically used thanks to the way in which surf_routing_cluster is written, and it's very useful to connect clusters together
1566   XBT_DEBUG(" ");
1567   XBT_DEBUG("<router id=\"%s\"/>", struct_cluster->router_id);
1568   s_sg_platf_router_cbarg_t router;
1569   char *newid=NULL;
1570   memset(&router,0,sizeof(router));
1571   router.id = struct_cluster->router_id;
1572   router.coord = "";
1573   if (!router.id || !strcmp(router.id,""))
1574     router.id = newid = bprintf("%s%s_router%s", struct_cluster->prefix, struct_cluster->id, struct_cluster->suffix);
1575   sg_platf_new_router(&router);
1576   if (newid)
1577     free(newid);
1578
1579   //Make the backbone
1580   if( (struct_cluster->bb_bw!= 0)  && (struct_cluster->bb_lat!=0)  ){
1581           char *link_backbone = bprintf("%s_backbone", struct_cluster->id);
1582           XBT_DEBUG("<link\tid=\"%s\" bw=\"%f\" lat=\"%f\"/>", link_backbone,struct_cluster->bb_bw, struct_cluster->bb_lat);
1583
1584           memset(&link,0,sizeof(link));
1585           link.id = link_backbone;
1586           link.bandwidth = struct_cluster->bb_bw;
1587           link.latency = struct_cluster->bb_lat;
1588           link.state = SURF_RESOURCE_ON;
1589
1590           switch (AX_surfxml_cluster_bb_sharing_policy) {
1591           case A_surfxml_cluster_bb_sharing_policy_FATPIPE:
1592             link.policy = SURF_LINK_FATPIPE;
1593             break;
1594           case A_surfxml_cluster_bb_sharing_policy_SHARED:
1595              link.policy = SURF_LINK_SHARED;
1596              break;
1597           default:
1598             surf_parse_error(bprintf("Invalid bb sharing policy in cluster %s",struct_cluster->id));
1599           }
1600
1601           sg_platf_new_link(&link);
1602
1603           surf_parsing_link_up_down_t info = xbt_new0(s_surf_parsing_link_up_down_t, 1);
1604           info->link_up   = xbt_lib_get_or_null(link_lib, link_backbone, SURF_LINK_LEVEL);
1605           info->link_down = info->link_up;
1606           surf_routing_cluster_add_link(struct_cluster->id, info);
1607
1608           free(link_backbone);
1609   }
1610
1611   XBT_DEBUG(" ");
1612
1613   char *new_suffix = xbt_strdup("");
1614
1615   radical_elements = xbt_str_split(struct_cluster->suffix, ".");
1616   xbt_dynar_foreach(radical_elements, iter, groups) {
1617     if (strcmp(groups, "")) {
1618       char *old_suffix = new_suffix;
1619       new_suffix = bprintf("%s\\.%s", old_suffix, groups);
1620       free(old_suffix);
1621     }
1622   }
1623
1624   xbt_dynar_free(&radical_elements);
1625   xbt_free(new_suffix);
1626
1627   if( strcmp(struct_cluster->availability_trace,"")
1628                   || strcmp(struct_cluster->state_trace,"") )
1629           xbt_dict_free(&patterns);
1630
1631   XBT_DEBUG("</AS>");
1632   SURFXML_END_TAG(AS);
1633   XBT_DEBUG(" ");
1634
1635   surfxml_bufferstack_pop(1);
1636 }
1637 /*
1638  * This function take a string and replace parameters from patterns dict.
1639  * It returns the new value.
1640  */
1641 static char* replace_random_parameter(char * string)
1642 {
1643   char *test_string = NULL;
1644
1645   if(xbt_dict_size(random_value)==0)
1646     return string;
1647
1648   string = xbt_str_varsubst(string, patterns); // for patterns of cluster
1649   test_string = bprintf("${%s}", string);
1650   test_string = xbt_str_varsubst(test_string,random_value); //Add ${xxxxx} for random Generator
1651
1652   if (strcmp(test_string,"")) { //if not empty, keep this value.
1653     xbt_free(string);
1654     string = test_string;
1655   } //In other case take old value (without ${})
1656   else
1657         free(test_string);
1658   return string;
1659 }
1660
1661 static void clean_routing_after_parse(void)
1662 {
1663         xbt_dict_free(&random_value);
1664         xbt_dict_free(&patterns);
1665 }
1666
1667 static void routing_parse_Speer(sg_platf_peer_cbarg_t peer)
1668 {
1669   static int AX_ptr = 0;
1670   char *host_id = NULL;
1671   char *router_id, *link_router, *link_backbone, *link_id_up, *link_id_down;
1672
1673   static unsigned int surfxml_buffer_stack_stack_ptr = 1;
1674   static unsigned int surfxml_buffer_stack_stack[1024];
1675
1676   surfxml_buffer_stack_stack[0] = 0;
1677
1678   surfxml_bufferstack_push(1);
1679
1680   XBT_DEBUG("<AS id=\"%s\"\trouting=\"Full\">", peer->id);
1681   sg_platf_new_AS_open(peer->id, "Full");
1682
1683   XBT_DEBUG(" ");
1684   host_id = HOST_PEER(peer->id);
1685   router_id = ROUTER_PEER(peer->id);
1686   link_id_up = LINK_UP_PEER(peer->id);
1687   link_id_down = LINK_DOWN_PEER(peer->id);
1688
1689   link_router = bprintf("%s_link_router", peer->id);
1690   link_backbone = bprintf("%s_backbone", peer->id);
1691
1692   XBT_DEBUG("<host\tid=\"%s\"\tpower=\"%f\"/>", host_id, peer->power);
1693   s_sg_platf_host_cbarg_t host;
1694   memset(&host,0,sizeof(host));
1695   host.initial_state = SURF_RESOURCE_ON;
1696   host.id = host_id;
1697   host.power_peak = peer->power;
1698   host.power_scale = 1.0;
1699   host.power_trace = peer->availability_trace;
1700   host.state_trace = peer->state_trace;
1701   host.core_amount = 1;
1702   sg_platf_new_host(&host);
1703
1704
1705   XBT_DEBUG("<router id=\"%s\"\tcoordinates=\"%s\"/>", router_id, peer->coord);
1706   s_sg_platf_router_cbarg_t router;
1707   memset(&router,0,sizeof(router));
1708   router.id = router_id;
1709   router.coord = peer->coord;
1710   sg_platf_new_router(&router);
1711
1712   XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%f\"\tlat=\"%f\"/>", link_id_up, peer->bw_in, peer->lat);
1713   s_sg_platf_link_cbarg_t link;
1714   memset(&link,0,sizeof(link));
1715   link.state = SURF_RESOURCE_ON;
1716   link.policy = SURF_LINK_SHARED;
1717   link.id = link_id_up;
1718   link.bandwidth = peer->bw_in;
1719   link.latency = peer->lat;
1720   sg_platf_new_link(&link);
1721
1722   // FIXME: dealing with full duplex is not the role of this piece of code, I'd say [Mt]
1723   // Instead, it should be created fullduplex, and the models will do what's needed in this case
1724   XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%f\"\tlat=\"%f\"/>", link_id_down, peer->bw_out, peer->lat);
1725   link.id = link_id_down;
1726   sg_platf_new_link(&link);
1727
1728   XBT_DEBUG(" ");
1729
1730   // begin here
1731   XBT_DEBUG("<route\tsrc=\"%s\"\tdst=\"%s\"", host_id, router_id);
1732   XBT_DEBUG("symmetrical=\"NO\">");
1733   SURFXML_BUFFER_SET(route_src, host_id);
1734   SURFXML_BUFFER_SET(route_dst, router_id);
1735   A_surfxml_route_symmetrical = A_surfxml_route_symmetrical_NO;
1736   SURFXML_START_TAG(route);
1737
1738   XBT_DEBUG("<link_ctn\tid=\"%s\"/>", link_id_up);
1739   SURFXML_BUFFER_SET(link_ctn_id, link_id_up);
1740   A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
1741   SURFXML_START_TAG(link_ctn);
1742   SURFXML_END_TAG(link_ctn);
1743
1744   XBT_DEBUG("</route>");
1745   SURFXML_END_TAG(route);
1746
1747   //Opposite Route
1748   XBT_DEBUG("<route\tsrc=\"%s\"\tdst=\"%s\"", router_id, host_id);
1749   XBT_DEBUG("symmetrical=\"NO\">");
1750   SURFXML_BUFFER_SET(route_src, router_id);
1751   SURFXML_BUFFER_SET(route_dst, host_id);
1752   A_surfxml_route_symmetrical = A_surfxml_route_symmetrical_NO;
1753   SURFXML_START_TAG(route);
1754
1755   XBT_DEBUG("<link_ctn\tid=\"%s\"/>", link_id_down);
1756   SURFXML_BUFFER_SET(link_ctn_id, link_id_down);
1757   A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
1758   SURFXML_START_TAG(link_ctn);
1759   SURFXML_END_TAG(link_ctn);
1760
1761   XBT_DEBUG("</route>");
1762   SURFXML_END_TAG(route);
1763
1764   XBT_DEBUG("</AS>");
1765   sg_platf_new_AS_close();
1766   XBT_DEBUG(" ");
1767
1768   //xbt_dynar_free(&tab_elements_num);
1769         free(host_id);
1770         free(router_id);
1771         free(link_router);
1772         free(link_backbone);
1773         free(link_id_up);
1774         free(link_id_down);
1775   surfxml_bufferstack_pop(1);
1776 }
1777
1778 static void routing_parse_Srandom(void)
1779 {
1780     double mean, std, min, max, seed;
1781     char *random_id = A_surfxml_random_id;
1782     char *random_radical = A_surfxml_random_radical;
1783     char *rd_name = NULL;
1784     char *rd_value;
1785     mean = surf_parse_get_double(A_surfxml_random_mean);
1786     std  = surf_parse_get_double(A_surfxml_random_std_deviation);
1787     min  = surf_parse_get_double(A_surfxml_random_min);
1788     max  = surf_parse_get_double(A_surfxml_random_max);
1789     seed = surf_parse_get_double(A_surfxml_random_seed);
1790
1791     double res = 0;
1792     int i = 0;
1793     random_data_t random = xbt_new0(s_random_data_t, 1);
1794           char *tmpbuf;
1795
1796     xbt_dynar_t radical_elements;
1797     unsigned int iter;
1798     char *groups;
1799     int start, end;
1800     xbt_dynar_t radical_ends;
1801
1802     random->generator = A_surfxml_random_generator;
1803     random->seed = seed;
1804     random->min = min;
1805     random->max = max;
1806
1807     /* Check user stupidities */
1808     if (max < min)
1809       THROWF(arg_error, 0, "random->max < random->min (%f < %f)", max, min);
1810     if (mean < min)
1811       THROWF(arg_error, 0, "random->mean < random->min (%f < %f)", mean,
1812        min);
1813     if (mean > max)
1814       THROWF(arg_error, 0, "random->mean > random->max (%f > %f)", mean,
1815        max);
1816
1817     /* normalize the mean and standard deviation before storing */
1818     random->mean = (mean - min) / (max - min);
1819     random->std = std / (max - min);
1820
1821     if (random->mean * (1 - random->mean) < random->std * random->std)
1822       THROWF(arg_error, 0, "Invalid mean and standard deviation (%f and %f)",
1823        random->mean, random->std);
1824
1825     XBT_DEBUG("id = '%s' min = '%f' max = '%f' mean = '%f' std_deviatinon = '%f' generator = '%d' seed = '%ld' radical = '%s'",
1826     random_id,
1827     random->min,
1828     random->max,
1829     random->mean,
1830     random->std,
1831     random->generator,
1832     random->seed,
1833     random_radical);
1834
1835     if(xbt_dict_size(random_value)==0)
1836       random_value = xbt_dict_new();
1837
1838     if(!strcmp(random_radical,""))
1839     {
1840       res = random_generate(random);
1841       rd_value = bprintf("%f",res);
1842       xbt_dict_set(random_value, random_id, rd_value, free);
1843     }
1844     else
1845     {
1846       radical_elements = xbt_str_split(random_radical, ",");
1847       xbt_dynar_foreach(radical_elements, iter, groups) {
1848       radical_ends = xbt_str_split(groups, "-");
1849       switch (xbt_dynar_length(radical_ends)) {
1850       case 1:
1851             xbt_assert(!xbt_dict_get_or_null(random_value,random_id),"Custom Random '%s' already exists !",random_id);
1852             res = random_generate(random);
1853                                           tmpbuf = bprintf("%s%d",random_id,atoi(xbt_dynar_getfirst_as(radical_ends,char *)));
1854                                           xbt_dict_set(random_value, tmpbuf, bprintf("%f",res), free);
1855                                           xbt_free(tmpbuf);
1856             break;
1857
1858       case 2:
1859             start = surf_parse_get_int(xbt_dynar_get_as(radical_ends, 0, char *));
1860             end = surf_parse_get_int(xbt_dynar_get_as(radical_ends, 1, char *));
1861             for (i = start; i <= end; i++) {
1862               xbt_assert(!xbt_dict_get_or_null(random_value,random_id),"Custom Random '%s' already exists !",bprintf("%s%d",random_id,i));
1863               res = random_generate(random);
1864                           tmpbuf = bprintf("%s%d",random_id,i);
1865               xbt_dict_set(random_value, tmpbuf, bprintf("%f",res), free);
1866                           xbt_free(tmpbuf);
1867             }
1868             break;
1869       default:
1870         XBT_INFO("Malformed radical");
1871         break;
1872       }
1873       res = random_generate(random);
1874       rd_name  = bprintf("%s_router",random_id);
1875       rd_value = bprintf("%f",res);
1876       xbt_dict_set(random_value, rd_name, rd_value, free);
1877
1878       xbt_dynar_free(&radical_ends);
1879       }
1880       free(rd_name);
1881       xbt_dynar_free(&radical_elements);
1882     }
1883 }