Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Initilized vars.
[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
22 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route, surf, "Routing part of surf");
23
24 /* Global vars */
25 routing_global_t global_routing = NULL;
26 routing_component_t current_routing = NULL;
27 model_type_t current_routing_model = NULL;
28
29 /* Prototypes of each model */
30 static void *model_full_create(void);   /* create structures for full routing model */
31 static void model_full_load(void);      /* load parse functions for full routing model */
32 static void model_full_unload(void);    /* unload parse functions for full routing model */
33 static void model_full_end(void);       /* finalize the creation of full routing model */
34
35 static void *model_floyd_create(void);  /* create structures for floyd routing model */
36 static void model_floyd_load(void);     /* load parse functions for floyd routing model */
37 static void model_floyd_unload(void);   /* unload parse functions for floyd routing model */
38 static void model_floyd_end(void);      /* finalize the creation of floyd routing model */
39
40 static void *model_dijkstra_both_create(int cached);    /* create by calling dijkstra or dijkstracache */
41 static void *model_dijkstra_create(void);       /* create structures for dijkstra routing model */
42 static void *model_dijkstracache_create(void);  /* create structures for dijkstracache routing model */
43 static void model_dijkstra_both_load(void);     /* load parse functions for dijkstra routing model */
44 static void model_dijkstra_both_unload(void);   /* unload parse functions for dijkstra routing model */
45 static void model_dijkstra_both_end(void);      /* finalize the creation of dijkstra routing model */
46
47 static void *model_rulebased_create(void);      /* create structures for rulebased routing model */
48 static void model_rulebased_load(void); /* load parse functions for rulebased routing model */
49 static void model_rulebased_unload(void);       /* unload parse functions for rulebased routing model */
50 static void model_rulebased_end(void);  /* finalize the creation of rulebased routing model */
51
52 static void *model_none_create(void);   /* none routing model */
53 static void model_none_load(void);      /* none routing model */
54 static void model_none_unload(void);    /* none routing model */
55 static void model_none_end(void);       /* none routing model */
56
57 static void routing_full_parse_Scluster(void);  /*cluster bypass */
58
59 static void parse_Sconfig(void);        /*config Tag */
60
61 /* this lines are only for replace use like index in the model table */
62 typedef enum {
63   SURF_MODEL_FULL = 0,
64   SURF_MODEL_FLOYD,
65   SURF_MODEL_DIJKSTRA,
66   SURF_MODEL_DIJKSTRACACHE,
67   SURF_MODEL_NONE,
68 #ifdef HAVE_PCRE_LIB
69   SURF_MODEL_RULEBASED
70 #endif
71 } e_routing_types;
72
73
74 /* must be finish with null and carefull if change de order */
75 struct s_model_type routing_models[] = { {"Full",
76                                           "Full routing data (fast, large memory requirements, fully expressive)",
77                                           model_full_create,
78                                           model_full_load,
79                                           model_full_unload,
80                                           model_full_end},
81 {"Floyd",
82  "Floyd routing data (slow initialization, fast lookup, lesser memory requirements, shortest path routing only)",
83  model_floyd_create, model_floyd_load, model_floyd_unload,
84  model_floyd_end},
85 {"Dijkstra",
86  "Dijkstra routing data (fast initialization, slow lookup, small memory requirements, shortest path routing only)",
87  model_dijkstra_create, model_dijkstra_both_load,
88  model_dijkstra_both_unload, model_dijkstra_both_end},
89 {"DijkstraCache",
90  "Dijkstra routing data (fast initialization, fast lookup, small memory requirements, shortest path routing only)",
91  model_dijkstracache_create, model_dijkstra_both_load,
92  model_dijkstra_both_unload, model_dijkstra_both_end},
93 {"none", "No routing (usable with Constant network only)",
94  model_none_create, model_none_load, model_none_unload, model_none_end},
95 #ifdef HAVE_PCRE_LIB
96 {"RuleBased", "Rule-Based routing data (...)", model_rulebased_create,
97  model_rulebased_load, model_rulebased_unload, model_rulebased_end},
98 #endif
99 {NULL, NULL, NULL, NULL, NULL, NULL}
100 };
101
102 /* ************************************************************************** */
103 /* ***************** GENERIC PARSE FUNCTIONS (declarations) ***************** */
104
105 static void generic_set_processing_unit(routing_component_t rc,
106                                         const char *name);
107 static void generic_set_autonomous_system(routing_component_t rc,
108                                           const char *name);
109 static void generic_set_route(routing_component_t rc, const char *src,
110                               const char *dst, route_t route);
111 static void generic_set_ASroute(routing_component_t rc, const char *src,
112                                 const char *dst, route_extended_t e_route);
113 static void generic_set_bypassroute(routing_component_t rc,
114                                     const char *src, const char *dst,
115                                     route_extended_t e_route);
116
117 /* ************************************************************************** */
118 /* *************** GENERIC BUSINESS METHODS (declarations) ****************** */
119
120 static xbt_dynar_t generic_get_onelink_routes(routing_component_t rc);
121 static route_extended_t generic_get_bypassroute(routing_component_t rc,
122                                                 const char *src,
123                                                 const char *dst);
124
125 /* ************************************************************************** */
126 /* ****************** GENERIC AUX FUNCTIONS (declarations) ****************** */
127
128 static route_extended_t
129 generic_new_extended_route(e_surf_routing_hierarchy_t hierarchy,
130                            void *data, int order);
131 static void generic_free_route(route_t route);
132 static void generic_free_extended_route(route_extended_t e_route);
133 static routing_component_t
134 generic_autonomous_system_exist(routing_component_t rc, char *element);
135 static routing_component_t
136 generic_processing_units_exist(routing_component_t rc, char *element);
137 static void generic_src_dst_check(routing_component_t rc, const char *src,
138                                   const char *dst);
139
140 /* ************************************************************************** */
141 /* **************************** GLOBAL FUNCTIONS **************************** */
142
143 /* global parse functions */
144 static char *src = NULL;        /* temporary store the source name of a route */
145 static char *dst = NULL;        /* temporary store the destination name of a route */
146 static char *gw_src = NULL;     /* temporary store the gateway source name of a route */
147 static char *gw_dst = NULL;     /* temporary store the gateway destination name of a route */
148 static xbt_dynar_t link_list = NULL;    /* temporary store of current list link of a route */
149
150 /**
151  * \brief Add a "host" to the network element list
152  */
153 static void parse_S_host(char *host_id)
154 {
155   network_element_info_t info = NULL;
156   if (current_routing->hierarchy == SURF_ROUTING_NULL)
157     current_routing->hierarchy = SURF_ROUTING_BASE;
158   xbt_assert1(!xbt_dict_get_or_null
159               (global_routing->where_network_elements, host_id),
160               "Reading a host, processing unit \"%s\" already exist",
161               host_id);
162   xbt_assert1(current_routing->set_processing_unit,
163               "no defined method \"set_processing_unit\" in \"%s\"",
164               current_routing->name);
165   (*(current_routing->set_processing_unit)) (current_routing, host_id);
166   info = xbt_new0(s_network_element_info_t, 1);
167   info->rc_component = current_routing;
168   info->rc_type = SURF_NETWORK_ELEMENT_HOST;
169   xbt_dict_set(global_routing->where_network_elements, host_id,
170                (void *) info, NULL);
171 }
172
173 /*
174  * \brief Add a host to the network element list from XML
175  */
176 static void parse_S_host_XML(void)
177 {
178   parse_S_host(A_surfxml_host_id);
179 }
180
181 /*
182  * \brief Add a host to the network element list from lua script
183  */
184 static void parse_S_host_lua(char *host_id)
185 {
186   parse_S_host(host_id);
187 }
188
189 /*
190  *
191  */
192
193 /**
194  * \brief Add a "router" to the network element list
195  */
196 static void parse_S_router(void)
197 {
198   network_element_info_t info = NULL;
199
200   if (current_routing->hierarchy == SURF_ROUTING_NULL)
201     current_routing->hierarchy = SURF_ROUTING_BASE;
202   xbt_assert1(!xbt_dict_get_or_null
203               (global_routing->where_network_elements,
204                A_surfxml_router_id),
205               "Reading a router, processing unit \"%s\" already exist",
206               A_surfxml_router_id);
207   xbt_assert1(current_routing->set_processing_unit,
208               "no defined method \"set_processing_unit\" in \"%s\"",
209               current_routing->name);
210   (*(current_routing->set_processing_unit)) (current_routing,
211                                              A_surfxml_router_id);
212   info = xbt_new0(s_network_element_info_t, 1);
213   info->rc_component = current_routing;
214   info->rc_type = SURF_NETWORK_ELEMENT_ROUTER;
215   xbt_dict_set(global_routing->where_network_elements, A_surfxml_router_id,
216                (void *) info, NULL);
217 #ifdef HAVE_TRACING
218   TRACE_surf_host_declaration(A_surfxml_router_id, 0);
219 #endif
220 }
221
222 /**
223  * \brief Set the endponints for a route
224  */
225 static void parse_S_route_new_and_endpoints(char *src_id, char *dst_id)
226 {
227   if (src != NULL && dst != NULL && link_list != NULL)
228     THROW2(arg_error, 0, "Route between %s to %s can not be defined",
229            src_id, dst_id);
230   src = src_id;
231   dst = dst_id;
232   xbt_assert2(strlen(src) > 0 || strlen(dst) > 0,
233               "Some limits are null in the route between \"%s\" and \"%s\"",
234               src, dst);
235   link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
236 }
237
238 /**
239  * \breif Set the endpoints for a route from XML
240  */
241 static void parse_S_route_new_and_endpoints_XML(void)
242 {
243   parse_S_route_new_and_endpoints(A_surfxml_route_src,
244                                   A_surfxml_route_dst);
245 }
246
247 /**
248  * \breif Set the endpoints for a route from lua
249  */
250 static void parse_S_route_new_and_endpoints_lua(char *id_src, char *id_dst)
251 {
252   parse_S_route_new_and_endpoints(id_src, id_dst);
253 }
254
255 /**
256  * \brief Set the endponints and gateways for a ASroute
257  */
258 static void parse_S_ASroute_new_and_endpoints(void)
259 {
260   if (src != NULL && dst != NULL && link_list != NULL)
261     THROW2(arg_error, 0, "Route between %s to %s can not be defined",
262            A_surfxml_ASroute_src, A_surfxml_ASroute_dst);
263   src = A_surfxml_ASroute_src;
264   dst = A_surfxml_ASroute_dst;
265   gw_src = A_surfxml_ASroute_gw_src;
266   gw_dst = A_surfxml_ASroute_gw_dst;
267   xbt_assert2(strlen(src) > 0 || strlen(dst) > 0 || strlen(gw_src) > 0
268               || strlen(gw_dst) > 0,
269               "Some limits are null in the route between \"%s\" and \"%s\"",
270               src, dst);
271   link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
272 }
273
274 /**
275  * \brief Set the endponints for a bypassRoute
276  */
277 static void parse_S_bypassRoute_new_and_endpoints(void)
278 {
279   if (src != NULL && dst != NULL && link_list != NULL)
280     THROW2(arg_error, 0,
281            "Bypass Route between %s to %s can not be defined",
282            A_surfxml_bypassRoute_src, A_surfxml_bypassRoute_dst);
283   src = A_surfxml_bypassRoute_src;
284   dst = A_surfxml_bypassRoute_dst;
285   gw_src = A_surfxml_bypassRoute_gw_src;
286   gw_dst = A_surfxml_bypassRoute_gw_dst;
287   xbt_assert2(strlen(src) > 0 || strlen(dst) > 0 || strlen(gw_src) > 0
288               || strlen(gw_dst) > 0,
289               "Some limits are null in the route between \"%s\" and \"%s\"",
290               src, dst);
291   link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
292 }
293
294 /**
295  * \brief Set a new link on the actual list of link for a route or ASroute
296  */
297 static void parse_E_link_ctn_new_elem(char *link_id)
298 {
299   char *val;
300   val = xbt_strdup(link_id);
301   xbt_dynar_push(link_list, &val);
302 }
303
304 /**
305  * \brief Set a new link on the actual list of link for a route or ASroute from XML
306  */
307
308 static void parse_E_link_ctn_new_elem_XML(void)
309 {
310   parse_E_link_ctn_new_elem(A_surfxml_link_ctn_id);
311 }
312
313 /**
314  * \brief Set a new link on the actual list of link for a route or ASroute from lua
315  */
316 static void parse_E_link_c_ctn_new_elem_lua(char *link_id)
317 {
318
319   parse_E_link_ctn_new_elem(link_id);
320 }
321
322 /**
323  * \brief Store the route by calling the set_route function of the current routing component
324  */
325 static void parse_E_route_store_route(void)
326 {
327   route_t route = xbt_new0(s_route_t, 1);
328   route->link_list = link_list;
329 //   xbt_assert1(generic_processing_units_exist(current_routing,src),"the \"%s\" processing units gateway does not exist",src);
330 //   xbt_assert1(generic_processing_units_exist(current_routing,dst),"the \"%s\" processing units gateway does not exist",dst);
331   xbt_assert1(current_routing->set_route,
332               "no defined method \"set_route\" in \"%s\"",
333               current_routing->name);
334   (*(current_routing->set_route)) (current_routing, src, dst, route);
335   link_list = NULL;
336   src = NULL;
337   dst = NULL;
338 }
339
340 /**
341  * \brief Store the ASroute by calling the set_ASroute function of the current routing component
342  */
343 static void parse_E_ASroute_store_route(void)
344 {
345   route_extended_t e_route = xbt_new0(s_route_extended_t, 1);
346   e_route->generic_route.link_list = link_list;
347   e_route->src_gateway = xbt_strdup(gw_src);
348   e_route->dst_gateway = xbt_strdup(gw_dst);
349 //   xbt_assert1(generic_autonomous_system_exist(current_routing,src),"the \"%s\" autonomous system does not exist",src);
350 //   xbt_assert1(generic_autonomous_system_exist(current_routing,dst),"the \"%s\" autonomous system does not exist",dst);
351 //   xbt_assert1(generic_processing_units_exist(current_routing,gw_src),"the \"%s\" processing units gateway does not exist",gw_src);
352 //   xbt_assert1(generic_processing_units_exist(current_routing,gw_dst),"the \"%s\" processing units gateway does not exist",gw_dst);
353   xbt_assert1(current_routing->set_ASroute,
354               "no defined method \"set_ASroute\" in \"%s\"",
355               current_routing->name);
356   (*(current_routing->set_ASroute)) (current_routing, src, dst, e_route);
357   link_list = NULL;
358   src = NULL;
359   dst = NULL;
360   gw_src = NULL;
361   gw_dst = NULL;
362 }
363
364 /**
365  * \brief Store the bypass route by calling the set_bypassroute function of the current routing component
366  */
367 static void parse_E_bypassRoute_store_route(void)
368 {
369   route_extended_t e_route = xbt_new0(s_route_extended_t, 1);
370   e_route->generic_route.link_list = link_list;
371   e_route->src_gateway = xbt_strdup(gw_src);
372   e_route->dst_gateway = xbt_strdup(gw_dst);
373 //   xbt_assert1(generic_autonomous_system_exist(current_routing,src),"the \"%s\" autonomous system does not exist",src);
374 //   xbt_assert1(generic_autonomous_system_exist(current_routing,dst),"the \"%s\" autonomous system does not exist",dst);
375 //   xbt_assert1(generic_processing_units_exist(current_routing,gw_src),"the \"%s\" processing units gateway does not exist",gw_src);
376 //   xbt_assert1(generic_processing_units_exist(current_routing,gw_dst),"the \"%s\" processing units gateway does not exist",gw_dst);
377   xbt_assert1(current_routing->set_bypassroute,
378               "no defined method \"set_bypassroute\" in \"%s\"",
379               current_routing->name);
380   (*(current_routing->set_bypassroute)) (current_routing, src, dst,
381                                          e_route);
382   link_list = NULL;
383   src = NULL;
384   dst = NULL;
385   gw_src = NULL;
386   gw_dst = NULL;
387 }
388
389 /**
390  * \brief Make a new routing component
391  *
392  * make the new structure and
393  * set the parsing functions to allows parsing the part of the routing tree
394  */
395 static void parse_S_AS(char *AS_id, char *AS_routing)
396 {
397   routing_component_t new_routing;
398   model_type_t model = NULL;
399   char *wanted = AS_routing;
400   int cpt;
401   /* seach the routing model */
402   for (cpt = 0; routing_models[cpt].name; cpt++)
403     if (!strcmp(wanted, routing_models[cpt].name))
404       model = &routing_models[cpt];
405   /* if its not exist, error */
406   if (model == NULL) {
407     fprintf(stderr, "Routing model %s not found. Existing models:\n",
408             wanted);
409     for (cpt = 0; routing_models[cpt].name; cpt++)
410       if (!strcmp(wanted, routing_models[cpt].name))
411         fprintf(stderr, "   %s: %s\n", routing_models[cpt].name,
412                 routing_models[cpt].desc);
413     xbt_die(NULL);
414   }
415
416   /* make a new routing component */
417   new_routing = (routing_component_t) (*(model->create)) ();
418   new_routing->routing = model;
419   new_routing->hierarchy = SURF_ROUTING_NULL;
420   new_routing->name = xbt_strdup(AS_id);
421   new_routing->routing_sons = xbt_dict_new();
422   //INFO2("Routing %s for AS %s",A_surfxml_AS_routing,A_surfxml_AS_id);
423
424   if (current_routing == NULL && global_routing->root == NULL) {
425
426     /* it is the first one */
427     new_routing->routing_father = NULL;
428     global_routing->root = new_routing;
429
430   } else if (current_routing != NULL && global_routing->root != NULL) {
431
432     xbt_assert1(!xbt_dict_get_or_null
433                 (current_routing->routing_sons, AS_id),
434                 "The AS \"%s\" already exist", AS_id);
435     /* it is a part of the tree */
436     new_routing->routing_father = current_routing;
437     /* set the father behavior */
438     if (current_routing->hierarchy == SURF_ROUTING_NULL)
439       current_routing->hierarchy = SURF_ROUTING_RECURSIVE;
440     /* add to the sons dictionary */
441     xbt_dict_set(current_routing->routing_sons, AS_id,
442                  (void *) new_routing, NULL);
443     /* add to the father element list */
444     (*(current_routing->set_autonomous_system)) (current_routing, AS_id);
445     /* unload the prev parse rules */
446     (*(current_routing->routing->unload)) ();
447
448   } else {
449     THROW0(arg_error, 0, "All defined components must be belong to a AS");
450   }
451   /* set the new parse rules */
452   (*(new_routing->routing->load)) ();
453   /* set the new current component of the tree */
454   current_routing = new_routing;
455 }
456
457 /*
458  * Detect the routing model type of the routing component from XML platforms
459  */
460 static void parse_S_AS_XML(void)
461 {
462   parse_S_AS(A_surfxml_AS_id, A_surfxml_AS_routing);
463 }
464
465 /*
466  * define the routing model type of routing component from lua script
467  */
468 static void parse_S_AS_lua(char *id, char *mode)
469 {
470   parse_S_AS(id, mode);
471 }
472
473
474 /**
475  * \brief Finish the creation of a new routing component
476  *
477  * When you finish to read the routing component, other structures must be created. 
478  * the "end" method allow to do that for any routing model type
479  */
480 static void parse_E_AS(char *AS_id)
481 {
482
483   if (current_routing == NULL) {
484     THROW1(arg_error, 0, "Close AS(%s), that never open", AS_id);
485   } else {
486     network_element_info_t info = NULL;
487     xbt_assert1(!xbt_dict_get_or_null
488                 (global_routing->where_network_elements,
489                  current_routing->name), "The AS \"%s\" already exist",
490                 current_routing->name);
491     info = xbt_new0(s_network_element_info_t, 1);
492     info->rc_component = current_routing->routing_father;
493     info->rc_type = SURF_NETWORK_ELEMENT_AS;
494     xbt_dict_set(global_routing->where_network_elements,
495                  current_routing->name, info, NULL);
496     (*(current_routing->routing->unload)) ();
497     (*(current_routing->routing->end)) ();
498     current_routing = current_routing->routing_father;
499     if (current_routing != NULL)
500       (*(current_routing->routing->load)) ();
501   }
502 }
503
504 /*
505  * \brief Finish the creation of a new routing component from XML
506  */
507 static void parse_E_AS_XML(void)
508 {
509   parse_E_AS(A_surfxml_AS_id);
510 }
511
512 /*
513  * \brief Finish the creation of a new routing component from lua
514  */
515 static void parse_E_AS_lua(char *id)
516 {
517   parse_E_AS(id);
518 }
519
520 /* Aux Business methods */
521
522 /**
523  * \brief Get the AS father and the first elements of the chain
524  *
525  * \param src the source host name 
526  * \param dst the destination host name
527  * 
528  * Get the common father of the to processing units, and the first different 
529  * father in the chain
530  */
531 static xbt_dynar_t elements_father(const char *src, const char *dst)
532 {
533
534   xbt_assert0(src && dst, "bad parameters for \"elements_father\" method");
535
536   xbt_dynar_t result = xbt_dynar_new(sizeof(char *), NULL);
537
538   routing_component_t src_as, dst_as;
539   int index_src, index_dst, index_father_src, index_father_dst;
540   xbt_dynar_t path_src = NULL;
541   xbt_dynar_t path_dst = NULL;
542   routing_component_t current = NULL;
543   routing_component_t *current_src = NULL;
544   routing_component_t *current_dst = NULL;
545   routing_component_t *father = NULL;
546
547   /* (1) find the as where the src and dst are located */
548   src_as = ((network_element_info_t)
549             xbt_dict_get_or_null(global_routing->where_network_elements,
550                                  src))->rc_component;
551   dst_as = ((network_element_info_t)
552             xbt_dict_get_or_null(global_routing->where_network_elements,
553                                  dst))->rc_component;
554   xbt_assert2(src_as
555               && dst_as,
556               "Ask for route \"from\"(%s) or \"to\"(%s) no found", src,
557               dst);
558
559   /* (2) find the path to the root routing component */
560   path_src = xbt_dynar_new(sizeof(routing_component_t), NULL);
561   current = src_as;
562   while (current != NULL) {
563     xbt_dynar_push(path_src, &current);
564     current = current->routing_father;
565   }
566   path_dst = xbt_dynar_new(sizeof(routing_component_t), NULL);
567   current = dst_as;
568   while (current != NULL) {
569     xbt_dynar_push(path_dst, &current);
570     current = current->routing_father;
571   }
572
573   /* (3) find the common father */
574   index_src = (path_src->used) - 1;
575   index_dst = (path_dst->used) - 1;
576   current_src = xbt_dynar_get_ptr(path_src, index_src);
577   current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
578   while (index_src >= 0 && index_dst >= 0 && *current_src == *current_dst) {
579     current_src = xbt_dynar_get_ptr(path_src, index_src);
580     current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
581     index_src--;
582     index_dst--;
583   }
584   index_src++;
585   index_dst++;
586   current_src = xbt_dynar_get_ptr(path_src, index_src);
587   current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
588
589   /* (4) they are not in the same routing component, make the path */
590   index_father_src = index_src + 1;
591   index_father_dst = index_dst + 1;
592
593   if (*current_src == *current_dst)
594     father = current_src;
595   else
596     father = xbt_dynar_get_ptr(path_src, index_father_src);
597
598   /* (5) result generation */
599   xbt_dynar_push(result, father);       /* first same the father of src and dst */
600   xbt_dynar_push(result, current_src);  /* second the first different father of src */
601   xbt_dynar_push(result, current_dst);  /* three  the first different father of dst */
602
603   xbt_dynar_free(&path_src);
604   xbt_dynar_free(&path_dst);
605
606   return result;
607 }
608
609 /* Global Business methods */
610
611 /**
612  * \brief Recursive function for get_route
613  *
614  * \param src the source host name 
615  * \param dst the destination host name
616  * 
617  * This fuction is call by "get_route". It allow to walk through the 
618  * routing components tree.
619  */
620 static route_extended_t _get_route(const char *src, const char *dst)
621 {
622
623   void *link;
624   unsigned int cpt = 0;
625
626   DEBUG2("Solve route  \"%s\" to \"%s\"", src, dst);
627
628   xbt_assert0(src && dst, "bad parameters for \"_get_route\" method");
629
630   route_extended_t e_route, e_route_cnt, e_route_src = NULL, e_route_dst =
631       NULL;
632
633   xbt_dynar_t elem_father_list = elements_father(src, dst);
634
635   routing_component_t common_father =
636       xbt_dynar_get_as(elem_father_list, 0, routing_component_t);
637   routing_component_t src_father =
638       xbt_dynar_get_as(elem_father_list, 1, routing_component_t);
639   routing_component_t dst_father =
640       xbt_dynar_get_as(elem_father_list, 2, routing_component_t);
641
642   e_route = xbt_new0(s_route_extended_t, 1);
643   e_route->src_gateway = NULL;
644   e_route->dst_gateway = NULL;
645   e_route->generic_route.link_list =
646       xbt_dynar_new(global_routing->size_of_link, NULL);
647
648   if (src_father == dst_father) {       /* SURF_ROUTING_BASE */
649
650     if (strcmp(src, dst)) {
651       e_route_cnt =
652           (*(common_father->get_route)) (common_father, src, dst);
653       xbt_assert2(e_route_cnt, "no route between \"%s\" and \"%s\"", src,
654                   dst);
655       xbt_dynar_foreach(e_route_cnt->generic_route.link_list, cpt, link) {
656         xbt_dynar_push(e_route->generic_route.link_list, &link);
657       }
658       generic_free_extended_route(e_route_cnt);
659     }
660
661   } else {                      /* SURF_ROUTING_RECURSIVE */
662
663     route_extended_t e_route_bypass = NULL;
664
665     if (common_father->get_bypass_route)
666       e_route_bypass =
667           (*(common_father->get_bypass_route)) (common_father, src, dst);
668
669     if (e_route_bypass)
670       e_route_cnt = e_route_bypass;
671     else
672       e_route_cnt =
673           (*(common_father->get_route)) (common_father, src_father->name,
674                                          dst_father->name);
675
676     xbt_assert2(e_route_cnt, "no route between \"%s\" and \"%s\"",
677                 src_father->name, dst_father->name);
678
679     xbt_assert2((e_route_cnt->src_gateway == NULL) ==
680                 (e_route_cnt->dst_gateway == NULL),
681                 "bad gateway for route between \"%s\" and \"%s\"", src,
682                 dst);
683
684     if (src != e_route_cnt->src_gateway) {
685       e_route_src = _get_route(src, e_route_cnt->src_gateway);
686       xbt_assert2(e_route_src, "no route between \"%s\" and \"%s\"", src,
687                   e_route_cnt->src_gateway);
688       xbt_dynar_foreach(e_route_src->generic_route.link_list, cpt, link) {
689         xbt_dynar_push(e_route->generic_route.link_list, &link);
690       }
691     }
692
693     xbt_dynar_foreach(e_route_cnt->generic_route.link_list, cpt, link) {
694       xbt_dynar_push(e_route->generic_route.link_list, &link);
695     }
696
697     if (e_route_cnt->dst_gateway != dst) {
698       e_route_dst = _get_route(e_route_cnt->dst_gateway, dst);
699       xbt_assert2(e_route_dst, "no route between \"%s\" and \"%s\"",
700                   e_route_cnt->dst_gateway, dst);
701       xbt_dynar_foreach(e_route_dst->generic_route.link_list, cpt, link) {
702         xbt_dynar_push(e_route->generic_route.link_list, &link);
703       }
704     }
705
706     e_route->src_gateway = xbt_strdup(e_route_cnt->src_gateway);
707     e_route->dst_gateway = xbt_strdup(e_route_cnt->dst_gateway);
708
709     generic_free_extended_route(e_route_src);
710     generic_free_extended_route(e_route_cnt);
711     generic_free_extended_route(e_route_dst);
712   }
713
714   xbt_dynar_free(&elem_father_list);
715
716   return e_route;
717 }
718
719 /**
720  * \brief Generic method: find a route between hosts
721  *
722  * \param src the source host name 
723  * \param dst the destination host name
724  * 
725  * walk through the routing components tree and find a route between hosts
726  * by calling the differents "get_route" functions in each routing component
727  */
728 static xbt_dynar_t get_route(const char *src, const char *dst)
729 {
730
731   route_extended_t e_route;
732   xbt_dynar_t elem_father_list = elements_father(src, dst);
733   routing_component_t common_father =
734       xbt_dynar_get_as(elem_father_list, 0, routing_component_t);
735
736   if (strcmp(src, dst))
737     e_route = _get_route(src, dst);
738   else
739     e_route = (*(common_father->get_route)) (common_father, src, dst);
740
741   xbt_assert2(e_route, "no route between \"%s\" and \"%s\"", src, dst);
742
743   if (global_routing->last_route)
744     xbt_dynar_free(&(global_routing->last_route));
745   global_routing->last_route = e_route->generic_route.link_list;
746
747   if (e_route->src_gateway)
748     xbt_free(e_route->src_gateway);
749   if (e_route->dst_gateway)
750     xbt_free(e_route->dst_gateway);
751
752   xbt_free(e_route);
753   xbt_dynar_free(&elem_father_list);
754
755   if (xbt_dynar_length(global_routing->last_route) == 0)
756     return NULL;
757   else
758     return global_routing->last_route;
759 }
760
761 /**
762  * \brief Recursive function for finalize
763  *
764  * \param rc the source host name 
765  * 
766  * This fuction is call by "finalize". It allow to finalize the 
767  * AS or routing components. It delete all the structures.
768  */
769 static void _finalize(routing_component_t rc)
770 {
771   if (rc) {
772     xbt_dict_cursor_t cursor = NULL;
773     char *key;
774     routing_component_t elem;
775     xbt_dict_foreach(rc->routing_sons, cursor, key, elem) {
776       _finalize(elem);
777     }
778     xbt_dict_t tmp_sons = rc->routing_sons;
779     char *tmp_name = rc->name;
780     xbt_dict_free(&tmp_sons);
781     xbt_free(tmp_name);
782     xbt_assert1(rc->finalize, "no defined method \"finalize\" in \"%s\"",
783                 current_routing->name);
784     (*(rc->finalize)) (rc);
785   }
786 }
787
788 /**
789  * \brief Generic method: delete all the routing structures
790  * 
791  * walk through the routing components tree and delete the structures
792  * by calling the differents "finalize" functions in each routing component
793  */
794 static void finalize(void)
795 {
796   /* delete recursibly all the tree */
797   _finalize(global_routing->root);
798   /* delete "where" dict */
799   xbt_dict_free(&(global_routing->where_network_elements));
800   /* delete last_route */
801   xbt_dynar_free(&(global_routing->last_route));
802   /* delete global routing structure */
803   xbt_free(global_routing);
804 }
805
806 static xbt_dynar_t recursive_get_onelink_routes(routing_component_t rc)
807 {
808   xbt_dynar_t ret = xbt_dynar_new(sizeof(onelink_t), xbt_free);
809
810   //adding my one link routes
811   unsigned int cpt;
812   void *link;
813   xbt_dynar_t onelink_mine = rc->get_onelink_routes(rc);
814   if (onelink_mine) {
815     xbt_dynar_foreach(onelink_mine, cpt, link) {
816       xbt_dynar_push(ret, &link);
817     }
818   }
819   //recursing
820   char *key;
821   xbt_dict_cursor_t cursor = NULL;
822   routing_component_t rc_child;
823   xbt_dict_foreach(rc->routing_sons, cursor, key, rc_child) {
824     xbt_dynar_t onelink_child = recursive_get_onelink_routes(rc_child);
825     if (onelink_child) {
826       xbt_dynar_foreach(onelink_child, cpt, link) {
827         xbt_dynar_push(ret, &link);
828       }
829     }
830   }
831   return ret;
832 }
833
834 static xbt_dynar_t get_onelink_routes(void)
835 {
836   return recursive_get_onelink_routes(global_routing->root);
837 }
838
839 static e_surf_network_element_type_t get_network_element_type(const char
840                                                               *name)
841 {
842   network_element_info_t rc = NULL;
843   rc = xbt_dict_get(global_routing->where_network_elements, name);
844   return rc->rc_type;
845 }
846
847 /**
848  * \brief Generic method: create the global routing schema
849  * 
850  * Make a global routing structure and set all the parsing functions.
851  */
852 void routing_model_create(size_t size_of_links, void *loopback)
853 {
854
855   /* config the uniq global routing */
856   global_routing = xbt_new0(s_routing_global_t, 1);
857   global_routing->where_network_elements = xbt_dict_new();
858   global_routing->root = NULL;
859   global_routing->get_route = get_route;
860   global_routing->get_onelink_routes = get_onelink_routes;
861   global_routing->get_network_element_type = get_network_element_type;
862   global_routing->finalize = finalize;
863   global_routing->loopback = loopback;
864   global_routing->size_of_link = size_of_links;
865   global_routing->last_route = xbt_dynar_new(size_of_links, NULL);
866
867   /* no current routing at moment */
868   current_routing = NULL;
869
870   /* parse generic elements */
871   surfxml_add_callback(STag_surfxml_host_cb_list, &parse_S_host_XML);
872   surfxml_add_callback(STag_surfxml_router_cb_list, &parse_S_router);
873
874   surfxml_add_callback(STag_surfxml_route_cb_list,
875                        &parse_S_route_new_and_endpoints_XML);
876   surfxml_add_callback(STag_surfxml_ASroute_cb_list,
877                        &parse_S_ASroute_new_and_endpoints);
878   surfxml_add_callback(STag_surfxml_bypassRoute_cb_list,
879                        &parse_S_bypassRoute_new_and_endpoints);
880
881   surfxml_add_callback(ETag_surfxml_link_ctn_cb_list,
882                        &parse_E_link_ctn_new_elem_XML);
883
884   surfxml_add_callback(ETag_surfxml_route_cb_list,
885                        &parse_E_route_store_route);
886   surfxml_add_callback(ETag_surfxml_ASroute_cb_list,
887                        &parse_E_ASroute_store_route);
888   surfxml_add_callback(ETag_surfxml_bypassRoute_cb_list,
889                        &parse_E_bypassRoute_store_route);
890
891   surfxml_add_callback(STag_surfxml_AS_cb_list, &parse_S_AS_XML);
892   surfxml_add_callback(ETag_surfxml_AS_cb_list, &parse_E_AS_XML);
893
894   surfxml_add_callback(STag_surfxml_cluster_cb_list,
895                        &routing_full_parse_Scluster);
896
897   surfxml_add_callback(STag_surfxml_config_cb_list, &parse_Sconfig);
898 }
899
900 /* ************************************************************************** */
901 /* *************************** FULL ROUTING ********************************* */
902
903 #define TO_ROUTE_FULL(i,j) routing->routing_table[(i)+(j)*table_size]
904
905 /* Routing model structure */
906
907 typedef struct {
908   s_routing_component_t generic_routing;
909   xbt_dict_t parse_routes;      /* store data during the parse process */
910   xbt_dict_t bypassRoutes;
911   route_extended_t *routing_table;
912 } s_routing_component_full_t, *routing_component_full_t;
913
914 /* Business methods */
915 static xbt_dynar_t full_get_onelink_routes(routing_component_t rc)
916 {
917   xbt_dynar_t ret = xbt_dynar_new(sizeof(onelink_t), xbt_free);
918
919   routing_component_full_t routing = (routing_component_full_t) rc;
920   int table_size = xbt_dict_length(routing->generic_routing.to_index);
921   xbt_dict_cursor_t c1 = NULL, c2 = NULL;
922   char *k1, *d1, *k2, *d2;
923   xbt_dict_foreach(routing->generic_routing.to_index, c1, k1, d1) {
924     xbt_dict_foreach(routing->generic_routing.to_index, c2, k2, d2) {
925       int *src_id = xbt_dict_get_or_null(routing->generic_routing.to_index, k1);
926       int *dst_id = xbt_dict_get_or_null(routing->generic_routing.to_index, k2);
927       xbt_assert2(src_id
928                   && dst_id,
929                   "Ask for route \"from\"(%s)  or \"to\"(%s) no found in the local table",
930                   src, dst);
931       route_extended_t route = TO_ROUTE_FULL(*src_id, *dst_id);
932       if (route) {
933         if (xbt_dynar_length(route->generic_route.link_list) == 1) {
934           void *link =
935               *(void **) xbt_dynar_get_ptr(route->generic_route.link_list,
936                                            0);
937           onelink_t onelink = xbt_new0(s_onelink_t, 1);
938           onelink->link_ptr = link;
939           if (routing->generic_routing.hierarchy == SURF_ROUTING_BASE) {
940             onelink->src = xbt_strdup(k1);
941             onelink->dst = xbt_strdup(k2);
942           } else if (routing->generic_routing.hierarchy ==
943                      SURF_ROUTING_RECURSIVE) {
944             onelink->src = xbt_strdup(route->src_gateway);
945             onelink->dst = xbt_strdup(route->dst_gateway);
946           }
947           xbt_dynar_push(ret, &onelink);
948         }
949       }
950     }
951   }
952   return ret;
953 }
954
955 static route_extended_t full_get_route(routing_component_t rc,
956                                        const char *src, const char *dst)
957 {
958   xbt_assert1(rc && src
959               && dst,
960               "Invalid params for \"get_route\" function at AS \"%s\"",
961               rc->name);
962
963   /* set utils vars */
964   routing_component_full_t routing = (routing_component_full_t) rc;
965   int table_size = xbt_dict_length(routing->generic_routing.to_index);
966
967   generic_src_dst_check(rc, src, dst);
968   int *src_id = xbt_dict_get_or_null(routing->generic_routing.to_index, src);
969   int *dst_id = xbt_dict_get_or_null(routing->generic_routing.to_index, dst);
970   xbt_assert2(src_id
971               && dst_id,
972               "Ask for route \"from\"(%s)  or \"to\"(%s) no found in the local table",
973               src, dst);
974
975   route_extended_t e_route = NULL;
976   route_extended_t new_e_route = NULL;
977   void *link;
978   unsigned int cpt = 0;
979
980   e_route = TO_ROUTE_FULL(*src_id, *dst_id);
981
982   if (e_route) {
983     new_e_route = xbt_new0(s_route_extended_t, 1);
984     new_e_route->src_gateway = xbt_strdup(e_route->src_gateway);
985     new_e_route->dst_gateway = xbt_strdup(e_route->dst_gateway);
986     new_e_route->generic_route.link_list =
987         xbt_dynar_new(global_routing->size_of_link, NULL);
988     xbt_dynar_foreach(e_route->generic_route.link_list, cpt, link) {
989       xbt_dynar_push(new_e_route->generic_route.link_list, &link);
990     }
991   }
992   return new_e_route;
993 }
994
995 static void full_finalize(routing_component_t rc)
996 {
997   routing_component_full_t routing = (routing_component_full_t) rc;
998   int table_size = xbt_dict_length(routing->generic_routing.to_index);
999   int i, j;
1000   if (routing) {
1001     /* Delete routing table */
1002     for (i = 0; i < table_size; i++)
1003       for (j = 0; j < table_size; j++)
1004         generic_free_extended_route(TO_ROUTE_FULL(i, j));
1005     xbt_free(routing->routing_table);
1006     /* Delete bypass dict */
1007     xbt_dict_free(&routing->bypassRoutes);
1008     /* Delete index dict */
1009     xbt_dict_free(&(routing->generic_routing.to_index));
1010     /* Delete structure */
1011     xbt_free(rc);
1012   }
1013 }
1014
1015 /* Creation routing model functions */
1016
1017 static void *model_full_create(void)
1018 {
1019   routing_component_full_t new_component =
1020       xbt_new0(s_routing_component_full_t, 1);
1021   new_component->generic_routing.set_processing_unit =
1022       generic_set_processing_unit;
1023   new_component->generic_routing.set_autonomous_system =
1024       generic_set_autonomous_system;
1025   new_component->generic_routing.set_route = generic_set_route;
1026   new_component->generic_routing.set_ASroute = generic_set_ASroute;
1027   new_component->generic_routing.set_bypassroute = generic_set_bypassroute;
1028   new_component->generic_routing.get_route = full_get_route;
1029   new_component->generic_routing.get_onelink_routes =
1030       full_get_onelink_routes;
1031   new_component->generic_routing.get_bypass_route =
1032       generic_get_bypassroute;
1033   new_component->generic_routing.finalize = full_finalize;
1034   new_component->generic_routing.to_index = xbt_dict_new();
1035   new_component->bypassRoutes = xbt_dict_new();
1036   new_component->parse_routes = xbt_dict_new();
1037   return new_component;
1038 }
1039
1040 static void model_full_load(void)
1041 {
1042   /* use "surfxml_add_callback" to add a parse function call */
1043 }
1044
1045 static void model_full_unload(void)
1046 {
1047   /* use "surfxml_del_callback" to remove a parse function call */
1048 }
1049
1050 static void model_full_end(void)
1051 {
1052
1053   char *key, *end;
1054   const char *sep = "#";
1055   int src_id, dst_id;
1056   unsigned int i, j;
1057   route_t route;
1058   route_extended_t e_route;
1059   void *data;
1060
1061   xbt_dict_cursor_t cursor = NULL;
1062   xbt_dynar_t keys = NULL;
1063
1064   /* set utils vars */
1065   routing_component_full_t routing =
1066       ((routing_component_full_t) current_routing);
1067   int table_size = xbt_dict_length(routing->generic_routing.to_index);
1068
1069   /* Create the routing table */
1070   routing->routing_table =
1071       xbt_new0(route_extended_t, table_size * table_size);
1072
1073   /* Put the routes in position */
1074   xbt_dict_foreach(routing->parse_routes, cursor, key, data) {
1075     keys = xbt_str_split_str(key, sep);
1076     src_id = strtol(xbt_dynar_get_as(keys, 0, char *), &end, 10);
1077     dst_id = strtol(xbt_dynar_get_as(keys, 1, char *), &end, 10);
1078     TO_ROUTE_FULL(src_id, dst_id) =
1079         generic_new_extended_route(current_routing->hierarchy, data, 1);
1080     xbt_dynar_free(&keys);
1081   }
1082
1083   /* delete the parse table */
1084   xbt_dict_foreach(routing->parse_routes, cursor, key, data) {
1085     route = (route_t) data;
1086     xbt_dynar_free(&(route->link_list));
1087     xbt_free(data);
1088   }
1089
1090   /* delete parse dict */
1091   xbt_dict_free(&(routing->parse_routes));
1092
1093   /* Add the loopback if needed */
1094   if (current_routing->hierarchy == SURF_ROUTING_BASE) {
1095     for (i = 0; i < table_size; i++) {
1096       e_route = TO_ROUTE_FULL(i, i);
1097       if (!e_route) {
1098         e_route = xbt_new0(s_route_extended_t, 1);
1099         e_route->src_gateway = NULL;
1100         e_route->dst_gateway = NULL;
1101         e_route->generic_route.link_list =
1102             xbt_dynar_new(global_routing->size_of_link, NULL);
1103         xbt_dynar_push(e_route->generic_route.link_list,
1104                        &global_routing->loopback);
1105         TO_ROUTE_FULL(i, i) = e_route;
1106       }
1107     }
1108   }
1109
1110   /* Shrink the dynar routes (save unused slots) */
1111   for (i = 0; i < table_size; i++)
1112     for (j = 0; j < table_size; j++)
1113       if (TO_ROUTE_FULL(i, j))
1114         xbt_dynar_shrink(TO_ROUTE_FULL(i, j)->generic_route.link_list, 0);
1115 }
1116
1117 /* ************************************************************************** */
1118 /* *************************** FLOYD ROUTING ******************************** */
1119
1120 #define TO_FLOYD_COST(i,j) cost_table[(i)+(j)*table_size]
1121 #define TO_FLOYD_PRED(i,j) (routing->predecessor_table)[(i)+(j)*table_size]
1122 #define TO_FLOYD_LINK(i,j) (routing->link_table)[(i)+(j)*table_size]
1123
1124 /* Routing model structure */
1125
1126 typedef struct {
1127   s_routing_component_t generic_routing;
1128   /* vars for calculate the floyd algorith. */
1129   int *predecessor_table;
1130   route_extended_t *link_table; /* char* -> int* */
1131   xbt_dict_t bypassRoutes;
1132   /* store data during the parse process */
1133   xbt_dict_t parse_routes;
1134 } s_routing_component_floyd_t, *routing_component_floyd_t;
1135
1136 static route_extended_t floyd_get_route(routing_component_t rc,
1137                                         const char *src, const char *dst);
1138
1139 /* Business methods */
1140 static xbt_dynar_t floyd_get_onelink_routes(routing_component_t rc)
1141 {
1142   xbt_dynar_t ret = xbt_dynar_new(sizeof(onelink_t), xbt_free);
1143
1144   routing_component_floyd_t routing = (routing_component_floyd_t) rc;
1145   //int table_size = xbt_dict_length(routing->generic_routing.to_index);
1146   xbt_dict_cursor_t c1 = NULL, c2 = NULL;
1147   char *k1, *d1, *k2, *d2;
1148   xbt_dict_foreach(routing->generic_routing.to_index, c1, k1, d1) {
1149     xbt_dict_foreach(routing->generic_routing.to_index, c2, k2, d2) {
1150       route_extended_t route = floyd_get_route(rc, k1, k2);
1151       if (route) {
1152         if (xbt_dynar_length(route->generic_route.link_list) == 1) {
1153           void *link =
1154               *(void **) xbt_dynar_get_ptr(route->generic_route.link_list,
1155                                            0);
1156           onelink_t onelink = xbt_new0(s_onelink_t, 1);
1157           onelink->link_ptr = link;
1158           if (routing->generic_routing.hierarchy == SURF_ROUTING_BASE) {
1159             onelink->src = xbt_strdup(k1);
1160             onelink->dst = xbt_strdup(k2);
1161           } else if (routing->generic_routing.hierarchy ==
1162                      SURF_ROUTING_RECURSIVE) {
1163             onelink->src = xbt_strdup(route->src_gateway);
1164             onelink->dst = xbt_strdup(route->dst_gateway);
1165           }
1166           xbt_dynar_push(ret, &onelink);
1167         }
1168       }
1169     }
1170   }
1171   return ret;
1172 }
1173
1174 static route_extended_t floyd_get_route(routing_component_t rc,
1175                                         const char *src, const char *dst)
1176 {
1177   xbt_assert1(rc && src
1178               && dst,
1179               "Invalid params for \"get_route\" function at AS \"%s\"",
1180               rc->name);
1181
1182   /* set utils vars */
1183   routing_component_floyd_t routing = (routing_component_floyd_t) rc;
1184   int table_size = xbt_dict_length(routing->generic_routing.to_index);
1185
1186   generic_src_dst_check(rc, src, dst);
1187   int *src_id = xbt_dict_get_or_null(routing->generic_routing.to_index, src);
1188   int *dst_id = xbt_dict_get_or_null(routing->generic_routing.to_index, dst);
1189   xbt_assert2(src_id
1190               && dst_id,
1191               "Ask for route \"from\"(%s)  or \"to\"(%s) no found in the local table",
1192               src, dst);
1193
1194   /* create a result route */
1195   route_extended_t new_e_route = xbt_new0(s_route_extended_t, 1);
1196   new_e_route->generic_route.link_list =
1197       xbt_dynar_new(global_routing->size_of_link, NULL);
1198   new_e_route->src_gateway = NULL;
1199   new_e_route->dst_gateway = NULL;
1200
1201   int first = 1;
1202   int pred = *dst_id;
1203   int prev_pred = 0;
1204   char *gw_src = NULL, *gw_dst =
1205       NULL, *prev_gw_src, *prev_gw_dst, *first_gw = NULL;
1206   unsigned int cpt;
1207   void *link;
1208   xbt_dynar_t links;
1209
1210   do {
1211     prev_pred = pred;
1212     pred = TO_FLOYD_PRED(*src_id, pred);
1213     if (pred == -1)             /* if no pred in route -> no route to host */
1214       break;
1215     xbt_assert2(TO_FLOYD_LINK(pred, prev_pred),
1216                 "Invalid link for the route between \"%s\" or \"%s\"", src,
1217                 dst);
1218
1219     prev_gw_src = gw_src;
1220     prev_gw_dst = gw_dst;
1221
1222     route_extended_t e_route = TO_FLOYD_LINK(pred, prev_pred);
1223     gw_src = e_route->src_gateway;
1224     gw_dst = e_route->dst_gateway;
1225
1226     if (first)
1227       first_gw = gw_dst;
1228
1229     if (rc->hierarchy == SURF_ROUTING_RECURSIVE && !first
1230         && strcmp(gw_dst, prev_gw_src)) {
1231       xbt_dynar_t e_route_as_to_as =
1232           (*(global_routing->get_route)) (gw_dst, prev_gw_src);
1233       xbt_assert2(e_route_as_to_as, "no route between \"%s\" and \"%s\"",
1234                   gw_dst, prev_gw_src);
1235       links = e_route_as_to_as;
1236       int pos = 0;
1237       xbt_dynar_foreach(links, cpt, link) {
1238         xbt_dynar_insert_at(new_e_route->generic_route.link_list, pos,
1239                             &link);
1240         pos++;
1241       }
1242     }
1243
1244     links = e_route->generic_route.link_list;
1245     xbt_dynar_foreach(links, cpt, link) {
1246       xbt_dynar_unshift(new_e_route->generic_route.link_list, &link);
1247     }
1248     first = 0;
1249
1250   } while (pred != *src_id);
1251   xbt_assert4(pred != -1, "no route from host %d to %d (\"%s\" to \"%s\")",
1252               *src_id, *dst_id, src, dst);
1253
1254   if (rc->hierarchy == SURF_ROUTING_RECURSIVE) {
1255     new_e_route->src_gateway = xbt_strdup(gw_src);
1256     new_e_route->dst_gateway = xbt_strdup(first_gw);
1257   }
1258
1259   return new_e_route;
1260 }
1261
1262 static void floyd_finalize(routing_component_t rc)
1263 {
1264   routing_component_floyd_t routing = (routing_component_floyd_t) rc;
1265   int i, j, table_size;
1266   if (routing) {
1267     table_size = xbt_dict_length(routing->generic_routing.to_index);
1268     /* Delete link_table */
1269     for (i = 0; i < table_size; i++)
1270       for (j = 0; j < table_size; j++)
1271         generic_free_extended_route(TO_FLOYD_LINK(i, j));
1272     xbt_free(routing->link_table);
1273     /* Delete bypass dict */
1274     xbt_dict_free(&routing->bypassRoutes);
1275     /* Delete index dict */
1276     xbt_dict_free(&(routing->generic_routing.to_index));
1277     /* Delete dictionary index dict, predecessor and links table */
1278     xbt_free(routing->predecessor_table);
1279     /* Delete structure */
1280     xbt_free(rc);
1281   }
1282 }
1283
1284 static void *model_floyd_create(void)
1285 {
1286   routing_component_floyd_t new_component =
1287       xbt_new0(s_routing_component_floyd_t, 1);
1288   new_component->generic_routing.set_processing_unit =
1289       generic_set_processing_unit;
1290   new_component->generic_routing.set_autonomous_system =
1291       generic_set_autonomous_system;
1292   new_component->generic_routing.set_route = generic_set_route;
1293   new_component->generic_routing.set_ASroute = generic_set_ASroute;
1294   new_component->generic_routing.set_bypassroute = generic_set_bypassroute;
1295   new_component->generic_routing.get_route = floyd_get_route;
1296   new_component->generic_routing.get_onelink_routes =
1297       floyd_get_onelink_routes;
1298   new_component->generic_routing.get_bypass_route =
1299       generic_get_bypassroute;
1300   new_component->generic_routing.finalize = floyd_finalize;
1301   new_component->generic_routing.to_index = xbt_dict_new();
1302   new_component->bypassRoutes = xbt_dict_new();
1303   new_component->parse_routes = xbt_dict_new();
1304   return new_component;
1305 }
1306
1307 static void model_floyd_load(void)
1308 {
1309   /* use "surfxml_add_callback" to add a parse function call */
1310 }
1311
1312 static void model_floyd_unload(void)
1313 {
1314   /* use "surfxml_del_callback" to remove a parse function call */
1315 }
1316
1317 static void model_floyd_end(void)
1318 {
1319
1320   routing_component_floyd_t routing =
1321       ((routing_component_floyd_t) current_routing);
1322   xbt_dict_cursor_t cursor = NULL;
1323   double *cost_table;
1324   char *key, *data, *end;
1325   const char *sep = "#";
1326   xbt_dynar_t keys;
1327   int src_id, dst_id;
1328   unsigned int i, j, a, b, c;
1329
1330   /* set the size of inicial table */
1331   int table_size = xbt_dict_length(routing->generic_routing.to_index);
1332
1333   /* Create Cost, Predecessor and Link tables */
1334   cost_table = xbt_new0(double, table_size * table_size);       /* link cost from host to host */
1335   routing->predecessor_table = xbt_new0(int, table_size * table_size);  /* predecessor host numbers */
1336   routing->link_table = xbt_new0(route_extended_t, table_size * table_size);    /* actual link between src and dst */
1337
1338   /* Initialize costs and predecessors */
1339   for (i = 0; i < table_size; i++)
1340     for (j = 0; j < table_size; j++) {
1341       TO_FLOYD_COST(i, j) = DBL_MAX;
1342       TO_FLOYD_PRED(i, j) = -1;
1343       TO_FLOYD_LINK(i, j) = NULL;       /* fixed, missing in the previous version */
1344     }
1345
1346   /* Put the routes in position */
1347   xbt_dict_foreach(routing->parse_routes, cursor, key, data) {
1348     keys = xbt_str_split_str(key, sep);
1349     src_id = strtol(xbt_dynar_get_as(keys, 0, char *), &end, 10);
1350     dst_id = strtol(xbt_dynar_get_as(keys, 1, char *), &end, 10);
1351     TO_FLOYD_LINK(src_id, dst_id) =
1352         generic_new_extended_route(current_routing->hierarchy, data, 0);
1353     TO_FLOYD_PRED(src_id, dst_id) = src_id;
1354     /* set link cost */
1355     TO_FLOYD_COST(src_id, dst_id) = ((TO_FLOYD_LINK(src_id, dst_id))->generic_route.link_list)->used;   /* count of links, old model assume 1 */
1356     xbt_dynar_free(&keys);
1357   }
1358
1359   /* Add the loopback if needed */
1360   if (current_routing->hierarchy == SURF_ROUTING_BASE) {
1361     for (i = 0; i < table_size; i++) {
1362       route_extended_t e_route = TO_FLOYD_LINK(i, i);
1363       if (!e_route) {
1364         e_route = xbt_new0(s_route_extended_t, 1);
1365         e_route->src_gateway = NULL;
1366         e_route->dst_gateway = NULL;
1367         e_route->generic_route.link_list =
1368             xbt_dynar_new(global_routing->size_of_link, NULL);
1369         xbt_dynar_push(e_route->generic_route.link_list,
1370                        &global_routing->loopback);
1371         TO_FLOYD_LINK(i, i) = e_route;
1372         TO_FLOYD_PRED(i, i) = i;
1373         TO_FLOYD_COST(i, i) = 1;
1374       }
1375     }
1376   }
1377   /* Calculate path costs */
1378   for (c = 0; c < table_size; c++) {
1379     for (a = 0; a < table_size; a++) {
1380       for (b = 0; b < table_size; b++) {
1381         if (TO_FLOYD_COST(a, c) < DBL_MAX && TO_FLOYD_COST(c, b) < DBL_MAX) {
1382           if (TO_FLOYD_COST(a, b) == DBL_MAX ||
1383               (TO_FLOYD_COST(a, c) + TO_FLOYD_COST(c, b) <
1384                TO_FLOYD_COST(a, b))) {
1385             TO_FLOYD_COST(a, b) =
1386                 TO_FLOYD_COST(a, c) + TO_FLOYD_COST(c, b);
1387             TO_FLOYD_PRED(a, b) = TO_FLOYD_PRED(c, b);
1388           }
1389         }
1390       }
1391     }
1392   }
1393
1394   /* delete the parse table */
1395   xbt_dict_foreach(routing->parse_routes, cursor, key, data) {
1396     route_t route = (route_t) data;
1397     xbt_dynar_free(&(route->link_list));
1398     xbt_free(data);
1399   }
1400
1401   /* delete parse dict */
1402   xbt_dict_free(&(routing->parse_routes));
1403
1404   /* cleanup */
1405   xbt_free(cost_table);
1406 }
1407
1408 /* ************************************************************************** */
1409 /* ********** Dijkstra & Dijkstra Cached ROUTING **************************** */
1410
1411 typedef struct {
1412   s_routing_component_t generic_routing;
1413   xbt_dict_t bypassRoutes;
1414   xbt_graph_t route_graph;      /* xbt_graph */
1415   xbt_dict_t graph_node_map;    /* map */
1416   xbt_dict_t route_cache;       /* use in cache mode */
1417   int cached;
1418   xbt_dict_t parse_routes;
1419 } s_routing_component_dijkstra_t, *routing_component_dijkstra_t;
1420
1421
1422 typedef struct graph_node_data {
1423   int id;
1424   int graph_id;                 /* used for caching internal graph id's */
1425 } s_graph_node_data_t, *graph_node_data_t;
1426
1427 typedef struct graph_node_map_element {
1428   xbt_node_t node;
1429 } s_graph_node_map_element_t, *graph_node_map_element_t;
1430
1431 typedef struct route_cache_element {
1432   int *pred_arr;
1433   int size;
1434 } s_route_cache_element_t, *route_cache_element_t;
1435
1436 /* Free functions */
1437
1438 static void route_cache_elem_free(void *e)
1439 {
1440   route_cache_element_t elm = (route_cache_element_t) e;
1441   if (elm) {
1442     xbt_free(elm->pred_arr);
1443     xbt_free(elm);
1444   }
1445 }
1446
1447 static void graph_node_map_elem_free(void *e)
1448 {
1449   graph_node_map_element_t elm = (graph_node_map_element_t) e;
1450   if (elm) {
1451     xbt_free(elm);
1452   }
1453 }
1454
1455 static void graph_edge_data_free(void *e)
1456 {
1457   route_extended_t e_route = (route_extended_t) e;
1458   if (e_route) {
1459     xbt_dynar_free(&(e_route->generic_route.link_list));
1460     if (e_route->src_gateway)
1461       xbt_free(e_route->src_gateway);
1462     if (e_route->dst_gateway)
1463       xbt_free(e_route->dst_gateway);
1464     xbt_free(e_route);
1465   }
1466 }
1467
1468 /* Utility functions */
1469
1470 static xbt_node_t route_graph_new_node(routing_component_dijkstra_t rc,
1471                                        int id, int graph_id)
1472 {
1473   routing_component_dijkstra_t routing = (routing_component_dijkstra_t) rc;
1474   xbt_node_t node = NULL;
1475   graph_node_data_t data = NULL;
1476   graph_node_map_element_t elm = NULL;
1477
1478   data = xbt_new0(struct graph_node_data, 1);
1479   data->id = id;
1480   data->graph_id = graph_id;
1481   node = xbt_graph_new_node(routing->route_graph, data);
1482
1483   elm = xbt_new0(struct graph_node_map_element, 1);
1484   elm->node = node;
1485   xbt_dict_set_ext(routing->graph_node_map, (char *) (&id), sizeof(int),
1486                    (xbt_set_elm_t) elm, &graph_node_map_elem_free);
1487
1488   return node;
1489 }
1490
1491 static graph_node_map_element_t
1492 graph_node_map_search(routing_component_dijkstra_t rc, int id)
1493 {
1494   routing_component_dijkstra_t routing = (routing_component_dijkstra_t) rc;
1495   graph_node_map_element_t elm = (graph_node_map_element_t)
1496       xbt_dict_get_or_null_ext(routing->graph_node_map,
1497                                (char *) (&id),
1498                                sizeof(int));
1499   return elm;
1500 }
1501
1502 /* Parsing */
1503
1504 static void route_new_dijkstra(routing_component_dijkstra_t rc, int src_id,
1505                                int dst_id, route_extended_t e_route)
1506 {
1507   routing_component_dijkstra_t routing = (routing_component_dijkstra_t) rc;
1508
1509   xbt_node_t src = NULL;
1510   xbt_node_t dst = NULL;
1511   graph_node_map_element_t src_elm = (graph_node_map_element_t)
1512       xbt_dict_get_or_null_ext(routing->graph_node_map,
1513                                (char *) (&src_id),
1514                                sizeof(int));
1515   graph_node_map_element_t dst_elm = (graph_node_map_element_t)
1516       xbt_dict_get_or_null_ext(routing->graph_node_map,
1517                                (char *) (&dst_id),
1518                                sizeof(int));
1519
1520   if (src_elm)
1521     src = src_elm->node;
1522
1523   if (dst_elm)
1524     dst = dst_elm->node;
1525
1526   /* add nodes if they don't exist in the graph */
1527   if (src_id == dst_id && src == NULL && dst == NULL) {
1528     src = route_graph_new_node(rc, src_id, -1);
1529     dst = src;
1530   } else {
1531     if (src == NULL) {
1532       src = route_graph_new_node(rc, src_id, -1);
1533     }
1534     if (dst == NULL) {
1535       dst = route_graph_new_node(rc, dst_id, -1);
1536     }
1537   }
1538
1539   /* add link as edge to graph */
1540   xbt_graph_new_edge(routing->route_graph, src, dst, e_route);
1541 }
1542
1543 static void add_loopback_dijkstra(routing_component_dijkstra_t rc)
1544 {
1545   routing_component_dijkstra_t routing = (routing_component_dijkstra_t) rc;
1546
1547   xbt_dynar_t nodes = xbt_graph_get_nodes(routing->route_graph);
1548
1549   xbt_node_t node = NULL;
1550   unsigned int cursor2;
1551   xbt_dynar_foreach(nodes, cursor2, node) {
1552     xbt_dynar_t out_edges = xbt_graph_node_get_outedges(node);
1553     xbt_edge_t edge = NULL;
1554     unsigned int cursor;
1555
1556     int found = 0;
1557     xbt_dynar_foreach(out_edges, cursor, edge) {
1558       xbt_node_t other_node = xbt_graph_edge_get_target(edge);
1559       if (other_node == node) {
1560         found = 1;
1561         break;
1562       }
1563     }
1564
1565     if (!found) {
1566       route_extended_t e_route = xbt_new0(s_route_extended_t, 1);
1567       e_route->src_gateway = NULL;
1568       e_route->dst_gateway = NULL;
1569       e_route->generic_route.link_list =
1570           xbt_dynar_new(global_routing->size_of_link, NULL);
1571       xbt_dynar_push(e_route->generic_route.link_list,
1572                      &global_routing->loopback);
1573       xbt_graph_new_edge(routing->route_graph, node, node, e_route);
1574     }
1575   }
1576 }
1577
1578 /* Business methods */
1579 static xbt_dynar_t dijkstra_get_onelink_routes(routing_component_t rc)
1580 {
1581   xbt_die("\"dijkstra_get_onelink_routes\" function not implemented yet");
1582 }
1583
1584 static route_extended_t dijkstra_get_route(routing_component_t rc,
1585                                            const char *src,
1586                                            const char *dst)
1587 {
1588   xbt_assert1(rc && src
1589               && dst,
1590               "Invalid params for \"get_route\" function at AS \"%s\"",
1591               rc->name);
1592
1593   /* set utils vars */
1594   routing_component_dijkstra_t routing = (routing_component_dijkstra_t) rc;
1595
1596   generic_src_dst_check(rc, src, dst);
1597   int *src_id = xbt_dict_get_or_null(routing->generic_routing.to_index, src);
1598   int *dst_id = xbt_dict_get_or_null(routing->generic_routing.to_index, dst);
1599   xbt_assert2(src_id
1600               && dst_id,
1601               "Ask for route \"from\"(%s)  or \"to\"(%s) no found in the local table",
1602               src, dst);
1603
1604   /* create a result route */
1605   route_extended_t new_e_route = xbt_new0(s_route_extended_t, 1);
1606   new_e_route->generic_route.link_list =
1607       xbt_dynar_new(global_routing->size_of_link, NULL);
1608   new_e_route->src_gateway = NULL;
1609   new_e_route->dst_gateway = NULL;
1610
1611   int *pred_arr = NULL;
1612   int src_node_id = 0;
1613   int dst_node_id = 0;
1614   int *nodeid = NULL;
1615   int v;
1616   route_extended_t e_route;
1617   int size = 0;
1618   unsigned int cpt;
1619   void *link;
1620   xbt_dynar_t links = NULL;
1621   route_cache_element_t elm = NULL;
1622   xbt_dynar_t nodes = xbt_graph_get_nodes(routing->route_graph);
1623
1624   /* Use the graph_node id mapping set to quickly find the nodes */
1625   graph_node_map_element_t src_elm =
1626       graph_node_map_search(routing, *src_id);
1627   graph_node_map_element_t dst_elm =
1628       graph_node_map_search(routing, *dst_id);
1629   xbt_assert2(src_elm != NULL
1630               && dst_elm != NULL, "src %d or dst %d does not exist",
1631               *src_id, *dst_id);
1632   src_node_id = ((graph_node_data_t)
1633                  xbt_graph_node_get_data(src_elm->node))->graph_id;
1634   dst_node_id = ((graph_node_data_t)
1635                  xbt_graph_node_get_data(dst_elm->node))->graph_id;
1636
1637   /* if the src and dst are the same *//* fixed, missing in the previous version */
1638   if (src_node_id == dst_node_id) {
1639
1640     xbt_node_t node_s_v = xbt_dynar_get_as(nodes, src_node_id, xbt_node_t);
1641     xbt_node_t node_e_v = xbt_dynar_get_as(nodes, dst_node_id, xbt_node_t);
1642     xbt_edge_t edge =
1643         xbt_graph_get_edge(routing->route_graph, node_s_v, node_e_v);
1644
1645     xbt_assert2(edge != NULL, "no route between host %d and %d", *src_id,
1646                 *dst_id);
1647
1648     e_route = (route_extended_t) xbt_graph_edge_get_data(edge);
1649
1650     links = e_route->generic_route.link_list;
1651     xbt_dynar_foreach(links, cpt, link) {
1652       xbt_dynar_unshift(new_e_route->generic_route.link_list, &link);
1653     }
1654
1655     return new_e_route;
1656   }
1657
1658   if (routing->cached) {
1659     /*check if there is a cached predecessor list avail */
1660     elm = (route_cache_element_t)
1661         xbt_dict_get_or_null_ext(routing->route_cache, (char *) (&src_id),
1662                                  sizeof(int));
1663   }
1664
1665   if (elm) {                    /* cached mode and cache hit */
1666     pred_arr = elm->pred_arr;
1667   } else {                      /* not cached mode or cache miss */
1668     double *cost_arr = NULL;
1669     xbt_heap_t pqueue = NULL;
1670     int i = 0;
1671
1672     int nr_nodes = xbt_dynar_length(nodes);
1673     cost_arr = xbt_new0(double, nr_nodes);      /* link cost from src to other hosts */
1674     pred_arr = xbt_new0(int, nr_nodes); /* predecessors in path from src */
1675     pqueue = xbt_heap_new(nr_nodes, xbt_free);
1676
1677     /* initialize */
1678     cost_arr[src_node_id] = 0.0;
1679
1680     for (i = 0; i < nr_nodes; i++) {
1681       if (i != src_node_id) {
1682         cost_arr[i] = DBL_MAX;
1683       }
1684
1685       pred_arr[i] = 0;
1686
1687       /* initialize priority queue */
1688       nodeid = xbt_new0(int, 1);
1689       *nodeid = i;
1690       xbt_heap_push(pqueue, nodeid, cost_arr[i]);
1691
1692     }
1693
1694     /* apply dijkstra using the indexes from the graph's node array */
1695     while (xbt_heap_size(pqueue) > 0) {
1696       int *v_id = xbt_heap_pop(pqueue);
1697       xbt_node_t v_node = xbt_dynar_get_as(nodes, *v_id, xbt_node_t);
1698       xbt_dynar_t out_edges = xbt_graph_node_get_outedges(v_node);
1699       xbt_edge_t edge = NULL;
1700       unsigned int cursor;
1701
1702       xbt_dynar_foreach(out_edges, cursor, edge) {
1703         xbt_node_t u_node = xbt_graph_edge_get_target(edge);
1704         graph_node_data_t data = xbt_graph_node_get_data(u_node);
1705         int u_id = data->graph_id;
1706         route_extended_t tmp_e_route =
1707             (route_extended_t) xbt_graph_edge_get_data(edge);
1708         int cost_v_u = (tmp_e_route->generic_route.link_list)->used;    /* count of links, old model assume 1 */
1709
1710         if (cost_v_u + cost_arr[*v_id] < cost_arr[u_id]) {
1711           pred_arr[u_id] = *v_id;
1712           cost_arr[u_id] = cost_v_u + cost_arr[*v_id];
1713           nodeid = xbt_new0(int, 1);
1714           *nodeid = u_id;
1715           xbt_heap_push(pqueue, nodeid, cost_arr[u_id]);
1716         }
1717       }
1718
1719       /* free item popped from pqueue */
1720       xbt_free(v_id);
1721     }
1722
1723     xbt_free(cost_arr);
1724     xbt_heap_free(pqueue);
1725   }
1726
1727   /* compose route path with links */
1728   char *gw_src = NULL, *gw_dst =
1729       NULL, *prev_gw_src, *prev_gw_dst, *first_gw = NULL;
1730
1731   for (v = dst_node_id; v != src_node_id; v = pred_arr[v]) {
1732     xbt_node_t node_pred_v =
1733         xbt_dynar_get_as(nodes, pred_arr[v], xbt_node_t);
1734     xbt_node_t node_v = xbt_dynar_get_as(nodes, v, xbt_node_t);
1735     xbt_edge_t edge =
1736         xbt_graph_get_edge(routing->route_graph, node_pred_v, node_v);
1737
1738     xbt_assert2(edge != NULL, "no route between host %d and %d", *src_id,
1739                 *dst_id);
1740
1741     prev_gw_src = gw_src;
1742     prev_gw_dst = gw_dst;
1743
1744     e_route = (route_extended_t) xbt_graph_edge_get_data(edge);
1745     gw_src = e_route->src_gateway;
1746     gw_dst = e_route->dst_gateway;
1747
1748     if (v == dst_node_id)
1749       first_gw = gw_dst;
1750
1751     if (rc->hierarchy == SURF_ROUTING_RECURSIVE && v != dst_node_id
1752         && strcmp(gw_dst, prev_gw_src)) {
1753       xbt_dynar_t e_route_as_to_as =
1754           (*(global_routing->get_route)) (gw_dst, prev_gw_src);
1755       xbt_assert2(e_route_as_to_as, "no route between \"%s\" and \"%s\"",
1756                   gw_dst, prev_gw_src);
1757       links = e_route_as_to_as;
1758       int pos = 0;
1759       xbt_dynar_foreach(links, cpt, link) {
1760         xbt_dynar_insert_at(new_e_route->generic_route.link_list, pos,
1761                             &link);
1762         pos++;
1763       }
1764     }
1765
1766     links = e_route->generic_route.link_list;
1767     xbt_dynar_foreach(links, cpt, link) {
1768       xbt_dynar_unshift(new_e_route->generic_route.link_list, &link);
1769     }
1770     size++;
1771   }
1772
1773   if (rc->hierarchy == SURF_ROUTING_RECURSIVE) {
1774     new_e_route->src_gateway = xbt_strdup(gw_src);
1775     new_e_route->dst_gateway = xbt_strdup(first_gw);
1776   }
1777
1778   if (routing->cached && elm == NULL) {
1779     /* add to predecessor list of the current src-host to cache */
1780     elm = xbt_new0(struct route_cache_element, 1);
1781     elm->pred_arr = pred_arr;
1782     elm->size = size;
1783     xbt_dict_set_ext(routing->route_cache, (char *) (&src_id), sizeof(int),
1784                      (xbt_set_elm_t) elm, &route_cache_elem_free);
1785   }
1786
1787   if (!routing->cached)
1788     xbt_free(pred_arr);
1789
1790   return new_e_route;
1791 }
1792
1793 static void dijkstra_finalize(routing_component_t rc)
1794 {
1795   routing_component_dijkstra_t routing = (routing_component_dijkstra_t) rc;
1796
1797   if (routing) {
1798     xbt_graph_free_graph(routing->route_graph, &xbt_free,
1799                          &graph_edge_data_free, &xbt_free);
1800     xbt_dict_free(&routing->graph_node_map);
1801     if (routing->cached)
1802       xbt_dict_free(&routing->route_cache);
1803     /* Delete bypass dict */
1804     xbt_dict_free(&routing->bypassRoutes);
1805     /* Delete index dict */
1806     xbt_dict_free(&(routing->generic_routing.to_index));
1807     /* Delete structure */
1808     xbt_free(routing);
1809   }
1810 }
1811
1812 /* Creation routing model functions */
1813
1814 static void *model_dijkstra_both_create(int cached)
1815 {
1816   routing_component_dijkstra_t new_component =
1817       xbt_new0(s_routing_component_dijkstra_t, 1);
1818   new_component->generic_routing.set_processing_unit =
1819       generic_set_processing_unit;
1820   new_component->generic_routing.set_autonomous_system =
1821       generic_set_autonomous_system;
1822   new_component->generic_routing.set_route = generic_set_route;
1823   new_component->generic_routing.set_ASroute = generic_set_ASroute;
1824   new_component->generic_routing.set_bypassroute = generic_set_bypassroute;
1825   new_component->generic_routing.get_route = dijkstra_get_route;
1826   new_component->generic_routing.get_onelink_routes =
1827       dijkstra_get_onelink_routes;
1828   new_component->generic_routing.get_bypass_route =
1829       generic_get_bypassroute;
1830   new_component->generic_routing.finalize = dijkstra_finalize;
1831   new_component->cached = cached;
1832   new_component->generic_routing.to_index = xbt_dict_new();
1833   new_component->bypassRoutes = xbt_dict_new();
1834   new_component->parse_routes = xbt_dict_new();
1835   return new_component;
1836 }
1837
1838 static void *model_dijkstra_create(void)
1839 {
1840   return model_dijkstra_both_create(0);
1841 }
1842
1843 static void *model_dijkstracache_create(void)
1844 {
1845   return model_dijkstra_both_create(1);
1846 }
1847
1848 static void model_dijkstra_both_load(void)
1849 {
1850   /* use "surfxml_add_callback" to add a parse function call */
1851 }
1852
1853 static void model_dijkstra_both_unload(void)
1854 {
1855   /* use "surfxml_del_callback" to remove a parse function call */
1856 }
1857
1858 static void model_dijkstra_both_end(void)
1859 {
1860   routing_component_dijkstra_t routing =
1861       (routing_component_dijkstra_t) current_routing;
1862   xbt_dict_cursor_t cursor = NULL;
1863   char *key, *data, *end;
1864   const char *sep = "#";
1865   xbt_dynar_t keys;
1866   xbt_node_t node = NULL;
1867   unsigned int cursor2;
1868   xbt_dynar_t nodes = NULL;
1869   int src_id, dst_id;
1870   route_t route;
1871
1872   /* Create the topology graph */
1873   routing->route_graph = xbt_graph_new_graph(1, NULL);
1874   routing->graph_node_map = xbt_dict_new();
1875
1876   if (routing->cached)
1877     routing->route_cache = xbt_dict_new();
1878
1879   /* Put the routes in position */
1880   xbt_dict_foreach(routing->parse_routes, cursor, key, data) {
1881     keys = xbt_str_split_str(key, sep);
1882     src_id = strtol(xbt_dynar_get_as(keys, 0, char *), &end, 10);
1883     dst_id = strtol(xbt_dynar_get_as(keys, 1, char *), &end, 10);
1884     route_extended_t e_route =
1885         generic_new_extended_route(current_routing->hierarchy, data, 0);
1886     route_new_dijkstra(routing, src_id, dst_id, e_route);
1887     xbt_dynar_free(&keys);
1888   }
1889
1890   /* delete the parse table */
1891   xbt_dict_foreach(routing->parse_routes, cursor, key, data) {
1892     route = (route_t) data;
1893     xbt_dynar_free(&(route->link_list));
1894     xbt_free(data);
1895   }
1896
1897   /* delete parse dict */
1898   xbt_dict_free(&(routing->parse_routes));
1899
1900   /* Add the loopback if needed */
1901   if (current_routing->hierarchy == SURF_ROUTING_BASE)
1902     add_loopback_dijkstra(routing);
1903
1904   /* initialize graph indexes in nodes after graph has been built */
1905   nodes = xbt_graph_get_nodes(routing->route_graph);
1906
1907   xbt_dynar_foreach(nodes, cursor2, node) {
1908     graph_node_data_t data = xbt_graph_node_get_data(node);
1909     data->graph_id = cursor2;
1910   }
1911
1912 }
1913
1914 #ifdef HAVE_PCRE_LIB
1915 /* ************************************************** */
1916 /* ************** RULE-BASED ROUTING **************** */
1917
1918 /* Routing model structure */
1919
1920 typedef struct {
1921   s_routing_component_t generic_routing;
1922   xbt_dict_t dict_processing_units;
1923   xbt_dict_t dict_autonomous_systems;
1924   xbt_dynar_t list_route;
1925   xbt_dynar_t list_ASroute;
1926 } s_routing_component_rulebased_t, *routing_component_rulebased_t;
1927
1928 typedef struct s_rule_route s_rule_route_t, *rule_route_t;
1929 typedef struct s_rule_route_extended s_rule_route_extended_t,
1930     *rule_route_extended_t;
1931
1932 struct s_rule_route {
1933   xbt_dynar_t re_str_link;      // dynar of char*
1934   pcre *re_src;
1935   pcre *re_dst;
1936 };
1937
1938 struct s_rule_route_extended {
1939   s_rule_route_t generic_rule_route;
1940   char *re_src_gateway;
1941   char *re_dst_gateway;
1942 };
1943
1944 static void rule_route_free(void *e)
1945 {
1946   rule_route_t *elem = (rule_route_t *) (e);
1947   if (*elem) {
1948     xbt_dynar_free(&(*elem)->re_str_link);
1949     pcre_free((*elem)->re_src);
1950     pcre_free((*elem)->re_dst);
1951     xbt_free((*elem));
1952   }
1953   (*elem) = NULL;
1954 }
1955
1956 static void rule_route_extended_free(void *e)
1957 {
1958   rule_route_extended_t *elem = (rule_route_extended_t *) e;
1959   if (*elem) {
1960     xbt_dynar_free(&(*elem)->generic_rule_route.re_str_link);
1961     pcre_free((*elem)->generic_rule_route.re_src);
1962     pcre_free((*elem)->generic_rule_route.re_dst);
1963     xbt_free((*elem)->re_src_gateway);
1964     xbt_free((*elem)->re_dst_gateway);
1965     xbt_free((*elem));
1966   }
1967 }
1968
1969 /* Parse routing model functions */
1970
1971 static void model_rulebased_set_processing_unit(routing_component_t rc,
1972                                                 const char *name)
1973 {
1974   routing_component_rulebased_t routing =
1975       (routing_component_rulebased_t) rc;
1976   xbt_dict_set(routing->dict_processing_units, name, (void *) (-1), NULL);
1977 }
1978
1979 static void model_rulebased_set_autonomous_system(routing_component_t rc,
1980                                                   const char *name)
1981 {
1982   routing_component_rulebased_t routing =
1983       (routing_component_rulebased_t) rc;
1984   xbt_dict_set(routing->dict_autonomous_systems, name, (void *) (-1),
1985                NULL);
1986 }
1987
1988 static void model_rulebased_set_route(routing_component_t rc,
1989                                       const char *src, const char *dst,
1990                                       route_t route)
1991 {
1992   routing_component_rulebased_t routing =
1993       (routing_component_rulebased_t) rc;
1994   rule_route_t ruleroute = xbt_new0(s_rule_route_t, 1);
1995   const char *error;
1996   int erroffset;
1997   ruleroute->re_src = pcre_compile(src, 0, &error, &erroffset, NULL);
1998   xbt_assert3(ruleroute->re_src,
1999               "PCRE compilation failed at offset %d (\"%s\"): %s\n",
2000               erroffset, src, error);
2001   ruleroute->re_dst = pcre_compile(dst, 0, &error, &erroffset, NULL);
2002   xbt_assert3(ruleroute->re_src,
2003               "PCRE compilation failed at offset %d (\"%s\"): %s\n",
2004               erroffset, dst, error);
2005   ruleroute->re_str_link = route->link_list;
2006   xbt_dynar_push(routing->list_route, &ruleroute);
2007   xbt_free(route);
2008 }
2009
2010 static void model_rulebased_set_ASroute(routing_component_t rc,
2011                                         const char *src, const char *dst,
2012                                         route_extended_t route)
2013 {
2014   routing_component_rulebased_t routing =
2015       (routing_component_rulebased_t) rc;
2016   rule_route_extended_t ruleroute_e = xbt_new0(s_rule_route_extended_t, 1);
2017   const char *error;
2018   int erroffset;
2019   ruleroute_e->generic_rule_route.re_src =
2020       pcre_compile(src, 0, &error, &erroffset, NULL);
2021   xbt_assert3(ruleroute_e->generic_rule_route.re_src,
2022               "PCRE compilation failed at offset %d (\"%s\"): %s\n",
2023               erroffset, src, error);
2024   ruleroute_e->generic_rule_route.re_dst =
2025       pcre_compile(dst, 0, &error, &erroffset, NULL);
2026   xbt_assert3(ruleroute_e->generic_rule_route.re_src,
2027               "PCRE compilation failed at offset %d (\"%s\"): %s\n",
2028               erroffset, dst, error);
2029   ruleroute_e->generic_rule_route.re_str_link =
2030       route->generic_route.link_list;
2031   ruleroute_e->re_src_gateway = route->src_gateway;
2032   ruleroute_e->re_dst_gateway = route->dst_gateway;
2033   xbt_dynar_push(routing->list_ASroute, &ruleroute_e);
2034   xbt_free(route->src_gateway);
2035   xbt_free(route->dst_gateway);
2036   xbt_free(route);
2037 }
2038
2039 static void model_rulebased_set_bypassroute(routing_component_t rc,
2040                                             const char *src,
2041                                             const char *dst,
2042                                             route_extended_t e_route)
2043 {
2044   xbt_die("bypass routing not supported for Route-Based model");
2045 }
2046
2047 #define BUFFER_SIZE 4096        /* result buffer size */
2048 #define OVECCOUNT 30            /* should be a multiple of 3 */
2049
2050 static char *remplace(char *value, const char **src_list, int src_size,
2051                       const char **dst_list, int dst_size)
2052 {
2053
2054   char result_result[BUFFER_SIZE];
2055   int i_result_buffer;
2056   int value_length = (int) strlen(value);
2057   int number = 0;
2058
2059   int i = 0;
2060   i_result_buffer = 0;
2061   do {
2062     if (value[i] == '$') {
2063       i++;                      // skip $
2064
2065       // find the number      
2066       int number_length = 0;
2067       while ('0' <= value[i + number_length]
2068              && value[i + number_length] <= '9') {
2069         number_length++;
2070       }
2071       xbt_assert2(number_length != 0,
2072                   "bad string parameter, no number indication, at offset: %d (\"%s\")",
2073                   i, value);
2074
2075       // solve number
2076       number = atoi(value + i);
2077       i = i + number_length;
2078       xbt_assert2(i + 2 < value_length,
2079                   "bad string parameter, too few chars, at offset: %d (\"%s\")",
2080                   i, value);
2081
2082       // solve the indication
2083       const char **param_list;
2084       int param_size;
2085       if (value[i] == 's' && value[i + 1] == 'r' && value[i + 2] == 'c') {
2086         param_list = src_list;
2087         param_size = src_size;
2088       } else if (value[i] == 'd' && value[i + 1] == 's'
2089                  && value[i + 2] == 't') {
2090         param_list = dst_list;
2091         param_size = dst_size;
2092       } else {
2093         xbt_assert2(0,
2094                     "bad string parameter, support only \"src\" and \"dst\", at offset: %d (\"%s\")",
2095                     i, value);
2096       }
2097       i = i + 3;
2098
2099       xbt_assert4(param_size >= number,
2100                   "bad string parameter, not enough length param_size, at offset: %d (\"%s\") %d %d",
2101                   i, value, param_size, number);
2102
2103       const char *param = param_list[number];
2104       int size = strlen(param);
2105       int cp;
2106       for (cp = 0; cp < size; cp++) {
2107         result_result[i_result_buffer] = param[cp];
2108         i_result_buffer++;
2109         if (i_result_buffer >= BUFFER_SIZE)
2110           break;
2111       }
2112     } else {
2113       result_result[i_result_buffer] = value[i];
2114       i_result_buffer++;
2115       i++;                      // next char
2116     }
2117
2118   } while (i < value_length && i_result_buffer < BUFFER_SIZE);
2119
2120   xbt_assert2(i_result_buffer < BUFFER_SIZE,
2121               "solving string \"%s\", small buffer size (%d)", value,
2122               BUFFER_SIZE);
2123   result_result[i_result_buffer] = 0;
2124   return xbt_strdup(result_result);
2125 }
2126
2127 static route_extended_t rulebased_get_route(routing_component_t rc,
2128                                             const char *src,
2129                                             const char *dst);
2130 static xbt_dynar_t rulebased_get_onelink_routes(routing_component_t rc)
2131 {
2132   xbt_dynar_t ret = xbt_dynar_new (sizeof(onelink_t), xbt_free);
2133   routing_component_rulebased_t routing = (routing_component_rulebased_t)rc;
2134
2135   xbt_dict_cursor_t c1 = NULL;
2136   char *k1, *d1;
2137
2138   //find router
2139   char *router = NULL;
2140   xbt_dict_foreach(routing->dict_processing_units, c1, k1, d1) {
2141     if (strstr (k1, "router")){
2142       router = k1;
2143     }
2144   }
2145   if (!router){
2146     xbt_die ("rulebased_get_onelink_routes works only if the AS is a cluster, sorry.");
2147   }
2148
2149   xbt_dict_foreach(routing->dict_processing_units, c1, k1, d1) {
2150     route_extended_t route = rulebased_get_route (rc, router, k1);
2151
2152     int number_of_links = xbt_dynar_length(route->generic_route.link_list);
2153     if (number_of_links != 3) {
2154       xbt_die ("rulebased_get_onelink_routes works only if the AS is a cluster, sorry.");
2155     }
2156
2157     void *link_ptr;
2158     xbt_dynar_get_cpy (route->generic_route.link_list, 2, &link_ptr);
2159     onelink_t onelink = xbt_new0 (s_onelink_t, 1);
2160     onelink->src = xbt_strdup (k1);
2161     onelink->dst = xbt_strdup (router);
2162     onelink->link_ptr = link_ptr;
2163     xbt_dynar_push (ret, &onelink);
2164   }
2165   return ret;
2166 }
2167
2168 /* Business methods */
2169 static route_extended_t rulebased_get_route(routing_component_t rc,
2170                                             const char *src,
2171                                             const char *dst)
2172 {
2173   xbt_assert1(rc && src
2174               && dst,
2175               "Invalid params for \"get_route\" function at AS \"%s\"",
2176               rc->name);
2177
2178   /* set utils vars */
2179   routing_component_rulebased_t routing =
2180       (routing_component_rulebased_t) rc;
2181
2182   int are_processing_units;
2183   xbt_dynar_t rule_list;
2184   if (xbt_dict_get_or_null(routing->dict_processing_units, src)
2185       && xbt_dict_get_or_null(routing->dict_processing_units, dst)) {
2186     are_processing_units = 1;
2187     rule_list = routing->list_route;
2188   } else if (xbt_dict_get_or_null(routing->dict_autonomous_systems, src)
2189              && xbt_dict_get_or_null(routing->dict_autonomous_systems,
2190                                      dst)) {
2191     are_processing_units = 0;
2192     rule_list = routing->list_ASroute;
2193   } else
2194     xbt_assert2(NULL,
2195                 "Ask for route \"from\"(%s)  or \"to\"(%s) no found in the local table",
2196                 src, dst);
2197
2198   int rc_src = -1;
2199   int rc_dst = -1;
2200   int src_length = (int) strlen(src);
2201   int dst_length = (int) strlen(dst);
2202
2203   xbt_dynar_t links_list =
2204       xbt_dynar_new(global_routing->size_of_link, NULL);
2205
2206   rule_route_t ruleroute;
2207   unsigned int cpt;
2208   int ovector_src[OVECCOUNT];
2209   int ovector_dst[OVECCOUNT];
2210   const char **list_src = NULL;
2211   const char **list_dst = NULL;
2212   xbt_dynar_foreach(rule_list, cpt, ruleroute) {
2213     rc_src =
2214         pcre_exec(ruleroute->re_src, NULL, src, src_length, 0, 0,
2215                   ovector_src, OVECCOUNT);
2216     if (rc_src >= 0) {
2217       rc_dst =
2218           pcre_exec(ruleroute->re_dst, NULL, dst, dst_length, 0, 0,
2219                     ovector_dst, OVECCOUNT);
2220       if (rc_dst >= 0) {
2221         xbt_assert1(!pcre_get_substring_list
2222                     (src, ovector_src, rc_src, &list_src),
2223                     "error solving substring list for src \"%s\"", src);
2224         xbt_assert1(!pcre_get_substring_list
2225                     (dst, ovector_dst, rc_dst, &list_dst),
2226                     "error solving substring list for src \"%s\"", dst);
2227         char *link_name;
2228         xbt_dynar_foreach(ruleroute->re_str_link, cpt, link_name) {
2229           char *new_link_name =
2230               remplace(link_name, list_src, rc_src, list_dst, rc_dst);
2231           void *link =
2232               xbt_dict_get_or_null(surf_network_model->resource_set,
2233                                    new_link_name);
2234           if (link)
2235             xbt_dynar_push(links_list, &link);
2236           else
2237             THROW1(mismatch_error, 0, "Link %s not found", new_link_name);
2238           xbt_free(new_link_name);
2239         }
2240       }
2241     }
2242     if (rc_src >= 0 && rc_dst >= 0)
2243       break;
2244   }
2245
2246   route_extended_t new_e_route = NULL;
2247   if (rc_src >= 0 && rc_dst >= 0) {
2248     new_e_route = xbt_new0(s_route_extended_t, 1);
2249     new_e_route->generic_route.link_list = links_list;
2250   } else if (!strcmp(src, dst) && are_processing_units) {
2251     new_e_route = xbt_new0(s_route_extended_t, 1);
2252     xbt_dynar_push(links_list, &(global_routing->loopback));
2253     new_e_route->generic_route.link_list = links_list;
2254   } else {
2255     xbt_dynar_free(&link_list);
2256   }
2257
2258   if (!are_processing_units && new_e_route) {
2259     rule_route_extended_t ruleroute_extended =
2260         (rule_route_extended_t) ruleroute;
2261     new_e_route->src_gateway =
2262         remplace(ruleroute_extended->re_src_gateway, list_src, rc_src,
2263                  list_dst, rc_dst);
2264     new_e_route->dst_gateway =
2265         remplace(ruleroute_extended->re_dst_gateway, list_src, rc_src,
2266                  list_dst, rc_dst);
2267   }
2268
2269   if (list_src)
2270     pcre_free_substring_list(list_src);
2271   if (list_dst)
2272     pcre_free_substring_list(list_dst);
2273
2274   return new_e_route;
2275 }
2276
2277 static route_extended_t rulebased_get_bypass_route(routing_component_t rc,
2278                                                    const char *src,
2279                                                    const char *dst)
2280 {
2281   return NULL;
2282 }
2283
2284 static void rulebased_finalize(routing_component_t rc)
2285 {
2286   routing_component_rulebased_t routing =
2287       (routing_component_rulebased_t) rc;
2288   if (routing) {
2289     xbt_dict_free(&routing->dict_processing_units);
2290     xbt_dict_free(&routing->dict_autonomous_systems);
2291     xbt_dynar_free(&routing->list_route);
2292     xbt_dynar_free(&routing->list_ASroute);
2293     /* Delete structure */
2294     xbt_free(routing);
2295   }
2296 }
2297
2298 /* Creation routing model functions */
2299 static void *model_rulebased_create(void)
2300 {
2301   routing_component_rulebased_t new_component =
2302       xbt_new0(s_routing_component_rulebased_t, 1);
2303   new_component->generic_routing.set_processing_unit =
2304       model_rulebased_set_processing_unit;
2305   new_component->generic_routing.set_autonomous_system =
2306       model_rulebased_set_autonomous_system;
2307   new_component->generic_routing.set_route = model_rulebased_set_route;
2308   new_component->generic_routing.set_ASroute = model_rulebased_set_ASroute;
2309   new_component->generic_routing.set_bypassroute =
2310       model_rulebased_set_bypassroute;
2311   new_component->generic_routing.get_onelink_routes =
2312       rulebased_get_onelink_routes;
2313   new_component->generic_routing.get_route = rulebased_get_route;
2314   new_component->generic_routing.get_bypass_route = NULL;       //rulebased_get_bypass_route;
2315   new_component->generic_routing.finalize = rulebased_finalize;
2316   /* initialization of internal structures */
2317   new_component->dict_processing_units = xbt_dict_new();
2318   new_component->dict_autonomous_systems = xbt_dict_new();
2319   new_component->list_route =
2320       xbt_dynar_new(sizeof(rule_route_t), &rule_route_free);
2321   new_component->list_ASroute =
2322       xbt_dynar_new(sizeof(rule_route_extended_t),
2323                     &rule_route_extended_free);
2324   return new_component;
2325 }
2326
2327 static void model_rulebased_load(void)
2328 {
2329   /* use "surfxml_add_callback" to add a parse function call */
2330 }
2331
2332 static void model_rulebased_unload(void)
2333 {
2334   /* use "surfxml_del_callback" to remove a parse function call */
2335 }
2336
2337 static void model_rulebased_end(void)
2338 {
2339 }
2340
2341 #endif                          /* HAVE_PCRE_LIB */
2342
2343 /* ************************************************************************** */
2344 /* ******************************* NO ROUTING ******************************* */
2345
2346 /* Routing model structure */
2347 typedef struct {
2348   s_routing_component_t generic_routing;
2349 } s_routing_component_none_t, *routing_component_none_t;
2350
2351 /* Business methods */
2352 static xbt_dynar_t none_get_onelink_routes(routing_component_t rc)
2353 {
2354   return NULL;
2355 }
2356
2357 static route_extended_t none_get_route(routing_component_t rc,
2358                                        const char *src, const char *dst)
2359 {
2360   return NULL;
2361 }
2362
2363 static route_extended_t none_get_bypass_route(routing_component_t rc,
2364                                               const char *src,
2365                                               const char *dst)
2366 {
2367   return NULL;
2368 }
2369
2370 static void none_finalize(routing_component_t rc)
2371 {
2372   xbt_free(rc);
2373 }
2374
2375 static void none_set_processing_unit(routing_component_t rc,
2376                                      const char *name)
2377 {
2378 }
2379
2380 static void none_set_autonomous_system(routing_component_t rc,
2381                                        const char *name)
2382 {
2383 }
2384
2385 /* Creation routing model functions */
2386 static void *model_none_create(void)
2387 {
2388   routing_component_none_t new_component =
2389       xbt_new0(s_routing_component_none_t, 1);
2390   new_component->generic_routing.set_processing_unit =
2391       none_set_processing_unit;
2392   new_component->generic_routing.set_autonomous_system =
2393       none_set_autonomous_system;
2394   new_component->generic_routing.set_route = NULL;
2395   new_component->generic_routing.set_ASroute = NULL;
2396   new_component->generic_routing.set_bypassroute = NULL;
2397   new_component->generic_routing.get_route = none_get_route;
2398   new_component->generic_routing.get_onelink_routes =
2399       none_get_onelink_routes;
2400   new_component->generic_routing.get_bypass_route = none_get_bypass_route;
2401   new_component->generic_routing.finalize = none_finalize;
2402   return new_component;
2403 }
2404
2405 static void model_none_load(void)
2406 {
2407 }
2408
2409 static void model_none_unload(void)
2410 {
2411 }
2412
2413 static void model_none_end(void)
2414 {
2415 }
2416
2417 /* ************************************************** */
2418 /* ********** PATERN FOR NEW ROUTING **************** */
2419
2420 /* The minimal configuration of a new routing model need the next functions,
2421  * also you need to set at the start of the file, the new model in the model
2422  * list. Remember keep the null ending of the list.
2423  */
2424 /*** Routing model structure ***/
2425 // typedef struct {
2426 //   s_routing_component_t generic_routing;
2427 //   /* things that your routing model need */
2428 // } s_routing_component_NEW_t,*routing_component_NEW_t;
2429
2430 /*** Parse routing model functions ***/
2431 // static void model_NEW_set_processing_unit(routing_component_t rc, const char* name) {}
2432 // static void model_NEW_set_autonomous_system(routing_component_t rc, const char* name) {}
2433 // static void model_NEW_set_route(routing_component_t rc, const char* src, const char* dst, route_t route) {}
2434 // static void model_NEW_set_ASroute(routing_component_t rc, const char* src, const char* dst, route_extended_t route) {}
2435 // static void model_NEW_set_bypassroute(routing_component_t rc, const char* src, const char* dst, route_extended_t e_route) {}
2436
2437 /*** Business methods ***/
2438 // static route_extended_t NEW_get_route(routing_component_t rc, const char* src,const char* dst) {return NULL;}
2439 // static route_extended_t NEW_get_bypass_route(routing_component_t rc, const char* src,const char* dst) {return NULL;}
2440 // static void NEW_finalize(routing_component_t rc) { xbt_free(rc);}
2441
2442 /*** Creation routing model functions ***/
2443 // static void* model_NEW_create(void) {
2444 //   routing_component_NEW_t new_component =  xbt_new0(s_routing_component_NEW_t,1);
2445 //   new_component->generic_routing.set_processing_unit = model_NEW_set_processing_unit;
2446 //   new_component->generic_routing.set_autonomous_system = model_NEW_set_autonomous_system;
2447 //   new_component->generic_routing.set_route = model_NEW_set_route;
2448 //   new_component->generic_routing.set_ASroute = model_NEW_set_ASroute;
2449 //   new_component->generic_routing.set_bypassroute = model_NEW_set_bypassroute;
2450 //   new_component->generic_routing.get_route = NEW_get_route;
2451 //   new_component->generic_routing.get_bypass_route = NEW_get_bypass_route;
2452 //   new_component->generic_routing.finalize = NEW_finalize;
2453 //   /* initialization of internal structures */
2454 //   return new_component;
2455 // } /* mandatory */
2456 // static void  model_NEW_load(void) {}   /* mandatory */
2457 // static void  model_NEW_unload(void) {} /* mandatory */
2458 // static void  model_NEW_end(void) {}    /* mandatory */
2459
2460 /* ************************************************************************** */
2461 /* ************************* GENERIC PARSE FUNCTIONS ************************ */
2462
2463 static void generic_set_processing_unit(routing_component_t rc,
2464                                         const char *name)
2465 {
2466   DEBUG1("Load process unit \"%s\"", name);
2467   int *id = xbt_new0(int, 1);
2468   xbt_dict_t _to_index;
2469   _to_index = current_routing->to_index;
2470   *id = xbt_dict_length(_to_index);
2471   xbt_dict_set(_to_index, name, id, xbt_free);
2472 }
2473
2474 static void generic_set_autonomous_system(routing_component_t rc,
2475                                           const char *name)
2476 {
2477   DEBUG1("Load Autonomous system \"%s\"", name);
2478   int *id = xbt_new0(int, 1);
2479   xbt_dict_t _to_index;
2480   _to_index = current_routing->to_index;
2481   *id = xbt_dict_length(_to_index);
2482   xbt_dict_set(_to_index, name, id, xbt_free);
2483 }
2484
2485 static void generic_set_route(routing_component_t rc, const char *src,
2486                               const char *dst, route_t route)
2487 {
2488   DEBUG2("Load Route from \"%s\" to \"%s\"", src, dst);
2489   model_type_t modeltype = rc->routing;
2490   xbt_dict_t _parse_routes;
2491   xbt_dict_t _to_index;
2492   char *route_name;
2493   int *src_id, *dst_id;
2494   _to_index = current_routing->to_index;
2495
2496   if (modeltype == &routing_models[SURF_MODEL_FULL]) {
2497     _parse_routes = ((routing_component_full_t) rc)->parse_routes;
2498
2499   } else if (modeltype == &routing_models[SURF_MODEL_FLOYD]) {
2500     _parse_routes = ((routing_component_floyd_t) rc)->parse_routes;
2501
2502   } else if (modeltype == &routing_models[SURF_MODEL_DIJKSTRA] ||
2503              modeltype == &routing_models[SURF_MODEL_DIJKSTRACACHE]) {
2504     _parse_routes = ((routing_component_dijkstra_t) rc)->parse_routes;
2505
2506   } else
2507     xbt_die("\"generic_set_route\" not supported");
2508
2509   src_id = xbt_dict_get_or_null(_to_index, src);
2510   dst_id = xbt_dict_get_or_null(_to_index, dst);
2511
2512   xbt_assert2(src_id
2513               && dst_id, "Network elements %s or %s not found", src, dst);
2514   route_name = bprintf("%d#%d", *src_id, *dst_id);
2515
2516   xbt_assert2(xbt_dynar_length(route->link_list) > 0,
2517               "Invalid count of links, must be greater than zero (%s,%s)",
2518               src, dst);
2519   xbt_assert2(!xbt_dict_get_or_null(_parse_routes, route_name),
2520               "The route between \"%s\" and \"%s\" already exist", src,
2521               dst);
2522
2523   xbt_dict_set(_parse_routes, route_name, route, NULL);
2524   xbt_free(route_name);
2525 }
2526
2527 static void generic_set_ASroute(routing_component_t rc, const char *src,
2528                                 const char *dst, route_extended_t e_route)
2529 {
2530   DEBUG4("Load ASroute from \"%s(%s)\" to \"%s(%s)\"", src,
2531          e_route->src_gateway, dst, e_route->dst_gateway);
2532   model_type_t modeltype = rc->routing;
2533   xbt_dict_t _parse_routes;
2534   xbt_dict_t _to_index;
2535   char *route_name;
2536   int *src_id, *dst_id;
2537   _to_index = current_routing->to_index;
2538
2539   if (modeltype == &routing_models[SURF_MODEL_FULL]) {
2540     _parse_routes = ((routing_component_full_t) rc)->parse_routes;
2541
2542   } else if (modeltype == &routing_models[SURF_MODEL_FLOYD]) {
2543     _parse_routes = ((routing_component_floyd_t) rc)->parse_routes;
2544
2545   } else if (modeltype == &routing_models[SURF_MODEL_DIJKSTRA] ||
2546              modeltype == &routing_models[SURF_MODEL_DIJKSTRACACHE]) {
2547     _parse_routes = ((routing_component_dijkstra_t) rc)->parse_routes;
2548
2549   } else
2550     xbt_die("\"generic_set_route\" not supported");
2551
2552   src_id = xbt_dict_get_or_null(_to_index, src);
2553   dst_id = xbt_dict_get_or_null(_to_index, dst);
2554
2555   xbt_assert2(src_id
2556               && dst_id, "Network elements %s or %s not found", src, dst);
2557   route_name = bprintf("%d#%d", *src_id, *dst_id);
2558
2559   xbt_assert2(xbt_dynar_length(e_route->generic_route.link_list) > 0,
2560               "Invalid count of links, must be greater than zero (%s,%s)",
2561               src, dst);
2562   xbt_assert4(!xbt_dict_get_or_null(_parse_routes, route_name),
2563               "The route between \"%s\"(\"%s\") and \"%s\"(\"%s\") already exist",
2564               src, e_route->src_gateway, dst, e_route->dst_gateway);
2565
2566   xbt_dict_set(_parse_routes, route_name, e_route, NULL);
2567   xbt_free(route_name);
2568 }
2569
2570 static void generic_set_bypassroute(routing_component_t rc,
2571                                     const char *src, const char *dst,
2572                                     route_extended_t e_route)
2573 {
2574   DEBUG2("Load bypassRoute from \"%s\" to \"%s\"", src, dst);
2575   model_type_t modeltype = rc->routing;
2576   xbt_dict_t dict_bypassRoutes;
2577   char *route_name;
2578   if (modeltype == &routing_models[SURF_MODEL_FULL]) {
2579     dict_bypassRoutes = ((routing_component_full_t) rc)->bypassRoutes;
2580   } else if (modeltype == &routing_models[SURF_MODEL_FLOYD]) {
2581     dict_bypassRoutes = ((routing_component_floyd_t) rc)->bypassRoutes;
2582   } else if (modeltype == &routing_models[SURF_MODEL_DIJKSTRA] ||
2583              modeltype == &routing_models[SURF_MODEL_DIJKSTRACACHE]) {
2584     dict_bypassRoutes = ((routing_component_dijkstra_t) rc)->bypassRoutes;
2585   } else
2586     xbt_die("\"generic_set_bypassroute\" not supported");
2587   route_name = bprintf("%s#%s", src, dst);
2588   xbt_assert2(xbt_dynar_length(e_route->generic_route.link_list) > 0,
2589               "Invalid count of links, must be greater than zero (%s,%s)",
2590               src, dst);
2591   xbt_assert4(!xbt_dict_get_or_null(dict_bypassRoutes, route_name),
2592               "The bypass route between \"%s\"(\"%s\") and \"%s\"(\"%s\") already exist",
2593               src, e_route->src_gateway, dst, e_route->dst_gateway);
2594
2595   route_extended_t new_e_route =
2596       generic_new_extended_route(SURF_ROUTING_RECURSIVE, e_route, 0);
2597   xbt_dynar_free(&(e_route->generic_route.link_list));
2598   xbt_free(e_route);
2599
2600   xbt_dict_set(dict_bypassRoutes, route_name, new_e_route,
2601                (void (*)(void *)) generic_free_extended_route);
2602   xbt_free(route_name);
2603 }
2604
2605 /* ************************************************************************** */
2606 /* *********************** GENERIC BUSINESS METHODS ************************* */
2607
2608 static xbt_dynar_t generic_get_onelink_routes(routing_component_t rc)
2609 {
2610   xbt_die("\"generic_get_onelink_routes\" not implemented yet");
2611 }
2612
2613 static route_extended_t generic_get_bypassroute(routing_component_t rc,
2614                                                 const char *src,
2615                                                 const char *dst)
2616 {
2617   model_type_t modeltype = rc->routing;
2618   xbt_dict_t dict_bypassRoutes;
2619
2620   if (modeltype == &routing_models[SURF_MODEL_FULL]) {
2621     dict_bypassRoutes = ((routing_component_full_t) rc)->bypassRoutes;
2622   } else if (modeltype == &routing_models[SURF_MODEL_FLOYD]) {
2623     dict_bypassRoutes = ((routing_component_floyd_t) rc)->bypassRoutes;
2624   } else if (modeltype == &routing_models[SURF_MODEL_DIJKSTRA] ||
2625              modeltype == &routing_models[SURF_MODEL_DIJKSTRACACHE]) {
2626     dict_bypassRoutes = ((routing_component_dijkstra_t) rc)->bypassRoutes;
2627   } else
2628     xbt_die("\"generic_get_bypassroute\" not supported");
2629
2630
2631   routing_component_t src_as, dst_as;
2632   int index_src, index_dst;
2633   xbt_dynar_t path_src = NULL;
2634   xbt_dynar_t path_dst = NULL;
2635   routing_component_t current = NULL;
2636   routing_component_t *current_src = NULL;
2637   routing_component_t *current_dst = NULL;
2638
2639   /* (1) find the as where the src and dst are located */
2640   src_as = ((network_element_info_t)
2641             xbt_dict_get_or_null(global_routing->where_network_elements,
2642                                  src))->rc_component;
2643   dst_as = ((network_element_info_t)
2644             xbt_dict_get_or_null(global_routing->where_network_elements,
2645                                  dst))->rc_component;
2646   xbt_assert2(src_as
2647               && dst_as,
2648               "Ask for route \"from\"(%s) or \"to\"(%s) no found", src,
2649               dst);
2650
2651   /* (2) find the path to the root routing component */
2652   path_src = xbt_dynar_new(sizeof(routing_component_t), NULL);
2653   current = src_as;
2654   while (current != NULL) {
2655     xbt_dynar_push(path_src, &current);
2656     current = current->routing_father;
2657   }
2658   path_dst = xbt_dynar_new(sizeof(routing_component_t), NULL);
2659   current = dst_as;
2660   while (current != NULL) {
2661     xbt_dynar_push(path_dst, &current);
2662     current = current->routing_father;
2663   }
2664
2665   /* (3) find the common father */
2666   index_src = (path_src->used) - 1;
2667   index_dst = (path_dst->used) - 1;
2668   current_src = xbt_dynar_get_ptr(path_src, index_src);
2669   current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
2670   while (index_src >= 0 && index_dst >= 0 && *current_src == *current_dst) {
2671     routing_component_t *tmp_src, *tmp_dst;
2672     tmp_src = xbt_dynar_pop_ptr(path_src);
2673     tmp_dst = xbt_dynar_pop_ptr(path_dst);
2674     index_src--;
2675     index_dst--;
2676     current_src = xbt_dynar_get_ptr(path_src, index_src);
2677     current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
2678   }
2679
2680   int max_index_src = (path_src->used) - 1;
2681   int max_index_dst = (path_dst->used) - 1;
2682
2683   int max_index = max(max_index_src, max_index_dst);
2684   int i, max;
2685
2686   route_extended_t e_route_bypass = NULL;
2687
2688   for (max = 0; max <= max_index; max++) {
2689     for (i = 0; i < max; i++) {
2690       if (i <= max_index_src && max <= max_index_dst) {
2691         char *route_name = bprintf("%s#%s",
2692                                    (*(routing_component_t *)
2693                                     (xbt_dynar_get_ptr
2694                                      (path_src, i)))->name,
2695                                    (*(routing_component_t *)
2696                                     (xbt_dynar_get_ptr
2697                                      (path_dst, max)))->name);
2698         e_route_bypass =
2699             xbt_dict_get_or_null(dict_bypassRoutes, route_name);
2700         xbt_free(route_name);
2701       }
2702       if (e_route_bypass)
2703         break;
2704       if (max <= max_index_src && i <= max_index_dst) {
2705         char *route_name = bprintf("%s#%s",
2706                                    (*(routing_component_t *)
2707                                     (xbt_dynar_get_ptr
2708                                      (path_src, max)))->name,
2709                                    (*(routing_component_t *)
2710                                     (xbt_dynar_get_ptr
2711                                      (path_dst, i)))->name);
2712         e_route_bypass =
2713             xbt_dict_get_or_null(dict_bypassRoutes, route_name);
2714         xbt_free(route_name);
2715       }
2716       if (e_route_bypass)
2717         break;
2718     }
2719
2720     if (e_route_bypass)
2721       break;
2722
2723     if (max <= max_index_src && max <= max_index_dst) {
2724       char *route_name = bprintf("%s#%s",
2725                                  (*(routing_component_t *)
2726                                   (xbt_dynar_get_ptr
2727                                    (path_src, max)))->name,
2728                                  (*(routing_component_t *)
2729                                   (xbt_dynar_get_ptr
2730                                    (path_dst, max)))->name);
2731       e_route_bypass = xbt_dict_get_or_null(dict_bypassRoutes, route_name);
2732       xbt_free(route_name);
2733     }
2734     if (e_route_bypass)
2735       break;
2736   }
2737
2738   xbt_dynar_free(&path_src);
2739   xbt_dynar_free(&path_dst);
2740
2741   route_extended_t new_e_route = NULL;
2742
2743   if (e_route_bypass) {
2744     void *link;
2745     unsigned int cpt = 0;
2746     new_e_route = xbt_new0(s_route_extended_t, 1);
2747     new_e_route->src_gateway = xbt_strdup(e_route_bypass->src_gateway);
2748     new_e_route->dst_gateway = xbt_strdup(e_route_bypass->dst_gateway);
2749     new_e_route->generic_route.link_list =
2750         xbt_dynar_new(global_routing->size_of_link, NULL);
2751     xbt_dynar_foreach(e_route_bypass->generic_route.link_list, cpt, link) {
2752       xbt_dynar_push(new_e_route->generic_route.link_list, &link);
2753     }
2754   }
2755
2756   return new_e_route;
2757 }
2758
2759 /* ************************************************************************** */
2760 /* ************************* GENERIC AUX FUNCTIONS ************************** */
2761
2762 static route_extended_t
2763 generic_new_extended_route(e_surf_routing_hierarchy_t hierarchy,
2764                            void *data, int order)
2765 {
2766
2767   char *link_name;
2768   route_extended_t e_route, new_e_route;
2769   route_t route;
2770   unsigned int cpt;
2771   xbt_dynar_t links = NULL, links_id = NULL;
2772
2773   new_e_route = xbt_new0(s_route_extended_t, 1);
2774   new_e_route->generic_route.link_list =
2775       xbt_dynar_new(global_routing->size_of_link, NULL);
2776   new_e_route->src_gateway = NULL;
2777   new_e_route->dst_gateway = NULL;
2778
2779   xbt_assert0(hierarchy == SURF_ROUTING_BASE
2780               || hierarchy == SURF_ROUTING_RECURSIVE,
2781               "the hierarchy type is not defined");
2782
2783   if (hierarchy == SURF_ROUTING_BASE) {
2784
2785     route = (route_t) data;
2786     links = route->link_list;
2787
2788   } else if (hierarchy == SURF_ROUTING_RECURSIVE) {
2789
2790     e_route = (route_extended_t) data;
2791     xbt_assert0(e_route->src_gateway
2792                 && e_route->dst_gateway, "bad gateway, is null");
2793     links = e_route->generic_route.link_list;
2794
2795     /* remeber not erase the gateway names */
2796     new_e_route->src_gateway = e_route->src_gateway;
2797     new_e_route->dst_gateway = e_route->dst_gateway;
2798   }
2799
2800   links_id = new_e_route->generic_route.link_list;
2801
2802   xbt_dynar_foreach(links, cpt, link_name) {
2803
2804     void *link =
2805         xbt_dict_get_or_null(surf_network_model->resource_set, link_name);
2806     if (link) {
2807       if (order)
2808         xbt_dynar_push(links_id, &link);
2809       else
2810         xbt_dynar_unshift(links_id, &link);
2811     } else
2812       THROW1(mismatch_error, 0, "Link %s not found", link_name);
2813   }
2814
2815   return new_e_route;
2816 }
2817
2818 static void generic_free_route(route_t route)
2819 {
2820   if (route) {
2821     xbt_dynar_free(&(route->link_list));
2822     xbt_free(route);
2823   }
2824 }
2825
2826 static void generic_free_extended_route(route_extended_t e_route)
2827 {
2828   if (e_route) {
2829     xbt_dynar_free(&(e_route->generic_route.link_list));
2830     if (e_route->src_gateway)
2831       xbt_free(e_route->src_gateway);
2832     if (e_route->dst_gateway)
2833       xbt_free(e_route->dst_gateway);
2834     xbt_free(e_route);
2835   }
2836 }
2837
2838 static routing_component_t generic_as_exist(routing_component_t find_from,
2839                                             routing_component_t to_find)
2840 {
2841   //return to_find; // FIXME: BYPASSERROR OF FOREACH WITH BREAK
2842   xbt_dict_cursor_t cursor = NULL;
2843   char *key;
2844   int found = 0;
2845   routing_component_t elem;
2846   xbt_dict_foreach(find_from->routing_sons, cursor, key, elem) {
2847     if (to_find == elem || generic_as_exist(elem, to_find)) {
2848       found = 1;
2849       break;
2850     }
2851   }
2852   if (found)
2853     return to_find;
2854   return NULL;
2855 }
2856
2857 static routing_component_t
2858 generic_autonomous_system_exist(routing_component_t rc, char *element)
2859 {
2860   //return rc; // FIXME: BYPASSERROR OF FOREACH WITH BREAK
2861   routing_component_t element_as, result, elem;
2862   xbt_dict_cursor_t cursor = NULL;
2863   char *key;
2864   element_as = ((network_element_info_t)
2865                 xbt_dict_get_or_null
2866                 (global_routing->where_network_elements,
2867                  element))->rc_component;
2868   result = ((routing_component_t) - 1);
2869   if (element_as != rc)
2870     result = generic_as_exist(rc, element_as);
2871
2872   int found = 0;
2873   if (result) {
2874     xbt_dict_foreach(element_as->routing_sons, cursor, key, elem) {
2875       found = !strcmp(elem->name, element);
2876       if (found)
2877         break;
2878     }
2879     if (found)
2880       return element_as;
2881   }
2882   return NULL;
2883 }
2884
2885 static routing_component_t
2886 generic_processing_units_exist(routing_component_t rc, char *element)
2887 {
2888   routing_component_t element_as;
2889   element_as = ((network_element_info_t)
2890                 xbt_dict_get_or_null
2891                 (global_routing->where_network_elements,
2892                  element))->rc_component;
2893   if (element_as == rc)
2894     return element_as;
2895   return generic_as_exist(rc, element_as);
2896 }
2897
2898 static void generic_src_dst_check(routing_component_t rc, const char *src,
2899                                   const char *dst)
2900 {
2901
2902   routing_component_t src_as = ((network_element_info_t)
2903                                 xbt_dict_get_or_null
2904                                 (global_routing->where_network_elements,
2905                                  src))->rc_component;
2906   routing_component_t dst_as = ((network_element_info_t)
2907                                 xbt_dict_get_or_null
2908                                 (global_routing->where_network_elements,
2909                                  dst))->rc_component;
2910
2911   xbt_assert3(src_as != NULL && dst_as != NULL,
2912               "Ask for route \"from\"(%s) or \"to\"(%s) no found at AS \"%s\"",
2913               src, dst, rc->name);
2914   xbt_assert4(src_as == dst_as,
2915               "The src(%s in %s) and dst(%s in %s) are in differents AS",
2916               src, src_as->name, dst, dst_as->name);
2917   xbt_assert2(rc == dst_as,
2918               "The routing component of src and dst is not the same as the network elements belong (%s==%s)",
2919               rc->name, dst_as->name);
2920 }
2921
2922 static void parse_Sconfig(void)
2923 {
2924   //TODO
2925   DEBUG0("WARNING tag config not yet implemented.");
2926 }
2927
2928 static void routing_full_parse_Scluster(void)
2929 {
2930   static int AX_ptr = 0;
2931
2932   char *cluster_id = A_surfxml_cluster_id;
2933   char *cluster_prefix = A_surfxml_cluster_prefix;
2934   char *cluster_suffix = A_surfxml_cluster_suffix;
2935   char *cluster_radical = A_surfxml_cluster_radical;
2936   char *cluster_power = A_surfxml_cluster_power;
2937   char *cluster_bw = A_surfxml_cluster_bw;
2938   char *cluster_lat = A_surfxml_cluster_lat;
2939   char *cluster_bb_bw = A_surfxml_cluster_bb_bw;
2940   char *cluster_bb_lat = A_surfxml_cluster_bb_lat;
2941   char *host_id, *groups, *link_id;
2942   char *router_id, *link_router, *link_backbone, *route_src_dst;
2943   unsigned int iter;
2944   int start, end, i;
2945   xbt_dynar_t radical_elements;
2946   xbt_dynar_t radical_ends;
2947 #ifndef HAVE_PCRE_LIB
2948   xbt_dynar_t tab_elements_num = xbt_dynar_new(sizeof(int), NULL);
2949   char *route_src, *route_dst;
2950   int j;
2951 #endif
2952
2953   static unsigned int surfxml_buffer_stack_stack_ptr = 1;
2954   static unsigned int surfxml_buffer_stack_stack[1024];
2955
2956   surfxml_buffer_stack_stack[0] = 0;
2957
2958   surfxml_bufferstack_push(1);
2959
2960   SURFXML_BUFFER_SET(AS_id, cluster_id);
2961 #ifdef HAVE_PCRE_LIB
2962   SURFXML_BUFFER_SET(AS_routing, "RuleBased");
2963   DEBUG1("<AS id=\"%s\"\trouting=\"RuleBased\">", cluster_id);
2964 #else
2965   SURFXML_BUFFER_SET(AS_routing, "Full");
2966   DEBUG1("<AS id=\"%s\"\trouting=\"Full\">", cluster_id);
2967 #endif
2968   SURFXML_START_TAG(AS);
2969
2970   radical_elements = xbt_str_split(cluster_radical, ",");
2971   xbt_dynar_foreach(radical_elements, iter, groups) {
2972     radical_ends = xbt_str_split(groups, "-");
2973     switch (xbt_dynar_length(radical_ends)) {
2974     case 1:
2975       surf_parse_get_int(&start,
2976                          xbt_dynar_get_as(radical_ends, 0, char *));
2977       host_id = bprintf("%s%d%s", cluster_prefix, start, cluster_suffix);
2978 #ifndef HAVE_PCRE_LIB
2979       xbt_dynar_push_as(tab_elements_num, int, start);
2980 #endif
2981       link_id = bprintf("%s_link_%d", cluster_id, start);
2982
2983       DEBUG2("<host\tid=\"%s\"\tpower=\"%s\"/>", host_id, cluster_power);
2984       SURFXML_BUFFER_SET(host_id, host_id);
2985       SURFXML_BUFFER_SET(host_power, cluster_power);
2986       SURFXML_BUFFER_SET(host_availability, "1.0");
2987       SURFXML_BUFFER_SET(host_availability_file, "");
2988       A_surfxml_host_state = A_surfxml_host_state_ON;
2989       SURFXML_BUFFER_SET(host_state_file, "");
2990       SURFXML_START_TAG(host);
2991       SURFXML_END_TAG(host);
2992
2993       DEBUG3("<link\tid=\"%s\"\tbw=\"%s\"\tlat=\"%s\"/>", link_id,
2994              cluster_bw, cluster_lat);
2995       SURFXML_BUFFER_SET(link_id, link_id);
2996       SURFXML_BUFFER_SET(link_bandwidth, cluster_bw);
2997       SURFXML_BUFFER_SET(link_latency, cluster_lat);
2998       SURFXML_BUFFER_SET(link_bandwidth_file, "");
2999       SURFXML_BUFFER_SET(link_latency_file, "");
3000       A_surfxml_link_state = A_surfxml_link_state_ON;
3001       SURFXML_BUFFER_SET(link_state_file, "");
3002       A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
3003       SURFXML_START_TAG(link);
3004       SURFXML_END_TAG(link);
3005
3006       break;
3007
3008     case 2:
3009
3010       surf_parse_get_int(&start,
3011                          xbt_dynar_get_as(radical_ends, 0, char *));
3012       surf_parse_get_int(&end, xbt_dynar_get_as(radical_ends, 1, char *));
3013       DEBUG2("Create hosts and links from %d to %d", start, end);
3014       for (i = start; i <= end; i++) {
3015         host_id = bprintf("%s%d%s", cluster_prefix, i, cluster_suffix);
3016 #ifndef HAVE_PCRE_LIB
3017         xbt_dynar_push_as(tab_elements_num, int, i);
3018 #endif
3019         link_id = bprintf("%s_link_%d", cluster_id, i);
3020
3021         DEBUG2("<host\tid=\"%s\"\tpower=\"%s\"/>", host_id, cluster_power);
3022         SURFXML_BUFFER_SET(host_id, host_id);
3023         SURFXML_BUFFER_SET(host_power, cluster_power);
3024         SURFXML_BUFFER_SET(host_availability, "1.0");
3025         SURFXML_BUFFER_SET(host_availability_file, "");
3026         A_surfxml_host_state = A_surfxml_host_state_ON;
3027         SURFXML_BUFFER_SET(host_state_file, "");
3028         SURFXML_START_TAG(host);
3029         SURFXML_END_TAG(host);
3030
3031         DEBUG3("<link\tid=\"%s\"\tbw=\"%s\"\tlat=\"%s\"/>", link_id,
3032                cluster_bw, cluster_lat);
3033         SURFXML_BUFFER_SET(link_id, link_id);
3034         SURFXML_BUFFER_SET(link_bandwidth, cluster_bw);
3035         SURFXML_BUFFER_SET(link_latency, cluster_lat);
3036         SURFXML_BUFFER_SET(link_bandwidth_file, "");
3037         SURFXML_BUFFER_SET(link_latency_file, "");
3038         A_surfxml_link_state = A_surfxml_link_state_ON;
3039         SURFXML_BUFFER_SET(link_state_file, "");
3040         A_surfxml_link_sharing_policy =
3041             A_surfxml_link_sharing_policy_SHARED;
3042         SURFXML_START_TAG(link);
3043         SURFXML_END_TAG(link);
3044       }
3045       break;
3046
3047     default:
3048       DEBUG0("Malformed radical");
3049     }
3050
3051     xbt_dynar_free(&radical_ends);
3052   }
3053
3054   DEBUG0(" ");
3055   router_id =
3056       bprintf("%s%s_router%s", cluster_prefix, cluster_id,
3057               cluster_suffix);
3058   link_router = bprintf("%s_link_%s_router", cluster_id, cluster_id);
3059   link_backbone = bprintf("%s_backbone", cluster_id);
3060
3061   DEBUG1("<router id=\"%s\"/>", router_id);
3062   SURFXML_BUFFER_SET(router_id, router_id);;
3063   SURFXML_START_TAG(router);
3064   SURFXML_END_TAG(router);
3065
3066   DEBUG3("<link\tid=\"%s\"\tbw=\"%s\"\tlat=\"%s\"/>", link_router,
3067          cluster_bw, cluster_lat);
3068   SURFXML_BUFFER_SET(link_id, link_router);
3069   SURFXML_BUFFER_SET(link_bandwidth, cluster_bw);
3070   SURFXML_BUFFER_SET(link_latency, cluster_lat);
3071   SURFXML_BUFFER_SET(link_bandwidth_file, "");
3072   SURFXML_BUFFER_SET(link_latency_file, "");
3073   A_surfxml_link_state = A_surfxml_link_state_ON;
3074   SURFXML_BUFFER_SET(link_state_file, "");
3075   A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
3076   SURFXML_START_TAG(link);
3077   SURFXML_END_TAG(link);
3078
3079   DEBUG3("<link\tid=\"%s\"\tbw=\"%s\"\tlat=\"%s\"/>", link_backbone,
3080          cluster_bb_bw, cluster_bb_lat);
3081   SURFXML_BUFFER_SET(link_id, link_backbone);
3082   SURFXML_BUFFER_SET(link_bandwidth, cluster_bb_bw);
3083   SURFXML_BUFFER_SET(link_latency, cluster_bb_lat);
3084   SURFXML_BUFFER_SET(link_bandwidth_file, "");
3085   SURFXML_BUFFER_SET(link_latency_file, "");
3086   A_surfxml_link_state = A_surfxml_link_state_ON;
3087   SURFXML_BUFFER_SET(link_state_file, "");
3088   A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
3089   SURFXML_START_TAG(link);
3090   SURFXML_END_TAG(link);
3091
3092   char *new_suffix = bprintf("%s", "");
3093
3094   radical_elements = xbt_str_split(cluster_suffix, ".");
3095   xbt_dynar_foreach(radical_elements, iter, groups) {
3096     if (strcmp(groups, "")) {
3097       new_suffix = bprintf("%s\\.%s", new_suffix, groups);
3098     }
3099   }
3100   route_src_dst = bprintf("%s(.*)%s", cluster_prefix, new_suffix);
3101
3102   DEBUG0(" ");
3103
3104 #ifdef HAVE_PCRE_LIB
3105
3106   DEBUG2("<route\tsrc=\"%s\"\tdst=\"%s\">", route_src_dst, route_src_dst);
3107   SURFXML_BUFFER_SET(route_src, route_src_dst);
3108   SURFXML_BUFFER_SET(route_dst, route_src_dst);
3109   SURFXML_START_TAG(route);
3110
3111   DEBUG1("<link:ctn\tid=\"%s_link_$1src\"/>", cluster_id);
3112   SURFXML_BUFFER_SET(link_ctn_id, bprintf("%s_link_$1src", cluster_id));
3113   SURFXML_START_TAG(link_ctn);
3114   SURFXML_END_TAG(link_ctn);
3115
3116   DEBUG1("<link:ctn\tid=\"%s_backbone\"/>", cluster_id);
3117   SURFXML_BUFFER_SET(link_ctn_id, bprintf("%s_backbone", cluster_id));
3118   SURFXML_START_TAG(link_ctn);
3119   SURFXML_END_TAG(link_ctn);
3120
3121   DEBUG1("<link:ctn\tid=\"%s_link_$1dst\"/>", cluster_id);
3122   SURFXML_BUFFER_SET(link_ctn_id, bprintf("%s_link_$1dst", cluster_id));
3123   SURFXML_START_TAG(link_ctn);
3124   SURFXML_END_TAG(link_ctn);
3125
3126   DEBUG0("</route>");
3127   SURFXML_END_TAG(route);
3128 #else
3129   for (i = 0; i <= xbt_dynar_length(tab_elements_num); i++) {
3130     for (j = 0; j <= xbt_dynar_length(tab_elements_num); j++) {
3131       if (i == xbt_dynar_length(tab_elements_num)) {
3132         route_src = router_id;
3133       } else {
3134         route_src =
3135             bprintf("%s%d%s", cluster_prefix,
3136                     xbt_dynar_get_as(tab_elements_num, i, int),
3137                     cluster_suffix);
3138       }
3139
3140       if (j == xbt_dynar_length(tab_elements_num)) {
3141         route_dst = router_id;
3142       } else {
3143         route_dst =
3144             bprintf("%s%d%s", cluster_prefix,
3145                     xbt_dynar_get_as(tab_elements_num, j, int),
3146                     cluster_suffix);
3147       }
3148
3149       DEBUG2("<route\tsrc=\"%s\"\tdst=\"%s\">", route_src, route_dst);
3150       SURFXML_BUFFER_SET(route_src, route_src);
3151       SURFXML_BUFFER_SET(route_dst, route_dst);
3152       SURFXML_START_TAG(route);
3153
3154       if (i == xbt_dynar_length(tab_elements_num)) {
3155         route_src = link_router;
3156       } else {
3157         route_src =
3158             bprintf("%s_link_%d", cluster_id,
3159                     xbt_dynar_get_as(tab_elements_num, i, int));
3160       }
3161
3162       if (j == xbt_dynar_length(tab_elements_num)) {
3163         route_dst = link_router;
3164       } else {
3165         route_dst =
3166             bprintf("%s_link_%d", cluster_id,
3167                     xbt_dynar_get_as(tab_elements_num, j, int));
3168       }
3169
3170       DEBUG1("<link:ctn\tid=\"%s\"/>", route_src);
3171       SURFXML_BUFFER_SET(link_ctn_id, route_src);
3172       SURFXML_START_TAG(link_ctn);
3173       SURFXML_END_TAG(link_ctn);
3174
3175       DEBUG1("<link:ctn\tid=\"%s_backbone\"/>", cluster_id);
3176       SURFXML_BUFFER_SET(link_ctn_id, bprintf("%s_backbone", cluster_id));
3177       SURFXML_START_TAG(link_ctn);
3178       SURFXML_END_TAG(link_ctn);
3179
3180       DEBUG1("<link:ctn\tid=\"%s\"/>", route_dst);
3181       SURFXML_BUFFER_SET(link_ctn_id, route_dst);
3182       SURFXML_START_TAG(link_ctn);
3183       SURFXML_END_TAG(link_ctn);
3184
3185       DEBUG0("</route>");
3186       SURFXML_END_TAG(route);
3187     }
3188   }
3189   xbt_dynar_free(&tab_elements_num);
3190 #endif
3191
3192   DEBUG0("</AS>");
3193   SURFXML_END_TAG(AS);
3194   DEBUG0(" ");
3195
3196   surfxml_bufferstack_pop(1);
3197 }
3198
3199 /*
3200  * New methods to init the routing model component from the lua script
3201  */
3202
3203 /*
3204  * calling parse_S_AS_lua with lua values
3205  */
3206 void routing_AS_init(const char *AS_id, const char *AS_routing)
3207 {
3208   parse_S_AS_lua((char *) AS_id, (char *) AS_routing);
3209 }
3210
3211 /*
3212  * calling parse_E_AS_lua to fisnish the creation of routing component
3213  */
3214 void routing_AS_end(const char *AS_id)
3215 {
3216   parse_E_AS_lua((char *) AS_id);
3217 }
3218
3219 /*
3220  * add a host to the network element list
3221  */
3222
3223 void routing_add_host(const char *host_id)
3224 {
3225   parse_S_host_lua((char *) host_id);
3226 }
3227
3228 /*
3229  * Set a new link on the actual list of link for a route or ASroute
3230  */
3231 void routing_add_link(const char *link_id)
3232 {
3233   parse_E_link_c_ctn_new_elem_lua((char *) link_id);
3234 }
3235
3236 /*
3237  *Set the endpoints for a route
3238  */
3239 void routing_set_route(const char *src_id, const char *dst_id)
3240 {
3241   parse_S_route_new_and_endpoints_lua((char *) src_id, (char *) dst_id);
3242 }
3243
3244 /*
3245  * Store the route by calling parse_E_route_store_route
3246  */
3247 void routing_store_route(void)
3248 {
3249   parse_E_route_store_route();
3250 }