Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of github.com:simgrid/simgrid into dev_7
[simgrid.git] / src / surf / xml / surfxml_sax_cb.cpp
1 /* Copyright (c) 2006-2020. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "simgrid/Exception.hpp"
7 #include "simgrid/kernel/routing/NetPoint.hpp"
8 #include "simgrid/s4u/Engine.hpp"
9 #include "simgrid/sg_config.hpp"
10 #include "src/kernel/resource/profile/FutureEvtSet.hpp"
11 #include "src/kernel/resource/profile/Profile.hpp"
12 #include "src/surf/network_interface.hpp"
13 #include "src/surf/surf_interface.hpp"
14 #include "src/surf/xml/platf_private.hpp"
15 #include "surf/surf.hpp"
16 #include "xbt/file.hpp"
17 #include "xbt/parse_units.hpp"
18
19 #include <boost/algorithm/string/classification.hpp>
20 #include <boost/algorithm/string/split.hpp>
21 #include <string>
22 #include <tuple>
23 #include <unordered_map>
24 #include <vector>
25
26 #include "simgrid_dtd.c"
27
28 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_parse, surf, "Logging specific to the SURF parsing module");
29
30 std::string surf_parsed_filename; // Currently parsed file (for the error messages)
31 std::vector<simgrid::kernel::resource::LinkImpl*>
32     parsed_link_list; /* temporary store of current link list of a route */
33 std::vector<simgrid::kernel::resource::DiskImpl*> parsed_disk_list; /* temporary store of current disk list of a host */
34 /*
35  * Helping functions
36  */
37 void surf_parse_assert(bool cond, const std::string& msg)
38 {
39   if (not cond)
40     surf_parse_error(msg);
41 }
42
43 void surf_parse_error(const std::string& msg)
44 {
45   throw simgrid::ParseError(surf_parsed_filename, surf_parse_lineno, msg);
46 }
47
48 void surf_parse_assert_netpoint(const std::string& hostname, const std::string& pre, const std::string& post)
49 {
50   if (simgrid::s4u::Engine::get_instance()->netpoint_by_name_or_null(hostname) != nullptr) // found
51     return;
52
53   std::string msg = pre + hostname + post + " Existing netpoints: \n";
54
55   std::vector<simgrid::kernel::routing::NetPoint*> netpoints =
56       simgrid::s4u::Engine::get_instance()->get_all_netpoints();
57   std::sort(netpoints.begin(), netpoints.end(),
58             [](const simgrid::kernel::routing::NetPoint* a, const simgrid::kernel::routing::NetPoint* b) {
59               return a->get_name() < b->get_name();
60             });
61   bool first = true;
62   for (auto const& np : netpoints) {
63     if (np->is_netzone())
64       continue;
65
66     if (not first)
67       msg += ",";
68     first = false;
69     msg += "'" + np->get_name() + "'";
70     if (msg.length() > 4096) {
71       msg.pop_back(); // remove trailing quote
72       msg += "...(list truncated)......";
73       break;
74     }
75   }
76   surf_parse_error(msg);
77 }
78
79 double surf_parse_get_double(const std::string& s)
80 {
81   try {
82     return std::stod(s);
83   } catch (const std::invalid_argument&) {
84     surf_parse_error(s + " is not a double");
85   }
86 }
87
88 int surf_parse_get_int(const std::string& s)
89 {
90   try {
91     return std::stoi(s);
92   } catch (const std::invalid_argument&) {
93     surf_parse_error(s + " is not a double");
94   }
95 }
96
97 /* Turn something like "1-4,6,9-11" into the vector {1,2,3,4,6,9,10,11} */
98 static std::vector<int>* explodesRadical(const std::string& radicals)
99 {
100   auto* exploded = new std::vector<int>();
101
102   // Make all hosts
103   std::vector<std::string> radical_elements;
104   boost::split(radical_elements, radicals, boost::is_any_of(","));
105   for (auto const& group : radical_elements) {
106     std::vector<std::string> radical_ends;
107     boost::split(radical_ends, group, boost::is_any_of("-"));
108     int start = surf_parse_get_int(radical_ends.front());
109     int end   = 0;
110
111     switch (radical_ends.size()) {
112       case 1:
113         end = start;
114         break;
115       case 2:
116         end = surf_parse_get_int(radical_ends.back());
117         break;
118       default:
119         surf_parse_error(std::string("Malformed radical: ") + group);
120     }
121     for (int i = start; i <= end; i++)
122       exploded->push_back(i);
123   }
124
125   return exploded;
126 }
127
128
129 /*
130  * All the callback lists that can be overridden anywhere.
131  * (this list should probably be reduced to the bare minimum to allow the models to work)
132  */
133
134 /* make sure these symbols are defined as strong ones in this file so that the linker can resolve them */
135
136 std::vector<std::unordered_map<std::string, std::string>*> property_sets;
137
138 /* The default current property receiver. Setup in the corresponding opening callbacks. */
139 std::unordered_map<std::string, std::string>* current_model_property_set = nullptr;
140
141 FILE *surf_file_to_parse = nullptr;
142
143 /* Stuff relative to storage */
144 void STag_surfxml_storage()
145 {
146   XBT_DEBUG("STag_surfxml_storage");
147   property_sets.push_back(new std::unordered_map<std::string, std::string>());
148 }
149
150 void ETag_surfxml_storage()
151 {
152   simgrid::kernel::routing::StorageCreationArgs storage;
153
154   storage.properties = property_sets.back();
155   property_sets.pop_back();
156
157   storage.filename     = surf_parsed_filename;
158   storage.lineno       = surf_parse_lineno;
159   storage.id           = A_surfxml_storage_id;
160   storage.type_id      = A_surfxml_storage_typeId;
161   storage.content      = A_surfxml_storage_content;
162   storage.attach       = A_surfxml_storage_attach;
163
164   sg_platf_new_storage(&storage);
165 }
166 void STag_surfxml_storage___type()
167 {
168   XBT_DEBUG("STag_surfxml_storage___type");
169   property_sets.push_back(new std::unordered_map<std::string, std::string>());
170   xbt_assert(current_model_property_set == nullptr, "Someone forgot to reset the model property set to nullptr in its closing tag (or XML malformed)");
171 }
172 void ETag_surfxml_storage___type()
173 {
174   simgrid::kernel::routing::StorageTypeCreationArgs storage_type;
175
176   storage_type.properties = property_sets.back();
177   property_sets.pop_back();
178
179   storage_type.model_properties = current_model_property_set;
180   current_model_property_set    = nullptr;
181
182   storage_type.content = A_surfxml_storage___type_content;
183   storage_type.id      = A_surfxml_storage___type_id;
184   storage_type.model   = A_surfxml_storage___type_model;
185   storage_type.size =
186       static_cast<sg_size_t>(surf_parse_get_size(surf_parsed_filename, surf_parse_lineno, A_surfxml_storage___type_size,
187                                                  "size of storage type", storage_type.id.c_str()));
188   sg_platf_new_storage_type(&storage_type);
189 }
190
191 void STag_surfxml_mount()
192 {
193   XBT_DEBUG("STag_surfxml_mount");
194 }
195
196 void ETag_surfxml_mount()
197 {
198   simgrid::kernel::routing::MountCreationArgs mount;
199
200   mount.name      = A_surfxml_mount_name;
201   mount.storageId = A_surfxml_mount_storageId;
202   sg_platf_new_mount(&mount);
203 }
204
205 void STag_surfxml_include()
206 {
207   xbt_die("<include> tag was removed in SimGrid v3.18. Please stop using it now.");
208 }
209
210 void ETag_surfxml_include()
211 {
212   /* Won't happen since <include> is now removed since v3.18. */
213 }
214
215 /* Stag and Etag parse functions */
216 void STag_surfxml_platform() {
217   double version = surf_parse_get_double(A_surfxml_platform_version);
218
219   surf_parse_assert((version >= 1.0), "******* BIG FAT WARNING *********\n "
220       "You're using an ancient XML file.\n"
221       "Since SimGrid 3.1, units are Bytes, Flops, and seconds "
222       "instead of MBytes, MFlops and seconds.\n"
223
224       "Use simgrid_update_xml to update your file automatically. "
225       "This program is installed automatically with SimGrid, or "
226       "available in the tools/ directory of the source archive.\n"
227
228       "Please check also out the SURF section of the ChangeLog for "
229       "the 3.1 version for more information. \n"
230
231       "Last, do not forget to also update your values for "
232       "the calls to MSG_task_create (if any).");
233   surf_parse_assert((version >= 3.0), "******* BIG FAT WARNING *********\n "
234       "You're using an old XML file.\n"
235       "Use simgrid_update_xml to update your file automatically. "
236       "This program is installed automatically with SimGrid, or "
237       "available in the tools/ directory of the source archive.");
238   surf_parse_assert((version >= 4.0),
239              std::string("******* THIS FILE IS TOO OLD (v:")+std::to_string(version)+") *********\n "
240              "Changes introduced in SimGrid 3.13:\n"
241              "  - 'power' attribute of hosts (and others) got renamed to 'speed'.\n"
242              "  - In <trace_connect>, attribute kind=\"POWER\" is now kind=\"SPEED\".\n"
243              "  - DOCTYPE now point to the rignt URL.\n"
244              "  - speed, bandwidth and latency attributes now MUST have an explicit unit (f, Bps, s by default)"
245              "\n\n"
246              "Use simgrid_update_xml to update your file automatically. "
247              "This program is installed automatically with SimGrid, or "
248              "available in the tools/ directory of the source archive.");
249   if (version < 4.1) {
250     XBT_INFO("You're using a v%.1f XML file (%s) while the current standard is v4.1 "
251              "That's fine, the new version is backward compatible. \n\n"
252              "Use simgrid_update_xml to update your file automatically to get rid of this warning. "
253              "This program is installed automatically with SimGrid, or "
254              "available in the tools/ directory of the source archive.",
255              version, surf_parsed_filename.c_str());
256   }
257   surf_parse_assert(version <= 4.1,
258              std::string("******* THIS FILE COMES FROM THE FUTURE (v:")+std::to_string(version)+") *********\n "
259              "The most recent formalism that this version of SimGrid understands is v4.1.\n"
260              "Please update your code, or use another, more adapted, file.");
261 }
262 void ETag_surfxml_platform(){
263   simgrid::s4u::Engine::on_platform_created();
264 }
265
266 void STag_surfxml_host(){
267   property_sets.push_back(new std::unordered_map<std::string, std::string>());
268 }
269
270 void STag_surfxml_prop()
271 {
272   property_sets.back()->insert({A_surfxml_prop_id, A_surfxml_prop_value});
273   XBT_DEBUG("add prop %s=%s into current property set %p", A_surfxml_prop_id, A_surfxml_prop_value,
274             property_sets.back());
275 }
276
277 void ETag_surfxml_host()    {
278   simgrid::kernel::routing::HostCreationArgs host;
279
280   host.properties = property_sets.back();
281   property_sets.pop_back();
282
283   host.id = A_surfxml_host_id;
284
285   host.speed_per_pstate =
286       xbt_parse_get_all_speeds(surf_parsed_filename, surf_parse_lineno, A_surfxml_host_speed, "speed of host", host.id);
287
288   XBT_DEBUG("pstate: %s", A_surfxml_host_pstate);
289   host.core_amount = surf_parse_get_int(A_surfxml_host_core);
290
291   host.speed_trace = nullptr;
292   if (A_surfxml_host_availability___file[0] != '\0') {
293     XBT_WARN("The availability_file attribute in <host> is now deprecated. Please, use 'speed_file' instead.");
294     host.speed_trace = simgrid::kernel::profile::Profile::from_file(A_surfxml_host_availability___file);
295   }
296   if (A_surfxml_host_speed___file[0] != '\0')
297     host.speed_trace = simgrid::kernel::profile::Profile::from_file(A_surfxml_host_speed___file);
298   host.state_trace = A_surfxml_host_state___file[0]
299                          ? simgrid::kernel::profile::Profile::from_file(A_surfxml_host_state___file)
300                          : nullptr;
301   host.pstate      = surf_parse_get_int(A_surfxml_host_pstate);
302   host.coord       = A_surfxml_host_coordinates;
303   host.disks.swap(parsed_disk_list);
304
305   sg_platf_new_host(&host);
306 }
307
308 void STag_surfxml_disk() {
309   property_sets.push_back(new std::unordered_map<std::string, std::string>());
310 }
311
312 void ETag_surfxml_disk() {
313   simgrid::kernel::routing::DiskCreationArgs disk;
314   disk.properties = property_sets.back();
315   property_sets.pop_back();
316
317   disk.id       = A_surfxml_disk_id;
318   disk.read_bw  = xbt_parse_get_bandwidth(surf_parsed_filename, surf_parse_lineno, A_surfxml_disk_read___bw,
319                                          "read_bw of disk ", disk.id);
320   disk.write_bw = xbt_parse_get_bandwidth(surf_parsed_filename, surf_parse_lineno, A_surfxml_disk_write___bw,
321                                           "write_bw of disk ", disk.id);
322
323   parsed_disk_list.push_back(sg_platf_new_disk(&disk));
324 }
325
326 void STag_surfxml_host___link(){
327   XBT_DEBUG("Create a Host_link for %s",A_surfxml_host___link_id);
328   simgrid::kernel::routing::HostLinkCreationArgs host_link;
329
330   host_link.id        = A_surfxml_host___link_id;
331   host_link.link_up   = A_surfxml_host___link_up;
332   host_link.link_down = A_surfxml_host___link_down;
333   sg_platf_new_hostlink(&host_link);
334 }
335
336 void STag_surfxml_router(){
337   sg_platf_new_router(A_surfxml_router_id, A_surfxml_router_coordinates);
338 }
339
340 void ETag_surfxml_cluster(){
341   simgrid::kernel::routing::ClusterCreationArgs cluster;
342   cluster.properties = property_sets.back();
343   property_sets.pop_back();
344
345   cluster.id          = A_surfxml_cluster_id;
346   cluster.prefix      = A_surfxml_cluster_prefix;
347   cluster.suffix      = A_surfxml_cluster_suffix;
348   cluster.radicals    = explodesRadical(A_surfxml_cluster_radical);
349   cluster.speeds      = xbt_parse_get_all_speeds(surf_parsed_filename, surf_parse_lineno, A_surfxml_cluster_speed,
350                                             "speed of cluster", cluster.id);
351   cluster.core_amount = surf_parse_get_int(A_surfxml_cluster_core);
352   cluster.bw = xbt_parse_get_bandwidth(surf_parsed_filename, surf_parse_lineno, A_surfxml_cluster_bw, "bw of cluster",
353                                        cluster.id);
354   cluster.lat =
355       xbt_parse_get_time(surf_parsed_filename, surf_parse_lineno, A_surfxml_cluster_lat, "lat of cluster", cluster.id);
356   if(strcmp(A_surfxml_cluster_bb___bw,""))
357     cluster.bb_bw = xbt_parse_get_bandwidth(surf_parsed_filename, surf_parse_lineno, A_surfxml_cluster_bb___bw,
358                                             "bb_bw of cluster", cluster.id);
359   if(strcmp(A_surfxml_cluster_bb___lat,""))
360     cluster.bb_lat = xbt_parse_get_time(surf_parsed_filename, surf_parse_lineno, A_surfxml_cluster_bb___lat,
361                                         "bb_lat of cluster", cluster.id);
362   if(strcmp(A_surfxml_cluster_limiter___link,""))
363     cluster.limiter_link =
364         xbt_parse_get_bandwidth(surf_parsed_filename, surf_parse_lineno, A_surfxml_cluster_limiter___link,
365                                 "limiter_link of cluster", cluster.id);
366   if(strcmp(A_surfxml_cluster_loopback___bw,""))
367     cluster.loopback_bw = xbt_parse_get_bandwidth(
368         surf_parsed_filename, surf_parse_lineno, A_surfxml_cluster_loopback___bw, "loopback_bw of cluster", cluster.id);
369   if(strcmp(A_surfxml_cluster_loopback___lat,""))
370     cluster.loopback_lat = xbt_parse_get_time(surf_parsed_filename, surf_parse_lineno, A_surfxml_cluster_loopback___lat,
371                                               "loopback_lat of cluster", cluster.id);
372
373   switch(AX_surfxml_cluster_topology){
374   case A_surfxml_cluster_topology_FLAT:
375     cluster.topology = simgrid::kernel::routing::ClusterTopology::FLAT;
376     break;
377   case A_surfxml_cluster_topology_TORUS:
378     cluster.topology = simgrid::kernel::routing::ClusterTopology::TORUS;
379     break;
380   case A_surfxml_cluster_topology_FAT___TREE:
381     cluster.topology = simgrid::kernel::routing::ClusterTopology::FAT_TREE;
382     break;
383   case A_surfxml_cluster_topology_DRAGONFLY:
384     cluster.topology = simgrid::kernel::routing::ClusterTopology::DRAGONFLY;
385     break;
386   default:
387     surf_parse_error(std::string("Invalid cluster topology for cluster ") + cluster.id);
388   }
389   cluster.topo_parameters = A_surfxml_cluster_topo___parameters;
390   cluster.router_id = A_surfxml_cluster_router___id;
391
392   switch (AX_surfxml_cluster_sharing___policy) {
393   case A_surfxml_cluster_sharing___policy_SHARED:
394     cluster.sharing_policy = simgrid::s4u::Link::SharingPolicy::SHARED;
395     break;
396   case A_surfxml_cluster_sharing___policy_FULLDUPLEX:
397     XBT_WARN("FULLDUPLEX is now deprecated. Please update your platform file to use SPLITDUPLEX instead.");
398     cluster.sharing_policy = simgrid::s4u::Link::SharingPolicy::SPLITDUPLEX;
399     break;
400   case A_surfxml_cluster_sharing___policy_SPLITDUPLEX:
401     cluster.sharing_policy = simgrid::s4u::Link::SharingPolicy::SPLITDUPLEX;
402     break;
403   case A_surfxml_cluster_sharing___policy_FATPIPE:
404     cluster.sharing_policy = simgrid::s4u::Link::SharingPolicy::FATPIPE;
405     break;
406   default:
407     surf_parse_error(std::string("Invalid cluster sharing policy for cluster ") + cluster.id);
408   }
409   switch (AX_surfxml_cluster_bb___sharing___policy) {
410   case A_surfxml_cluster_bb___sharing___policy_FATPIPE:
411     cluster.bb_sharing_policy = simgrid::s4u::Link::SharingPolicy::FATPIPE;
412     break;
413   case A_surfxml_cluster_bb___sharing___policy_SHARED:
414     cluster.bb_sharing_policy = simgrid::s4u::Link::SharingPolicy::SHARED;
415     break;
416   default:
417     surf_parse_error(std::string("Invalid bb sharing policy in cluster ") + cluster.id);
418   }
419
420   sg_platf_new_cluster(&cluster);
421 }
422
423 void STag_surfxml_cluster(){
424   property_sets.push_back(new std::unordered_map<std::string, std::string>());
425 }
426
427 void STag_surfxml_cabinet(){
428   simgrid::kernel::routing::CabinetCreationArgs cabinet;
429   cabinet.id      = A_surfxml_cabinet_id;
430   cabinet.prefix  = A_surfxml_cabinet_prefix;
431   cabinet.suffix  = A_surfxml_cabinet_suffix;
432   cabinet.speed   = xbt_parse_get_speed(surf_parsed_filename, surf_parse_lineno, A_surfxml_cabinet_speed,
433                                       "speed of cabinet", cabinet.id.c_str());
434   cabinet.bw  = xbt_parse_get_bandwidth(surf_parsed_filename, surf_parse_lineno, A_surfxml_cabinet_bw, "bw of cabinet",
435                                        cabinet.id.c_str());
436   cabinet.lat = xbt_parse_get_time(surf_parsed_filename, surf_parse_lineno, A_surfxml_cabinet_lat, "lat of cabinet",
437                                    cabinet.id.c_str());
438   cabinet.radicals = explodesRadical(A_surfxml_cabinet_radical);
439
440   sg_platf_new_cabinet(&cabinet);
441 }
442
443 void STag_surfxml_peer(){
444   simgrid::kernel::routing::PeerCreationArgs peer;
445
446   peer.id          = std::string(A_surfxml_peer_id);
447   peer.speed       = xbt_parse_get_speed(surf_parsed_filename, surf_parse_lineno, A_surfxml_peer_speed, "speed of peer",
448                                    peer.id.c_str());
449   peer.bw_in = xbt_parse_get_bandwidth(surf_parsed_filename, surf_parse_lineno, A_surfxml_peer_bw___in, "bw_in of peer",
450                                        peer.id.c_str());
451   peer.bw_out      = xbt_parse_get_bandwidth(surf_parsed_filename, surf_parse_lineno, A_surfxml_peer_bw___out,
452                                         "bw_out of peer", peer.id.c_str());
453   peer.coord       = A_surfxml_peer_coordinates;
454   peer.speed_trace = nullptr;
455   if (A_surfxml_peer_availability___file[0] != '\0') {
456     XBT_WARN("The availability_file attribute in <peer> is now deprecated. Please, use 'speed_file' instead.");
457     peer.speed_trace = simgrid::kernel::profile::Profile::from_file(A_surfxml_peer_availability___file);
458   }
459   if (A_surfxml_peer_speed___file[0] != '\0')
460     peer.speed_trace = simgrid::kernel::profile::Profile::from_file(A_surfxml_peer_speed___file);
461   peer.state_trace = A_surfxml_peer_state___file[0]
462                          ? simgrid::kernel::profile::Profile::from_file(A_surfxml_peer_state___file)
463                          : nullptr;
464
465   if (A_surfxml_peer_lat[0] != '\0')
466     XBT_WARN("The latency attribute in <peer> is now deprecated. Use the z coordinate instead of '%s'.",
467              A_surfxml_peer_lat);
468
469   sg_platf_new_peer(&peer);
470 }
471
472 void STag_surfxml_link(){
473   property_sets.push_back(new std::unordered_map<std::string, std::string>());
474 }
475
476 void ETag_surfxml_link(){
477   simgrid::kernel::routing::LinkCreationArgs link;
478
479   link.properties = property_sets.back();
480   property_sets.pop_back();
481
482   link.id                  = std::string(A_surfxml_link_id);
483   link.bandwidths          = xbt_parse_get_bandwidths(surf_parsed_filename, surf_parse_lineno, A_surfxml_link_bandwidth,
484                                              "bandwidth of link", link.id.c_str());
485   link.bandwidth_trace     = A_surfxml_link_bandwidth___file[0]
486                              ? simgrid::kernel::profile::Profile::from_file(A_surfxml_link_bandwidth___file)
487                              : nullptr;
488   link.latency = xbt_parse_get_time(surf_parsed_filename, surf_parse_lineno, A_surfxml_link_latency, "latency of link",
489                                     link.id.c_str());
490   link.latency_trace       = A_surfxml_link_latency___file[0]
491                            ? simgrid::kernel::profile::Profile::from_file(A_surfxml_link_latency___file)
492                            : nullptr;
493   link.state_trace = A_surfxml_link_state___file[0]
494                          ? simgrid::kernel::profile::Profile::from_file(A_surfxml_link_state___file)
495                          : nullptr;
496
497   switch (A_surfxml_link_sharing___policy) {
498   case A_surfxml_link_sharing___policy_SHARED:
499     link.policy = simgrid::s4u::Link::SharingPolicy::SHARED;
500     break;
501   case A_surfxml_link_sharing___policy_FATPIPE:
502     link.policy = simgrid::s4u::Link::SharingPolicy::FATPIPE;
503     break;
504   case A_surfxml_link_sharing___policy_FULLDUPLEX:
505     XBT_WARN("FULLDUPLEX is now deprecated. Please update your platform file to use SPLITDUPLEX instead.");
506     link.policy = simgrid::s4u::Link::SharingPolicy::SPLITDUPLEX;
507     break;
508   case A_surfxml_link_sharing___policy_SPLITDUPLEX:
509     link.policy = simgrid::s4u::Link::SharingPolicy::SPLITDUPLEX;
510     break;
511   case A_surfxml_link_sharing___policy_WIFI:
512     link.policy = simgrid::s4u::Link::SharingPolicy::WIFI;
513     break;
514   default:
515     surf_parse_error(std::string("Invalid sharing policy in link ") + link.id);
516   }
517
518   sg_platf_new_link(&link);
519 }
520
521 void STag_surfxml_link___ctn()
522 {
523   simgrid::kernel::resource::LinkImpl* link = nullptr;
524   switch (A_surfxml_link___ctn_direction) {
525   case AU_surfxml_link___ctn_direction:
526   case A_surfxml_link___ctn_direction_NONE:
527     link = simgrid::s4u::Link::by_name(std::string(A_surfxml_link___ctn_id))->get_impl();
528     break;
529   case A_surfxml_link___ctn_direction_UP:
530     link = simgrid::s4u::Link::by_name(std::string(A_surfxml_link___ctn_id) + "_UP")->get_impl();
531     break;
532   case A_surfxml_link___ctn_direction_DOWN:
533     link = simgrid::s4u::Link::by_name(std::string(A_surfxml_link___ctn_id) + "_DOWN")->get_impl();
534     break;
535   default:
536     surf_parse_error(std::string("Invalid direction for link ") + A_surfxml_link___ctn_id);
537   }
538
539   const char* dirname = "";
540   switch (A_surfxml_link___ctn_direction) {
541     case A_surfxml_link___ctn_direction_UP:
542       dirname = " (upward)";
543       break;
544     case A_surfxml_link___ctn_direction_DOWN:
545       dirname = " (downward)";
546       break;
547     default:
548       dirname = "";
549   }
550   surf_parse_assert(link != nullptr, std::string("No such link: '") + A_surfxml_link___ctn_id + "'" + dirname);
551   parsed_link_list.push_back(link);
552 }
553
554 void ETag_surfxml_backbone(){
555   simgrid::kernel::routing::LinkCreationArgs link;
556
557   link.properties = nullptr;
558   link.id = std::string(A_surfxml_backbone_id);
559   link.bandwidths.push_back(xbt_parse_get_bandwidth(
560       surf_parsed_filename, surf_parse_lineno, A_surfxml_backbone_bandwidth, "bandwidth of backbone", link.id.c_str()));
561   link.latency    = xbt_parse_get_time(surf_parsed_filename, surf_parse_lineno, A_surfxml_backbone_latency,
562                                     "latency of backbone", link.id.c_str());
563   link.policy     = simgrid::s4u::Link::SharingPolicy::SHARED;
564
565   sg_platf_new_link(&link);
566   routing_cluster_add_backbone(simgrid::s4u::Link::by_name(std::string(A_surfxml_backbone_id))->get_impl());
567 }
568
569 void STag_surfxml_route(){
570   surf_parse_assert_netpoint(A_surfxml_route_src, "Route src='", "' does name a node.");
571   surf_parse_assert_netpoint(A_surfxml_route_dst, "Route dst='", "' does name a node.");
572 }
573
574 void STag_surfxml_ASroute(){
575   surf_parse_assert_netpoint(A_surfxml_ASroute_src, "ASroute src='", "' does name a node.");
576   surf_parse_assert_netpoint(A_surfxml_ASroute_dst, "ASroute dst='", "' does name a node.");
577
578   surf_parse_assert_netpoint(A_surfxml_ASroute_gw___src, "ASroute gw_src='", "' does name a node.");
579   surf_parse_assert_netpoint(A_surfxml_ASroute_gw___dst, "ASroute gw_dst='", "' does name a node.");
580 }
581 void STag_surfxml_zoneRoute(){
582   surf_parse_assert_netpoint(A_surfxml_zoneRoute_src, "zoneRoute src='", "' does name a node.");
583   surf_parse_assert_netpoint(A_surfxml_zoneRoute_dst, "zoneRoute dst='", "' does name a node.");
584   surf_parse_assert_netpoint(A_surfxml_zoneRoute_gw___src, "zoneRoute gw_src='", "' does name a node.");
585   surf_parse_assert_netpoint(A_surfxml_zoneRoute_gw___dst, "zoneRoute gw_dst='", "' does name a node.");
586 }
587
588 void STag_surfxml_bypassRoute(){
589   surf_parse_assert_netpoint(A_surfxml_bypassRoute_src, "bypassRoute src='", "' does name a node.");
590   surf_parse_assert_netpoint(A_surfxml_bypassRoute_dst, "bypassRoute dst='", "' does name a node.");
591 }
592
593 void STag_surfxml_bypassASroute(){
594   surf_parse_assert_netpoint(A_surfxml_bypassASroute_src, "bypassASroute src='", "' does name a node.");
595   surf_parse_assert_netpoint(A_surfxml_bypassASroute_dst, "bypassASroute dst='", "' does name a node.");
596   surf_parse_assert_netpoint(A_surfxml_bypassASroute_gw___src, "bypassASroute gw_src='", "' does name a node.");
597   surf_parse_assert_netpoint(A_surfxml_bypassASroute_gw___dst, "bypassASroute gw_dst='", "' does name a node.");
598 }
599 void STag_surfxml_bypassZoneRoute(){
600   surf_parse_assert_netpoint(A_surfxml_bypassZoneRoute_src, "bypassZoneRoute src='", "' does name a node.");
601   surf_parse_assert_netpoint(A_surfxml_bypassZoneRoute_dst, "bypassZoneRoute dst='", "' does name a node.");
602   surf_parse_assert_netpoint(A_surfxml_bypassZoneRoute_gw___src, "bypassZoneRoute gw_src='", "' does name a node.");
603   surf_parse_assert_netpoint(A_surfxml_bypassZoneRoute_gw___dst, "bypassZoneRoute gw_dst='", "' does name a node.");
604 }
605
606 void ETag_surfxml_route(){
607   simgrid::kernel::routing::RouteCreationArgs route;
608
609   route.src         = sg_netpoint_by_name_or_null(A_surfxml_route_src); // tested to not be nullptr in start tag
610   route.dst         = sg_netpoint_by_name_or_null(A_surfxml_route_dst); // tested to not be nullptr in start tag
611   route.gw_src    = nullptr;
612   route.gw_dst    = nullptr;
613   route.symmetrical = (A_surfxml_route_symmetrical == A_surfxml_route_symmetrical_YES);
614
615   route.link_list.swap(parsed_link_list);
616
617   sg_platf_new_route(&route);
618 }
619
620 void ETag_surfxml_ASroute()
621 {
622   AX_surfxml_zoneRoute_src = AX_surfxml_ASroute_src;
623   AX_surfxml_zoneRoute_dst = AX_surfxml_ASroute_dst;
624   AX_surfxml_zoneRoute_gw___src = AX_surfxml_ASroute_gw___src;
625   AX_surfxml_zoneRoute_gw___dst = AX_surfxml_ASroute_gw___dst;
626   AX_surfxml_zoneRoute_symmetrical = (AT_surfxml_zoneRoute_symmetrical)AX_surfxml_ASroute_symmetrical;
627   ETag_surfxml_zoneRoute();
628 }
629 void ETag_surfxml_zoneRoute()
630 {
631   simgrid::kernel::routing::RouteCreationArgs ASroute;
632
633   ASroute.src = sg_netpoint_by_name_or_null(A_surfxml_zoneRoute_src); // tested to not be nullptr in start tag
634   ASroute.dst = sg_netpoint_by_name_or_null(A_surfxml_zoneRoute_dst); // tested to not be nullptr in start tag
635
636   ASroute.gw_src = sg_netpoint_by_name_or_null(A_surfxml_zoneRoute_gw___src); // tested to not be nullptr in start tag
637   ASroute.gw_dst = sg_netpoint_by_name_or_null(A_surfxml_zoneRoute_gw___dst); // tested to not be nullptr in start tag
638
639   ASroute.link_list.swap(parsed_link_list);
640
641   switch (A_surfxml_zoneRoute_symmetrical) {
642   case AU_surfxml_zoneRoute_symmetrical:
643   case A_surfxml_zoneRoute_symmetrical_YES:
644     ASroute.symmetrical = true;
645     break;
646   case A_surfxml_zoneRoute_symmetrical_NO:
647     ASroute.symmetrical = false;
648     break;
649   default:
650     THROW_IMPOSSIBLE;
651   }
652
653   sg_platf_new_route(&ASroute);
654 }
655
656 void ETag_surfxml_bypassRoute(){
657   simgrid::kernel::routing::RouteCreationArgs route;
658
659   route.src         = sg_netpoint_by_name_or_null(A_surfxml_bypassRoute_src); // tested to not be nullptr in start tag
660   route.dst         = sg_netpoint_by_name_or_null(A_surfxml_bypassRoute_dst); // tested to not be nullptr in start tag
661   route.gw_src = nullptr;
662   route.gw_dst = nullptr;
663   route.symmetrical = false;
664
665   route.link_list.swap(parsed_link_list);
666
667   sg_platf_new_bypassRoute(&route);
668 }
669
670 void ETag_surfxml_bypassASroute()
671 {
672   AX_surfxml_bypassZoneRoute_src = AX_surfxml_bypassASroute_src;
673   AX_surfxml_bypassZoneRoute_dst = AX_surfxml_bypassASroute_dst;
674   AX_surfxml_bypassZoneRoute_gw___src = AX_surfxml_bypassASroute_gw___src;
675   AX_surfxml_bypassZoneRoute_gw___dst = AX_surfxml_bypassASroute_gw___dst;
676   ETag_surfxml_bypassZoneRoute();
677 }
678 void ETag_surfxml_bypassZoneRoute()
679 {
680   simgrid::kernel::routing::RouteCreationArgs ASroute;
681
682   ASroute.src         = sg_netpoint_by_name_or_null(A_surfxml_bypassZoneRoute_src);
683   ASroute.dst         = sg_netpoint_by_name_or_null(A_surfxml_bypassZoneRoute_dst);
684   ASroute.link_list.swap(parsed_link_list);
685
686   ASroute.symmetrical = false;
687
688   ASroute.gw_src = sg_netpoint_by_name_or_null(A_surfxml_bypassZoneRoute_gw___src);
689   ASroute.gw_dst = sg_netpoint_by_name_or_null(A_surfxml_bypassZoneRoute_gw___dst);
690
691   sg_platf_new_bypassRoute(&ASroute);
692 }
693
694 void ETag_surfxml_trace(){
695   simgrid::kernel::routing::ProfileCreationArgs trace;
696
697   trace.id = A_surfxml_trace_id;
698   trace.file = A_surfxml_trace_file;
699   trace.periodicity = surf_parse_get_double(A_surfxml_trace_periodicity);
700   trace.pc_data = surfxml_pcdata;
701
702   sg_platf_new_trace(&trace);
703 }
704
705 void STag_surfxml_trace___connect()
706 {
707   simgrid::kernel::routing::TraceConnectCreationArgs trace_connect;
708
709   trace_connect.element = A_surfxml_trace___connect_element;
710   trace_connect.trace = A_surfxml_trace___connect_trace;
711
712   switch (A_surfxml_trace___connect_kind) {
713   case AU_surfxml_trace___connect_kind:
714   case A_surfxml_trace___connect_kind_SPEED:
715     trace_connect.kind = simgrid::kernel::routing::TraceConnectKind::SPEED;
716     break;
717   case A_surfxml_trace___connect_kind_BANDWIDTH:
718     trace_connect.kind = simgrid::kernel::routing::TraceConnectKind::BANDWIDTH;
719     break;
720   case A_surfxml_trace___connect_kind_HOST___AVAIL:
721     trace_connect.kind = simgrid::kernel::routing::TraceConnectKind::HOST_AVAIL;
722     break;
723   case A_surfxml_trace___connect_kind_LATENCY:
724     trace_connect.kind = simgrid::kernel::routing::TraceConnectKind::LATENCY;
725     break;
726   case A_surfxml_trace___connect_kind_LINK___AVAIL:
727     trace_connect.kind = simgrid::kernel::routing::TraceConnectKind::LINK_AVAIL;
728     break;
729   default:
730     surf_parse_error("Invalid trace kind");
731   }
732   sg_platf_trace_connect(&trace_connect);
733 }
734
735 void STag_surfxml_AS()
736 {
737   AX_surfxml_zone_id = AX_surfxml_AS_id;
738   AX_surfxml_zone_routing = AX_surfxml_AS_routing;
739   STag_surfxml_zone();
740 }
741
742 void ETag_surfxml_AS()
743 {
744   ETag_surfxml_zone();
745 }
746
747 void STag_surfxml_zone()
748 {
749   property_sets.push_back(new std::unordered_map<std::string, std::string>());
750   simgrid::kernel::routing::ZoneCreationArgs zone;
751   zone.id      = A_surfxml_zone_id;
752   zone.routing = A_surfxml_zone_routing;
753   sg_platf_new_Zone_begin(&zone);
754 }
755
756 void ETag_surfxml_zone()
757 {
758   sg_platf_new_Zone_set_properties(property_sets.back());
759   delete property_sets.back();
760   property_sets.pop_back();
761
762   sg_platf_new_Zone_seal();
763 }
764
765 void STag_surfxml_config()
766 {
767   property_sets.push_back(new std::unordered_map<std::string, std::string>());
768   XBT_DEBUG("START configuration name = %s",A_surfxml_config_id);
769   if (_sg_cfg_init_status == 2) {
770     surf_parse_error("All <config> tags must be given before any platform elements (such as <zone>, <host>, <cluster>, "
771                      "<link>, etc).");
772   }
773 }
774
775 void ETag_surfxml_config()
776 {
777   // Sort config elements before applying.
778   // That's a little waste of time, but not doing so would break the tests
779   auto current_property_set = property_sets.back();
780
781   std::vector<std::string> keys;
782   for (auto const& kv : *current_property_set) {
783     keys.push_back(kv.first);
784   }
785   std::sort(keys.begin(), keys.end());
786   for (std::string key : keys) {
787     if (simgrid::config::is_default(key.c_str())) {
788       std::string cfg = key + ":" + current_property_set->at(key);
789       simgrid::config::set_parse(cfg);
790     } else
791       XBT_INFO("The custom configuration '%s' is already defined by user!", key.c_str());
792   }
793   XBT_DEBUG("End configuration name = %s",A_surfxml_config_id);
794
795   delete current_property_set;
796   property_sets.pop_back();
797 }
798
799 static std::vector<std::string> arguments;
800
801 void STag_surfxml_process()
802 {
803   AX_surfxml_actor_function = AX_surfxml_process_function;
804   STag_surfxml_actor();
805 }
806
807 void STag_surfxml_actor()
808 {
809   property_sets.push_back(new std::unordered_map<std::string, std::string>());
810   arguments.assign(1, A_surfxml_actor_function);
811 }
812
813 void ETag_surfxml_process()
814 {
815   AX_surfxml_actor_host = AX_surfxml_process_host;
816   AX_surfxml_actor_function = AX_surfxml_process_function;
817   AX_surfxml_actor_start___time = AX_surfxml_process_start___time;
818   AX_surfxml_actor_kill___time = AX_surfxml_process_kill___time;
819   AX_surfxml_actor_on___failure = (AT_surfxml_actor_on___failure)AX_surfxml_process_on___failure;
820   ETag_surfxml_actor();
821 }
822
823 void ETag_surfxml_actor()
824 {
825   simgrid::kernel::routing::ActorCreationArgs actor;
826
827   actor.properties = property_sets.back();
828   property_sets.pop_back();
829
830   actor.args.swap(arguments);
831   actor.host       = A_surfxml_actor_host;
832   actor.function   = A_surfxml_actor_function;
833   actor.start_time = surf_parse_get_double(A_surfxml_actor_start___time);
834   actor.kill_time  = surf_parse_get_double(A_surfxml_actor_kill___time);
835
836   switch (A_surfxml_actor_on___failure) {
837   case AU_surfxml_actor_on___failure:
838   case A_surfxml_actor_on___failure_DIE:
839     actor.restart_on_failure = false;
840     break;
841   case A_surfxml_actor_on___failure_RESTART:
842     actor.restart_on_failure = true;
843     break;
844   default:
845     surf_parse_error("Invalid on failure behavior");
846   }
847
848   sg_platf_new_actor(&actor);
849 }
850
851 void STag_surfxml_argument(){
852   arguments.emplace_back(A_surfxml_argument_value);
853 }
854
855 void STag_surfxml_model___prop(){
856   if (not current_model_property_set)
857     current_model_property_set = new std::unordered_map<std::string, std::string>();
858
859   current_model_property_set->insert({A_surfxml_model___prop_id, A_surfxml_model___prop_value});
860 }
861
862 void ETag_surfxml_prop(){/* Nothing to do */}
863 void STag_surfxml_random(){/* Nothing to do */}
864 void ETag_surfxml_random(){/* Nothing to do */}
865 void ETag_surfxml_trace___connect(){/* Nothing to do */}
866 void STag_surfxml_trace()
867 { /* Nothing to do */
868 }
869 void ETag_surfxml_router(){/*Nothing to do*/}
870 void ETag_surfxml_host___link(){/* Nothing to do */}
871 void ETag_surfxml_cabinet(){/* Nothing to do */}
872 void ETag_surfxml_peer(){/* Nothing to do */}
873 void STag_surfxml_backbone(){/* Nothing to do */}
874 void ETag_surfxml_link___ctn(){/* Nothing to do */}
875 void ETag_surfxml_argument(){/* Nothing to do */}
876 void ETag_surfxml_model___prop(){/* Nothing to do */}
877
878 /* Open and Close parse file */
879 YY_BUFFER_STATE surf_input_buffer;
880
881 void surf_parse_open(const std::string& file)
882 {
883   surf_parsed_filename = file;
884   std::string dir      = simgrid::xbt::Path(file).get_dir_name();
885   surf_path.push_back(dir);
886
887   surf_file_to_parse = surf_fopen(file, "r");
888   if (surf_file_to_parse == nullptr)
889     throw std::invalid_argument(std::string("Unable to open '") + file + "' from '" + simgrid::xbt::Path().get_name() +
890                                 "'. Does this file exist?");
891   surf_input_buffer = surf_parse__create_buffer(surf_file_to_parse, YY_BUF_SIZE);
892   surf_parse__switch_to_buffer(surf_input_buffer);
893   surf_parse_lineno = 1;
894 }
895
896 void surf_parse_close()
897 {
898   surf_path.pop_back(); // remove the dirname of the opened file, that was added in surf_parse_open()
899
900   if (surf_file_to_parse) {
901     surf_parse__delete_buffer(surf_input_buffer);
902     fclose(surf_file_to_parse);
903     surf_file_to_parse = nullptr; //Must be reset for Bypass
904   }
905 }
906
907 /* Call the lexer to parse the currently opened file */
908 void surf_parse()
909 {
910   bool err = surf_parse_lex();
911   surf_parse_assert(not err, "Flex returned an error code");
912 }