blob: ac583e48fa37a7ba0cb336a20af32a4f7e1111ee [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/*
Junxiao Shib8752932024-01-07 15:18:46 +00003 * Copyright (c) 2014-2024, 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,
Junxiao Shib8752932024-01-07 15:18:46 +000028 const ndn::time::system_clock::time_point& timepoint, AdjacencyList& adl)
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080029 : Lsa(originRouter, seqNo, timepoint)
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080030{
31 for (const auto& adjacent : adl.getAdjList()) {
32 if (adjacent.getStatus() == Adjacent::STATUS_ACTIVE) {
33 addAdjacent(adjacent);
34 }
35 }
36}
37
38AdjLsa::AdjLsa(const ndn::Block& block)
39{
40 wireDecode(block);
41}
42
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080043template<ndn::encoding::Tag TAG>
44size_t
45AdjLsa::wireEncode(ndn::EncodingImpl<TAG>& block) const
46{
47 size_t totalLength = 0;
48
49 auto list = m_adl.getAdjList();
50 for (auto it = list.rbegin(); it != list.rend(); ++it) {
51 totalLength += it->wireEncode(block);
52 }
53
54 totalLength += Lsa::wireEncode(block);
55
56 totalLength += block.prependVarNumber(totalLength);
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -040057 totalLength += block.prependVarNumber(nlsr::tlv::AdjacencyLsa);
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080058
59 return totalLength;
60}
61
62NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(AdjLsa);
63
64const ndn::Block&
65AdjLsa::wireEncode() const
66{
Ashlesh Gawande57a87172020-05-09 19:47:06 -070067 if (m_wire.hasWire()) {
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080068 return m_wire;
69 }
70
71 ndn::EncodingEstimator estimator;
72 size_t estimatedSize = wireEncode(estimator);
73
74 ndn::EncodingBuffer buffer(estimatedSize, 0);
75 wireEncode(buffer);
76
77 m_wire = buffer.block();
78
79 return m_wire;
80}
81
82void
83AdjLsa::wireDecode(const ndn::Block& wire)
84{
85 m_wire = wire;
86
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -040087 if (m_wire.type() != nlsr::tlv::AdjacencyLsa) {
Davide Pesaventod90338d2021-01-07 17:50:05 -050088 NDN_THROW(Error("AdjacencyLsa", m_wire.type()));
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080089 }
90
91 m_wire.parse();
92
Ashlesh Gawande57a87172020-05-09 19:47:06 -070093 auto val = m_wire.elements_begin();
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080094
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -040095 if (val != m_wire.elements_end() && val->type() == nlsr::tlv::Lsa) {
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080096 Lsa::wireDecode(*val);
97 ++val;
98 }
99 else {
Davide Pesaventod90338d2021-01-07 17:50:05 -0500100 NDN_THROW(Error("Missing required Lsa field"));
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800101 }
102
103 AdjacencyList adl;
104 for (; val != m_wire.elements_end(); ++val) {
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -0400105 if (val->type() == nlsr::tlv::Adjacency) {
Davide Pesaventod90338d2021-01-07 17:50:05 -0500106 adl.insert(Adjacent(*val));
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800107 }
108 else {
Davide Pesaventod90338d2021-01-07 17:50:05 -0500109 NDN_THROW(Error("Adjacency", val->type()));
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800110 }
111 }
112 m_adl = adl;
113}
114
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700115std::string
116AdjLsa::toString() const
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800117{
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700118 std::ostringstream os;
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700119 os << getString();
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700120 os << " Adjacent(s):\n";
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800121
122 int adjacencyIndex = 0;
123
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700124 for (const auto& adjacency : m_adl) {
125 os << " Adjacent " << adjacencyIndex++
126 << ": (name=" << adjacency.getName()
127 << ", uri=" << adjacency.getFaceUri()
128 << ", cost=" << adjacency.getLinkCost() << ")\n";
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800129 }
130
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700131 return os.str();
132}
133
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700134std::tuple<bool, std::list<ndn::Name>, std::list<ndn::Name>>
135AdjLsa::update(const std::shared_ptr<Lsa>& lsa)
136{
137 auto alsa = std::static_pointer_cast<AdjLsa>(lsa);
Junxiao Shib8752932024-01-07 15:18:46 +0000138 if (*this != *alsa) {
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700139 resetAdl();
140 for (const auto& adjacent : alsa->getAdl()) {
141 addAdjacent(adjacent);
142 }
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -0400143 return {true, std::list<ndn::Name>{}, std::list<ndn::Name>{}};
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700144 }
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -0400145 return {false, std::list<ndn::Name>{}, std::list<ndn::Name>{}};
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700146}
147
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700148std::ostream&
149operator<<(std::ostream& os, const AdjLsa& lsa)
150{
151 return os << lsa.toString();
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800152}
153
Ashlesh Gawande0421bc62020-05-08 20:42:19 -0700154} // namespace nlsr