Logo AND Algorithmique Numérique Distribuée

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