blob: 92f40b81ea646803cd0cc630710e5a44e8c7a993 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shibc19b372014-03-23 16:59:25 -07002/**
Davide Pesaventoaa82eb62016-04-22 19:08:40 +02003 * Copyright (c) 2013-2016 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
Junxiao Shibc19b372014-03-23 16:59:25 -070020 */
21
22#ifndef NDN_MANAGEMENT_NFD_CONTROL_PARAMETERS_HPP
23#define NDN_MANAGEMENT_NFD_CONTROL_PARAMETERS_HPP
24
Junxiao Shi65f1a712014-11-20 14:59:36 -070025#include "../encoding/nfd-constants.hpp"
Junxiao Shibc19b372014-03-23 16:59:25 -070026#include "../name.hpp"
Alexander Afanasyev15f67312014-07-22 15:11:09 -070027#include "../util/time.hpp"
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070028#include "../mgmt/control-parameters.hpp"
Junxiao Shibc19b372014-03-23 16:59:25 -070029
30namespace ndn {
31namespace nfd {
32
Alexander Afanasyev4671bf72014-05-19 09:01:37 -040033/**
34 * \ingroup management
35 */
Junxiao Shi5ec80222014-03-25 20:08:05 -070036enum ControlParameterField {
37 CONTROL_PARAMETER_NAME,
38 CONTROL_PARAMETER_FACE_ID,
39 CONTROL_PARAMETER_URI,
40 CONTROL_PARAMETER_LOCAL_CONTROL_FEATURE,
Junxiao Shi5f6c74f2014-04-18 16:29:44 -070041 CONTROL_PARAMETER_ORIGIN,
Junxiao Shi5ec80222014-03-25 20:08:05 -070042 CONTROL_PARAMETER_COST,
Junxiao Shi5f6c74f2014-04-18 16:29:44 -070043 CONTROL_PARAMETER_FLAGS,
Eric Newberryda916d62016-08-11 23:04:34 -070044 CONTROL_PARAMETER_MASK,
Junxiao Shi5ec80222014-03-25 20:08:05 -070045 CONTROL_PARAMETER_STRATEGY,
Junxiao Shi5f6c74f2014-04-18 16:29:44 -070046 CONTROL_PARAMETER_EXPIRATION_PERIOD,
Yukai Tud93c5fc2015-08-25 11:37:16 +080047 CONTROL_PARAMETER_FACE_PERSISTENCY,
Junxiao Shi5ec80222014-03-25 20:08:05 -070048 CONTROL_PARAMETER_UBOUND
49};
50
51const std::string CONTROL_PARAMETER_FIELD[CONTROL_PARAMETER_UBOUND] = {
52 "Name",
53 "FaceId",
54 "Uri",
55 "LocalControlFeature",
Junxiao Shi5f6c74f2014-04-18 16:29:44 -070056 "Origin",
Junxiao Shi5ec80222014-03-25 20:08:05 -070057 "Cost",
Junxiao Shi5f6c74f2014-04-18 16:29:44 -070058 "Flags",
Eric Newberryda916d62016-08-11 23:04:34 -070059 "Mask",
Junxiao Shi5ec80222014-03-25 20:08:05 -070060 "Strategy",
Junxiao Shi5f6c74f2014-04-18 16:29:44 -070061 "ExpirationPeriod",
Yanbiao Licbdacb22016-08-02 16:02:35 +080062 "FacePersistency"
Junxiao Shi5ec80222014-03-25 20:08:05 -070063};
64
Alexander Afanasyev4671bf72014-05-19 09:01:37 -040065/**
66 * \ingroup management
Eric Newberryda916d62016-08-11 23:04:34 -070067 * \deprecated use Flags+Mask fields instead
Alexander Afanasyev4671bf72014-05-19 09:01:37 -040068 */
Junxiao Shibc19b372014-03-23 16:59:25 -070069enum LocalControlFeature {
70 LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID = 1,
71 LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID = 2
72};
73
Alexander Afanasyev4671bf72014-05-19 09:01:37 -040074/**
Eric Newberryda916d62016-08-11 23:04:34 -070075 * \ingroup management
76 * \brief represents parameters in a ControlCommand request or response
77 * \sa http://redmine.named-data.net/projects/nfd/wiki/ControlCommand#ControlParameters
78 * \details This type is copyable because it's an abstraction of a TLV type.
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070079 */
Yanbiao Li8ee37ed2015-05-19 12:44:04 -070080class ControlParameters : public ndn::mgmt::ControlParameters
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070081{
Junxiao Shibc19b372014-03-23 16:59:25 -070082public:
Steve DiBenedetto54ce6682014-07-22 13:22:57 -060083 class Error : public tlv::Error
Junxiao Shibc19b372014-03-23 16:59:25 -070084 {
85 public:
86 explicit
87 Error(const std::string& what)
Steve DiBenedetto54ce6682014-07-22 13:22:57 -060088 : tlv::Error(what)
Junxiao Shibc19b372014-03-23 16:59:25 -070089 {
90 }
91 };
92
Junxiao Shi70911652014-08-12 10:14:24 -070093 ControlParameters();
Junxiao Shibc19b372014-03-23 16:59:25 -070094
95 explicit
Junxiao Shi70911652014-08-12 10:14:24 -070096 ControlParameters(const Block& block);
Junxiao Shibc19b372014-03-23 16:59:25 -070097
Alexander Afanasyev74633892015-02-08 18:08:46 -080098 template<encoding::Tag TAG>
Junxiao Shibc19b372014-03-23 16:59:25 -070099 size_t
Alexander Afanasyev74633892015-02-08 18:08:46 -0800100 wireEncode(EncodingImpl<TAG>& encoder) const;
Junxiao Shibc19b372014-03-23 16:59:25 -0700101
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700102 virtual Block
Davide Pesaventoaa82eb62016-04-22 19:08:40 +0200103 wireEncode() const final;
Junxiao Shibc19b372014-03-23 16:59:25 -0700104
Yanbiao Li8ee37ed2015-05-19 12:44:04 -0700105 virtual void
Davide Pesaventoaa82eb62016-04-22 19:08:40 +0200106 wireDecode(const Block& wire) final;
Junxiao Shibc19b372014-03-23 16:59:25 -0700107
108public: // getters & setters
109 bool
110 hasName() const
111 {
Junxiao Shi5ec80222014-03-25 20:08:05 -0700112 return m_hasFields[CONTROL_PARAMETER_NAME];
Junxiao Shibc19b372014-03-23 16:59:25 -0700113 }
114
115 const Name&
116 getName() const
117 {
Junxiao Shi5ec80222014-03-25 20:08:05 -0700118 BOOST_ASSERT(this->hasName());
Junxiao Shibc19b372014-03-23 16:59:25 -0700119 return m_name;
120 }
121
122 ControlParameters&
123 setName(const Name& name)
124 {
125 m_wire.reset();
126 m_name = name;
Junxiao Shi5ec80222014-03-25 20:08:05 -0700127 m_hasFields[CONTROL_PARAMETER_NAME] = true;
Junxiao Shibc19b372014-03-23 16:59:25 -0700128 return *this;
129 }
130
131 ControlParameters&
132 unsetName()
133 {
134 m_wire.reset();
Junxiao Shi5ec80222014-03-25 20:08:05 -0700135 m_hasFields[CONTROL_PARAMETER_NAME] = false;
Junxiao Shibc19b372014-03-23 16:59:25 -0700136 return *this;
137 }
138
139 bool
140 hasFaceId() const
141 {
Junxiao Shi5ec80222014-03-25 20:08:05 -0700142 return m_hasFields[CONTROL_PARAMETER_FACE_ID];
Junxiao Shibc19b372014-03-23 16:59:25 -0700143 }
144
145 uint64_t
146 getFaceId() const
147 {
Junxiao Shi5ec80222014-03-25 20:08:05 -0700148 BOOST_ASSERT(this->hasFaceId());
Junxiao Shibc19b372014-03-23 16:59:25 -0700149 return m_faceId;
150 }
151
152 ControlParameters&
153 setFaceId(uint64_t faceId)
154 {
155 m_wire.reset();
156 m_faceId = faceId;
Junxiao Shi5ec80222014-03-25 20:08:05 -0700157 m_hasFields[CONTROL_PARAMETER_FACE_ID] = true;
Junxiao Shibc19b372014-03-23 16:59:25 -0700158 return *this;
159 }
160
161 ControlParameters&
162 unsetFaceId()
163 {
164 m_wire.reset();
Junxiao Shi5ec80222014-03-25 20:08:05 -0700165 m_hasFields[CONTROL_PARAMETER_FACE_ID] = false;
Junxiao Shibc19b372014-03-23 16:59:25 -0700166 return *this;
167 }
168
169 bool
170 hasUri() const
171 {
Junxiao Shi5ec80222014-03-25 20:08:05 -0700172 return m_hasFields[CONTROL_PARAMETER_URI];
Junxiao Shibc19b372014-03-23 16:59:25 -0700173 }
174
175 const std::string&
176 getUri() const
177 {
Junxiao Shi5ec80222014-03-25 20:08:05 -0700178 BOOST_ASSERT(this->hasUri());
Junxiao Shibc19b372014-03-23 16:59:25 -0700179 return m_uri;
180 }
181
182 ControlParameters&
183 setUri(const std::string& uri)
184 {
185 m_wire.reset();
186 m_uri = uri;
Junxiao Shi5ec80222014-03-25 20:08:05 -0700187 m_hasFields[CONTROL_PARAMETER_URI] = true;
Junxiao Shibc19b372014-03-23 16:59:25 -0700188 return *this;
189 }
190
191 ControlParameters&
192 unsetUri()
193 {
194 m_wire.reset();
Junxiao Shi5ec80222014-03-25 20:08:05 -0700195 m_hasFields[CONTROL_PARAMETER_URI] = false;
Junxiao Shibc19b372014-03-23 16:59:25 -0700196 return *this;
197 }
198
Eric Newberryda916d62016-08-11 23:04:34 -0700199 /**
200 * \deprecated use Flags+Mask fields instead
201 */
Junxiao Shibc19b372014-03-23 16:59:25 -0700202 bool
203 hasLocalControlFeature() const
204 {
Junxiao Shi5ec80222014-03-25 20:08:05 -0700205 return m_hasFields[CONTROL_PARAMETER_LOCAL_CONTROL_FEATURE];
Junxiao Shibc19b372014-03-23 16:59:25 -0700206 }
207
Eric Newberryda916d62016-08-11 23:04:34 -0700208 /**
209 * \deprecated use Flags+Mask fields instead
210 */
Junxiao Shibc19b372014-03-23 16:59:25 -0700211 LocalControlFeature
212 getLocalControlFeature() const
213 {
Junxiao Shi5ec80222014-03-25 20:08:05 -0700214 BOOST_ASSERT(this->hasLocalControlFeature());
Junxiao Shibc19b372014-03-23 16:59:25 -0700215 return m_localControlFeature;
216 }
217
Eric Newberryda916d62016-08-11 23:04:34 -0700218 /**
219 * \deprecated use Flags+Mask fields instead
220 */
Junxiao Shibc19b372014-03-23 16:59:25 -0700221 ControlParameters&
222 setLocalControlFeature(LocalControlFeature localControlFeature)
223 {
224 m_wire.reset();
225 m_localControlFeature = localControlFeature;
Junxiao Shi5ec80222014-03-25 20:08:05 -0700226 m_hasFields[CONTROL_PARAMETER_LOCAL_CONTROL_FEATURE] = true;
Junxiao Shibc19b372014-03-23 16:59:25 -0700227 return *this;
228 }
229
Eric Newberryda916d62016-08-11 23:04:34 -0700230 /**
231 * \deprecated use Flags+Mask fields instead
232 */
Junxiao Shibc19b372014-03-23 16:59:25 -0700233 ControlParameters&
234 unsetLocalControlFeature()
235 {
236 m_wire.reset();
Junxiao Shi5ec80222014-03-25 20:08:05 -0700237 m_hasFields[CONTROL_PARAMETER_LOCAL_CONTROL_FEATURE] = false;
Junxiao Shibc19b372014-03-23 16:59:25 -0700238 return *this;
239 }
240
241 bool
Junxiao Shi5f6c74f2014-04-18 16:29:44 -0700242 hasOrigin() const
243 {
244 return m_hasFields[CONTROL_PARAMETER_ORIGIN];
245 }
246
247 uint64_t
248 getOrigin() const
249 {
250 BOOST_ASSERT(this->hasOrigin());
251 return m_origin;
252 }
253
254 ControlParameters&
255 setOrigin(uint64_t origin)
256 {
257 m_wire.reset();
258 m_origin = origin;
259 m_hasFields[CONTROL_PARAMETER_ORIGIN] = true;
260 return *this;
261 }
262
263 ControlParameters&
264 unsetOrigin()
265 {
266 m_wire.reset();
267 m_hasFields[CONTROL_PARAMETER_ORIGIN] = false;
268 return *this;
269 }
270
271 bool
Junxiao Shibc19b372014-03-23 16:59:25 -0700272 hasCost() const
273 {
Junxiao Shi5ec80222014-03-25 20:08:05 -0700274 return m_hasFields[CONTROL_PARAMETER_COST];
Junxiao Shibc19b372014-03-23 16:59:25 -0700275 }
276
277 uint64_t
278 getCost() const
279 {
Junxiao Shi5ec80222014-03-25 20:08:05 -0700280 BOOST_ASSERT(this->hasCost());
Junxiao Shibc19b372014-03-23 16:59:25 -0700281 return m_cost;
282 }
283
284 ControlParameters&
285 setCost(uint64_t cost)
286 {
287 m_wire.reset();
288 m_cost = cost;
Junxiao Shi5ec80222014-03-25 20:08:05 -0700289 m_hasFields[CONTROL_PARAMETER_COST] = true;
Junxiao Shibc19b372014-03-23 16:59:25 -0700290 return *this;
291 }
292
293 ControlParameters&
294 unsetCost()
295 {
296 m_wire.reset();
Junxiao Shi5ec80222014-03-25 20:08:05 -0700297 m_hasFields[CONTROL_PARAMETER_COST] = false;
Junxiao Shibc19b372014-03-23 16:59:25 -0700298 return *this;
299 }
300
301 bool
Junxiao Shi5f6c74f2014-04-18 16:29:44 -0700302 hasFlags() const
303 {
304 return m_hasFields[CONTROL_PARAMETER_FLAGS];
305 }
306
307 uint64_t
308 getFlags() const
309 {
310 BOOST_ASSERT(this->hasFlags());
311 return m_flags;
312 }
313
314 ControlParameters&
315 setFlags(uint64_t flags)
316 {
317 m_wire.reset();
318 m_flags = flags;
319 m_hasFields[CONTROL_PARAMETER_FLAGS] = true;
320 return *this;
321 }
322
323 ControlParameters&
324 unsetFlags()
325 {
326 m_wire.reset();
327 m_hasFields[CONTROL_PARAMETER_FLAGS] = false;
328 return *this;
329 }
330
331 bool
Eric Newberryda916d62016-08-11 23:04:34 -0700332 hasMask() const
333 {
334 return m_hasFields[CONTROL_PARAMETER_MASK];
335 }
336
337 uint64_t
338 getMask() const
339 {
340 BOOST_ASSERT(this->hasMask());
341 return m_mask;
342 }
343
344 ControlParameters&
345 setMask(uint64_t mask)
346 {
347 m_wire.reset();
348 m_mask = mask;
349 m_hasFields[CONTROL_PARAMETER_MASK] = true;
350 return *this;
351 }
352
353 ControlParameters&
354 unsetMask()
355 {
356 m_wire.reset();
357 m_hasFields[CONTROL_PARAMETER_MASK] = false;
358 return *this;
359 }
360
361 bool
Junxiao Shibc19b372014-03-23 16:59:25 -0700362 hasStrategy() const
363 {
Junxiao Shi5ec80222014-03-25 20:08:05 -0700364 return m_hasFields[CONTROL_PARAMETER_STRATEGY];
Junxiao Shibc19b372014-03-23 16:59:25 -0700365 }
366
367 const Name&
368 getStrategy() const
369 {
Junxiao Shi5ec80222014-03-25 20:08:05 -0700370 BOOST_ASSERT(this->hasStrategy());
Junxiao Shibc19b372014-03-23 16:59:25 -0700371 return m_strategy;
372 }
373
374 ControlParameters&
375 setStrategy(const Name& strategy)
376 {
377 m_wire.reset();
378 m_strategy = strategy;
Junxiao Shi5ec80222014-03-25 20:08:05 -0700379 m_hasFields[CONTROL_PARAMETER_STRATEGY] = true;
Junxiao Shibc19b372014-03-23 16:59:25 -0700380 return *this;
381 }
382
383 ControlParameters&
384 unsetStrategy()
385 {
386 m_wire.reset();
Junxiao Shi5ec80222014-03-25 20:08:05 -0700387 m_hasFields[CONTROL_PARAMETER_STRATEGY] = false;
Junxiao Shibc19b372014-03-23 16:59:25 -0700388 return *this;
389 }
390
Junxiao Shi5f6c74f2014-04-18 16:29:44 -0700391 bool
392 hasExpirationPeriod() const
393 {
394 return m_hasFields[CONTROL_PARAMETER_EXPIRATION_PERIOD];
395 }
396
397 const time::milliseconds&
398 getExpirationPeriod() const
399 {
400 BOOST_ASSERT(this->hasExpirationPeriod());
401 return m_expirationPeriod;
402 }
403
404 ControlParameters&
405 setExpirationPeriod(const time::milliseconds& expirationPeriod)
406 {
407 m_wire.reset();
408 m_expirationPeriod = expirationPeriod;
409 m_hasFields[CONTROL_PARAMETER_EXPIRATION_PERIOD] = true;
410 return *this;
411 }
412
413 ControlParameters&
414 unsetExpirationPeriod()
415 {
416 m_wire.reset();
417 m_hasFields[CONTROL_PARAMETER_EXPIRATION_PERIOD] = false;
418 return *this;
419 }
420
Yukai Tud93c5fc2015-08-25 11:37:16 +0800421 bool
422 hasFacePersistency() const
423 {
424 return m_hasFields[CONTROL_PARAMETER_FACE_PERSISTENCY];
425 }
426
427 FacePersistency
428 getFacePersistency() const
429 {
430 BOOST_ASSERT(this->hasFacePersistency());
431 return m_facePersistency;
432 }
433
434 ControlParameters&
435 setFacePersistency(FacePersistency persistency)
436 {
437 m_wire.reset();
438 m_facePersistency = persistency;
439 m_hasFields[CONTROL_PARAMETER_FACE_PERSISTENCY] = true;
440 return *this;
441 }
442
443 ControlParameters&
444 unsetFacePersistency()
445 {
446 m_wire.reset();
447 m_hasFields[CONTROL_PARAMETER_FACE_PERSISTENCY] = false;
448 return *this;
449 }
450
Junxiao Shi5ec80222014-03-25 20:08:05 -0700451 const std::vector<bool>&
452 getPresentFields() const
453 {
454 return m_hasFields;
455 }
456
Eric Newberryda916d62016-08-11 23:04:34 -0700457public: // Flags and Mask helpers
458 /**
459 * \return whether bit is enabled in Mask
460 * \param bit bit position within range [0, 64) (least significant bit is 0)
461 */
462 bool
463 hasFlagBit(size_t bit) const;
464
465 /**
466 * \return bit at a position in Flags
467 * \param bit bit position within range [0, 64) (least significant bit is 0)
468 */
469 bool
470 getFlagBit(size_t bit) const;
471
472 /**
473 * \brief set a bit in Flags
474 * \param bit bit position within range [0, 64) (least significant bit is 0)
475 * \param value new value in Flags
476 * \param wantMask if true, enable the bit in Mask
477 */
478 ControlParameters&
479 setFlagBit(size_t bit, bool value, bool wantMask = true);
480
481 /**
482 * \brief disable a bit in Mask
483 * \param bit bit position within range [0, 64) (least significant bit is 0)
484 * \post If all bits are disabled, Flags and Mask fields are deleted.
485 */
486 ControlParameters&
487 unsetFlagBit(size_t bit);
488
Junxiao Shibc19b372014-03-23 16:59:25 -0700489private: // fields
Junxiao Shi5ec80222014-03-25 20:08:05 -0700490 std::vector<bool> m_hasFields;
491
Junxiao Shibc19b372014-03-23 16:59:25 -0700492 Name m_name;
493 uint64_t m_faceId;
494 std::string m_uri;
495 LocalControlFeature m_localControlFeature;
Junxiao Shi5f6c74f2014-04-18 16:29:44 -0700496 uint64_t m_origin;
Junxiao Shibc19b372014-03-23 16:59:25 -0700497 uint64_t m_cost;
Junxiao Shi5f6c74f2014-04-18 16:29:44 -0700498 uint64_t m_flags;
Eric Newberryda916d62016-08-11 23:04:34 -0700499 uint64_t m_mask;
Junxiao Shibc19b372014-03-23 16:59:25 -0700500 Name m_strategy;
Junxiao Shi5f6c74f2014-04-18 16:29:44 -0700501 time::milliseconds m_expirationPeriod;
Yukai Tud93c5fc2015-08-25 11:37:16 +0800502 FacePersistency m_facePersistency;
Junxiao Shibc19b372014-03-23 16:59:25 -0700503
Junxiao Shibc19b372014-03-23 16:59:25 -0700504private:
505 mutable Block m_wire;
506};
507
Junxiao Shi70911652014-08-12 10:14:24 -0700508std::ostream&
509operator<<(std::ostream& os, const ControlParameters& parameters);
Junxiao Shibc19b372014-03-23 16:59:25 -0700510
511} // namespace nfd
512} // namespace ndn
513
514#endif // NDN_MANAGEMENT_NFD_CONTROL_PARAMETERS_HPP