Logo AND Algorithmique Numérique Distribuée

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