blob: e6d2f6eb38d0c2607b83baeb690ebe99658b342c [file] [log] [blame]
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Ashlesh Gawande0421bc62020-05-08 20:42:19 -07002/*
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -04003 * Copyright (c) 2014-2022, The University of Memphis,
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -08004 * Regents of the University of California,
5 * Arizona Board of Regents.
6 *
7 * This file is part of NLSR (Named-data Link State Routing).
8 * See AUTHORS.md for complete list of NLSR authors and contributors.
9 *
10 * NLSR is free software: you can redistribute it and/or modify it under the terms
11 * of the GNU General Public License as published by the Free Software Foundation,
12 * either version 3 of the License, or (at your option) any later version.
13 *
14 * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16 * PURPOSE. See the GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along with
19 * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070020 */
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080021
22#include "adj-lsa.hpp"
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070023#include "tlv-nlsr.hpp"
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080024
25namespace nlsr {
26
Ashlesh Gawande57a87172020-05-09 19:47:06 -070027AdjLsa::AdjLsa(const ndn::Name& originRouter, uint64_t seqNo,
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080028 const ndn::time::system_clock::TimePoint& timepoint,
29 uint32_t noLink, AdjacencyList& adl)
30 : Lsa(originRouter, seqNo, timepoint)
31 , m_noLink(noLink)
32{
33 for (const auto& adjacent : adl.getAdjList()) {
34 if (adjacent.getStatus() == Adjacent::STATUS_ACTIVE) {
35 addAdjacent(adjacent);
36 }
37 }
38}
39
40AdjLsa::AdjLsa(const ndn::Block& block)
41{
42 wireDecode(block);
43}
44
45bool
46AdjLsa::isEqualContent(const AdjLsa& alsa) const
47{
48 return m_adl == alsa.getAdl();
49}
50
51template<ndn::encoding::Tag TAG>
52size_t
53AdjLsa::wireEncode(ndn::EncodingImpl<TAG>& block) const
54{
55 size_t totalLength = 0;
56
57 auto list = m_adl.getAdjList();
58 for (auto it = list.rbegin(); it != list.rend(); ++it) {
59 totalLength += it->wireEncode(block);
60 }
61
62 totalLength += Lsa::wireEncode(block);
63
64 totalLength += block.prependVarNumber(totalLength);
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -040065 totalLength += block.prependVarNumber(nlsr::tlv::AdjacencyLsa);
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080066
67 return totalLength;
68}
69
70NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(AdjLsa);
71
72const ndn::Block&
73AdjLsa::wireEncode() const
74{
Ashlesh Gawande57a87172020-05-09 19:47:06 -070075 if (m_wire.hasWire()) {
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080076 return m_wire;
77 }
78
79 ndn::EncodingEstimator estimator;
80 size_t estimatedSize = wireEncode(estimator);
81
82 ndn::EncodingBuffer buffer(estimatedSize, 0);
83 wireEncode(buffer);
84
85 m_wire = buffer.block();
86
87 return m_wire;
88}
89
90void
91AdjLsa::wireDecode(const ndn::Block& wire)
92{
93 m_wire = wire;
94
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -040095 if (m_wire.type() != nlsr::tlv::AdjacencyLsa) {
Davide Pesaventod90338d2021-01-07 17:50:05 -050096 NDN_THROW(Error("AdjacencyLsa", m_wire.type()));
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080097 }
98
99 m_wire.parse();
100
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700101 auto val = m_wire.elements_begin();
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800102
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -0400103 if (val != m_wire.elements_end() && val->type() == nlsr::tlv::Lsa) {
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800104 Lsa::wireDecode(*val);
105 ++val;
106 }
107 else {
Davide Pesaventod90338d2021-01-07 17:50:05 -0500108 NDN_THROW(Error("Missing required Lsa field"));
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800109 }
110
111 AdjacencyList adl;
112 for (; val != m_wire.elements_end(); ++val) {
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -0400113 if (val->type() == nlsr::tlv::Adjacency) {
Davide Pesaventod90338d2021-01-07 17:50:05 -0500114 adl.insert(Adjacent(*val));
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800115 }
116 else {
Davide Pesaventod90338d2021-01-07 17:50:05 -0500117 NDN_THROW(Error("Adjacency", val->type()));
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800118 }
119 }
120 m_adl = adl;
121}
122
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700123std::string
124AdjLsa::toString() const
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800125{
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700126 std::ostringstream os;
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700127 os << getString();
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700128 os << " Adjacent(s):\n";
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800129
130 int adjacencyIndex = 0;
131
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700132 for (const auto& adjacency : m_adl) {
133 os << " Adjacent " << adjacencyIndex++
134 << ": (name=" << adjacency.getName()
135 << ", uri=" << adjacency.getFaceUri()
136 << ", cost=" << adjacency.getLinkCost() << ")\n";
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800137 }
138
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700139 return os.str();
140}
141
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700142std::tuple<bool, std::list<ndn::Name>, std::list<ndn::Name>>
143AdjLsa::update(const std::shared_ptr<Lsa>& lsa)
144{
145 auto alsa = std::static_pointer_cast<AdjLsa>(lsa);
146 if (!isEqualContent(*alsa)) {
147 resetAdl();
148 for (const auto& adjacent : alsa->getAdl()) {
149 addAdjacent(adjacent);
150 }
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -0400151 return {true, std::list<ndn::Name>{}, std::list<ndn::Name>{}};
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700152 }
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -0400153 return {false, std::list<ndn::Name>{}, std::list<ndn::Name>{}};
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700154}
155
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700156std::ostream&
157operator<<(std::ostream& os, const AdjLsa& lsa)
158{
159 return os << lsa.toString();
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800160}
161
Ashlesh Gawande0421bc62020-05-08 20:42:19 -0700162} // namespace nlsr