Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add function xbt_dynar_compare
[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   route_t route_to_test = xbt_dict_get_or_null(_parse_routes, route_name);
2511   if(route_to_test)
2512   xbt_assert2(xbt_dynar_compare(
2513                   (void*)route->link_list,
2514                   (void*)route_to_test->link_list,
2515                   (int_f_cpvoid_cpvoid_t) strcmp),
2516           "The route between \"%s\" and \"%s\" already exist", src,dst);
2517
2518   xbt_dict_set(_parse_routes, route_name, route, NULL);
2519   xbt_free(route_name);
2520
2521   if(A_surfxml_route_symetrical == A_surfxml_route_symetrical_YES)
2522   {
2523           int i;
2524           route_t route_sym = xbt_new0(s_route_t, 1);
2525           route_sym->link_list = xbt_dynar_new(sizeof(char *),NULL);
2526           for(i=nb_links ; i>0 ; i--)
2527           {
2528                  char *link_name = xbt_new0(char,strlen(xbt_dynar_get_as(route->link_list, i-1, char *)));
2529                  link_name = bprintf("%s",xbt_dynar_get_as(route->link_list, i-1, char *));
2530                  xbt_dynar_push_as(route_sym->link_list ,char *, link_name);
2531           }
2532           DEBUG2("Load Route from \"%s\" to \"%s\"", dst, src);
2533           route_to_test = xbt_dict_get_or_null(_parse_routes, bprintf("%d#%d",*dst_id, *src_id));
2534           if(route_to_test)
2535           xbt_assert2(xbt_dynar_compare(
2536                           (void*)route_sym->link_list,
2537                           (void*)route_to_test->link_list,
2538                           (int_f_cpvoid_cpvoid_t) strcmp),
2539                   "The route between \"%s\" and \"%s\" already exist", dst,src);
2540           xbt_dict_set(_parse_routes, bprintf("%d#%d",*dst_id, *src_id), route_sym, NULL);
2541    }
2542 }
2543
2544 static void generic_set_ASroute(routing_component_t rc, const char *src,
2545                                 const char *dst, route_extended_t e_route)
2546 {
2547   DEBUG4("Load ASroute from \"%s(%s)\" to \"%s(%s)\"", src,
2548          e_route->src_gateway, dst, e_route->dst_gateway);
2549
2550   xbt_dict_t _parse_routes;
2551   xbt_dict_t _to_index;
2552   char *route_name;
2553   int *src_id, *dst_id;
2554   _to_index = current_routing->to_index;
2555   _parse_routes = current_routing->parse_routes;
2556
2557
2558   src_id = xbt_dict_get_or_null(_to_index, src);
2559   dst_id = xbt_dict_get_or_null(_to_index, dst);
2560
2561   xbt_assert2(src_id
2562               && dst_id, "Network elements %s or %s not found", src, dst);
2563   route_name = bprintf("%d#%d", *src_id, *dst_id);
2564
2565   xbt_assert2(xbt_dynar_length(e_route->generic_route.link_list) > 0,
2566               "Invalid count of links, must be greater than zero (%s,%s)",
2567               src, dst);
2568
2569   route_t route_to_test = xbt_dict_get_or_null(_parse_routes, route_name);
2570   if(route_to_test)
2571   xbt_assert4(xbt_dynar_compare(
2572                   (void*) (&e_route->generic_route)->link_list,
2573                   (void*) route_to_test->link_list,
2574                   (int_f_cpvoid_cpvoid_t) strcmp),
2575                   "The route between \"%s\"(\"%s\") and \"%s\"(\"%s\") already exist",
2576                                 src, e_route->src_gateway, dst, e_route->dst_gateway);
2577
2578   xbt_dict_set(_parse_routes, route_name, e_route, NULL);
2579   xbt_free(route_name);
2580
2581   unsigned long nb_links = xbt_dynar_length(e_route->generic_route.link_list);
2582   if(A_surfxml_ASroute_symetrical == A_surfxml_ASroute_symetrical_YES)
2583   {
2584           int i;
2585           route_extended_t route_sym = xbt_new0(s_route_extended_t, 1);
2586           route_sym->generic_route.link_list = xbt_dynar_new(sizeof(char *),NULL);
2587           for(i=nb_links ; i>0 ; i--)
2588           {
2589                  char *link_name = xbt_new0(char,strlen(xbt_dynar_get_as(e_route->generic_route.link_list, i-1, char *)));
2590                  link_name = bprintf("%s",xbt_dynar_get_as(e_route->generic_route.link_list, i-1, char *));
2591                  xbt_dynar_push_as(route_sym->generic_route.link_list ,char *, link_name);
2592           }
2593           route_sym->src_gateway = xbt_new0( char,strlen(e_route->dst_gateway) );
2594           route_sym->src_gateway = bprintf("%s",e_route->dst_gateway);
2595           route_sym->dst_gateway = xbt_new0( char,strlen(e_route->src_gateway) );
2596           route_sym->dst_gateway = bprintf("%s",e_route->src_gateway);
2597           DEBUG4("Load ASroute from \"%s(%s)\" to \"%s(%s)\"",dst, route_sym->src_gateway,src,route_sym->dst_gateway);
2598
2599           route_to_test = xbt_dict_get_or_null(_parse_routes, bprintf("%d#%d", *dst_id, *src_id));
2600           if(route_to_test)
2601           xbt_assert4(xbt_dynar_compare(
2602                           (void*) (&route_sym->generic_route)->link_list,
2603                           (void*) route_to_test->link_list,
2604                           (int_f_cpvoid_cpvoid_t) strcmp),
2605                           "The route between \"%s\"(\"%s\") and \"%s\"(\"%s\") already exist",
2606                                               dst, route_sym->src_gateway, src, route_sym->dst_gateway);
2607
2608           xbt_dict_set(_parse_routes, bprintf("%d#%d", *dst_id, *src_id), route_sym, NULL);
2609    }
2610 }
2611
2612 static void generic_set_bypassroute(routing_component_t rc,
2613                                     const char *src, const char *dst,
2614                                     route_extended_t e_route)
2615 {
2616   DEBUG2("Load bypassRoute from \"%s\" to \"%s\"", src, dst);
2617   xbt_dict_t dict_bypassRoutes = rc->bypassRoutes;
2618   char *route_name;
2619
2620   route_name = bprintf("%s#%s", src, dst);
2621   xbt_assert2(xbt_dynar_length(e_route->generic_route.link_list) > 0,
2622               "Invalid count of links, must be greater than zero (%s,%s)",
2623               src, dst);
2624   xbt_assert4(!xbt_dict_get_or_null(dict_bypassRoutes, route_name),
2625               "The bypass route between \"%s\"(\"%s\") and \"%s\"(\"%s\") already exist",
2626               src, e_route->src_gateway, dst, e_route->dst_gateway);
2627
2628   route_extended_t new_e_route =
2629       generic_new_extended_route(SURF_ROUTING_RECURSIVE, e_route, 0);
2630   xbt_dynar_free(&(e_route->generic_route.link_list));
2631   xbt_free(e_route);
2632
2633   xbt_dict_set(dict_bypassRoutes, route_name, new_e_route,
2634                (void (*)(void *)) generic_free_extended_route);
2635   xbt_free(route_name);
2636 }
2637
2638 /* ************************************************************************** */
2639 /* *********************** GENERIC BUSINESS METHODS ************************* */
2640
2641 static xbt_dynar_t generic_get_onelink_routes(routing_component_t rc)
2642 {
2643   xbt_die("\"generic_get_onelink_routes\" not implemented yet");
2644 }
2645
2646 static route_extended_t generic_get_bypassroute(routing_component_t rc,
2647                                                 const char *src,
2648                                                 const char *dst)
2649 {
2650   xbt_dict_t dict_bypassRoutes = rc->bypassRoutes;
2651   routing_component_t src_as, dst_as;
2652   int index_src, index_dst;
2653   xbt_dynar_t path_src = NULL;
2654   xbt_dynar_t path_dst = NULL;
2655   routing_component_t current = NULL;
2656   routing_component_t *current_src = NULL;
2657   routing_component_t *current_dst = NULL;
2658
2659   /* (1) find the as where the src and dst are located */
2660   src_as = ((network_element_info_t)
2661             xbt_dict_get_or_null(global_routing->where_network_elements,
2662                                  src))->rc_component;
2663   dst_as = ((network_element_info_t)
2664             xbt_dict_get_or_null(global_routing->where_network_elements,
2665                                  dst))->rc_component;
2666   xbt_assert2(src_as
2667               && dst_as,
2668               "Ask for route \"from\"(%s) or \"to\"(%s) no found", src,
2669               dst);
2670
2671   /* (2) find the path to the root routing component */
2672   path_src = xbt_dynar_new(sizeof(routing_component_t), NULL);
2673   current = src_as;
2674   while (current != NULL) {
2675     xbt_dynar_push(path_src, &current);
2676     current = current->routing_father;
2677   }
2678   path_dst = xbt_dynar_new(sizeof(routing_component_t), NULL);
2679   current = dst_as;
2680   while (current != NULL) {
2681     xbt_dynar_push(path_dst, &current);
2682     current = current->routing_father;
2683   }
2684
2685   /* (3) find the common father */
2686   index_src = (path_src->used) - 1;
2687   index_dst = (path_dst->used) - 1;
2688   current_src = xbt_dynar_get_ptr(path_src, index_src);
2689   current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
2690   while (index_src >= 0 && index_dst >= 0 && *current_src == *current_dst) {
2691     routing_component_t *tmp_src, *tmp_dst;
2692     tmp_src = xbt_dynar_pop_ptr(path_src);
2693     tmp_dst = xbt_dynar_pop_ptr(path_dst);
2694     index_src--;
2695     index_dst--;
2696     current_src = xbt_dynar_get_ptr(path_src, index_src);
2697     current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
2698   }
2699
2700   int max_index_src = (path_src->used) - 1;
2701   int max_index_dst = (path_dst->used) - 1;
2702
2703   int max_index = max(max_index_src, max_index_dst);
2704   int i, max;
2705
2706   route_extended_t e_route_bypass = NULL;
2707
2708   for (max = 0; max <= max_index; max++) {
2709     for (i = 0; i < max; i++) {
2710       if (i <= max_index_src && max <= max_index_dst) {
2711         char *route_name = bprintf("%s#%s",
2712                                    (*(routing_component_t *)
2713                                     (xbt_dynar_get_ptr
2714                                      (path_src, i)))->name,
2715                                    (*(routing_component_t *)
2716                                     (xbt_dynar_get_ptr
2717                                      (path_dst, max)))->name);
2718         e_route_bypass =
2719             xbt_dict_get_or_null(dict_bypassRoutes, route_name);
2720         xbt_free(route_name);
2721       }
2722       if (e_route_bypass)
2723         break;
2724       if (max <= max_index_src && i <= max_index_dst) {
2725         char *route_name = bprintf("%s#%s",
2726                                    (*(routing_component_t *)
2727                                     (xbt_dynar_get_ptr
2728                                      (path_src, max)))->name,
2729                                    (*(routing_component_t *)
2730                                     (xbt_dynar_get_ptr
2731                                      (path_dst, i)))->name);
2732         e_route_bypass =
2733             xbt_dict_get_or_null(dict_bypassRoutes, route_name);
2734         xbt_free(route_name);
2735       }
2736       if (e_route_bypass)
2737         break;
2738     }
2739
2740     if (e_route_bypass)
2741       break;
2742
2743     if (max <= max_index_src && max <= max_index_dst) {
2744       char *route_name = bprintf("%s#%s",
2745                                  (*(routing_component_t *)
2746                                   (xbt_dynar_get_ptr
2747                                    (path_src, max)))->name,
2748                                  (*(routing_component_t *)
2749                                   (xbt_dynar_get_ptr
2750                                    (path_dst, max)))->name);
2751       e_route_bypass = xbt_dict_get_or_null(dict_bypassRoutes, route_name);
2752       xbt_free(route_name);
2753     }
2754     if (e_route_bypass)
2755       break;
2756   }
2757
2758   xbt_dynar_free(&path_src);
2759   xbt_dynar_free(&path_dst);
2760
2761   route_extended_t new_e_route = NULL;
2762
2763   if (e_route_bypass) {
2764     void *link;
2765     unsigned int cpt = 0;
2766     new_e_route = xbt_new0(s_route_extended_t, 1);
2767     new_e_route->src_gateway = xbt_strdup(e_route_bypass->src_gateway);
2768     new_e_route->dst_gateway = xbt_strdup(e_route_bypass->dst_gateway);
2769     new_e_route->generic_route.link_list =
2770         xbt_dynar_new(global_routing->size_of_link, NULL);
2771     xbt_dynar_foreach(e_route_bypass->generic_route.link_list, cpt, link) {
2772       xbt_dynar_push(new_e_route->generic_route.link_list, &link);
2773     }
2774   }
2775
2776   return new_e_route;
2777 }
2778
2779 /* ************************************************************************** */
2780 /* ************************* GENERIC AUX FUNCTIONS ************************** */
2781
2782 static route_extended_t
2783 generic_new_extended_route(e_surf_routing_hierarchy_t hierarchy,
2784                            void *data, int order)
2785 {
2786
2787   char *link_name;
2788   route_extended_t e_route, new_e_route;
2789   route_t route;
2790   unsigned int cpt;
2791   xbt_dynar_t links = NULL, links_id = NULL;
2792
2793   new_e_route = xbt_new0(s_route_extended_t, 1);
2794   new_e_route->generic_route.link_list =
2795       xbt_dynar_new(global_routing->size_of_link, NULL);
2796   new_e_route->src_gateway = NULL;
2797   new_e_route->dst_gateway = NULL;
2798
2799   xbt_assert0(hierarchy == SURF_ROUTING_BASE
2800               || hierarchy == SURF_ROUTING_RECURSIVE,
2801               "the hierarchy type is not defined");
2802
2803   if (hierarchy == SURF_ROUTING_BASE) {
2804
2805     route = (route_t) data;
2806     links = route->link_list;
2807
2808   } else if (hierarchy == SURF_ROUTING_RECURSIVE) {
2809
2810     e_route = (route_extended_t) data;
2811     xbt_assert0(e_route->src_gateway
2812                 && e_route->dst_gateway, "bad gateway, is null");
2813     links = e_route->generic_route.link_list;
2814
2815     /* remeber not erase the gateway names */
2816     new_e_route->src_gateway = e_route->src_gateway;
2817     new_e_route->dst_gateway = e_route->dst_gateway;
2818   }
2819
2820   links_id = new_e_route->generic_route.link_list;
2821
2822   xbt_dynar_foreach(links, cpt, link_name) {
2823
2824     void *link =
2825         xbt_dict_get_or_null(surf_network_model->resource_set, link_name);
2826     if (link) {
2827       if (order)
2828         xbt_dynar_push(links_id, &link);
2829       else
2830         xbt_dynar_unshift(links_id, &link);
2831     } else
2832       THROW1(mismatch_error, 0, "Link %s not found", link_name);
2833   }
2834
2835   return new_e_route;
2836 }
2837
2838 static void generic_free_route(route_t route)
2839 {
2840   if (route) {
2841     xbt_dynar_free(&(route->link_list));
2842     xbt_free(route);
2843   }
2844 }
2845
2846 static void generic_free_extended_route(route_extended_t e_route)
2847 {
2848   if (e_route) {
2849     xbt_dynar_free(&(e_route->generic_route.link_list));
2850     if (e_route->src_gateway)
2851       xbt_free(e_route->src_gateway);
2852     if (e_route->dst_gateway)
2853       xbt_free(e_route->dst_gateway);
2854     xbt_free(e_route);
2855   }
2856 }
2857
2858 static routing_component_t generic_as_exist(routing_component_t find_from,
2859                                             routing_component_t to_find)
2860 {
2861   //return to_find; // FIXME: BYPASSERROR OF FOREACH WITH BREAK
2862   xbt_dict_cursor_t cursor = NULL;
2863   char *key;
2864   int found = 0;
2865   routing_component_t elem;
2866   xbt_dict_foreach(find_from->routing_sons, cursor, key, elem) {
2867     if (to_find == elem || generic_as_exist(elem, to_find)) {
2868       found = 1;
2869       break;
2870     }
2871   }
2872   if (found)
2873     return to_find;
2874   return NULL;
2875 }
2876
2877 static routing_component_t
2878 generic_autonomous_system_exist(routing_component_t rc, char *element)
2879 {
2880   //return rc; // FIXME: BYPASSERROR OF FOREACH WITH BREAK
2881   routing_component_t element_as, result, elem;
2882   xbt_dict_cursor_t cursor = NULL;
2883   char *key;
2884   element_as = ((network_element_info_t)
2885                 xbt_dict_get_or_null
2886                 (global_routing->where_network_elements,
2887                  element))->rc_component;
2888   result = ((routing_component_t) - 1);
2889   if (element_as != rc)
2890     result = generic_as_exist(rc, element_as);
2891
2892   int found = 0;
2893   if (result) {
2894     xbt_dict_foreach(element_as->routing_sons, cursor, key, elem) {
2895       found = !strcmp(elem->name, element);
2896       if (found)
2897         break;
2898     }
2899     if (found)
2900       return element_as;
2901   }
2902   return NULL;
2903 }
2904
2905 static routing_component_t
2906 generic_processing_units_exist(routing_component_t rc, char *element)
2907 {
2908   routing_component_t element_as;
2909   element_as = ((network_element_info_t)
2910                 xbt_dict_get_or_null
2911                 (global_routing->where_network_elements,
2912                  element))->rc_component;
2913   if (element_as == rc)
2914     return element_as;
2915   return generic_as_exist(rc, element_as);
2916 }
2917
2918 static void generic_src_dst_check(routing_component_t rc, const char *src,
2919                                   const char *dst)
2920 {
2921
2922   routing_component_t src_as = ((network_element_info_t)
2923                                 xbt_dict_get_or_null
2924                                 (global_routing->where_network_elements,
2925                                  src))->rc_component;
2926   routing_component_t dst_as = ((network_element_info_t)
2927                                 xbt_dict_get_or_null
2928                                 (global_routing->where_network_elements,
2929                                  dst))->rc_component;
2930
2931   xbt_assert3(src_as != NULL && dst_as != NULL,
2932               "Ask for route \"from\"(%s) or \"to\"(%s) no found at AS \"%s\"",
2933               src, dst, rc->name);
2934   xbt_assert4(src_as == dst_as,
2935               "The src(%s in %s) and dst(%s in %s) are in differents AS",
2936               src, src_as->name, dst, dst_as->name);
2937   xbt_assert2(rc == dst_as,
2938               "The routing component of src and dst is not the same as the network elements belong (%s==%s)",
2939               rc->name, dst_as->name);
2940 }
2941
2942 static void routing_parse_Sconfig(void)
2943 {
2944   //TODO
2945   DEBUG0("WARNING tag config not yet implemented.");
2946   DEBUG1("Configuration name = %s",A_surfxml_config_id);
2947 }
2948
2949 static void routing_parse_Econfig(void)
2950 {
2951   //TODO
2952   xbt_dict_cursor_t cursor = NULL;
2953   char *key;
2954   char *elem;
2955   xbt_dict_foreach(current_property_set, cursor, key, elem) {
2956           DEBUG2("property : %s = %s",key,elem);
2957         }
2958 }
2959
2960 static void routing_parse_Scluster(void)
2961 {
2962   static int AX_ptr = 0;
2963
2964   char *cluster_id = A_surfxml_cluster_id;
2965   char *cluster_prefix = A_surfxml_cluster_prefix;
2966   char *cluster_suffix = A_surfxml_cluster_suffix;
2967   char *cluster_radical = A_surfxml_cluster_radical;
2968   char *cluster_power = A_surfxml_cluster_power;
2969   char *cluster_bw = A_surfxml_cluster_bw;
2970   char *cluster_lat = A_surfxml_cluster_lat;
2971   char *cluster_bb_bw = A_surfxml_cluster_bb_bw;
2972   char *cluster_bb_lat = A_surfxml_cluster_bb_lat;
2973   char *host_id, *groups, *link_id = NULL;
2974   char *router_id, *link_router, *link_backbone, *route_src_dst;
2975   unsigned int iter;
2976   int start, end, i;
2977   xbt_dynar_t radical_elements;
2978   xbt_dynar_t radical_ends;
2979   int cluster_sharing_policy = AX_surfxml_cluster_sharing_policy;
2980   int cluster_bb_sharing_policy = AX_surfxml_cluster_bb_sharing_policy;
2981
2982 #ifndef HAVE_PCRE_LIB
2983   xbt_dynar_t tab_elements_num = xbt_dynar_new(sizeof(int), NULL);
2984   char *route_src, *route_dst;
2985   int j;
2986 #endif
2987
2988   static unsigned int surfxml_buffer_stack_stack_ptr = 1;
2989   static unsigned int surfxml_buffer_stack_stack[1024];
2990
2991   surfxml_buffer_stack_stack[0] = 0;
2992
2993   surfxml_bufferstack_push(1);
2994
2995   SURFXML_BUFFER_SET(AS_id, cluster_id);
2996 #ifdef HAVE_PCRE_LIB
2997   SURFXML_BUFFER_SET(AS_routing, "RuleBased");
2998   DEBUG1("<AS id=\"%s\"\trouting=\"RuleBased\">", cluster_id);
2999 #else
3000   SURFXML_BUFFER_SET(AS_routing, "Full");
3001   DEBUG1("<AS id=\"%s\"\trouting=\"Full\">", cluster_id);
3002 #endif
3003   SURFXML_START_TAG(AS);
3004
3005   radical_elements = xbt_str_split(cluster_radical, ",");
3006   xbt_dynar_foreach(radical_elements, iter, groups) {
3007     radical_ends = xbt_str_split(groups, "-");
3008     switch (xbt_dynar_length(radical_ends)) {
3009     case 1:
3010       surf_parse_get_int(&start,
3011                          xbt_dynar_get_as(radical_ends, 0, char *));
3012       host_id = bprintf("%s%d%s", cluster_prefix, start, cluster_suffix);
3013 #ifndef HAVE_PCRE_LIB
3014       xbt_dynar_push_as(tab_elements_num, int, start);
3015 #endif
3016       link_id = bprintf("%s_link_%d", cluster_id, start);
3017
3018       DEBUG2("<host\tid=\"%s\"\tpower=\"%s\"/>", host_id, cluster_power);
3019       A_surfxml_host_state = A_surfxml_host_state_ON;
3020       SURFXML_BUFFER_SET(host_id, host_id);
3021       SURFXML_BUFFER_SET(host_power, cluster_power);
3022       SURFXML_BUFFER_SET(host_availability, "1.0");
3023       SURFXML_BUFFER_SET(host_availability_file, "");
3024       SURFXML_BUFFER_SET(host_state_file, "");
3025       SURFXML_START_TAG(host);
3026       SURFXML_END_TAG(host);
3027
3028       DEBUG3("<link\tid=\"%s\"\tbw=\"%s\"\tlat=\"%s\"/>", link_id,cluster_bw, cluster_lat);
3029       A_surfxml_link_state = A_surfxml_link_state_ON;
3030       A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
3031       if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
3032           {A_surfxml_link_sharing_policy =  A_surfxml_link_sharing_policy_FULLDUPLEX;}
3033       if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FATPIPE)
3034           {A_surfxml_link_sharing_policy =  A_surfxml_link_sharing_policy_FATPIPE;}
3035       SURFXML_BUFFER_SET(link_id, link_id);
3036       SURFXML_BUFFER_SET(link_bandwidth, cluster_bw);
3037       SURFXML_BUFFER_SET(link_latency, cluster_lat);
3038       SURFXML_BUFFER_SET(link_bandwidth_file, "");
3039       SURFXML_BUFFER_SET(link_latency_file, "");
3040       SURFXML_BUFFER_SET(link_state_file, "");
3041       SURFXML_START_TAG(link);
3042       SURFXML_END_TAG(link);
3043
3044       break;
3045
3046     case 2:
3047
3048       surf_parse_get_int(&start,
3049                          xbt_dynar_get_as(radical_ends, 0, char *));
3050       surf_parse_get_int(&end, xbt_dynar_get_as(radical_ends, 1, char *));
3051       DEBUG2("Create hosts and links from %d to %d", start, end);
3052       for (i = start; i <= end; i++) {
3053         host_id = bprintf("%s%d%s", cluster_prefix, i, cluster_suffix);
3054 #ifndef HAVE_PCRE_LIB
3055         xbt_dynar_push_as(tab_elements_num, int, i);
3056 #endif
3057         link_id = bprintf("%s_link_%d", cluster_id, i);
3058
3059         DEBUG2("<host\tid=\"%s\"\tpower=\"%s\"/>", host_id, cluster_power);
3060         A_surfxml_host_state = A_surfxml_host_state_ON;
3061         SURFXML_BUFFER_SET(host_id, host_id);
3062         SURFXML_BUFFER_SET(host_power, cluster_power);
3063         SURFXML_BUFFER_SET(host_availability, "1.0");
3064         SURFXML_BUFFER_SET(host_availability_file, "");
3065         SURFXML_BUFFER_SET(host_state_file, "");
3066         SURFXML_START_TAG(host);
3067         SURFXML_END_TAG(host);
3068
3069         DEBUG3("<link\tid=\"%s\"\tbw=\"%s\"\tlat=\"%s\"/>", link_id,cluster_bw, cluster_lat);
3070         A_surfxml_link_state = A_surfxml_link_state_ON;
3071         A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
3072         if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
3073             {A_surfxml_link_sharing_policy =  A_surfxml_link_sharing_policy_FULLDUPLEX;}
3074         if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FATPIPE)
3075             {A_surfxml_link_sharing_policy =  A_surfxml_link_sharing_policy_FATPIPE;}
3076         SURFXML_BUFFER_SET(link_id, link_id);
3077         SURFXML_BUFFER_SET(link_bandwidth, cluster_bw);
3078         SURFXML_BUFFER_SET(link_latency, cluster_lat);
3079         SURFXML_BUFFER_SET(link_bandwidth_file, "");
3080         SURFXML_BUFFER_SET(link_latency_file, "");
3081         SURFXML_BUFFER_SET(link_state_file, "");
3082         SURFXML_START_TAG(link);
3083         SURFXML_END_TAG(link);
3084       }
3085       break;
3086
3087     default:
3088       DEBUG0("Malformed radical");
3089     }
3090
3091     xbt_dynar_free(&radical_ends);
3092   }
3093
3094   DEBUG0(" ");
3095   router_id =
3096       bprintf("%s%s_router%s", cluster_prefix, cluster_id,
3097               cluster_suffix);
3098   link_router = bprintf("%s_link_%s_router", cluster_id, cluster_id);
3099   link_backbone = bprintf("%s_backbone", cluster_id);
3100
3101   DEBUG1("<router id=\"%s\"/>", router_id);
3102   SURFXML_BUFFER_SET(router_id, router_id);;
3103   SURFXML_START_TAG(router);
3104   SURFXML_END_TAG(router);
3105
3106   DEBUG3("<link\tid=\"%s\" bw=\"%s\" lat=\"%s\"/>", link_router,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_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
3110   {A_surfxml_link_sharing_policy =  A_surfxml_link_sharing_policy_FULLDUPLEX;}
3111   if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FATPIPE)
3112   {A_surfxml_link_sharing_policy =  A_surfxml_link_sharing_policy_FATPIPE;}
3113   SURFXML_BUFFER_SET(link_id, link_router);
3114   SURFXML_BUFFER_SET(link_bandwidth, cluster_bw);
3115   SURFXML_BUFFER_SET(link_latency, cluster_lat);
3116   SURFXML_BUFFER_SET(link_bandwidth_file, "");
3117   SURFXML_BUFFER_SET(link_latency_file, "");
3118   SURFXML_BUFFER_SET(link_state_file, "");
3119   SURFXML_START_TAG(link);
3120   SURFXML_END_TAG(link);
3121
3122   DEBUG3("<link\tid=\"%s\" bw=\"%s\" lat=\"%s\"/>", link_backbone,cluster_bw, cluster_lat);
3123   A_surfxml_link_state = A_surfxml_link_state_ON;
3124   A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
3125   if(cluster_bb_sharing_policy == A_surfxml_cluster_bb_sharing_policy_FATPIPE)
3126   {A_surfxml_link_sharing_policy =  A_surfxml_link_sharing_policy_FATPIPE;}
3127   SURFXML_BUFFER_SET(link_id, link_backbone);
3128   SURFXML_BUFFER_SET(link_bandwidth, cluster_bb_bw);
3129   SURFXML_BUFFER_SET(link_latency, cluster_bb_lat);
3130   SURFXML_BUFFER_SET(link_bandwidth_file, "");
3131   SURFXML_BUFFER_SET(link_latency_file, "");
3132   SURFXML_BUFFER_SET(link_state_file, "");
3133   SURFXML_START_TAG(link);
3134   SURFXML_END_TAG(link);
3135
3136   char *new_suffix = bprintf("%s", "");
3137
3138   radical_elements = xbt_str_split(cluster_suffix, ".");
3139   xbt_dynar_foreach(radical_elements, iter, groups) {
3140     if (strcmp(groups, "")) {
3141       new_suffix = bprintf("%s\\.%s", new_suffix, groups);
3142     }
3143   }
3144   route_src_dst = bprintf("%s(.*)%s", cluster_prefix, new_suffix);
3145
3146   DEBUG0(" ");
3147
3148 #ifdef HAVE_PCRE_LIB
3149
3150   DEBUG2("<route\tsrc=\"%s\"\tdst=\"%s\"", route_src_dst, route_src_dst);
3151   DEBUG0("symetrical=\"NO\">");
3152   SURFXML_BUFFER_SET(route_src, route_src_dst);
3153   SURFXML_BUFFER_SET(route_dst, route_src_dst);
3154   A_surfxml_route_symetrical = A_surfxml_route_symetrical_NO;
3155   SURFXML_START_TAG(route);
3156
3157   DEBUG1("<link_ctn\tid=\"%s_link_$1src\"/>", cluster_id);
3158   SURFXML_BUFFER_SET(link_ctn_id, bprintf("%s_link_$1src", cluster_id));
3159   A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
3160   if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
3161   {A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_UP;}
3162   SURFXML_START_TAG(link_ctn);
3163   SURFXML_END_TAG(link_ctn);
3164
3165   DEBUG1("<link_ctn\tid=\"%s_backbone\"/>", cluster_id);
3166   SURFXML_BUFFER_SET(link_ctn_id, bprintf("%s_backbone", cluster_id));
3167   A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
3168   SURFXML_START_TAG(link_ctn);
3169   SURFXML_END_TAG(link_ctn);
3170
3171   DEBUG1("<link_ctn\tid=\"%s_link_$1dst\"/>", cluster_id);
3172   SURFXML_BUFFER_SET(link_ctn_id, bprintf("%s_link_$1dst", cluster_id));
3173   A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
3174   if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
3175   {A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_DOWN;}
3176   SURFXML_START_TAG(link_ctn);
3177   SURFXML_END_TAG(link_ctn);
3178
3179   DEBUG0("</route>");
3180   SURFXML_END_TAG(route);
3181 #else
3182   for (i = 0; i <= xbt_dynar_length(tab_elements_num); i++) {
3183     for (j = 0; j <= xbt_dynar_length(tab_elements_num); j++) {
3184       if (i == xbt_dynar_length(tab_elements_num)) {
3185         route_src = router_id;
3186       } else {
3187         route_src =
3188             bprintf("%s%d%s", cluster_prefix,
3189                     xbt_dynar_get_as(tab_elements_num, i, int),
3190                     cluster_suffix);
3191       }
3192
3193       if (j == xbt_dynar_length(tab_elements_num)) {
3194         route_dst = router_id;
3195       } else {
3196         route_dst =
3197             bprintf("%s%d%s", cluster_prefix,
3198                     xbt_dynar_get_as(tab_elements_num, j, int),
3199                     cluster_suffix);
3200       }
3201
3202       DEBUG2("<route\tsrc=\"%s\"\tdst=\"%s\"", route_src, route_dst);
3203       DEBUG0("symetrical=\"NO\">");
3204       SURFXML_BUFFER_SET(route_src, route_src);
3205       SURFXML_BUFFER_SET(route_dst, route_dst);
3206       A_surfxml_route_symetrical = A_surfxml_route_symetrical_NO;
3207       SURFXML_START_TAG(route);
3208
3209       if (i == xbt_dynar_length(tab_elements_num)) {
3210         route_src = link_router;
3211       } else {
3212         route_src =
3213             bprintf("%s_link_%d", cluster_id,
3214                     xbt_dynar_get_as(tab_elements_num, i, int));
3215       }
3216
3217       if (j == xbt_dynar_length(tab_elements_num)) {
3218         route_dst = link_router;
3219       } else {
3220         route_dst =
3221             bprintf("%s_link_%d", cluster_id,
3222                     xbt_dynar_get_as(tab_elements_num, j, int));
3223       }
3224
3225       DEBUG1("<link_ctn\tid=\"%s\"/>", route_src);
3226       SURFXML_BUFFER_SET(link_ctn_id, route_src);
3227       A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
3228       if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
3229       {A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_UP;}
3230       SURFXML_START_TAG(link_ctn);
3231       SURFXML_END_TAG(link_ctn);
3232
3233       DEBUG1("<link_ctn\tid=\"%s_backbone\"/>", cluster_id);
3234       SURFXML_BUFFER_SET(link_ctn_id, bprintf("%s_backbone", cluster_id));
3235       A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
3236       SURFXML_START_TAG(link_ctn);
3237       SURFXML_END_TAG(link_ctn);
3238
3239       DEBUG1("<link_ctn\tid=\"%s\"/>", route_dst);
3240       SURFXML_BUFFER_SET(link_ctn_id, route_dst);
3241       A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
3242       if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
3243       {A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_DOWN;}
3244       SURFXML_START_TAG(link_ctn);
3245       SURFXML_END_TAG(link_ctn);
3246
3247       DEBUG0("</route>");
3248       SURFXML_END_TAG(route);
3249     }
3250   }
3251   xbt_dynar_free(&tab_elements_num);
3252 #endif
3253
3254   DEBUG0("</AS>");
3255   SURFXML_END_TAG(AS);
3256   DEBUG0(" ");
3257
3258   surfxml_bufferstack_pop(1);
3259 }
3260
3261 /*
3262  * New methods to init the routing model component from the lua script
3263  */
3264
3265 /*
3266  * calling parse_S_AS_lua with lua values
3267  */
3268 void routing_AS_init(const char *AS_id, const char *AS_routing)
3269 {
3270   parse_S_AS_lua((char *) AS_id, (char *) AS_routing);
3271 }
3272
3273 /*
3274  * calling parse_E_AS_lua to fisnish the creation of routing component
3275  */
3276 void routing_AS_end(const char *AS_id)
3277 {
3278   parse_E_AS_lua((char *) AS_id);
3279 }
3280
3281 /*
3282  * add a host to the network element list
3283  */
3284
3285 void routing_add_host(const char *host_id)
3286 {
3287   parse_S_host_lua((char *) host_id);
3288 }
3289
3290 /*
3291  * Set a new link on the actual list of link for a route or ASroute
3292  */
3293 void routing_add_link(const char *link_id)
3294 {
3295   parse_E_link_c_ctn_new_elem_lua((char *) link_id);
3296 }
3297
3298 /*
3299  *Set the endpoints for a route
3300  */
3301 void routing_set_route(const char *src_id, const char *dst_id)
3302 {
3303   parse_S_route_new_and_endpoints_lua((char *) src_id, (char *) dst_id);
3304 }
3305
3306 /*
3307  * Store the route by calling parse_E_route_store_route
3308  */
3309 void routing_store_route(void)
3310 {
3311   parse_E_route_store_route();
3312 }