Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
dce12594c10f07cd428194d07ae795e823e89411
[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   route_extended_t *routing_table;
911 } s_routing_component_full_t, *routing_component_full_t;
912
913 /* Business methods */
914 static xbt_dynar_t full_get_onelink_routes(routing_component_t rc)
915 {
916   xbt_dynar_t ret = xbt_dynar_new(sizeof(onelink_t), xbt_free);
917
918   routing_component_full_t routing = (routing_component_full_t) rc;
919   size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
920   xbt_dict_cursor_t c1 = NULL, c2 = NULL;
921   char *k1, *d1, *k2, *d2;
922   xbt_dict_foreach(routing->generic_routing.to_index, c1, k1, d1) {
923     xbt_dict_foreach(routing->generic_routing.to_index, c2, k2, d2) {
924       int *src_id = xbt_dict_get_or_null(routing->generic_routing.to_index, k1);
925       int *dst_id = xbt_dict_get_or_null(routing->generic_routing.to_index, k2);
926       xbt_assert2(src_id
927                   && dst_id,
928                   "Ask for route \"from\"(%s)  or \"to\"(%s) no found in the local table",
929                   src, dst);
930       route_extended_t route = TO_ROUTE_FULL(*src_id, *dst_id);
931       if (route) {
932         if (xbt_dynar_length(route->generic_route.link_list) == 1) {
933           void *link =
934               *(void **) xbt_dynar_get_ptr(route->generic_route.link_list,
935                                            0);
936           onelink_t onelink = xbt_new0(s_onelink_t, 1);
937           onelink->link_ptr = link;
938           if (routing->generic_routing.hierarchy == SURF_ROUTING_BASE) {
939             onelink->src = xbt_strdup(k1);
940             onelink->dst = xbt_strdup(k2);
941           } else if (routing->generic_routing.hierarchy ==
942                      SURF_ROUTING_RECURSIVE) {
943             onelink->src = xbt_strdup(route->src_gateway);
944             onelink->dst = xbt_strdup(route->dst_gateway);
945           }
946           xbt_dynar_push(ret, &onelink);
947         }
948       }
949     }
950   }
951   return ret;
952 }
953
954 static route_extended_t full_get_route(routing_component_t rc,
955                                        const char *src, const char *dst)
956 {
957   xbt_assert1(rc && src
958               && dst,
959               "Invalid params for \"get_route\" function at AS \"%s\"",
960               rc->name);
961
962   /* set utils vars */
963   routing_component_full_t routing = (routing_component_full_t) rc;
964   size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
965
966   generic_src_dst_check(rc, src, dst);
967   int *src_id = xbt_dict_get_or_null(routing->generic_routing.to_index, src);
968   int *dst_id = xbt_dict_get_or_null(routing->generic_routing.to_index, dst);
969   xbt_assert2(src_id
970               && dst_id,
971               "Ask for route \"from\"(%s)  or \"to\"(%s) no found in the local table",
972               src, dst);
973
974   route_extended_t e_route = NULL;
975   route_extended_t new_e_route = NULL;
976   void *link;
977   unsigned int cpt = 0;
978
979   e_route = TO_ROUTE_FULL(*src_id, *dst_id);
980
981   if (e_route) {
982     new_e_route = xbt_new0(s_route_extended_t, 1);
983     new_e_route->src_gateway = xbt_strdup(e_route->src_gateway);
984     new_e_route->dst_gateway = xbt_strdup(e_route->dst_gateway);
985     new_e_route->generic_route.link_list =
986         xbt_dynar_new(global_routing->size_of_link, NULL);
987     xbt_dynar_foreach(e_route->generic_route.link_list, cpt, link) {
988       xbt_dynar_push(new_e_route->generic_route.link_list, &link);
989     }
990   }
991   return new_e_route;
992 }
993
994 static void full_finalize(routing_component_t rc)
995 {
996   routing_component_full_t routing = (routing_component_full_t) rc;
997   size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
998   int i, j;
999   if (routing) {
1000     /* Delete routing table */
1001     for (i = 0; i < table_size; i++)
1002       for (j = 0; j < table_size; j++)
1003         generic_free_extended_route(TO_ROUTE_FULL(i, j));
1004     xbt_free(routing->routing_table);
1005     /* Delete bypass dict */
1006     xbt_dict_free(&routing->generic_routing.bypassRoutes);
1007     /* Delete index dict */
1008     xbt_dict_free(&(routing->generic_routing.to_index));
1009     /* Delete structure */
1010     xbt_free(rc);
1011   }
1012 }
1013
1014 /* Creation routing model functions */
1015
1016 static void *model_full_create(void)
1017 {
1018   routing_component_full_t new_component =
1019       xbt_new0(s_routing_component_full_t, 1);
1020   new_component->generic_routing.set_processing_unit =
1021       generic_set_processing_unit;
1022   new_component->generic_routing.set_autonomous_system =
1023       generic_set_autonomous_system;
1024   new_component->generic_routing.set_route = generic_set_route;
1025   new_component->generic_routing.set_ASroute = generic_set_ASroute;
1026   new_component->generic_routing.set_bypassroute = generic_set_bypassroute;
1027   new_component->generic_routing.get_route = full_get_route;
1028   new_component->generic_routing.get_onelink_routes =
1029       full_get_onelink_routes;
1030   new_component->generic_routing.get_bypass_route =
1031       generic_get_bypassroute;
1032   new_component->generic_routing.finalize = full_finalize;
1033   new_component->generic_routing.to_index = xbt_dict_new();
1034   new_component->generic_routing.bypassRoutes = xbt_dict_new();
1035   new_component->generic_routing.parse_routes = xbt_dict_new();
1036   return new_component;
1037 }
1038
1039 static void model_full_load(void)
1040 {
1041   /* use "surfxml_add_callback" to add a parse function call */
1042 }
1043
1044 static void model_full_unload(void)
1045 {
1046   /* use "surfxml_del_callback" to remove a parse function call */
1047 }
1048
1049 static void model_full_end(void)
1050 {
1051
1052   char *key, *end;
1053   const char *sep = "#";
1054   int src_id, dst_id;
1055   unsigned int i, j;
1056   route_t route;
1057   route_extended_t e_route;
1058   void *data;
1059
1060   xbt_dict_cursor_t cursor = NULL;
1061   xbt_dynar_t keys = NULL;
1062
1063   /* set utils vars */
1064   routing_component_full_t routing =
1065       ((routing_component_full_t) current_routing);
1066   size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
1067
1068   /* Create the routing table */
1069   routing->routing_table =
1070       xbt_new0(route_extended_t, table_size * table_size);
1071
1072   /* Put the routes in position */
1073   xbt_dict_foreach(routing->generic_routing.parse_routes, cursor, key, data) {
1074     keys = xbt_str_split_str(key, sep);
1075     src_id = strtol(xbt_dynar_get_as(keys, 0, char *), &end, 10);
1076     dst_id = strtol(xbt_dynar_get_as(keys, 1, char *), &end, 10);
1077     TO_ROUTE_FULL(src_id, dst_id) =
1078         generic_new_extended_route(current_routing->hierarchy, data, 1);
1079     xbt_dynar_free(&keys);
1080   }
1081
1082   /* delete the parse table */
1083   xbt_dict_foreach(routing->generic_routing.parse_routes, cursor, key, data) {
1084     route = (route_t) data;
1085     xbt_dynar_free(&(route->link_list));
1086     xbt_free(data);
1087   }
1088
1089   /* delete parse dict */
1090   xbt_dict_free(&(routing->generic_routing.parse_routes));
1091
1092   /* Add the loopback if needed */
1093   if (current_routing->hierarchy == SURF_ROUTING_BASE) {
1094     for (i = 0; i < table_size; i++) {
1095       e_route = TO_ROUTE_FULL(i, i);
1096       if (!e_route) {
1097         e_route = xbt_new0(s_route_extended_t, 1);
1098         e_route->src_gateway = NULL;
1099         e_route->dst_gateway = NULL;
1100         e_route->generic_route.link_list =
1101             xbt_dynar_new(global_routing->size_of_link, NULL);
1102         xbt_dynar_push(e_route->generic_route.link_list,
1103                        &global_routing->loopback);
1104         TO_ROUTE_FULL(i, i) = e_route;
1105       }
1106     }
1107   }
1108
1109   /* Shrink the dynar routes (save unused slots) */
1110   for (i = 0; i < table_size; i++)
1111     for (j = 0; j < table_size; j++)
1112       if (TO_ROUTE_FULL(i, j))
1113         xbt_dynar_shrink(TO_ROUTE_FULL(i, j)->generic_route.link_list, 0);
1114 }
1115
1116 /* ************************************************************************** */
1117 /* *************************** FLOYD ROUTING ******************************** */
1118
1119 #define TO_FLOYD_COST(i,j) cost_table[(i)+(j)*table_size]
1120 #define TO_FLOYD_PRED(i,j) (routing->predecessor_table)[(i)+(j)*table_size]
1121 #define TO_FLOYD_LINK(i,j) (routing->link_table)[(i)+(j)*table_size]
1122
1123 /* Routing model structure */
1124
1125 typedef struct {
1126   s_routing_component_t generic_routing;
1127   /* vars for calculate the floyd algorith. */
1128   int *predecessor_table;
1129   route_extended_t *link_table; /* char* -> int* */
1130 } s_routing_component_floyd_t, *routing_component_floyd_t;
1131
1132 static route_extended_t floyd_get_route(routing_component_t rc,
1133                                         const char *src, const char *dst);
1134
1135 /* Business methods */
1136 static xbt_dynar_t floyd_get_onelink_routes(routing_component_t rc)
1137 {
1138   xbt_dynar_t ret = xbt_dynar_new(sizeof(onelink_t), xbt_free);
1139
1140   routing_component_floyd_t routing = (routing_component_floyd_t) rc;
1141   //size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
1142   xbt_dict_cursor_t c1 = NULL, c2 = NULL;
1143   char *k1, *d1, *k2, *d2;
1144   xbt_dict_foreach(routing->generic_routing.to_index, c1, k1, d1) {
1145     xbt_dict_foreach(routing->generic_routing.to_index, c2, k2, d2) {
1146       route_extended_t route = floyd_get_route(rc, k1, k2);
1147       if (route) {
1148         if (xbt_dynar_length(route->generic_route.link_list) == 1) {
1149           void *link =
1150               *(void **) xbt_dynar_get_ptr(route->generic_route.link_list,
1151                                            0);
1152           onelink_t onelink = xbt_new0(s_onelink_t, 1);
1153           onelink->link_ptr = link;
1154           if (routing->generic_routing.hierarchy == SURF_ROUTING_BASE) {
1155             onelink->src = xbt_strdup(k1);
1156             onelink->dst = xbt_strdup(k2);
1157           } else if (routing->generic_routing.hierarchy ==
1158                      SURF_ROUTING_RECURSIVE) {
1159             onelink->src = xbt_strdup(route->src_gateway);
1160             onelink->dst = xbt_strdup(route->dst_gateway);
1161           }
1162           xbt_dynar_push(ret, &onelink);
1163         }
1164       }
1165     }
1166   }
1167   return ret;
1168 }
1169
1170 static route_extended_t floyd_get_route(routing_component_t rc,
1171                                         const char *src, const char *dst)
1172 {
1173   xbt_assert1(rc && src
1174               && dst,
1175               "Invalid params for \"get_route\" function at AS \"%s\"",
1176               rc->name);
1177
1178   /* set utils vars */
1179   routing_component_floyd_t routing = (routing_component_floyd_t) rc;
1180   size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
1181
1182   generic_src_dst_check(rc, src, dst);
1183   int *src_id = xbt_dict_get_or_null(routing->generic_routing.to_index, src);
1184   int *dst_id = xbt_dict_get_or_null(routing->generic_routing.to_index, dst);
1185   xbt_assert2(src_id
1186               && dst_id,
1187               "Ask for route \"from\"(%s)  or \"to\"(%s) no found in the local table",
1188               src, dst);
1189
1190   /* create a result route */
1191   route_extended_t new_e_route = xbt_new0(s_route_extended_t, 1);
1192   new_e_route->generic_route.link_list =
1193       xbt_dynar_new(global_routing->size_of_link, NULL);
1194   new_e_route->src_gateway = NULL;
1195   new_e_route->dst_gateway = NULL;
1196
1197   int first = 1;
1198   int pred = *dst_id;
1199   int prev_pred = 0;
1200   char *gw_src = NULL, *gw_dst =
1201       NULL, *prev_gw_src, *prev_gw_dst, *first_gw = NULL;
1202   unsigned int cpt;
1203   void *link;
1204   xbt_dynar_t links;
1205
1206   do {
1207     prev_pred = pred;
1208     pred = TO_FLOYD_PRED(*src_id, pred);
1209     if (pred == -1)             /* if no pred in route -> no route to host */
1210       break;
1211     xbt_assert2(TO_FLOYD_LINK(pred, prev_pred),
1212                 "Invalid link for the route between \"%s\" or \"%s\"", src,
1213                 dst);
1214
1215     prev_gw_src = gw_src;
1216     prev_gw_dst = gw_dst;
1217
1218     route_extended_t e_route = TO_FLOYD_LINK(pred, prev_pred);
1219     gw_src = e_route->src_gateway;
1220     gw_dst = e_route->dst_gateway;
1221
1222     if (first)
1223       first_gw = gw_dst;
1224
1225     if (rc->hierarchy == SURF_ROUTING_RECURSIVE && !first
1226         && strcmp(gw_dst, prev_gw_src)) {
1227       xbt_dynar_t e_route_as_to_as =
1228           (*(global_routing->get_route)) (gw_dst, prev_gw_src);
1229       xbt_assert2(e_route_as_to_as, "no route between \"%s\" and \"%s\"",
1230                   gw_dst, prev_gw_src);
1231       links = e_route_as_to_as;
1232       int pos = 0;
1233       xbt_dynar_foreach(links, cpt, link) {
1234         xbt_dynar_insert_at(new_e_route->generic_route.link_list, pos,
1235                             &link);
1236         pos++;
1237       }
1238     }
1239
1240     links = e_route->generic_route.link_list;
1241     xbt_dynar_foreach(links, cpt, link) {
1242       xbt_dynar_unshift(new_e_route->generic_route.link_list, &link);
1243     }
1244     first = 0;
1245
1246   } while (pred != *src_id);
1247   xbt_assert4(pred != -1, "no route from host %d to %d (\"%s\" to \"%s\")",
1248               *src_id, *dst_id, src, dst);
1249
1250   if (rc->hierarchy == SURF_ROUTING_RECURSIVE) {
1251     new_e_route->src_gateway = xbt_strdup(gw_src);
1252     new_e_route->dst_gateway = xbt_strdup(first_gw);
1253   }
1254
1255   return new_e_route;
1256 }
1257
1258 static void floyd_finalize(routing_component_t rc)
1259 {
1260   routing_component_floyd_t routing = (routing_component_floyd_t) rc;
1261   int i, j;
1262   size_t table_size;
1263   if (routing) {
1264     table_size = xbt_dict_length(routing->generic_routing.to_index);
1265     /* Delete link_table */
1266     for (i = 0; i < table_size; i++)
1267       for (j = 0; j < table_size; j++)
1268         generic_free_extended_route(TO_FLOYD_LINK(i, j));
1269     xbt_free(routing->link_table);
1270     /* Delete bypass dict */
1271     xbt_dict_free(&routing->generic_routing.bypassRoutes);
1272     /* Delete index dict */
1273     xbt_dict_free(&(routing->generic_routing.to_index));
1274     /* Delete dictionary index dict, predecessor and links table */
1275     xbt_free(routing->predecessor_table);
1276     /* Delete structure */
1277     xbt_free(rc);
1278   }
1279 }
1280
1281 static void *model_floyd_create(void)
1282 {
1283   routing_component_floyd_t new_component =
1284       xbt_new0(s_routing_component_floyd_t, 1);
1285   new_component->generic_routing.set_processing_unit =
1286       generic_set_processing_unit;
1287   new_component->generic_routing.set_autonomous_system =
1288       generic_set_autonomous_system;
1289   new_component->generic_routing.set_route = generic_set_route;
1290   new_component->generic_routing.set_ASroute = generic_set_ASroute;
1291   new_component->generic_routing.set_bypassroute = generic_set_bypassroute;
1292   new_component->generic_routing.get_route = floyd_get_route;
1293   new_component->generic_routing.get_onelink_routes =
1294       floyd_get_onelink_routes;
1295   new_component->generic_routing.get_bypass_route =
1296       generic_get_bypassroute;
1297   new_component->generic_routing.finalize = floyd_finalize;
1298   new_component->generic_routing.to_index = xbt_dict_new();
1299   new_component->generic_routing.bypassRoutes = xbt_dict_new();
1300   new_component->generic_routing.parse_routes = xbt_dict_new();
1301   return new_component;
1302 }
1303
1304 static void model_floyd_load(void)
1305 {
1306   /* use "surfxml_add_callback" to add a parse function call */
1307 }
1308
1309 static void model_floyd_unload(void)
1310 {
1311   /* use "surfxml_del_callback" to remove a parse function call */
1312 }
1313
1314 static void model_floyd_end(void)
1315 {
1316
1317   routing_component_floyd_t routing =
1318       ((routing_component_floyd_t) current_routing);
1319   xbt_dict_cursor_t cursor = NULL;
1320   double *cost_table;
1321   char *key, *data, *end;
1322   const char *sep = "#";
1323   xbt_dynar_t keys;
1324   int src_id, dst_id;
1325   unsigned int i, j, a, b, c;
1326
1327   /* set the size of inicial table */
1328   size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
1329
1330   /* Create Cost, Predecessor and Link tables */
1331   cost_table = xbt_new0(double, table_size * table_size);       /* link cost from host to host */
1332   routing->predecessor_table = xbt_new0(int, table_size * table_size);  /* predecessor host numbers */
1333   routing->link_table = xbt_new0(route_extended_t, table_size * table_size);    /* actual link between src and dst */
1334
1335   /* Initialize costs and predecessors */
1336   for (i = 0; i < table_size; i++)
1337     for (j = 0; j < table_size; j++) {
1338       TO_FLOYD_COST(i, j) = DBL_MAX;
1339       TO_FLOYD_PRED(i, j) = -1;
1340       TO_FLOYD_LINK(i, j) = NULL;       /* fixed, missing in the previous version */
1341     }
1342
1343   /* Put the routes in position */
1344   xbt_dict_foreach(routing->generic_routing.parse_routes, cursor, key, data) {
1345     keys = xbt_str_split_str(key, sep);
1346     src_id = strtol(xbt_dynar_get_as(keys, 0, char *), &end, 10);
1347     dst_id = strtol(xbt_dynar_get_as(keys, 1, char *), &end, 10);
1348     TO_FLOYD_LINK(src_id, dst_id) =
1349         generic_new_extended_route(current_routing->hierarchy, data, 0);
1350     TO_FLOYD_PRED(src_id, dst_id) = src_id;
1351     /* set link cost */
1352     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 */
1353     xbt_dynar_free(&keys);
1354   }
1355
1356   /* Add the loopback if needed */
1357   if (current_routing->hierarchy == SURF_ROUTING_BASE) {
1358     for (i = 0; i < table_size; i++) {
1359       route_extended_t e_route = TO_FLOYD_LINK(i, i);
1360       if (!e_route) {
1361         e_route = xbt_new0(s_route_extended_t, 1);
1362         e_route->src_gateway = NULL;
1363         e_route->dst_gateway = NULL;
1364         e_route->generic_route.link_list =
1365             xbt_dynar_new(global_routing->size_of_link, NULL);
1366         xbt_dynar_push(e_route->generic_route.link_list,
1367                        &global_routing->loopback);
1368         TO_FLOYD_LINK(i, i) = e_route;
1369         TO_FLOYD_PRED(i, i) = i;
1370         TO_FLOYD_COST(i, i) = 1;
1371       }
1372     }
1373   }
1374   /* Calculate path costs */
1375   for (c = 0; c < table_size; c++) {
1376     for (a = 0; a < table_size; a++) {
1377       for (b = 0; b < table_size; b++) {
1378         if (TO_FLOYD_COST(a, c) < DBL_MAX && TO_FLOYD_COST(c, b) < DBL_MAX) {
1379           if (TO_FLOYD_COST(a, b) == DBL_MAX ||
1380               (TO_FLOYD_COST(a, c) + TO_FLOYD_COST(c, b) <
1381                TO_FLOYD_COST(a, b))) {
1382             TO_FLOYD_COST(a, b) =
1383                 TO_FLOYD_COST(a, c) + TO_FLOYD_COST(c, b);
1384             TO_FLOYD_PRED(a, b) = TO_FLOYD_PRED(c, b);
1385           }
1386         }
1387       }
1388     }
1389   }
1390
1391   /* delete the parse table */
1392   xbt_dict_foreach(routing->generic_routing.parse_routes, cursor, key, data) {
1393     route_t route = (route_t) data;
1394     xbt_dynar_free(&(route->link_list));
1395     xbt_free(data);
1396   }
1397
1398   /* delete parse dict */
1399   xbt_dict_free(&(routing->generic_routing.parse_routes));
1400
1401   /* cleanup */
1402   xbt_free(cost_table);
1403 }
1404
1405 /* ************************************************************************** */
1406 /* ********** Dijkstra & Dijkstra Cached ROUTING **************************** */
1407
1408 typedef struct {
1409   s_routing_component_t generic_routing;
1410   xbt_graph_t route_graph;      /* xbt_graph */
1411   xbt_dict_t graph_node_map;    /* map */
1412   xbt_dict_t route_cache;       /* use in cache mode */
1413   int cached;
1414 } s_routing_component_dijkstra_t, *routing_component_dijkstra_t;
1415
1416
1417 typedef struct graph_node_data {
1418   int id;
1419   int graph_id;                 /* used for caching internal graph id's */
1420 } s_graph_node_data_t, *graph_node_data_t;
1421
1422 typedef struct graph_node_map_element {
1423   xbt_node_t node;
1424 } s_graph_node_map_element_t, *graph_node_map_element_t;
1425
1426 typedef struct route_cache_element {
1427   int *pred_arr;
1428   int size;
1429 } s_route_cache_element_t, *route_cache_element_t;
1430
1431 /* Free functions */
1432
1433 static void route_cache_elem_free(void *e)
1434 {
1435   route_cache_element_t elm = (route_cache_element_t) e;
1436   if (elm) {
1437     xbt_free(elm->pred_arr);
1438     xbt_free(elm);
1439   }
1440 }
1441
1442 static void graph_node_map_elem_free(void *e)
1443 {
1444   graph_node_map_element_t elm = (graph_node_map_element_t) e;
1445   if (elm) {
1446     xbt_free(elm);
1447   }
1448 }
1449
1450 static void graph_edge_data_free(void *e)
1451 {
1452   route_extended_t e_route = (route_extended_t) e;
1453   if (e_route) {
1454     xbt_dynar_free(&(e_route->generic_route.link_list));
1455     if (e_route->src_gateway)
1456       xbt_free(e_route->src_gateway);
1457     if (e_route->dst_gateway)
1458       xbt_free(e_route->dst_gateway);
1459     xbt_free(e_route);
1460   }
1461 }
1462
1463 /* Utility functions */
1464
1465 static xbt_node_t route_graph_new_node(routing_component_dijkstra_t rc,
1466                                        int id, int graph_id)
1467 {
1468   routing_component_dijkstra_t routing = (routing_component_dijkstra_t) rc;
1469   xbt_node_t node = NULL;
1470   graph_node_data_t data = NULL;
1471   graph_node_map_element_t elm = NULL;
1472
1473   data = xbt_new0(struct graph_node_data, 1);
1474   data->id = id;
1475   data->graph_id = graph_id;
1476   node = xbt_graph_new_node(routing->route_graph, data);
1477
1478   elm = xbt_new0(struct graph_node_map_element, 1);
1479   elm->node = node;
1480   xbt_dict_set_ext(routing->graph_node_map, (char *) (&id), sizeof(int),
1481                    (xbt_set_elm_t) elm, &graph_node_map_elem_free);
1482
1483   return node;
1484 }
1485
1486 static graph_node_map_element_t
1487 graph_node_map_search(routing_component_dijkstra_t rc, int id)
1488 {
1489   routing_component_dijkstra_t routing = (routing_component_dijkstra_t) rc;
1490   graph_node_map_element_t elm = (graph_node_map_element_t)
1491       xbt_dict_get_or_null_ext(routing->graph_node_map,
1492                                (char *) (&id),
1493                                sizeof(int));
1494   return elm;
1495 }
1496
1497 /* Parsing */
1498
1499 static void route_new_dijkstra(routing_component_dijkstra_t rc, int src_id,
1500                                int dst_id, route_extended_t e_route)
1501 {
1502   routing_component_dijkstra_t routing = (routing_component_dijkstra_t) rc;
1503
1504   xbt_node_t src = NULL;
1505   xbt_node_t dst = NULL;
1506   graph_node_map_element_t src_elm = (graph_node_map_element_t)
1507       xbt_dict_get_or_null_ext(routing->graph_node_map,
1508                                (char *) (&src_id),
1509                                sizeof(int));
1510   graph_node_map_element_t dst_elm = (graph_node_map_element_t)
1511       xbt_dict_get_or_null_ext(routing->graph_node_map,
1512                                (char *) (&dst_id),
1513                                sizeof(int));
1514
1515   if (src_elm)
1516     src = src_elm->node;
1517
1518   if (dst_elm)
1519     dst = dst_elm->node;
1520
1521   /* add nodes if they don't exist in the graph */
1522   if (src_id == dst_id && src == NULL && dst == NULL) {
1523     src = route_graph_new_node(rc, src_id, -1);
1524     dst = src;
1525   } else {
1526     if (src == NULL) {
1527       src = route_graph_new_node(rc, src_id, -1);
1528     }
1529     if (dst == NULL) {
1530       dst = route_graph_new_node(rc, dst_id, -1);
1531     }
1532   }
1533
1534   /* add link as edge to graph */
1535   xbt_graph_new_edge(routing->route_graph, src, dst, e_route);
1536 }
1537
1538 static void add_loopback_dijkstra(routing_component_dijkstra_t rc)
1539 {
1540   routing_component_dijkstra_t routing = (routing_component_dijkstra_t) rc;
1541
1542   xbt_dynar_t nodes = xbt_graph_get_nodes(routing->route_graph);
1543
1544   xbt_node_t node = NULL;
1545   unsigned int cursor2;
1546   xbt_dynar_foreach(nodes, cursor2, node) {
1547     xbt_dynar_t out_edges = xbt_graph_node_get_outedges(node);
1548     xbt_edge_t edge = NULL;
1549     unsigned int cursor;
1550
1551     int found = 0;
1552     xbt_dynar_foreach(out_edges, cursor, edge) {
1553       xbt_node_t other_node = xbt_graph_edge_get_target(edge);
1554       if (other_node == node) {
1555         found = 1;
1556         break;
1557       }
1558     }
1559
1560     if (!found) {
1561       route_extended_t e_route = xbt_new0(s_route_extended_t, 1);
1562       e_route->src_gateway = NULL;
1563       e_route->dst_gateway = NULL;
1564       e_route->generic_route.link_list =
1565           xbt_dynar_new(global_routing->size_of_link, NULL);
1566       xbt_dynar_push(e_route->generic_route.link_list,
1567                      &global_routing->loopback);
1568       xbt_graph_new_edge(routing->route_graph, node, node, e_route);
1569     }
1570   }
1571 }
1572
1573 /* Business methods */
1574 static xbt_dynar_t dijkstra_get_onelink_routes(routing_component_t rc)
1575 {
1576   xbt_die("\"dijkstra_get_onelink_routes\" function not implemented yet");
1577 }
1578
1579 static route_extended_t dijkstra_get_route(routing_component_t rc,
1580                                            const char *src,
1581                                            const char *dst)
1582 {
1583   xbt_assert1(rc && src
1584               && dst,
1585               "Invalid params for \"get_route\" function at AS \"%s\"",
1586               rc->name);
1587
1588   /* set utils vars */
1589   routing_component_dijkstra_t routing = (routing_component_dijkstra_t) rc;
1590
1591   generic_src_dst_check(rc, src, dst);
1592   int *src_id = xbt_dict_get_or_null(routing->generic_routing.to_index, src);
1593   int *dst_id = xbt_dict_get_or_null(routing->generic_routing.to_index, dst);
1594   xbt_assert2(src_id
1595               && dst_id,
1596               "Ask for route \"from\"(%s)  or \"to\"(%s) no found in the local table",
1597               src, dst);
1598
1599   /* create a result route */
1600   route_extended_t new_e_route = xbt_new0(s_route_extended_t, 1);
1601   new_e_route->generic_route.link_list =
1602       xbt_dynar_new(global_routing->size_of_link, NULL);
1603   new_e_route->src_gateway = NULL;
1604   new_e_route->dst_gateway = NULL;
1605
1606   int *pred_arr = NULL;
1607   int src_node_id = 0;
1608   int dst_node_id = 0;
1609   int *nodeid = NULL;
1610   int v;
1611   route_extended_t e_route;
1612   int size = 0;
1613   unsigned int cpt;
1614   void *link;
1615   xbt_dynar_t links = NULL;
1616   route_cache_element_t elm = NULL;
1617   xbt_dynar_t nodes = xbt_graph_get_nodes(routing->route_graph);
1618
1619   /* Use the graph_node id mapping set to quickly find the nodes */
1620   graph_node_map_element_t src_elm =
1621       graph_node_map_search(routing, *src_id);
1622   graph_node_map_element_t dst_elm =
1623       graph_node_map_search(routing, *dst_id);
1624   xbt_assert2(src_elm != NULL
1625               && dst_elm != NULL, "src %d or dst %d does not exist",
1626               *src_id, *dst_id);
1627   src_node_id = ((graph_node_data_t)
1628                  xbt_graph_node_get_data(src_elm->node))->graph_id;
1629   dst_node_id = ((graph_node_data_t)
1630                  xbt_graph_node_get_data(dst_elm->node))->graph_id;
1631
1632   /* if the src and dst are the same *//* fixed, missing in the previous version */
1633   if (src_node_id == dst_node_id) {
1634
1635     xbt_node_t node_s_v = xbt_dynar_get_as(nodes, src_node_id, xbt_node_t);
1636     xbt_node_t node_e_v = xbt_dynar_get_as(nodes, dst_node_id, xbt_node_t);
1637     xbt_edge_t edge =
1638         xbt_graph_get_edge(routing->route_graph, node_s_v, node_e_v);
1639
1640     xbt_assert2(edge != NULL, "no route between host %d and %d", *src_id,
1641                 *dst_id);
1642
1643     e_route = (route_extended_t) xbt_graph_edge_get_data(edge);
1644
1645     links = e_route->generic_route.link_list;
1646     xbt_dynar_foreach(links, cpt, link) {
1647       xbt_dynar_unshift(new_e_route->generic_route.link_list, &link);
1648     }
1649
1650     return new_e_route;
1651   }
1652
1653   if (routing->cached) {
1654     /*check if there is a cached predecessor list avail */
1655     elm = (route_cache_element_t)
1656         xbt_dict_get_or_null_ext(routing->route_cache, (char *) (&src_id),
1657                                  sizeof(int));
1658   }
1659
1660   if (elm) {                    /* cached mode and cache hit */
1661     pred_arr = elm->pred_arr;
1662   } else {                      /* not cached mode or cache miss */
1663     double *cost_arr = NULL;
1664     xbt_heap_t pqueue = NULL;
1665     int i = 0;
1666
1667     int nr_nodes = xbt_dynar_length(nodes);
1668     cost_arr = xbt_new0(double, nr_nodes);      /* link cost from src to other hosts */
1669     pred_arr = xbt_new0(int, nr_nodes); /* predecessors in path from src */
1670     pqueue = xbt_heap_new(nr_nodes, xbt_free);
1671
1672     /* initialize */
1673     cost_arr[src_node_id] = 0.0;
1674
1675     for (i = 0; i < nr_nodes; i++) {
1676       if (i != src_node_id) {
1677         cost_arr[i] = DBL_MAX;
1678       }
1679
1680       pred_arr[i] = 0;
1681
1682       /* initialize priority queue */
1683       nodeid = xbt_new0(int, 1);
1684       *nodeid = i;
1685       xbt_heap_push(pqueue, nodeid, cost_arr[i]);
1686
1687     }
1688
1689     /* apply dijkstra using the indexes from the graph's node array */
1690     while (xbt_heap_size(pqueue) > 0) {
1691       int *v_id = xbt_heap_pop(pqueue);
1692       xbt_node_t v_node = xbt_dynar_get_as(nodes, *v_id, xbt_node_t);
1693       xbt_dynar_t out_edges = xbt_graph_node_get_outedges(v_node);
1694       xbt_edge_t edge = NULL;
1695       unsigned int cursor;
1696
1697       xbt_dynar_foreach(out_edges, cursor, edge) {
1698         xbt_node_t u_node = xbt_graph_edge_get_target(edge);
1699         graph_node_data_t data = xbt_graph_node_get_data(u_node);
1700         int u_id = data->graph_id;
1701         route_extended_t tmp_e_route =
1702             (route_extended_t) xbt_graph_edge_get_data(edge);
1703         int cost_v_u = (tmp_e_route->generic_route.link_list)->used;    /* count of links, old model assume 1 */
1704
1705         if (cost_v_u + cost_arr[*v_id] < cost_arr[u_id]) {
1706           pred_arr[u_id] = *v_id;
1707           cost_arr[u_id] = cost_v_u + cost_arr[*v_id];
1708           nodeid = xbt_new0(int, 1);
1709           *nodeid = u_id;
1710           xbt_heap_push(pqueue, nodeid, cost_arr[u_id]);
1711         }
1712       }
1713
1714       /* free item popped from pqueue */
1715       xbt_free(v_id);
1716     }
1717
1718     xbt_free(cost_arr);
1719     xbt_heap_free(pqueue);
1720   }
1721
1722   /* compose route path with links */
1723   char *gw_src = NULL, *gw_dst =
1724       NULL, *prev_gw_src, *prev_gw_dst, *first_gw = NULL;
1725
1726   for (v = dst_node_id; v != src_node_id; v = pred_arr[v]) {
1727     xbt_node_t node_pred_v =
1728         xbt_dynar_get_as(nodes, pred_arr[v], xbt_node_t);
1729     xbt_node_t node_v = xbt_dynar_get_as(nodes, v, xbt_node_t);
1730     xbt_edge_t edge =
1731         xbt_graph_get_edge(routing->route_graph, node_pred_v, node_v);
1732
1733     xbt_assert2(edge != NULL, "no route between host %d and %d", *src_id,
1734                 *dst_id);
1735
1736     prev_gw_src = gw_src;
1737     prev_gw_dst = gw_dst;
1738
1739     e_route = (route_extended_t) xbt_graph_edge_get_data(edge);
1740     gw_src = e_route->src_gateway;
1741     gw_dst = e_route->dst_gateway;
1742
1743     if (v == dst_node_id)
1744       first_gw = gw_dst;
1745
1746     if (rc->hierarchy == SURF_ROUTING_RECURSIVE && v != dst_node_id
1747         && strcmp(gw_dst, prev_gw_src)) {
1748       xbt_dynar_t e_route_as_to_as =
1749           (*(global_routing->get_route)) (gw_dst, prev_gw_src);
1750       xbt_assert2(e_route_as_to_as, "no route between \"%s\" and \"%s\"",
1751                   gw_dst, prev_gw_src);
1752       links = e_route_as_to_as;
1753       int pos = 0;
1754       xbt_dynar_foreach(links, cpt, link) {
1755         xbt_dynar_insert_at(new_e_route->generic_route.link_list, pos,
1756                             &link);
1757         pos++;
1758       }
1759     }
1760
1761     links = e_route->generic_route.link_list;
1762     xbt_dynar_foreach(links, cpt, link) {
1763       xbt_dynar_unshift(new_e_route->generic_route.link_list, &link);
1764     }
1765     size++;
1766   }
1767
1768   if (rc->hierarchy == SURF_ROUTING_RECURSIVE) {
1769     new_e_route->src_gateway = xbt_strdup(gw_src);
1770     new_e_route->dst_gateway = xbt_strdup(first_gw);
1771   }
1772
1773   if (routing->cached && elm == NULL) {
1774     /* add to predecessor list of the current src-host to cache */
1775     elm = xbt_new0(struct route_cache_element, 1);
1776     elm->pred_arr = pred_arr;
1777     elm->size = size;
1778     xbt_dict_set_ext(routing->route_cache, (char *) (&src_id), sizeof(int),
1779                      (xbt_set_elm_t) elm, &route_cache_elem_free);
1780   }
1781
1782   if (!routing->cached)
1783     xbt_free(pred_arr);
1784
1785   return new_e_route;
1786 }
1787
1788 static void dijkstra_finalize(routing_component_t rc)
1789 {
1790   routing_component_dijkstra_t routing = (routing_component_dijkstra_t) rc;
1791
1792   if (routing) {
1793     xbt_graph_free_graph(routing->route_graph, &xbt_free,
1794                          &graph_edge_data_free, &xbt_free);
1795     xbt_dict_free(&routing->graph_node_map);
1796     if (routing->cached)
1797       xbt_dict_free(&routing->route_cache);
1798     /* Delete bypass dict */
1799     xbt_dict_free(&routing->generic_routing.bypassRoutes);
1800     /* Delete index dict */
1801     xbt_dict_free(&(routing->generic_routing.to_index));
1802     /* Delete structure */
1803     xbt_free(routing);
1804   }
1805 }
1806
1807 /* Creation routing model functions */
1808
1809 static void *model_dijkstra_both_create(int cached)
1810 {
1811   routing_component_dijkstra_t new_component =
1812       xbt_new0(s_routing_component_dijkstra_t, 1);
1813   new_component->generic_routing.set_processing_unit =
1814       generic_set_processing_unit;
1815   new_component->generic_routing.set_autonomous_system =
1816       generic_set_autonomous_system;
1817   new_component->generic_routing.set_route = generic_set_route;
1818   new_component->generic_routing.set_ASroute = generic_set_ASroute;
1819   new_component->generic_routing.set_bypassroute = generic_set_bypassroute;
1820   new_component->generic_routing.get_route = dijkstra_get_route;
1821   new_component->generic_routing.get_onelink_routes =
1822       dijkstra_get_onelink_routes;
1823   new_component->generic_routing.get_bypass_route =
1824       generic_get_bypassroute;
1825   new_component->generic_routing.finalize = dijkstra_finalize;
1826   new_component->cached = cached;
1827   new_component->generic_routing.to_index = xbt_dict_new();
1828   new_component->generic_routing.bypassRoutes = xbt_dict_new();
1829   new_component->generic_routing.parse_routes = xbt_dict_new();
1830   return new_component;
1831 }
1832
1833 static void *model_dijkstra_create(void)
1834 {
1835   return model_dijkstra_both_create(0);
1836 }
1837
1838 static void *model_dijkstracache_create(void)
1839 {
1840   return model_dijkstra_both_create(1);
1841 }
1842
1843 static void model_dijkstra_both_load(void)
1844 {
1845   /* use "surfxml_add_callback" to add a parse function call */
1846 }
1847
1848 static void model_dijkstra_both_unload(void)
1849 {
1850   /* use "surfxml_del_callback" to remove a parse function call */
1851 }
1852
1853 static void model_dijkstra_both_end(void)
1854 {
1855   routing_component_dijkstra_t routing =
1856       (routing_component_dijkstra_t) current_routing;
1857   xbt_dict_cursor_t cursor = NULL;
1858   char *key, *data, *end;
1859   const char *sep = "#";
1860   xbt_dynar_t keys;
1861   xbt_node_t node = NULL;
1862   unsigned int cursor2;
1863   xbt_dynar_t nodes = NULL;
1864   int src_id, dst_id;
1865   route_t route;
1866
1867   /* Create the topology graph */
1868   routing->route_graph = xbt_graph_new_graph(1, NULL);
1869   routing->graph_node_map = xbt_dict_new();
1870
1871   if (routing->cached)
1872     routing->route_cache = xbt_dict_new();
1873
1874   /* Put the routes in position */
1875   xbt_dict_foreach(routing->generic_routing.parse_routes, cursor, key, data) {
1876     keys = xbt_str_split_str(key, sep);
1877     src_id = strtol(xbt_dynar_get_as(keys, 0, char *), &end, 10);
1878     dst_id = strtol(xbt_dynar_get_as(keys, 1, char *), &end, 10);
1879     route_extended_t e_route =
1880         generic_new_extended_route(current_routing->hierarchy, data, 0);
1881     route_new_dijkstra(routing, src_id, dst_id, e_route);
1882     xbt_dynar_free(&keys);
1883   }
1884
1885   /* delete the parse table */
1886   xbt_dict_foreach(routing->generic_routing.parse_routes, cursor, key, data) {
1887     route = (route_t) data;
1888     xbt_dynar_free(&(route->link_list));
1889     xbt_free(data);
1890   }
1891
1892   /* delete parse dict */
1893   xbt_dict_free(&(routing->generic_routing.parse_routes));
1894
1895   /* Add the loopback if needed */
1896   if (current_routing->hierarchy == SURF_ROUTING_BASE)
1897     add_loopback_dijkstra(routing);
1898
1899   /* initialize graph indexes in nodes after graph has been built */
1900   nodes = xbt_graph_get_nodes(routing->route_graph);
1901
1902   xbt_dynar_foreach(nodes, cursor2, node) {
1903     graph_node_data_t data = xbt_graph_node_get_data(node);
1904     data->graph_id = cursor2;
1905   }
1906
1907 }
1908
1909 #ifdef HAVE_PCRE_LIB
1910 /* ************************************************** */
1911 /* ************** RULE-BASED ROUTING **************** */
1912
1913 /* Routing model structure */
1914
1915 typedef struct {
1916   s_routing_component_t generic_routing;
1917   xbt_dict_t dict_processing_units;
1918   xbt_dict_t dict_autonomous_systems;
1919   xbt_dynar_t list_route;
1920   xbt_dynar_t list_ASroute;
1921 } s_routing_component_rulebased_t, *routing_component_rulebased_t;
1922
1923 typedef struct s_rule_route s_rule_route_t, *rule_route_t;
1924 typedef struct s_rule_route_extended s_rule_route_extended_t,
1925     *rule_route_extended_t;
1926
1927 struct s_rule_route {
1928   xbt_dynar_t re_str_link;      // dynar of char*
1929   pcre *re_src;
1930   pcre *re_dst;
1931 };
1932
1933 struct s_rule_route_extended {
1934   s_rule_route_t generic_rule_route;
1935   char *re_src_gateway;
1936   char *re_dst_gateway;
1937 };
1938
1939 static void rule_route_free(void *e)
1940 {
1941   rule_route_t *elem = (rule_route_t *) (e);
1942   if (*elem) {
1943     xbt_dynar_free(&(*elem)->re_str_link);
1944     pcre_free((*elem)->re_src);
1945     pcre_free((*elem)->re_dst);
1946     xbt_free((*elem));
1947   }
1948   (*elem) = NULL;
1949 }
1950
1951 static void rule_route_extended_free(void *e)
1952 {
1953   rule_route_extended_t *elem = (rule_route_extended_t *) e;
1954   if (*elem) {
1955     xbt_dynar_free(&(*elem)->generic_rule_route.re_str_link);
1956     pcre_free((*elem)->generic_rule_route.re_src);
1957     pcre_free((*elem)->generic_rule_route.re_dst);
1958     xbt_free((*elem)->re_src_gateway);
1959     xbt_free((*elem)->re_dst_gateway);
1960     xbt_free((*elem));
1961   }
1962 }
1963
1964 /* Parse routing model functions */
1965
1966 static void model_rulebased_set_processing_unit(routing_component_t rc,
1967                                                 const char *name)
1968 {
1969   routing_component_rulebased_t routing =
1970       (routing_component_rulebased_t) rc;
1971   xbt_dict_set(routing->dict_processing_units, name, (void *) (-1), NULL);
1972 }
1973
1974 static void model_rulebased_set_autonomous_system(routing_component_t rc,
1975                                                   const char *name)
1976 {
1977   routing_component_rulebased_t routing =
1978       (routing_component_rulebased_t) rc;
1979   xbt_dict_set(routing->dict_autonomous_systems, name, (void *) (-1),
1980                NULL);
1981 }
1982
1983 static void model_rulebased_set_route(routing_component_t rc,
1984                                       const char *src, const char *dst,
1985                                       route_t route)
1986 {
1987   routing_component_rulebased_t routing =
1988       (routing_component_rulebased_t) rc;
1989   rule_route_t ruleroute = xbt_new0(s_rule_route_t, 1);
1990   const char *error;
1991   int erroffset;
1992   ruleroute->re_src = pcre_compile(src, 0, &error, &erroffset, NULL);
1993   xbt_assert3(ruleroute->re_src,
1994               "PCRE compilation failed at offset %d (\"%s\"): %s\n",
1995               erroffset, src, error);
1996   ruleroute->re_dst = pcre_compile(dst, 0, &error, &erroffset, NULL);
1997   xbt_assert3(ruleroute->re_src,
1998               "PCRE compilation failed at offset %d (\"%s\"): %s\n",
1999               erroffset, dst, error);
2000   ruleroute->re_str_link = route->link_list;
2001   xbt_dynar_push(routing->list_route, &ruleroute);
2002   xbt_free(route);
2003 }
2004
2005 static void model_rulebased_set_ASroute(routing_component_t rc,
2006                                         const char *src, const char *dst,
2007                                         route_extended_t route)
2008 {
2009   routing_component_rulebased_t routing =
2010       (routing_component_rulebased_t) rc;
2011   rule_route_extended_t ruleroute_e = xbt_new0(s_rule_route_extended_t, 1);
2012   const char *error;
2013   int erroffset;
2014   ruleroute_e->generic_rule_route.re_src =
2015       pcre_compile(src, 0, &error, &erroffset, NULL);
2016   xbt_assert3(ruleroute_e->generic_rule_route.re_src,
2017               "PCRE compilation failed at offset %d (\"%s\"): %s\n",
2018               erroffset, src, error);
2019   ruleroute_e->generic_rule_route.re_dst =
2020       pcre_compile(dst, 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, dst, error);
2024   ruleroute_e->generic_rule_route.re_str_link =
2025       route->generic_route.link_list;
2026   ruleroute_e->re_src_gateway = route->src_gateway;
2027   ruleroute_e->re_dst_gateway = route->dst_gateway;
2028   xbt_dynar_push(routing->list_ASroute, &ruleroute_e);
2029   xbt_free(route->src_gateway);
2030   xbt_free(route->dst_gateway);
2031   xbt_free(route);
2032 }
2033
2034 static void model_rulebased_set_bypassroute(routing_component_t rc,
2035                                             const char *src,
2036                                             const char *dst,
2037                                             route_extended_t e_route)
2038 {
2039   xbt_die("bypass routing not supported for Route-Based model");
2040 }
2041
2042 #define BUFFER_SIZE 4096        /* result buffer size */
2043 #define OVECCOUNT 30            /* should be a multiple of 3 */
2044
2045 static char *remplace(char *value, const char **src_list, int src_size,
2046                       const char **dst_list, int dst_size)
2047 {
2048
2049   char result_result[BUFFER_SIZE];
2050   int i_result_buffer;
2051   int value_length = (int) strlen(value);
2052   int number = 0;
2053
2054   int i = 0;
2055   i_result_buffer = 0;
2056   do {
2057     if (value[i] == '$') {
2058       i++;                      // skip $
2059
2060       // find the number      
2061       int number_length = 0;
2062       while ('0' <= value[i + number_length]
2063              && value[i + number_length] <= '9') {
2064         number_length++;
2065       }
2066       xbt_assert2(number_length != 0,
2067                   "bad string parameter, no number indication, at offset: %d (\"%s\")",
2068                   i, value);
2069
2070       // solve number
2071       number = atoi(value + i);
2072       i = i + number_length;
2073       xbt_assert2(i + 2 < value_length,
2074                   "bad string parameter, too few chars, at offset: %d (\"%s\")",
2075                   i, value);
2076
2077       // solve the indication
2078       const char **param_list;
2079       int param_size;
2080       if (value[i] == 's' && value[i + 1] == 'r' && value[i + 2] == 'c') {
2081         param_list = src_list;
2082         param_size = src_size;
2083       } else if (value[i] == 'd' && value[i + 1] == 's'
2084                  && value[i + 2] == 't') {
2085         param_list = dst_list;
2086         param_size = dst_size;
2087       } else {
2088         xbt_assert2(0,
2089                     "bad string parameter, support only \"src\" and \"dst\", at offset: %d (\"%s\")",
2090                     i, value);
2091       }
2092       i = i + 3;
2093
2094       xbt_assert4(param_size >= number,
2095                   "bad string parameter, not enough length param_size, at offset: %d (\"%s\") %d %d",
2096                   i, value, param_size, number);
2097
2098       const char *param = param_list[number];
2099       int size = strlen(param);
2100       int cp;
2101       for (cp = 0; cp < size; cp++) {
2102         result_result[i_result_buffer] = param[cp];
2103         i_result_buffer++;
2104         if (i_result_buffer >= BUFFER_SIZE)
2105           break;
2106       }
2107     } else {
2108       result_result[i_result_buffer] = value[i];
2109       i_result_buffer++;
2110       i++;                      // next char
2111     }
2112
2113   } while (i < value_length && i_result_buffer < BUFFER_SIZE);
2114
2115   xbt_assert2(i_result_buffer < BUFFER_SIZE,
2116               "solving string \"%s\", small buffer size (%d)", value,
2117               BUFFER_SIZE);
2118   result_result[i_result_buffer] = 0;
2119   return xbt_strdup(result_result);
2120 }
2121
2122 static route_extended_t rulebased_get_route(routing_component_t rc,
2123                                             const char *src,
2124                                             const char *dst);
2125 static xbt_dynar_t rulebased_get_onelink_routes(routing_component_t rc)
2126 {
2127   xbt_dynar_t ret = xbt_dynar_new (sizeof(onelink_t), xbt_free);
2128   routing_component_rulebased_t routing = (routing_component_rulebased_t)rc;
2129
2130   xbt_dict_cursor_t c1 = NULL;
2131   char *k1, *d1;
2132
2133   //find router
2134   char *router = NULL;
2135   xbt_dict_foreach(routing->dict_processing_units, c1, k1, d1) {
2136     if (strstr (k1, "router")){
2137       router = k1;
2138     }
2139   }
2140   if (!router){
2141     xbt_die ("rulebased_get_onelink_routes works only if the AS is a cluster, sorry.");
2142   }
2143
2144   xbt_dict_foreach(routing->dict_processing_units, c1, k1, d1) {
2145     route_extended_t route = rulebased_get_route (rc, router, k1);
2146
2147     int number_of_links = xbt_dynar_length(route->generic_route.link_list);
2148     if (number_of_links != 3) {
2149       xbt_die ("rulebased_get_onelink_routes works only if the AS is a cluster, sorry.");
2150     }
2151
2152     void *link_ptr;
2153     xbt_dynar_get_cpy (route->generic_route.link_list, 2, &link_ptr);
2154     onelink_t onelink = xbt_new0 (s_onelink_t, 1);
2155     onelink->src = xbt_strdup (k1);
2156     onelink->dst = xbt_strdup (router);
2157     onelink->link_ptr = link_ptr;
2158     xbt_dynar_push (ret, &onelink);
2159   }
2160   return ret;
2161 }
2162
2163 /* Business methods */
2164 static route_extended_t rulebased_get_route(routing_component_t rc,
2165                                             const char *src,
2166                                             const char *dst)
2167 {
2168   xbt_assert1(rc && src
2169               && dst,
2170               "Invalid params for \"get_route\" function at AS \"%s\"",
2171               rc->name);
2172
2173   /* set utils vars */
2174   routing_component_rulebased_t routing =
2175       (routing_component_rulebased_t) rc;
2176
2177   int are_processing_units;
2178   xbt_dynar_t rule_list;
2179   if (xbt_dict_get_or_null(routing->dict_processing_units, src)
2180       && xbt_dict_get_or_null(routing->dict_processing_units, dst)) {
2181     are_processing_units = 1;
2182     rule_list = routing->list_route;
2183   } else if (xbt_dict_get_or_null(routing->dict_autonomous_systems, src)
2184              && xbt_dict_get_or_null(routing->dict_autonomous_systems,
2185                                      dst)) {
2186     are_processing_units = 0;
2187     rule_list = routing->list_ASroute;
2188   } else
2189     xbt_assert2(NULL,
2190                 "Ask for route \"from\"(%s)  or \"to\"(%s) no found in the local table",
2191                 src, dst);
2192
2193   int rc_src = -1;
2194   int rc_dst = -1;
2195   int src_length = (int) strlen(src);
2196   int dst_length = (int) strlen(dst);
2197
2198   xbt_dynar_t links_list =
2199       xbt_dynar_new(global_routing->size_of_link, NULL);
2200
2201   rule_route_t ruleroute;
2202   unsigned int cpt;
2203   int ovector_src[OVECCOUNT];
2204   int ovector_dst[OVECCOUNT];
2205   const char **list_src = NULL;
2206   const char **list_dst = NULL;
2207   xbt_dynar_foreach(rule_list, cpt, ruleroute) {
2208     rc_src =
2209         pcre_exec(ruleroute->re_src, NULL, src, src_length, 0, 0,
2210                   ovector_src, OVECCOUNT);
2211     if (rc_src >= 0) {
2212       rc_dst =
2213           pcre_exec(ruleroute->re_dst, NULL, dst, dst_length, 0, 0,
2214                     ovector_dst, OVECCOUNT);
2215       if (rc_dst >= 0) {
2216         xbt_assert1(!pcre_get_substring_list
2217                     (src, ovector_src, rc_src, &list_src),
2218                     "error solving substring list for src \"%s\"", src);
2219         xbt_assert1(!pcre_get_substring_list
2220                     (dst, ovector_dst, rc_dst, &list_dst),
2221                     "error solving substring list for src \"%s\"", dst);
2222         char *link_name;
2223         xbt_dynar_foreach(ruleroute->re_str_link, cpt, link_name) {
2224           char *new_link_name =
2225               remplace(link_name, list_src, rc_src, list_dst, rc_dst);
2226           void *link =
2227               xbt_dict_get_or_null(surf_network_model->resource_set,
2228                                    new_link_name);
2229           if (link)
2230             xbt_dynar_push(links_list, &link);
2231           else
2232             THROW1(mismatch_error, 0, "Link %s not found", new_link_name);
2233           xbt_free(new_link_name);
2234         }
2235       }
2236     }
2237     if (rc_src >= 0 && rc_dst >= 0)
2238       break;
2239   }
2240
2241   route_extended_t new_e_route = NULL;
2242   if (rc_src >= 0 && rc_dst >= 0) {
2243     new_e_route = xbt_new0(s_route_extended_t, 1);
2244     new_e_route->generic_route.link_list = links_list;
2245   } else if (!strcmp(src, dst) && are_processing_units) {
2246     new_e_route = xbt_new0(s_route_extended_t, 1);
2247     xbt_dynar_push(links_list, &(global_routing->loopback));
2248     new_e_route->generic_route.link_list = links_list;
2249   } else {
2250     xbt_dynar_free(&link_list);
2251   }
2252
2253   if (!are_processing_units && new_e_route) {
2254     rule_route_extended_t ruleroute_extended =
2255         (rule_route_extended_t) ruleroute;
2256     new_e_route->src_gateway =
2257         remplace(ruleroute_extended->re_src_gateway, list_src, rc_src,
2258                  list_dst, rc_dst);
2259     new_e_route->dst_gateway =
2260         remplace(ruleroute_extended->re_dst_gateway, list_src, rc_src,
2261                  list_dst, rc_dst);
2262   }
2263
2264   if (list_src)
2265     pcre_free_substring_list(list_src);
2266   if (list_dst)
2267     pcre_free_substring_list(list_dst);
2268
2269   return new_e_route;
2270 }
2271
2272 static route_extended_t rulebased_get_bypass_route(routing_component_t rc,
2273                                                    const char *src,
2274                                                    const char *dst)
2275 {
2276   return NULL;
2277 }
2278
2279 static void rulebased_finalize(routing_component_t rc)
2280 {
2281   routing_component_rulebased_t routing =
2282       (routing_component_rulebased_t) rc;
2283   if (routing) {
2284     xbt_dict_free(&routing->dict_processing_units);
2285     xbt_dict_free(&routing->dict_autonomous_systems);
2286     xbt_dynar_free(&routing->list_route);
2287     xbt_dynar_free(&routing->list_ASroute);
2288     /* Delete structure */
2289     xbt_free(routing);
2290   }
2291 }
2292
2293 /* Creation routing model functions */
2294 static void *model_rulebased_create(void)
2295 {
2296   routing_component_rulebased_t new_component =
2297       xbt_new0(s_routing_component_rulebased_t, 1);
2298   new_component->generic_routing.set_processing_unit =
2299       model_rulebased_set_processing_unit;
2300   new_component->generic_routing.set_autonomous_system =
2301       model_rulebased_set_autonomous_system;
2302   new_component->generic_routing.set_route = model_rulebased_set_route;
2303   new_component->generic_routing.set_ASroute = model_rulebased_set_ASroute;
2304   new_component->generic_routing.set_bypassroute = model_rulebased_set_bypassroute;
2305   new_component->generic_routing.get_onelink_routes = rulebased_get_onelink_routes;
2306   new_component->generic_routing.get_route = rulebased_get_route;
2307   new_component->generic_routing.get_bypass_route = generic_get_bypassroute;       //rulebased_get_bypass_route;
2308   new_component->generic_routing.finalize = rulebased_finalize;
2309   /* initialization of internal structures */
2310   new_component->dict_processing_units = xbt_dict_new();
2311   new_component->dict_autonomous_systems = xbt_dict_new();
2312   new_component->list_route = xbt_dynar_new(sizeof(rule_route_t), &rule_route_free);
2313   new_component->list_ASroute =
2314       xbt_dynar_new(sizeof(rule_route_extended_t),
2315                     &rule_route_extended_free);
2316   return new_component;
2317 }
2318
2319 static void model_rulebased_load(void)
2320 {
2321   /* use "surfxml_add_callback" to add a parse function call */
2322 }
2323
2324 static void model_rulebased_unload(void)
2325 {
2326   /* use "surfxml_del_callback" to remove a parse function call */
2327 }
2328
2329 static void model_rulebased_end(void)
2330 {
2331 }
2332
2333 #endif                          /* HAVE_PCRE_LIB */
2334
2335 /* ************************************************************************** */
2336 /* ******************************* NO ROUTING ******************************* */
2337
2338 /* Routing model structure */
2339 typedef struct {
2340   s_routing_component_t generic_routing;
2341 } s_routing_component_none_t, *routing_component_none_t;
2342
2343 /* Business methods */
2344 static xbt_dynar_t none_get_onelink_routes(routing_component_t rc)
2345 {
2346   return NULL;
2347 }
2348
2349 static route_extended_t none_get_route(routing_component_t rc,
2350                                        const char *src, const char *dst)
2351 {
2352   return NULL;
2353 }
2354
2355 static route_extended_t none_get_bypass_route(routing_component_t rc,
2356                                               const char *src,
2357                                               const char *dst)
2358 {
2359   return NULL;
2360 }
2361
2362 static void none_finalize(routing_component_t rc)
2363 {
2364   xbt_free(rc);
2365 }
2366
2367 static void none_set_processing_unit(routing_component_t rc,
2368                                      const char *name)
2369 {
2370 }
2371
2372 static void none_set_autonomous_system(routing_component_t rc,
2373                                        const char *name)
2374 {
2375 }
2376
2377 /* Creation routing model functions */
2378 static void *model_none_create(void)
2379 {
2380   routing_component_none_t new_component =
2381       xbt_new0(s_routing_component_none_t, 1);
2382   new_component->generic_routing.set_processing_unit =
2383       none_set_processing_unit;
2384   new_component->generic_routing.set_autonomous_system =
2385       none_set_autonomous_system;
2386   new_component->generic_routing.set_route = NULL;
2387   new_component->generic_routing.set_ASroute = NULL;
2388   new_component->generic_routing.set_bypassroute = NULL;
2389   new_component->generic_routing.get_route = none_get_route;
2390   new_component->generic_routing.get_onelink_routes =
2391       none_get_onelink_routes;
2392   new_component->generic_routing.get_bypass_route = none_get_bypass_route;
2393   new_component->generic_routing.finalize = none_finalize;
2394   return new_component;
2395 }
2396
2397 static void model_none_load(void)
2398 {
2399 }
2400
2401 static void model_none_unload(void)
2402 {
2403 }
2404
2405 static void model_none_end(void)
2406 {
2407 }
2408
2409 /* ************************************************** */
2410 /* ********** PATERN FOR NEW ROUTING **************** */
2411
2412 /* The minimal configuration of a new routing model need the next functions,
2413  * also you need to set at the start of the file, the new model in the model
2414  * list. Remember keep the null ending of the list.
2415  */
2416 /*** Routing model structure ***/
2417 // typedef struct {
2418 //   s_routing_component_t generic_routing;
2419 //   /* things that your routing model need */
2420 // } s_routing_component_NEW_t,*routing_component_NEW_t;
2421
2422 /*** Parse routing model functions ***/
2423 // static void model_NEW_set_processing_unit(routing_component_t rc, const char* name) {}
2424 // static void model_NEW_set_autonomous_system(routing_component_t rc, const char* name) {}
2425 // static void model_NEW_set_route(routing_component_t rc, const char* src, const char* dst, route_t route) {}
2426 // static void model_NEW_set_ASroute(routing_component_t rc, const char* src, const char* dst, route_extended_t route) {}
2427 // static void model_NEW_set_bypassroute(routing_component_t rc, const char* src, const char* dst, route_extended_t e_route) {}
2428
2429 /*** Business methods ***/
2430 // static route_extended_t NEW_get_route(routing_component_t rc, const char* src,const char* dst) {return NULL;}
2431 // static route_extended_t NEW_get_bypass_route(routing_component_t rc, const char* src,const char* dst) {return NULL;}
2432 // static void NEW_finalize(routing_component_t rc) { xbt_free(rc);}
2433
2434 /*** Creation routing model functions ***/
2435 // static void* model_NEW_create(void) {
2436 //   routing_component_NEW_t new_component =  xbt_new0(s_routing_component_NEW_t,1);
2437 //   new_component->generic_routing.set_processing_unit = model_NEW_set_processing_unit;
2438 //   new_component->generic_routing.set_autonomous_system = model_NEW_set_autonomous_system;
2439 //   new_component->generic_routing.set_route = model_NEW_set_route;
2440 //   new_component->generic_routing.set_ASroute = model_NEW_set_ASroute;
2441 //   new_component->generic_routing.set_bypassroute = model_NEW_set_bypassroute;
2442 //   new_component->generic_routing.get_route = NEW_get_route;
2443 //   new_component->generic_routing.get_bypass_route = NEW_get_bypass_route;
2444 //   new_component->generic_routing.finalize = NEW_finalize;
2445 //   /* initialization of internal structures */
2446 //   return new_component;
2447 // } /* mandatory */
2448 // static void  model_NEW_load(void) {}   /* mandatory */
2449 // static void  model_NEW_unload(void) {} /* mandatory */
2450 // static void  model_NEW_end(void) {}    /* mandatory */
2451
2452 /* ************************************************************************** */
2453 /* ************************* GENERIC PARSE FUNCTIONS ************************ */
2454
2455 static void generic_set_processing_unit(routing_component_t rc,
2456                                         const char *name)
2457 {
2458   DEBUG1("Load process unit \"%s\"", name);
2459   int *id = xbt_new0(int, 1);
2460   xbt_dict_t _to_index;
2461   _to_index = current_routing->to_index;
2462   *id = xbt_dict_length(_to_index);
2463   xbt_dict_set(_to_index, name, id, xbt_free);
2464 }
2465
2466 static void generic_set_autonomous_system(routing_component_t rc,
2467                                           const char *name)
2468 {
2469   DEBUG1("Load Autonomous system \"%s\"", name);
2470   int *id = xbt_new0(int, 1);
2471   xbt_dict_t _to_index;
2472   _to_index = current_routing->to_index;
2473   *id = xbt_dict_length(_to_index);
2474   xbt_dict_set(_to_index, name, id, xbt_free);
2475 }
2476
2477 static void generic_set_route(routing_component_t rc, const char *src,
2478                               const char *dst, route_t route)
2479 {
2480   DEBUG2("Load Route from \"%s\" to \"%s\"", src, dst);
2481   xbt_dict_t _parse_routes;
2482   xbt_dict_t _to_index;
2483   char *route_name;
2484   int *src_id, *dst_id;
2485   _to_index = current_routing->to_index;
2486   //TODO
2487   _parse_routes = current_routing->parse_routes;
2488
2489   src_id = xbt_dict_get_or_null(_to_index, src);
2490   dst_id = xbt_dict_get_or_null(_to_index, dst);
2491
2492   xbt_assert2(src_id
2493               && dst_id, "Network elements %s or %s not found", src, dst);
2494   route_name = bprintf("%d#%d", *src_id, *dst_id);
2495
2496   xbt_assert2(xbt_dynar_length(route->link_list) > 0,
2497               "Invalid count of links, must be greater than zero (%s,%s)",
2498               src, dst);
2499   xbt_assert2(!xbt_dict_get_or_null(_parse_routes, route_name),
2500               "The route between \"%s\" and \"%s\" already exist", src,
2501               dst);
2502
2503   xbt_dict_set(_parse_routes, route_name, route, NULL);
2504   xbt_free(route_name);
2505 }
2506
2507 static void generic_set_ASroute(routing_component_t rc, const char *src,
2508                                 const char *dst, route_extended_t e_route)
2509 {
2510   DEBUG4("Load ASroute from \"%s(%s)\" to \"%s(%s)\"", src,
2511          e_route->src_gateway, dst, e_route->dst_gateway);
2512
2513   xbt_dict_t _parse_routes;
2514   xbt_dict_t _to_index;
2515   char *route_name;
2516   int *src_id, *dst_id;
2517   _to_index = current_routing->to_index;
2518   //TODO
2519   _parse_routes = current_routing->parse_routes;
2520
2521   src_id = xbt_dict_get_or_null(_to_index, src);
2522   dst_id = xbt_dict_get_or_null(_to_index, dst);
2523
2524   xbt_assert2(src_id
2525               && dst_id, "Network elements %s or %s not found", src, dst);
2526   route_name = bprintf("%d#%d", *src_id, *dst_id);
2527
2528   xbt_assert2(xbt_dynar_length(e_route->generic_route.link_list) > 0,
2529               "Invalid count of links, must be greater than zero (%s,%s)",
2530               src, dst);
2531   xbt_assert4(!xbt_dict_get_or_null(_parse_routes, route_name),
2532               "The route between \"%s\"(\"%s\") and \"%s\"(\"%s\") already exist",
2533               src, e_route->src_gateway, dst, e_route->dst_gateway);
2534
2535   xbt_dict_set(_parse_routes, route_name, e_route, NULL);
2536   xbt_free(route_name);
2537 }
2538
2539 static void generic_set_bypassroute(routing_component_t rc,
2540                                     const char *src, const char *dst,
2541                                     route_extended_t e_route)
2542 {
2543   DEBUG2("Load bypassRoute from \"%s\" to \"%s\"", src, dst);
2544   xbt_dict_t dict_bypassRoutes = rc->bypassRoutes;
2545   char *route_name;
2546
2547   route_name = bprintf("%s#%s", src, dst);
2548   xbt_assert2(xbt_dynar_length(e_route->generic_route.link_list) > 0,
2549               "Invalid count of links, must be greater than zero (%s,%s)",
2550               src, dst);
2551   xbt_assert4(!xbt_dict_get_or_null(dict_bypassRoutes, route_name),
2552               "The bypass route between \"%s\"(\"%s\") and \"%s\"(\"%s\") already exist",
2553               src, e_route->src_gateway, dst, e_route->dst_gateway);
2554
2555   route_extended_t new_e_route =
2556       generic_new_extended_route(SURF_ROUTING_RECURSIVE, e_route, 0);
2557   xbt_dynar_free(&(e_route->generic_route.link_list));
2558   xbt_free(e_route);
2559
2560   xbt_dict_set(dict_bypassRoutes, route_name, new_e_route,
2561                (void (*)(void *)) generic_free_extended_route);
2562   xbt_free(route_name);
2563 }
2564
2565 /* ************************************************************************** */
2566 /* *********************** GENERIC BUSINESS METHODS ************************* */
2567
2568 static xbt_dynar_t generic_get_onelink_routes(routing_component_t rc)
2569 {
2570   xbt_die("\"generic_get_onelink_routes\" not implemented yet");
2571 }
2572
2573 static route_extended_t generic_get_bypassroute(routing_component_t rc,
2574                                                 const char *src,
2575                                                 const char *dst)
2576 {
2577   xbt_dict_t dict_bypassRoutes = rc->bypassRoutes;
2578   routing_component_t src_as, dst_as;
2579   int index_src, index_dst;
2580   xbt_dynar_t path_src = NULL;
2581   xbt_dynar_t path_dst = NULL;
2582   routing_component_t current = NULL;
2583   routing_component_t *current_src = NULL;
2584   routing_component_t *current_dst = NULL;
2585
2586   /* (1) find the as where the src and dst are located */
2587   src_as = ((network_element_info_t)
2588             xbt_dict_get_or_null(global_routing->where_network_elements,
2589                                  src))->rc_component;
2590   dst_as = ((network_element_info_t)
2591             xbt_dict_get_or_null(global_routing->where_network_elements,
2592                                  dst))->rc_component;
2593   xbt_assert2(src_as
2594               && dst_as,
2595               "Ask for route \"from\"(%s) or \"to\"(%s) no found", src,
2596               dst);
2597
2598   /* (2) find the path to the root routing component */
2599   path_src = xbt_dynar_new(sizeof(routing_component_t), NULL);
2600   current = src_as;
2601   while (current != NULL) {
2602     xbt_dynar_push(path_src, &current);
2603     current = current->routing_father;
2604   }
2605   path_dst = xbt_dynar_new(sizeof(routing_component_t), NULL);
2606   current = dst_as;
2607   while (current != NULL) {
2608     xbt_dynar_push(path_dst, &current);
2609     current = current->routing_father;
2610   }
2611
2612   /* (3) find the common father */
2613   index_src = (path_src->used) - 1;
2614   index_dst = (path_dst->used) - 1;
2615   current_src = xbt_dynar_get_ptr(path_src, index_src);
2616   current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
2617   while (index_src >= 0 && index_dst >= 0 && *current_src == *current_dst) {
2618     routing_component_t *tmp_src, *tmp_dst;
2619     tmp_src = xbt_dynar_pop_ptr(path_src);
2620     tmp_dst = xbt_dynar_pop_ptr(path_dst);
2621     index_src--;
2622     index_dst--;
2623     current_src = xbt_dynar_get_ptr(path_src, index_src);
2624     current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
2625   }
2626
2627   int max_index_src = (path_src->used) - 1;
2628   int max_index_dst = (path_dst->used) - 1;
2629
2630   int max_index = max(max_index_src, max_index_dst);
2631   int i, max;
2632
2633   route_extended_t e_route_bypass = NULL;
2634
2635   for (max = 0; max <= max_index; max++) {
2636     for (i = 0; i < max; i++) {
2637       if (i <= max_index_src && max <= max_index_dst) {
2638         char *route_name = bprintf("%s#%s",
2639                                    (*(routing_component_t *)
2640                                     (xbt_dynar_get_ptr
2641                                      (path_src, i)))->name,
2642                                    (*(routing_component_t *)
2643                                     (xbt_dynar_get_ptr
2644                                      (path_dst, max)))->name);
2645         e_route_bypass =
2646             xbt_dict_get_or_null(dict_bypassRoutes, route_name);
2647         xbt_free(route_name);
2648       }
2649       if (e_route_bypass)
2650         break;
2651       if (max <= max_index_src && i <= max_index_dst) {
2652         char *route_name = bprintf("%s#%s",
2653                                    (*(routing_component_t *)
2654                                     (xbt_dynar_get_ptr
2655                                      (path_src, max)))->name,
2656                                    (*(routing_component_t *)
2657                                     (xbt_dynar_get_ptr
2658                                      (path_dst, i)))->name);
2659         e_route_bypass =
2660             xbt_dict_get_or_null(dict_bypassRoutes, route_name);
2661         xbt_free(route_name);
2662       }
2663       if (e_route_bypass)
2664         break;
2665     }
2666
2667     if (e_route_bypass)
2668       break;
2669
2670     if (max <= max_index_src && max <= max_index_dst) {
2671       char *route_name = bprintf("%s#%s",
2672                                  (*(routing_component_t *)
2673                                   (xbt_dynar_get_ptr
2674                                    (path_src, max)))->name,
2675                                  (*(routing_component_t *)
2676                                   (xbt_dynar_get_ptr
2677                                    (path_dst, max)))->name);
2678       e_route_bypass = xbt_dict_get_or_null(dict_bypassRoutes, route_name);
2679       xbt_free(route_name);
2680     }
2681     if (e_route_bypass)
2682       break;
2683   }
2684
2685   xbt_dynar_free(&path_src);
2686   xbt_dynar_free(&path_dst);
2687
2688   route_extended_t new_e_route = NULL;
2689
2690   if (e_route_bypass) {
2691     void *link;
2692     unsigned int cpt = 0;
2693     new_e_route = xbt_new0(s_route_extended_t, 1);
2694     new_e_route->src_gateway = xbt_strdup(e_route_bypass->src_gateway);
2695     new_e_route->dst_gateway = xbt_strdup(e_route_bypass->dst_gateway);
2696     new_e_route->generic_route.link_list =
2697         xbt_dynar_new(global_routing->size_of_link, NULL);
2698     xbt_dynar_foreach(e_route_bypass->generic_route.link_list, cpt, link) {
2699       xbt_dynar_push(new_e_route->generic_route.link_list, &link);
2700     }
2701   }
2702
2703   return new_e_route;
2704 }
2705
2706 /* ************************************************************************** */
2707 /* ************************* GENERIC AUX FUNCTIONS ************************** */
2708
2709 static route_extended_t
2710 generic_new_extended_route(e_surf_routing_hierarchy_t hierarchy,
2711                            void *data, int order)
2712 {
2713
2714   char *link_name;
2715   route_extended_t e_route, new_e_route;
2716   route_t route;
2717   unsigned int cpt;
2718   xbt_dynar_t links = NULL, links_id = NULL;
2719
2720   new_e_route = xbt_new0(s_route_extended_t, 1);
2721   new_e_route->generic_route.link_list =
2722       xbt_dynar_new(global_routing->size_of_link, NULL);
2723   new_e_route->src_gateway = NULL;
2724   new_e_route->dst_gateway = NULL;
2725
2726   xbt_assert0(hierarchy == SURF_ROUTING_BASE
2727               || hierarchy == SURF_ROUTING_RECURSIVE,
2728               "the hierarchy type is not defined");
2729
2730   if (hierarchy == SURF_ROUTING_BASE) {
2731
2732     route = (route_t) data;
2733     links = route->link_list;
2734
2735   } else if (hierarchy == SURF_ROUTING_RECURSIVE) {
2736
2737     e_route = (route_extended_t) data;
2738     xbt_assert0(e_route->src_gateway
2739                 && e_route->dst_gateway, "bad gateway, is null");
2740     links = e_route->generic_route.link_list;
2741
2742     /* remeber not erase the gateway names */
2743     new_e_route->src_gateway = e_route->src_gateway;
2744     new_e_route->dst_gateway = e_route->dst_gateway;
2745   }
2746
2747   links_id = new_e_route->generic_route.link_list;
2748
2749   xbt_dynar_foreach(links, cpt, link_name) {
2750
2751     void *link =
2752         xbt_dict_get_or_null(surf_network_model->resource_set, link_name);
2753     if (link) {
2754       if (order)
2755         xbt_dynar_push(links_id, &link);
2756       else
2757         xbt_dynar_unshift(links_id, &link);
2758     } else
2759       THROW1(mismatch_error, 0, "Link %s not found", link_name);
2760   }
2761
2762   return new_e_route;
2763 }
2764
2765 static void generic_free_route(route_t route)
2766 {
2767   if (route) {
2768     xbt_dynar_free(&(route->link_list));
2769     xbt_free(route);
2770   }
2771 }
2772
2773 static void generic_free_extended_route(route_extended_t e_route)
2774 {
2775   if (e_route) {
2776     xbt_dynar_free(&(e_route->generic_route.link_list));
2777     if (e_route->src_gateway)
2778       xbt_free(e_route->src_gateway);
2779     if (e_route->dst_gateway)
2780       xbt_free(e_route->dst_gateway);
2781     xbt_free(e_route);
2782   }
2783 }
2784
2785 static routing_component_t generic_as_exist(routing_component_t find_from,
2786                                             routing_component_t to_find)
2787 {
2788   //return to_find; // FIXME: BYPASSERROR OF FOREACH WITH BREAK
2789   xbt_dict_cursor_t cursor = NULL;
2790   char *key;
2791   int found = 0;
2792   routing_component_t elem;
2793   xbt_dict_foreach(find_from->routing_sons, cursor, key, elem) {
2794     if (to_find == elem || generic_as_exist(elem, to_find)) {
2795       found = 1;
2796       break;
2797     }
2798   }
2799   if (found)
2800     return to_find;
2801   return NULL;
2802 }
2803
2804 static routing_component_t
2805 generic_autonomous_system_exist(routing_component_t rc, char *element)
2806 {
2807   //return rc; // FIXME: BYPASSERROR OF FOREACH WITH BREAK
2808   routing_component_t element_as, result, elem;
2809   xbt_dict_cursor_t cursor = NULL;
2810   char *key;
2811   element_as = ((network_element_info_t)
2812                 xbt_dict_get_or_null
2813                 (global_routing->where_network_elements,
2814                  element))->rc_component;
2815   result = ((routing_component_t) - 1);
2816   if (element_as != rc)
2817     result = generic_as_exist(rc, element_as);
2818
2819   int found = 0;
2820   if (result) {
2821     xbt_dict_foreach(element_as->routing_sons, cursor, key, elem) {
2822       found = !strcmp(elem->name, element);
2823       if (found)
2824         break;
2825     }
2826     if (found)
2827       return element_as;
2828   }
2829   return NULL;
2830 }
2831
2832 static routing_component_t
2833 generic_processing_units_exist(routing_component_t rc, char *element)
2834 {
2835   routing_component_t element_as;
2836   element_as = ((network_element_info_t)
2837                 xbt_dict_get_or_null
2838                 (global_routing->where_network_elements,
2839                  element))->rc_component;
2840   if (element_as == rc)
2841     return element_as;
2842   return generic_as_exist(rc, element_as);
2843 }
2844
2845 static void generic_src_dst_check(routing_component_t rc, const char *src,
2846                                   const char *dst)
2847 {
2848
2849   routing_component_t src_as = ((network_element_info_t)
2850                                 xbt_dict_get_or_null
2851                                 (global_routing->where_network_elements,
2852                                  src))->rc_component;
2853   routing_component_t dst_as = ((network_element_info_t)
2854                                 xbt_dict_get_or_null
2855                                 (global_routing->where_network_elements,
2856                                  dst))->rc_component;
2857
2858   xbt_assert3(src_as != NULL && dst_as != NULL,
2859               "Ask for route \"from\"(%s) or \"to\"(%s) no found at AS \"%s\"",
2860               src, dst, rc->name);
2861   xbt_assert4(src_as == dst_as,
2862               "The src(%s in %s) and dst(%s in %s) are in differents AS",
2863               src, src_as->name, dst, dst_as->name);
2864   xbt_assert2(rc == dst_as,
2865               "The routing component of src and dst is not the same as the network elements belong (%s==%s)",
2866               rc->name, dst_as->name);
2867 }
2868
2869 static void parse_Sconfig(void)
2870 {
2871   //TODO
2872   DEBUG0("WARNING tag config not yet implemented.");
2873 }
2874
2875 static void routing_full_parse_Scluster(void)
2876 {
2877   static int AX_ptr = 0;
2878
2879   char *cluster_id = A_surfxml_cluster_id;
2880   char *cluster_prefix = A_surfxml_cluster_prefix;
2881   char *cluster_suffix = A_surfxml_cluster_suffix;
2882   char *cluster_radical = A_surfxml_cluster_radical;
2883   char *cluster_power = A_surfxml_cluster_power;
2884   char *cluster_bw = A_surfxml_cluster_bw;
2885   char *cluster_lat = A_surfxml_cluster_lat;
2886   char *cluster_bb_bw = A_surfxml_cluster_bb_bw;
2887   char *cluster_bb_lat = A_surfxml_cluster_bb_lat;
2888   char *host_id, *groups, *link_id;
2889   char *router_id, *link_router, *link_backbone, *route_src_dst;
2890   unsigned int iter;
2891   int start, end, i;
2892   xbt_dynar_t radical_elements;
2893   xbt_dynar_t radical_ends;
2894 #ifndef HAVE_PCRE_LIB
2895   xbt_dynar_t tab_elements_num = xbt_dynar_new(sizeof(int), NULL);
2896   char *route_src, *route_dst;
2897   int j;
2898 #endif
2899
2900   static unsigned int surfxml_buffer_stack_stack_ptr = 1;
2901   static unsigned int surfxml_buffer_stack_stack[1024];
2902
2903   surfxml_buffer_stack_stack[0] = 0;
2904
2905   surfxml_bufferstack_push(1);
2906
2907   SURFXML_BUFFER_SET(AS_id, cluster_id);
2908 #ifdef HAVE_PCRE_LIB
2909   SURFXML_BUFFER_SET(AS_routing, "RuleBased");
2910   DEBUG1("<AS id=\"%s\"\trouting=\"RuleBased\">", cluster_id);
2911 #else
2912   SURFXML_BUFFER_SET(AS_routing, "Full");
2913   DEBUG1("<AS id=\"%s\"\trouting=\"Full\">", cluster_id);
2914 #endif
2915   SURFXML_START_TAG(AS);
2916
2917   radical_elements = xbt_str_split(cluster_radical, ",");
2918   xbt_dynar_foreach(radical_elements, iter, groups) {
2919     radical_ends = xbt_str_split(groups, "-");
2920     switch (xbt_dynar_length(radical_ends)) {
2921     case 1:
2922       surf_parse_get_int(&start,
2923                          xbt_dynar_get_as(radical_ends, 0, char *));
2924       host_id = bprintf("%s%d%s", cluster_prefix, start, cluster_suffix);
2925 #ifndef HAVE_PCRE_LIB
2926       xbt_dynar_push_as(tab_elements_num, int, start);
2927 #endif
2928       link_id = bprintf("%s_link_%d", cluster_id, start);
2929
2930       DEBUG2("<host\tid=\"%s\"\tpower=\"%s\"/>", host_id, cluster_power);
2931       SURFXML_BUFFER_SET(host_id, host_id);
2932       SURFXML_BUFFER_SET(host_power, cluster_power);
2933       SURFXML_BUFFER_SET(host_availability, "1.0");
2934       SURFXML_BUFFER_SET(host_availability_file, "");
2935       A_surfxml_host_state = A_surfxml_host_state_ON;
2936       SURFXML_BUFFER_SET(host_state_file, "");
2937       SURFXML_START_TAG(host);
2938       SURFXML_END_TAG(host);
2939
2940       DEBUG3("<link\tid=\"%s\"\tbw=\"%s\"\tlat=\"%s\"/>", link_id,
2941              cluster_bw, cluster_lat);
2942       SURFXML_BUFFER_SET(link_id, link_id);
2943       SURFXML_BUFFER_SET(link_bandwidth, cluster_bw);
2944       SURFXML_BUFFER_SET(link_latency, cluster_lat);
2945       SURFXML_BUFFER_SET(link_bandwidth_file, "");
2946       SURFXML_BUFFER_SET(link_latency_file, "");
2947       A_surfxml_link_state = A_surfxml_link_state_ON;
2948       SURFXML_BUFFER_SET(link_state_file, "");
2949       A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
2950       SURFXML_START_TAG(link);
2951       SURFXML_END_TAG(link);
2952
2953       break;
2954
2955     case 2:
2956
2957       surf_parse_get_int(&start,
2958                          xbt_dynar_get_as(radical_ends, 0, char *));
2959       surf_parse_get_int(&end, xbt_dynar_get_as(radical_ends, 1, char *));
2960       DEBUG2("Create hosts and links from %d to %d", start, end);
2961       for (i = start; i <= end; i++) {
2962         host_id = bprintf("%s%d%s", cluster_prefix, i, cluster_suffix);
2963 #ifndef HAVE_PCRE_LIB
2964         xbt_dynar_push_as(tab_elements_num, int, i);
2965 #endif
2966         link_id = bprintf("%s_link_%d", cluster_id, i);
2967
2968         DEBUG2("<host\tid=\"%s\"\tpower=\"%s\"/>", host_id, cluster_power);
2969         SURFXML_BUFFER_SET(host_id, host_id);
2970         SURFXML_BUFFER_SET(host_power, cluster_power);
2971         SURFXML_BUFFER_SET(host_availability, "1.0");
2972         SURFXML_BUFFER_SET(host_availability_file, "");
2973         A_surfxml_host_state = A_surfxml_host_state_ON;
2974         SURFXML_BUFFER_SET(host_state_file, "");
2975         SURFXML_START_TAG(host);
2976         SURFXML_END_TAG(host);
2977
2978         DEBUG3("<link\tid=\"%s\"\tbw=\"%s\"\tlat=\"%s\"/>", link_id,
2979                cluster_bw, cluster_lat);
2980         SURFXML_BUFFER_SET(link_id, link_id);
2981         SURFXML_BUFFER_SET(link_bandwidth, cluster_bw);
2982         SURFXML_BUFFER_SET(link_latency, cluster_lat);
2983         SURFXML_BUFFER_SET(link_bandwidth_file, "");
2984         SURFXML_BUFFER_SET(link_latency_file, "");
2985         A_surfxml_link_state = A_surfxml_link_state_ON;
2986         SURFXML_BUFFER_SET(link_state_file, "");
2987         A_surfxml_link_sharing_policy =
2988             A_surfxml_link_sharing_policy_SHARED;
2989         SURFXML_START_TAG(link);
2990         SURFXML_END_TAG(link);
2991       }
2992       break;
2993
2994     default:
2995       DEBUG0("Malformed radical");
2996     }
2997
2998     xbt_dynar_free(&radical_ends);
2999   }
3000
3001   DEBUG0(" ");
3002   router_id =
3003       bprintf("%s%s_router%s", cluster_prefix, cluster_id,
3004               cluster_suffix);
3005   link_router = bprintf("%s_link_%s_router", cluster_id, cluster_id);
3006   link_backbone = bprintf("%s_backbone", cluster_id);
3007
3008   DEBUG1("<router id=\"%s\"/>", router_id);
3009   SURFXML_BUFFER_SET(router_id, router_id);;
3010   SURFXML_START_TAG(router);
3011   SURFXML_END_TAG(router);
3012
3013   DEBUG3("<link\tid=\"%s\"\tbw=\"%s\"\tlat=\"%s\"/>", link_router,
3014          cluster_bw, cluster_lat);
3015   SURFXML_BUFFER_SET(link_id, link_router);
3016   SURFXML_BUFFER_SET(link_bandwidth, cluster_bw);
3017   SURFXML_BUFFER_SET(link_latency, cluster_lat);
3018   SURFXML_BUFFER_SET(link_bandwidth_file, "");
3019   SURFXML_BUFFER_SET(link_latency_file, "");
3020   A_surfxml_link_state = A_surfxml_link_state_ON;
3021   SURFXML_BUFFER_SET(link_state_file, "");
3022   A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
3023   SURFXML_START_TAG(link);
3024   SURFXML_END_TAG(link);
3025
3026   DEBUG3("<link\tid=\"%s\"\tbw=\"%s\"\tlat=\"%s\"/>", link_backbone,
3027          cluster_bb_bw, cluster_bb_lat);
3028   SURFXML_BUFFER_SET(link_id, link_backbone);
3029   SURFXML_BUFFER_SET(link_bandwidth, cluster_bb_bw);
3030   SURFXML_BUFFER_SET(link_latency, cluster_bb_lat);
3031   SURFXML_BUFFER_SET(link_bandwidth_file, "");
3032   SURFXML_BUFFER_SET(link_latency_file, "");
3033   A_surfxml_link_state = A_surfxml_link_state_ON;
3034   SURFXML_BUFFER_SET(link_state_file, "");
3035   A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
3036   SURFXML_START_TAG(link);
3037   SURFXML_END_TAG(link);
3038
3039   char *new_suffix = bprintf("%s", "");
3040
3041   radical_elements = xbt_str_split(cluster_suffix, ".");
3042   xbt_dynar_foreach(radical_elements, iter, groups) {
3043     if (strcmp(groups, "")) {
3044       new_suffix = bprintf("%s\\.%s", new_suffix, groups);
3045     }
3046   }
3047   route_src_dst = bprintf("%s(.*)%s", cluster_prefix, new_suffix);
3048
3049   DEBUG0(" ");
3050
3051 #ifdef HAVE_PCRE_LIB
3052
3053   DEBUG2("<route\tsrc=\"%s\"\tdst=\"%s\">", route_src_dst, route_src_dst);
3054   SURFXML_BUFFER_SET(route_src, route_src_dst);
3055   SURFXML_BUFFER_SET(route_dst, route_src_dst);
3056   SURFXML_START_TAG(route);
3057
3058   DEBUG1("<link_ctn\tid=\"%s_link_$1src\"/>", cluster_id);
3059   SURFXML_BUFFER_SET(link_ctn_id, bprintf("%s_link_$1src", cluster_id));
3060   SURFXML_START_TAG(link_ctn);
3061   SURFXML_END_TAG(link_ctn);
3062
3063   DEBUG1("<link_ctn\tid=\"%s_backbone\"/>", cluster_id);
3064   SURFXML_BUFFER_SET(link_ctn_id, bprintf("%s_backbone", cluster_id));
3065   SURFXML_START_TAG(link_ctn);
3066   SURFXML_END_TAG(link_ctn);
3067
3068   DEBUG1("<link_ctn\tid=\"%s_link_$1dst\"/>", cluster_id);
3069   SURFXML_BUFFER_SET(link_ctn_id, bprintf("%s_link_$1dst", cluster_id));
3070   SURFXML_START_TAG(link_ctn);
3071   SURFXML_END_TAG(link_ctn);
3072
3073   DEBUG0("</route>");
3074   SURFXML_END_TAG(route);
3075 #else
3076   for (i = 0; i <= xbt_dynar_length(tab_elements_num); i++) {
3077     for (j = 0; j <= xbt_dynar_length(tab_elements_num); j++) {
3078       if (i == xbt_dynar_length(tab_elements_num)) {
3079         route_src = router_id;
3080       } else {
3081         route_src =
3082             bprintf("%s%d%s", cluster_prefix,
3083                     xbt_dynar_get_as(tab_elements_num, i, int),
3084                     cluster_suffix);
3085       }
3086
3087       if (j == xbt_dynar_length(tab_elements_num)) {
3088         route_dst = router_id;
3089       } else {
3090         route_dst =
3091             bprintf("%s%d%s", cluster_prefix,
3092                     xbt_dynar_get_as(tab_elements_num, j, int),
3093                     cluster_suffix);
3094       }
3095
3096       DEBUG2("<route\tsrc=\"%s\"\tdst=\"%s\">", route_src, route_dst);
3097       SURFXML_BUFFER_SET(route_src, route_src);
3098       SURFXML_BUFFER_SET(route_dst, route_dst);
3099       SURFXML_START_TAG(route);
3100
3101       if (i == xbt_dynar_length(tab_elements_num)) {
3102         route_src = link_router;
3103       } else {
3104         route_src =
3105             bprintf("%s_link_%d", cluster_id,
3106                     xbt_dynar_get_as(tab_elements_num, i, int));
3107       }
3108
3109       if (j == xbt_dynar_length(tab_elements_num)) {
3110         route_dst = link_router;
3111       } else {
3112         route_dst =
3113             bprintf("%s_link_%d", cluster_id,
3114                     xbt_dynar_get_as(tab_elements_num, j, int));
3115       }
3116
3117       DEBUG1("<link_ctn\tid=\"%s\"/>", route_src);
3118       SURFXML_BUFFER_SET(link_ctn_id, route_src);
3119       SURFXML_START_TAG(link_ctn);
3120       SURFXML_END_TAG(link_ctn);
3121
3122       DEBUG1("<link_ctn\tid=\"%s_backbone\"/>", cluster_id);
3123       SURFXML_BUFFER_SET(link_ctn_id, bprintf("%s_backbone", cluster_id));
3124       SURFXML_START_TAG(link_ctn);
3125       SURFXML_END_TAG(link_ctn);
3126
3127       DEBUG1("<link_ctn\tid=\"%s\"/>", route_dst);
3128       SURFXML_BUFFER_SET(link_ctn_id, route_dst);
3129       SURFXML_START_TAG(link_ctn);
3130       SURFXML_END_TAG(link_ctn);
3131
3132       DEBUG0("</route>");
3133       SURFXML_END_TAG(route);
3134     }
3135   }
3136   xbt_dynar_free(&tab_elements_num);
3137 #endif
3138
3139   DEBUG0("</AS>");
3140   SURFXML_END_TAG(AS);
3141   DEBUG0(" ");
3142
3143   surfxml_bufferstack_pop(1);
3144 }
3145
3146 /*
3147  * New methods to init the routing model component from the lua script
3148  */
3149
3150 /*
3151  * calling parse_S_AS_lua with lua values
3152  */
3153 void routing_AS_init(const char *AS_id, const char *AS_routing)
3154 {
3155   parse_S_AS_lua((char *) AS_id, (char *) AS_routing);
3156 }
3157
3158 /*
3159  * calling parse_E_AS_lua to fisnish the creation of routing component
3160  */
3161 void routing_AS_end(const char *AS_id)
3162 {
3163   parse_E_AS_lua((char *) AS_id);
3164 }
3165
3166 /*
3167  * add a host to the network element list
3168  */
3169
3170 void routing_add_host(const char *host_id)
3171 {
3172   parse_S_host_lua((char *) host_id);
3173 }
3174
3175 /*
3176  * Set a new link on the actual list of link for a route or ASroute
3177  */
3178 void routing_add_link(const char *link_id)
3179 {
3180   parse_E_link_c_ctn_new_elem_lua((char *) link_id);
3181 }
3182
3183 /*
3184  *Set the endpoints for a route
3185  */
3186 void routing_set_route(const char *src_id, const char *dst_id)
3187 {
3188   parse_S_route_new_and_endpoints_lua((char *) src_id, (char *) dst_id);
3189 }
3190
3191 /*
3192  * Store the route by calling parse_E_route_store_route
3193  */
3194 void routing_store_route(void)
3195 {
3196   parse_E_route_store_route();
3197 }