Logo AND Algorithmique Numérique Distribuée

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