Steve DiBenedetto | 042bfe9 | 2014-01-30 15:05:08 -0700 | [diff] [blame^] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2014 Named Data Networking Project |
| 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
| 7 | #include "fib-manager.hpp" |
| 8 | |
| 9 | #include "table/fib.hpp" |
| 10 | #include "fw/forwarder.hpp" |
| 11 | #include "mgmt/internal-face.hpp" |
| 12 | #include "mgmt/app-face.hpp" |
| 13 | |
| 14 | #include <ndn-cpp-dev/management/fib-management-options.hpp> |
| 15 | |
| 16 | namespace nfd { |
| 17 | |
| 18 | NFD_LOG_INIT("FibManager"); |
| 19 | |
| 20 | const Name FibManager::FIB_MANAGER_REQUEST_PREFIX = "/localhost/nfd/fib"; |
| 21 | |
| 22 | // In the future this should be 3 (Signature TLV, Signature Info TLV, Timestamp) |
| 23 | const size_t FibManager::FIB_MANAGER_REQUEST_SIGNED_INTEREST_NCOMPS = 0; |
| 24 | |
| 25 | const size_t FibManager::FIB_MANAGER_REQUEST_COMMAND_MIN_NCOMPS = |
| 26 | FibManager::FIB_MANAGER_REQUEST_PREFIX.size() + |
| 27 | 1 + // verb |
| 28 | 1 + // verb options |
| 29 | FibManager::FIB_MANAGER_REQUEST_SIGNED_INTEREST_NCOMPS; |
| 30 | |
| 31 | const Name::Component FibManager::FIB_MANAGER_REQUEST_VERB_INSERT = "insert"; |
| 32 | const Name::Component FibManager::FIB_MANAGER_REQUEST_VERB_DELETE = "delete"; |
| 33 | const Name::Component FibManager::FIB_MANAGER_REQUEST_VERB_ADD_NEXTHOP = "add-nexthop"; |
| 34 | const Name::Component FibManager::FIB_MANAGER_REQUEST_VERB_REMOVE_NEXTHOP = "remove-nexthop"; |
| 35 | const Name::Component FibManager::FIB_MANAGER_REQUEST_VERB_STRATEGY = "strategy"; |
| 36 | |
| 37 | |
| 38 | const FibManager::VerbAndProcessor FibManager::FIB_MANAGER_REQUEST_VERBS[] = |
| 39 | { |
| 40 | // Unsupported |
| 41 | |
| 42 | // VerbAndProcessor( |
| 43 | // FibManager::FIB_MANAGER_REQUEST_VERB_INSERT, |
| 44 | // &FibManager::fibInsert |
| 45 | // ), |
| 46 | |
| 47 | // VerbAndProcessor( |
| 48 | // FibManager::FIB_MANAGER_REQUEST_VERB_DELETE, |
| 49 | // &FibManager::fibDelete |
| 50 | // ), |
| 51 | |
| 52 | VerbAndProcessor( |
| 53 | FibManager::FIB_MANAGER_REQUEST_VERB_ADD_NEXTHOP, |
| 54 | &FibManager::fibAddNextHop |
| 55 | ), |
| 56 | |
| 57 | // Unsupported |
| 58 | |
| 59 | // VerbAndProcessor( |
| 60 | // FibManager::FIB_MANAGER_REQUEST_VERB_REMOVE_NEXTHOP, |
| 61 | // &FibManager::fibRemoveNextHop |
| 62 | // ), |
| 63 | |
| 64 | // VerbAndProcessor( |
| 65 | // FibManager::FIB_MANAGER_REQUEST_VERB_STRATEGY, |
| 66 | // &FibManager::fibStrategy |
| 67 | // ) |
| 68 | |
| 69 | }; |
| 70 | |
| 71 | FibManager::FibManager(Fib& fib, |
| 72 | function<shared_ptr<Face>(FaceId)> getFace) |
| 73 | : ManagerBase(shared_ptr<AppFace>(new InternalFace(*this))), |
| 74 | m_managedFib(fib), |
| 75 | m_getFace(getFace), |
| 76 | m_verbDispatch(FIB_MANAGER_REQUEST_VERBS, |
| 77 | FIB_MANAGER_REQUEST_VERBS + |
| 78 | (sizeof(FIB_MANAGER_REQUEST_VERBS) / sizeof(VerbAndProcessor))) |
| 79 | { |
| 80 | |
| 81 | } |
| 82 | |
| 83 | void |
| 84 | FibManager::onFibRequest(const Interest& request) |
| 85 | { |
| 86 | const Name& requestCommand = request.getName(); |
| 87 | |
| 88 | /// \todo Separate out response status codes 400 and 401 |
| 89 | |
| 90 | if (requestCommand.size() < FIB_MANAGER_REQUEST_COMMAND_MIN_NCOMPS || |
| 91 | !FIB_MANAGER_REQUEST_PREFIX.isPrefixOf(requestCommand)) |
| 92 | { |
| 93 | NFD_LOG_INFO("Malformed command: " << requestCommand); |
| 94 | sendResponse(request.getName(), 404, "MALFORMED"); |
| 95 | return; |
| 96 | } |
| 97 | |
| 98 | const Name::Component& requestVerb = requestCommand.get(FIB_MANAGER_REQUEST_PREFIX.size()); |
| 99 | |
| 100 | VerbDispatchTable::const_iterator verbProcessor = m_verbDispatch.find (requestVerb); |
| 101 | if (verbProcessor == m_verbDispatch.end()) |
| 102 | { |
| 103 | NFD_LOG_INFO("Unsupported command verb: " << requestVerb); |
| 104 | sendResponse(request.getName(), 404, "UNSUPPORTED"); |
| 105 | |
| 106 | } |
| 107 | else |
| 108 | { |
| 109 | NFD_LOG_INFO("Processing command verb: " << requestVerb); |
| 110 | (verbProcessor->second)(this, request); |
| 111 | } |
| 112 | |
| 113 | } |
| 114 | |
| 115 | void |
| 116 | FibManager::fibInsert(const Interest& request) |
| 117 | { |
| 118 | |
| 119 | } |
| 120 | |
| 121 | void |
| 122 | FibManager::fibDelete(const Interest& request) |
| 123 | { |
| 124 | |
| 125 | } |
| 126 | |
| 127 | void |
| 128 | FibManager::fibAddNextHop(const Interest& request) |
| 129 | { |
| 130 | ndn::FibManagementOptions options; |
| 131 | const size_t optionCompIndex = |
| 132 | FIB_MANAGER_REQUEST_PREFIX.size() + 1; |
| 133 | |
| 134 | const ndn::Buffer &optionBuffer = |
| 135 | request.getName()[optionCompIndex].getValue(); |
| 136 | shared_ptr<const ndn::Buffer> tmpOptionBuffer(new ndn::Buffer(optionBuffer)); |
| 137 | Block rawOptions(tmpOptionBuffer); |
| 138 | |
| 139 | options.wireDecode(rawOptions); |
| 140 | |
| 141 | /// \todo authorize command |
| 142 | if (false) |
| 143 | { |
| 144 | NFD_LOG_INFO("Unauthorized command attempt"); |
| 145 | |
| 146 | sendResponse(request.getName(), 403, ""); |
| 147 | return; |
| 148 | } |
| 149 | |
| 150 | NFD_LOG_INFO("add-nexthop Name: " << options.getName() |
| 151 | << " FaceId: " << options.getFaceId() |
| 152 | << " Cost: " << options.getCost()); |
| 153 | |
| 154 | shared_ptr<Face> nextHopFace = m_getFace(options.getFaceId()); |
| 155 | if (nextHopFace) |
| 156 | { |
| 157 | std::pair<shared_ptr<fib::Entry>, bool> insertResult = m_managedFib.insert(options.getName()); |
| 158 | insertResult.first->addNextHop(nextHopFace, options.getCost()); |
| 159 | sendResponse(request.getName(), 200, "OK"); |
| 160 | } |
| 161 | else |
| 162 | { |
| 163 | sendResponse(request.getName(), 400, "INCORRECT"); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | void |
| 168 | FibManager::fibRemoveNextHop(const Interest& request) |
| 169 | { |
| 170 | |
| 171 | } |
| 172 | |
| 173 | void |
| 174 | FibManager::fibStrategy(const Interest& request) |
| 175 | { |
| 176 | |
| 177 | } |
| 178 | |
| 179 | // void |
| 180 | // FibManager::onConfig(ConfigFile::Node section, bool isDryRun) |
| 181 | // { |
| 182 | |
| 183 | // } |
| 184 | |
| 185 | } // namespace nfd |