Alexander Lane | 6f7a64f | 2018-05-17 15:01:14 -0500 | [diff] [blame] | 1 | # -*- Mode:python; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | # |
Saurab Dulal | 576a419 | 2020-08-25 00:55:22 -0500 | [diff] [blame] | 3 | # Copyright (C) 2015-2020, The University of Memphis, |
Alexander Lane | 6f7a64f | 2018-05-17 15:01:14 -0500 | [diff] [blame] | 4 | # 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 Dulal | 8ae870a | 2018-07-31 05:17:49 +0000 | [diff] [blame] | 24 | from mininet.log import debug |
Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 25 | from minindn.minindn import Minindn |
Saurab Dulal | 8ae870a | 2018-07-31 05:17:49 +0000 | [diff] [blame] | 26 | |
| 27 | SLEEP_TIME = 0.2 |
Alexander Lane | 6f7a64f | 2018-05-17 15:01:14 -0500 | [diff] [blame] | 28 | |
Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 29 | class Nfdc(object): |
| 30 | STRATEGY_ASF = 'asf' |
| 31 | STRATEGY_BEST_ROUTE = 'best-route' |
| 32 | STRATEGY_MULTICAST = 'multicast' |
| 33 | STRATEGY_NCC = 'ncc' |
| 34 | PROTOCOL_UDP = 'udp' |
| 35 | PROTOCOL_TCP = 'tcp' |
| 36 | PROTOCOL_ETHER = 'ether' |
Alexander Lane | 6f7a64f | 2018-05-17 15:01:14 -0500 | [diff] [blame] | 37 | |
| 38 | @staticmethod |
Alex Lane | 1d3c0a8 | 2021-07-22 17:28:16 -0500 | [diff] [blame] | 39 | def registerRoute(node, namePrefix, remoteNode, protocol=PROTOCOL_UDP, origin=255, |
Ashlesh Gawande | 6651a74 | 2019-01-03 18:13:06 -0600 | [diff] [blame] | 40 | cost=0, inheritFlag=True, captureFlag=False, expirationInMillis=None): |
Alex Lane | 1d3c0a8 | 2021-07-22 17:28:16 -0500 | [diff] [blame] | 41 | cmd = "" |
| 42 | if remoteNode.isdigit() and not protocol == "fd": |
| 43 | cmd = ('nfdc route add {} {} origin {} cost {} {}{}{}').format( |
| 44 | namePrefix, |
| 45 | remoteNode, |
| 46 | origin, |
| 47 | cost, |
| 48 | 'no-inherit ' if not inheritFlag else '', |
| 49 | 'capture ' if captureFlag else '', |
| 50 | 'expires {}'.format(expirationInMillis) if expirationInMillis else '' |
| 51 | ) |
| 52 | else: |
| 53 | cmd = ('nfdc route add {} {}://{} origin {} cost {} {}{}{}').format( |
| 54 | namePrefix, |
| 55 | protocol, |
| 56 | remoteNode, |
| 57 | origin, |
| 58 | cost, |
| 59 | 'no-inherit ' if not inheritFlag else '', |
| 60 | 'capture ' if captureFlag else '', |
| 61 | 'expires {}'.format(expirationInMillis) if expirationInMillis else '' |
| 62 | ) |
dulalsaurab | 2b89953 | 2018-10-25 18:02:15 +0000 | [diff] [blame] | 63 | debug(node.cmd(cmd)) |
Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 64 | Minindn.sleep(SLEEP_TIME) |
Alexander Lane | 6f7a64f | 2018-05-17 15:01:14 -0500 | [diff] [blame] | 65 | |
| 66 | @staticmethod |
Alex Lane | 1d3c0a8 | 2021-07-22 17:28:16 -0500 | [diff] [blame] | 67 | def unregisterRoute(node, namePrefix, remoteNode, origin=255): |
| 68 | cmd = "" |
| 69 | if remoteNode.isdigit() and not protocol == "fd": |
| 70 | cmd = 'nfdc route remove {} {} {}'.format(namePrefix, remoteNode, origin) |
| 71 | else: |
| 72 | cmd = 'nfdc route remove {} {} {}'.format(namePrefix, remoteNode, origin) |
dulalsaurab | 0dcdb32 | 2018-08-15 20:39:07 +0000 | [diff] [blame] | 73 | debug(node.cmd(cmd)) |
Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 74 | Minindn.sleep(SLEEP_TIME) |
Alexander Lane | 6f7a64f | 2018-05-17 15:01:14 -0500 | [diff] [blame] | 75 | |
| 76 | @staticmethod |
Varun Patil | c69041f | 2022-05-18 14:17:54 -0700 | [diff] [blame^] | 77 | def createFace(node, remoteNodeAddress, protocol='udp', isPermanent=False, allowExisting=True): |
Alex Lane | 1d3c0a8 | 2021-07-22 17:28:16 -0500 | [diff] [blame] | 78 | '''Create face in node's NFD instance. Returns FaceID of created face or -1 if failed.''' |
Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 79 | cmd = ('nfdc face create {}://{} {}'.format( |
Alexander Lane | 6f7a64f | 2018-05-17 15:01:14 -0500 | [diff] [blame] | 80 | protocol, |
Ashlesh Gawande | 6651a74 | 2019-01-03 18:13:06 -0600 | [diff] [blame] | 81 | remoteNodeAddress, |
Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 82 | 'permanent' if isPermanent else 'persistent' |
Alexander Lane | 6f7a64f | 2018-05-17 15:01:14 -0500 | [diff] [blame] | 83 | )) |
Alex Lane | 1d3c0a8 | 2021-07-22 17:28:16 -0500 | [diff] [blame] | 84 | output = node.cmd(cmd) |
| 85 | debug(output) |
Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 86 | Minindn.sleep(SLEEP_TIME) |
Varun Patil | c69041f | 2022-05-18 14:17:54 -0700 | [diff] [blame^] | 87 | if "face-created" in output or (allowExisting and "face-exists" in output): |
| 88 | faceID = output.split(" ")[1][3:] |
| 89 | return faceID |
| 90 | return -1 |
Alexander Lane | 6f7a64f | 2018-05-17 15:01:14 -0500 | [diff] [blame] | 91 | |
| 92 | @staticmethod |
Alex Lane | 1d3c0a8 | 2021-07-22 17:28:16 -0500 | [diff] [blame] | 93 | def destroyFace(node, remoteNode, protocol='udp'): |
| 94 | if remoteNode.isdigit() and not protocol == "fd": |
| 95 | debug(node.cmd('nfdc face destroy {}'.format(protocol, remoteNode))) |
| 96 | else: |
| 97 | debug(node.cmd('nfdc face destroy {}://{}'.format(protocol, remoteNode))) |
Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 98 | Minindn.sleep(SLEEP_TIME) |
Alexander Lane | 6f7a64f | 2018-05-17 15:01:14 -0500 | [diff] [blame] | 99 | |
| 100 | @staticmethod |
| 101 | def setStrategy(node, namePrefix, strategy): |
Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 102 | cmd = 'nfdc strategy set {} ndn:/localhost/nfd/strategy/{}'.format(namePrefix, strategy) |
dulalsaurab | 2b89953 | 2018-10-25 18:02:15 +0000 | [diff] [blame] | 103 | debug(node.cmd(cmd)) |
Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 104 | Minindn.sleep(SLEEP_TIME) |
Alexander Lane | 6f7a64f | 2018-05-17 15:01:14 -0500 | [diff] [blame] | 105 | |
| 106 | @staticmethod |
| 107 | def unsetStrategy(node, namePrefix): |
dulalsaurab | 2b89953 | 2018-10-25 18:02:15 +0000 | [diff] [blame] | 108 | debug(node.cmd("nfdc strategy unset {}".format(namePrefix))) |
Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 109 | Minindn.sleep(SLEEP_TIME) |
Alex Lane | 1d3c0a8 | 2021-07-22 17:28:16 -0500 | [diff] [blame] | 110 | |
| 111 | @staticmethod |
| 112 | def getFaceId(node, remoteNodeAddress, localEndpoint=None, protocol="udp", portNum="6363"): |
| 113 | '''Returns the faceId for a remote node based on FaceURI, or -1 if a face is not found''' |
| 114 | #Should this be cached or is the hit not worth it? |
| 115 | local = "" |
| 116 | if localEndpoint: |
| 117 | local = " local {}".format(localEndpoint) |
| 118 | output = node.cmd("nfdc face list remote {}://{}:{}{}".format(protocol, remoteNodeAddress, portNum, local)) |
| 119 | debug(output) |
| 120 | Minindn.sleep(SLEEP_TIME) |
| 121 | # This is fragile but we don't have that many better options |
| 122 | if "faceid=" not in output: |
| 123 | return -1 |
| 124 | faceId = output.split(" ")[0][7:] |
| 125 | return faceId |