Logo AND Algorithmique Numérique Distribuée

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