|
9 | 9 |
|
10 | 10 | #include "sai.h"
|
11 | 11 | #include "macaddress.h"
|
| 12 | +#include "ipaddress.h" |
12 | 13 | #include "orch.h"
|
13 | 14 | #include "request_parser.h"
|
14 | 15 |
|
@@ -87,6 +88,12 @@ void Request::parseKey(const KeyOpFieldsValuesTuple& request)
|
87 | 88 | case REQ_T_MAC_ADDRESS:
|
88 | 89 | key_item_mac_addresses_[i] = parseMacAddress(key_items[i]);
|
89 | 90 | break;
|
| 91 | + case REQ_T_IP: |
| 92 | + key_item_ip_addresses_[i] = parseIpAddress(key_items[i]); |
| 93 | + break; |
| 94 | + case REQ_T_UINT: |
| 95 | + key_item_uint_[i] = parseUint(key_items[i]); |
| 96 | + break; |
90 | 97 | default:
|
91 | 98 | throw std::logic_error(std::string("Not implemented key type parser. Key '")
|
92 | 99 | + full_key_
|
@@ -129,6 +136,15 @@ void Request::parseAttrs(const KeyOpFieldsValuesTuple& request)
|
129 | 136 | case REQ_T_PACKET_ACTION:
|
130 | 137 | attr_item_packet_actions_[fvField(*i)] = parsePacketAction(fvValue(*i));
|
131 | 138 | break;
|
| 139 | + case REQ_T_VLAN: |
| 140 | + attr_item_vlan_[fvField(*i)] = parseVlan(fvValue(*i)); |
| 141 | + break; |
| 142 | + case REQ_T_IP: |
| 143 | + attr_item_ip_[fvField(*i)] = parseIpAddress(fvValue(*i)); |
| 144 | + break; |
| 145 | + case REQ_T_UINT: |
| 146 | + attr_item_uint_[fvField(*i)] = parseUint(fvValue(*i)); |
| 147 | + break; |
132 | 148 | default:
|
133 | 149 | throw std::logic_error(std::string("Not implemented attribute type parser for attribute:") + fvField(*i));
|
134 | 150 | }
|
@@ -178,6 +194,69 @@ MacAddress Request::parseMacAddress(const std::string& str)
|
178 | 194 | return MacAddress(mac);
|
179 | 195 | }
|
180 | 196 |
|
| 197 | +IpAddress Request::parseIpAddress(const std::string& str) |
| 198 | +{ |
| 199 | + try |
| 200 | + { |
| 201 | + IpAddress addr(str); |
| 202 | + return addr; |
| 203 | + } |
| 204 | + catch (std::invalid_argument& _) |
| 205 | + { |
| 206 | + throw std::invalid_argument(std::string("Invalid ip address: ") + str); |
| 207 | + } |
| 208 | +} |
| 209 | + |
| 210 | +uint64_t Request::parseUint(const std::string& str) |
| 211 | +{ |
| 212 | + try |
| 213 | + { |
| 214 | + uint64_t ret = std::stoul(str); |
| 215 | + return ret; |
| 216 | + } |
| 217 | + catch(std::invalid_argument& _) |
| 218 | + { |
| 219 | + throw std::invalid_argument(std::string("Invalid unsigned integer: ") + str); |
| 220 | + } |
| 221 | + catch(std::out_of_range& _) |
| 222 | + { |
| 223 | + throw std::invalid_argument(std::string("Out of range unsigned integer: ") + str); |
| 224 | + } |
| 225 | +} |
| 226 | + |
| 227 | +uint16_t Request::parseVlan(const std::string& str) |
| 228 | +{ |
| 229 | + uint16_t ret = 0; |
| 230 | + |
| 231 | + const auto vlan_prefix = std::string("Vlan"); |
| 232 | + const auto prefix_len = vlan_prefix.length(); |
| 233 | + |
| 234 | + if (str.substr(0, prefix_len) != vlan_prefix) |
| 235 | + { |
| 236 | + throw std::invalid_argument(std::string("Invalid vlan interface: ") + str); |
| 237 | + } |
| 238 | + |
| 239 | + try |
| 240 | + { |
| 241 | + ret = static_cast<uint16_t>(std::stoul(str.substr(prefix_len))); |
| 242 | + } |
| 243 | + catch(std::invalid_argument& _) |
| 244 | + { |
| 245 | + throw std::invalid_argument(std::string("Invalid vlan id: ") + str); |
| 246 | + } |
| 247 | + catch(std::out_of_range& _) |
| 248 | + { |
| 249 | + throw std::invalid_argument(std::string("Out of range vlan id: ") + str); |
| 250 | + } |
| 251 | + |
| 252 | + if (ret == 0 || ret > 4094) |
| 253 | + { |
| 254 | + throw std::invalid_argument(std::string("Out of range vlan id: ") + str); |
| 255 | + } |
| 256 | + |
| 257 | + return ret; |
| 258 | +} |
| 259 | + |
181 | 260 | sai_packet_action_t Request::parsePacketAction(const std::string& str)
|
182 | 261 | {
|
183 | 262 | std::unordered_map<std::string, sai_packet_action_t> m = {
|
|
0 commit comments