blob: 50c005ba8239ee606560fd6f91daf4dc3fbcb214 [file] [log] [blame]
Alexander Lane6f7a64f2018-05-17 15:01:14 -05001# -*- Mode:python; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2#
Saurab Dulal576a4192020-08-25 00:55:22 -05003# Copyright (C) 2015-2020, The University of Memphis,
Alexander Lane6f7a64f2018-05-17 15:01:14 -05004# Arizona Board of Regents,
5# Regents of the University of California.
6#
7# This file is part of Mini-NDN.
8# See AUTHORS.md for a complete list of Mini-NDN authors and contributors.
9#
10# Mini-NDN is free software: you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation, either version 3 of the License, or
13# (at your option) any later version.
14#
15# Mini-NDN is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with Mini-NDN, e.g., in COPYING.md file.
22# If not, see <http://www.gnu.org/licenses/>.
23
Saurab Dulal8ae870a2018-07-31 05:17:49 +000024from mininet.log import debug
Ashlesh Gawande6c86e302019-09-17 22:27:05 -050025from minindn.minindn import Minindn
Saurab Dulal8ae870a2018-07-31 05:17:49 +000026
Varun Patil63a330d2022-05-18 14:23:13 -070027# If needed (e.g. to speed up the process), use a smaller (or larger value)
28# based on your machines resource (CPU, memory)
29SLEEP_TIME = 0.1
Alexander Lane6f7a64f2018-05-17 15:01:14 -050030
Ashlesh Gawande6c86e302019-09-17 22:27:05 -050031class Nfdc(object):
32 STRATEGY_ASF = 'asf'
33 STRATEGY_BEST_ROUTE = 'best-route'
34 STRATEGY_MULTICAST = 'multicast'
35 STRATEGY_NCC = 'ncc'
36 PROTOCOL_UDP = 'udp'
37 PROTOCOL_TCP = 'tcp'
38 PROTOCOL_ETHER = 'ether'
Alexander Lane6f7a64f2018-05-17 15:01:14 -050039
40 @staticmethod
Alex Lane1d3c0a82021-07-22 17:28:16 -050041 def registerRoute(node, namePrefix, remoteNode, protocol=PROTOCOL_UDP, origin=255,
Ashlesh Gawande6651a742019-01-03 18:13:06 -060042 cost=0, inheritFlag=True, captureFlag=False, expirationInMillis=None):
Alex Lane1d3c0a82021-07-22 17:28:16 -050043 cmd = ""
44 if remoteNode.isdigit() and not protocol == "fd":
45 cmd = ('nfdc route add {} {} origin {} cost {} {}{}{}').format(
46 namePrefix,
47 remoteNode,
48 origin,
49 cost,
50 'no-inherit ' if not inheritFlag else '',
51 'capture ' if captureFlag else '',
52 'expires {}'.format(expirationInMillis) if expirationInMillis else ''
53 )
54 else:
55 cmd = ('nfdc route add {} {}://{} origin {} cost {} {}{}{}').format(
56 namePrefix,
57 protocol,
58 remoteNode,
59 origin,
60 cost,
61 'no-inherit ' if not inheritFlag else '',
62 'capture ' if captureFlag else '',
63 'expires {}'.format(expirationInMillis) if expirationInMillis else ''
64 )
dulalsaurab2b899532018-10-25 18:02:15 +000065 debug(node.cmd(cmd))
Ashlesh Gawande6c86e302019-09-17 22:27:05 -050066 Minindn.sleep(SLEEP_TIME)
Alexander Lane6f7a64f2018-05-17 15:01:14 -050067
68 @staticmethod
Alex Lane1d3c0a82021-07-22 17:28:16 -050069 def unregisterRoute(node, namePrefix, remoteNode, origin=255):
70 cmd = ""
71 if remoteNode.isdigit() and not protocol == "fd":
72 cmd = 'nfdc route remove {} {} {}'.format(namePrefix, remoteNode, origin)
73 else:
74 cmd = 'nfdc route remove {} {} {}'.format(namePrefix, remoteNode, origin)
dulalsaurab0dcdb322018-08-15 20:39:07 +000075 debug(node.cmd(cmd))
Ashlesh Gawande6c86e302019-09-17 22:27:05 -050076 Minindn.sleep(SLEEP_TIME)
Alexander Lane6f7a64f2018-05-17 15:01:14 -050077
78 @staticmethod
Varun Patilc69041f2022-05-18 14:17:54 -070079 def createFace(node, remoteNodeAddress, protocol='udp', isPermanent=False, allowExisting=True):
Alex Lane1d3c0a82021-07-22 17:28:16 -050080 '''Create face in node's NFD instance. Returns FaceID of created face or -1 if failed.'''
Ashlesh Gawande6c86e302019-09-17 22:27:05 -050081 cmd = ('nfdc face create {}://{} {}'.format(
Alexander Lane6f7a64f2018-05-17 15:01:14 -050082 protocol,
Ashlesh Gawande6651a742019-01-03 18:13:06 -060083 remoteNodeAddress,
Ashlesh Gawande6c86e302019-09-17 22:27:05 -050084 'permanent' if isPermanent else 'persistent'
Alexander Lane6f7a64f2018-05-17 15:01:14 -050085 ))
Alex Lane1d3c0a82021-07-22 17:28:16 -050086 output = node.cmd(cmd)
87 debug(output)
Ashlesh Gawande6c86e302019-09-17 22:27:05 -050088 Minindn.sleep(SLEEP_TIME)
Varun Patilc69041f2022-05-18 14:17:54 -070089 if "face-created" in output or (allowExisting and "face-exists" in output):
90 faceID = output.split(" ")[1][3:]
91 return faceID
92 return -1
Alexander Lane6f7a64f2018-05-17 15:01:14 -050093
94 @staticmethod
Alex Lane1d3c0a82021-07-22 17:28:16 -050095 def destroyFace(node, remoteNode, protocol='udp'):
96 if remoteNode.isdigit() and not protocol == "fd":
97 debug(node.cmd('nfdc face destroy {}'.format(protocol, remoteNode)))
98 else:
99 debug(node.cmd('nfdc face destroy {}://{}'.format(protocol, remoteNode)))
Ashlesh Gawande6c86e302019-09-17 22:27:05 -0500100 Minindn.sleep(SLEEP_TIME)
Alexander Lane6f7a64f2018-05-17 15:01:14 -0500101
102 @staticmethod
103 def setStrategy(node, namePrefix, strategy):
Ashlesh Gawande6c86e302019-09-17 22:27:05 -0500104 cmd = 'nfdc strategy set {} ndn:/localhost/nfd/strategy/{}'.format(namePrefix, strategy)
dulalsaurab2b899532018-10-25 18:02:15 +0000105 debug(node.cmd(cmd))
Ashlesh Gawande6c86e302019-09-17 22:27:05 -0500106 Minindn.sleep(SLEEP_TIME)
Alexander Lane6f7a64f2018-05-17 15:01:14 -0500107
108 @staticmethod
109 def unsetStrategy(node, namePrefix):
dulalsaurab2b899532018-10-25 18:02:15 +0000110 debug(node.cmd("nfdc strategy unset {}".format(namePrefix)))
Ashlesh Gawande6c86e302019-09-17 22:27:05 -0500111 Minindn.sleep(SLEEP_TIME)
Alex Lane1d3c0a82021-07-22 17:28:16 -0500112
113 @staticmethod
114 def getFaceId(node, remoteNodeAddress, localEndpoint=None, protocol="udp", portNum="6363"):
115 '''Returns the faceId for a remote node based on FaceURI, or -1 if a face is not found'''
116 #Should this be cached or is the hit not worth it?
117 local = ""
118 if localEndpoint:
119 local = " local {}".format(localEndpoint)
120 output = node.cmd("nfdc face list remote {}://{}:{}{}".format(protocol, remoteNodeAddress, portNum, local))
121 debug(output)
122 Minindn.sleep(SLEEP_TIME)
123 # This is fragile but we don't have that many better options
124 if "faceid=" not in output:
125 return -1
126 faceId = output.split(" ")[0][7:]
127 return faceId