Yuanzhi Gao | dd516fe | 2015-04-23 04:18:24 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2011-2015 Regents of the University of California. |
| 4 | * |
| 5 | * This file is part of ndnSIM. See AUTHORS for complete list of ndnSIM authors and |
| 6 | * contributors. |
| 7 | * |
| 8 | * ndnSIM is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, |
| 10 | * either version 3 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * ndnSIM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * ndnSIM, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | **/ |
| 19 | |
| 20 | #include "helper/ndn-global-routing-helper.hpp" |
| 21 | |
| 22 | #include "model/ndn-global-router.hpp" |
| 23 | #include "model/ndn-l3-protocol.hpp" |
Spyridon Mastorakis | 5ea3322 | 2016-12-07 14:33:53 -0800 | [diff] [blame^] | 24 | #include "model/ndn-net-device-transport.hpp" |
Yuanzhi Gao | dd516fe | 2015-04-23 04:18:24 -0700 | [diff] [blame] | 25 | |
| 26 | #include "ns3/channel.h" |
| 27 | #include "ns3/net-device.h" |
| 28 | #include "ns3/core-module.h" |
| 29 | #include "ns3/network-module.h" |
| 30 | #include "ns3/ndnSIM-module.h" |
| 31 | #include "ns3/point-to-point-net-device.h" |
| 32 | #include "ns3/point-to-point-module.h" |
| 33 | #include "ns3/point-to-point-layout-module.h" |
| 34 | |
| 35 | #include "../tests-common.hpp" |
| 36 | |
Alexander Afanasyev | 929b17e | 2015-08-13 16:54:09 -0700 | [diff] [blame] | 37 | #include <boost/filesystem.hpp> |
| 38 | |
Yuanzhi Gao | dd516fe | 2015-04-23 04:18:24 -0700 | [diff] [blame] | 39 | namespace ns3 { |
| 40 | namespace ndn { |
| 41 | |
Alexander Afanasyev | 929b17e | 2015-08-13 16:54:09 -0700 | [diff] [blame] | 42 | const boost::filesystem::path TEST_TOPO_TXT = boost::filesystem::path(TEST_CONFIG_PATH) / "topo.txt"; |
| 43 | |
| 44 | class GlobalRoutingHelperFixture : public CleanupFixture |
| 45 | { |
| 46 | public: |
| 47 | GlobalRoutingHelperFixture() |
| 48 | { |
| 49 | boost::filesystem::create_directories(TEST_CONFIG_PATH); |
| 50 | } |
| 51 | |
| 52 | ~GlobalRoutingHelperFixture() |
| 53 | { |
| 54 | boost::filesystem::remove(TEST_TOPO_TXT); |
| 55 | } |
| 56 | }; |
| 57 | |
| 58 | BOOST_FIXTURE_TEST_SUITE(HelperGlobalRoutingHelper, GlobalRoutingHelperFixture) |
Yuanzhi Gao | dd516fe | 2015-04-23 04:18:24 -0700 | [diff] [blame] | 59 | |
| 60 | BOOST_AUTO_TEST_CASE(CalculateRouteCase1) |
| 61 | { |
Alexander Afanasyev | 929b17e | 2015-08-13 16:54:09 -0700 | [diff] [blame] | 62 | ofstream file1(TEST_TOPO_TXT.string().c_str()); |
Yuanzhi Gao | dd516fe | 2015-04-23 04:18:24 -0700 | [diff] [blame] | 63 | file1 << "router\n\n" |
| 64 | << "#node city y x mpi-partition\n" |
| 65 | << "A1 NA 1 1 1\n" |
| 66 | << "B1 NA 80 -40 1\n" |
| 67 | << "C1 NA 80 40 1\n\n" |
| 68 | << "link\n\n" |
| 69 | << "# from to capacity metric delay queue\n" |
| 70 | << "A1 B1 10Mbps 100 1ms 100\n" |
| 71 | << "A1 C1 10Mbps 50 1ms 100\n" |
| 72 | << "B1 C1 10Mbps 1 1ms 100\n"; |
| 73 | file1.close(); |
| 74 | |
| 75 | AnnotatedTopologyReader topologyReader(""); |
Alexander Afanasyev | 929b17e | 2015-08-13 16:54:09 -0700 | [diff] [blame] | 76 | topologyReader.SetFileName(TEST_TOPO_TXT.string().c_str()); |
Yuanzhi Gao | dd516fe | 2015-04-23 04:18:24 -0700 | [diff] [blame] | 77 | topologyReader.Read(); |
| 78 | |
| 79 | // Install NDN stack on all nodes |
| 80 | ndn::StackHelper ndnHelper; |
| 81 | ndnHelper.InstallAll(); |
| 82 | |
| 83 | topologyReader.ApplyOspfMetric(); |
| 84 | |
| 85 | ndn::GlobalRoutingHelper ndnGlobalRoutingHelper; |
| 86 | BOOST_CHECK_NO_THROW(ndnGlobalRoutingHelper.InstallAll()); |
| 87 | |
| 88 | ndnGlobalRoutingHelper.AddOrigins("/test/prefix", Names::Find<Node>("C1")); |
| 89 | BOOST_CHECK_NO_THROW(ndn::GlobalRoutingHelper::CalculateRoutes()); |
| 90 | |
| 91 | auto ndn = Names::Find<Node>("A1")->GetObject<ndn::L3Protocol>(); |
| 92 | for (const auto& entry : ndn->getForwarder()->getFib()) { |
| 93 | bool isFirst = true; |
| 94 | for (auto& nextHop : entry.getNextHops()) { |
Spyridon Mastorakis | b0b2241 | 2016-12-07 14:33:46 -0800 | [diff] [blame] | 95 | auto& face = nextHop.getFace(); |
Spyridon Mastorakis | 5ea3322 | 2016-12-07 14:33:53 -0800 | [diff] [blame^] | 96 | auto transport = dynamic_cast<NetDeviceTransport*>(face.getTransport()); |
| 97 | if (transport == nullptr) |
Yuanzhi Gao | dd516fe | 2015-04-23 04:18:24 -0700 | [diff] [blame] | 98 | continue; |
Spyridon Mastorakis | 5ea3322 | 2016-12-07 14:33:53 -0800 | [diff] [blame^] | 99 | BOOST_CHECK_EQUAL(Names::FindName(transport->GetNetDevice()->GetChannel()->GetDevice(1)->GetNode()), "C1"); |
Yuanzhi Gao | dd516fe | 2015-04-23 04:18:24 -0700 | [diff] [blame] | 100 | isFirst = false; |
| 101 | } |
| 102 | } |
Yuanzhi Gao | dd516fe | 2015-04-23 04:18:24 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | BOOST_AUTO_TEST_CASE(CalculateRouteCase2) |
| 106 | { |
Alexander Afanasyev | 929b17e | 2015-08-13 16:54:09 -0700 | [diff] [blame] | 107 | ofstream file1(TEST_TOPO_TXT.string().c_str()); |
Yuanzhi Gao | dd516fe | 2015-04-23 04:18:24 -0700 | [diff] [blame] | 108 | file1 << "router\n\n" |
| 109 | << "#node city y x mpi-partition\n" |
| 110 | << "A2 NA 1 1 1\n" |
| 111 | << "B2 NA 80 -40 1\n" |
| 112 | << "C2 NA 80 40 1\n\n" |
| 113 | << "link\n\n" |
| 114 | << "# from to capacity metric delay queue\n" |
| 115 | << "A2 B2 10Mbps 100 1ms 100\n" |
| 116 | << "A2 C2 10Mbps 500 1ms 100\n" |
| 117 | << "B2 C2 10Mbps 1 1ms 100\n"; |
| 118 | file1.close(); |
| 119 | |
| 120 | AnnotatedTopologyReader topologyReader(""); |
Alexander Afanasyev | 929b17e | 2015-08-13 16:54:09 -0700 | [diff] [blame] | 121 | topologyReader.SetFileName(TEST_TOPO_TXT.string().c_str()); |
Yuanzhi Gao | dd516fe | 2015-04-23 04:18:24 -0700 | [diff] [blame] | 122 | topologyReader.Read(); |
| 123 | |
| 124 | // Install NDN stack on all nodes |
| 125 | ndn::StackHelper ndnHelper; |
| 126 | ndnHelper.InstallAll(); |
| 127 | |
| 128 | topologyReader.ApplyOspfMetric(); |
| 129 | |
| 130 | ndn::GlobalRoutingHelper ndnGlobalRoutingHelper; |
| 131 | ndnGlobalRoutingHelper.InstallAll(); |
| 132 | |
| 133 | ndnGlobalRoutingHelper.AddOrigins("/prefix", Names::Find<Node>("C2")); |
| 134 | ndn::GlobalRoutingHelper::CalculateRoutes(); |
| 135 | |
| 136 | auto ndn = Names::Find<Node>("A2")->GetObject<ndn::L3Protocol>(); |
| 137 | for (const auto& entry : ndn->getForwarder()->getFib()) { |
| 138 | bool isFirst = true; |
| 139 | for (auto& nextHop : entry.getNextHops()) { |
Spyridon Mastorakis | b0b2241 | 2016-12-07 14:33:46 -0800 | [diff] [blame] | 140 | auto& face = nextHop.getFace(); |
Spyridon Mastorakis | 5ea3322 | 2016-12-07 14:33:53 -0800 | [diff] [blame^] | 141 | auto transport = dynamic_cast<NetDeviceTransport*>(face.getTransport()); |
| 142 | if (transport == nullptr) |
Yuanzhi Gao | dd516fe | 2015-04-23 04:18:24 -0700 | [diff] [blame] | 143 | continue; |
Spyridon Mastorakis | 5ea3322 | 2016-12-07 14:33:53 -0800 | [diff] [blame^] | 144 | BOOST_CHECK_EQUAL(Names::FindName(transport->GetNetDevice()->GetChannel()->GetDevice(1)->GetNode()), "B2"); |
Yuanzhi Gao | dd516fe | 2015-04-23 04:18:24 -0700 | [diff] [blame] | 145 | isFirst = false; |
| 146 | } |
| 147 | } |
Yuanzhi Gao | dd516fe | 2015-04-23 04:18:24 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | BOOST_AUTO_TEST_SUITE_END() |
| 151 | |
| 152 | } // namespace ndn |
| 153 | } // namespace ns3 |