blob: af74386db2c3f89e386b6e98b1f1c9a328085a84 [file] [log] [blame]
Alexander Afanasyevabc0d912015-08-13 16:49:02 -07001/* -*- 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 "ndn-scenario-helper.hpp"
21#include "ndn-fib-helper.hpp"
22#include "ndn-app-helper.hpp"
23
24#include "ns3/ndnSIM/model/ndn-l3-protocol.hpp"
25
26#include "ns3/names.h"
27#include "ns3/point-to-point-helper.h"
28#include "ns3/string.h"
29
30namespace ns3 {
31namespace ndn {
32
33ScenarioHelper::ScenarioHelper()
34 : m_isTopologyInitialized(false)
35{
36}
37
38void
Alexander Afanasyeva9d889b2016-09-08 18:34:25 -070039ScenarioHelper::createTopology(std::initializer_list<std::initializer_list<std::string>/*node clique*/> topology,
40 bool shouldInstallNdnStack)
Alexander Afanasyevabc0d912015-08-13 16:49:02 -070041{
42 if (m_isTopologyInitialized) {
43 throw std::logic_error("Topology cannot be created twice");
44 }
45
46 PointToPointHelper p2p;
47
48 for (auto&& clique : topology) {
49 for (auto i = clique.begin(); i != clique.end(); ++i) {
50 auto node1 = getOrCreateNode(*i);
51 for (auto j = i + 1; j != clique.end(); ++j) {
52 auto node2 = getOrCreateNode(*j);
53
54 auto link = p2p.Install(node1, node2);
55 links[*i][*j] = link.Get(0);
56 links[*j][*i] = link.Get(1);
57 }
58 }
59 }
60
Alexander Afanasyeva9d889b2016-09-08 18:34:25 -070061 if (shouldInstallNdnStack) {
62 ndnHelper.InstallAll();
63 }
Alexander Afanasyevabc0d912015-08-13 16:49:02 -070064 m_isTopologyInitialized = true;
65}
66
67void
spirosmastorakis0df15ba2015-11-14 08:46:24 -080068ScenarioHelper::disableRibManager()
69{
70 ndnHelper.disableRibManager();
71}
72
Alexander Afanasyevca3c67e2016-09-08 15:48:23 -070073// void
74// ScenarioHelper::disableFaceManager()
75// {
76// ndnHelper.disableFaceManager();
77// }
spirosmastorakis0df15ba2015-11-14 08:46:24 -080078
79void
80ScenarioHelper::disableStrategyChoiceManager()
81{
82 ndnHelper.disableStrategyChoiceManager();
83}
84
85void
Alexander Afanasyevca3c67e2016-09-08 15:48:23 -070086ScenarioHelper::disableForwarderStatusManager()
spirosmastorakis0df15ba2015-11-14 08:46:24 -080087{
Alexander Afanasyevca3c67e2016-09-08 15:48:23 -070088 ndnHelper.disableForwarderStatusManager();
spirosmastorakis0df15ba2015-11-14 08:46:24 -080089}
90
91void
Alexander Afanasyevabc0d912015-08-13 16:49:02 -070092ScenarioHelper::addRoutes(std::initializer_list<ScenarioHelper::RouteInfo> routes)
93{
94 for (auto&& route : routes) {
95 FibHelper::AddRoute(getNode(route.node1), route.prefix,
96 getFace(route.node1, route.node2), route.metric);
97 }
98}
99
100void
101ScenarioHelper::addApps(std::initializer_list<ScenarioHelper::AppInfo> apps)
102{
103 for (auto&& app : apps) {
104 AppHelper appHelper(app.name);
105 for (auto&& param : app.params) {
106 appHelper.SetAttribute(param.first, StringValue(param.second));
107 }
108 auto installedApp = appHelper.Install(getNode(app.node));
109 installedApp.Start(Time(app.start));
110 installedApp.Stop(Time(app.end));
111 }
112}
113
114Ptr<Node>
115ScenarioHelper::getOrCreateNode(const std::string& nodeName)
116{
117 auto node = nodes.find(nodeName);
118 if (node == nodes.end()) {
119 std::tie(node, std::ignore) = nodes.insert(std::make_pair(nodeName, CreateObject<Node>()));
120 Names::Add(nodeName, node->second);
121 }
122 return node->second;
123}
124
125Ptr<Node>
126ScenarioHelper::getNode(const std::string& nodeName)
127{
128 auto node = nodes.find(nodeName);
129 if (node != nodes.end()) {
130 return node->second;
131 }
132
133 throw std::invalid_argument("Node " + nodeName + " does not exist");
134}
135
136shared_ptr<Face>
137ScenarioHelper::getFace(const std::string& node1, const std::string& node2)
138{
Alexander Afanasyeva9d889b2016-09-08 18:34:25 -0700139 Ptr<NetDevice> netDevice = getNetDevice(node1, node2);
140 return netDevice->GetNode()->GetObject<L3Protocol>()->getFaceByNetDevice(netDevice);
141}
142
143Ptr<NetDevice>
144ScenarioHelper::getNetDevice(const std::string& node1, const std::string& node2)
145{
Alexander Afanasyevabc0d912015-08-13 16:49:02 -0700146 auto i = links.find(node1);
147 if (i != links.end()) {
148 auto j = i->second.find(node2);
149 if (j != i->second.end()) {
Alexander Afanasyeva9d889b2016-09-08 18:34:25 -0700150 return j->second;
Alexander Afanasyevabc0d912015-08-13 16:49:02 -0700151 }
152 }
153
154 throw std::invalid_argument("Link between " + node1 + " and " + node2 + " does not exist");
155}
156
Alexander Afanasyevb4102ce2016-06-26 21:37:40 -0700157StackHelper&
158ScenarioHelper::getStackHelper()
159{
160 return ndnHelper;
161}
162
Alexander Afanasyevabc0d912015-08-13 16:49:02 -0700163} // namespace ndn
164} // namespace ns3