blob: 781422ce87b3428d568f07467bb7ba2e3b048cff [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 Pesaventod90338d2021-01-07 17:50:05 -05003 * Copyright (c) 2014-2021, 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);
65 totalLength += block.prependVarNumber(ndn::tlv::nlsr::AdjacencyLsa);
66
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
95 if (m_wire.type() != ndn::tlv::nlsr::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
103 if (val != m_wire.elements_end() && val->type() == ndn::tlv::nlsr::Lsa) {
104 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) {
113 if (val->type() == ndn::tlv::nlsr::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;
127 os << Lsa::toString();
128 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
142std::ostream&
143operator<<(std::ostream& os, const AdjLsa& lsa)
144{
145 return os << lsa.toString();
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800146}
147
Ashlesh Gawande0421bc62020-05-08 20:42:19 -0700148} // namespace nlsr