Logo AND Algorithmique Numérique Distribuée

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