Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 Cisco and/or its affiliates. |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame] | 16 | #include "vom/dhcp_config_cmds.hpp" |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 17 | |
| 18 | DEFINE_VAPI_MSG_IDS_DHCP_API_JSON; |
| 19 | |
| 20 | namespace VOM { |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame] | 21 | namespace dhcp_config_cmds { |
| 22 | |
| 23 | bind_cmd::bind_cmd(HW::item<bool>& item, |
| 24 | const handle_t& itf, |
| 25 | const std::string& hostname, |
| 26 | const l2_address_t& client_id) |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 27 | : rpc_cmd(item) |
| 28 | , m_itf(itf) |
| 29 | , m_hostname(hostname) |
| 30 | , m_client_id(client_id) |
| 31 | { |
| 32 | } |
| 33 | |
| 34 | bool |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame] | 35 | bind_cmd::operator==(const bind_cmd& other) const |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 36 | { |
| 37 | return ((m_itf == other.m_itf) && (m_hostname == other.m_hostname)); |
| 38 | } |
| 39 | |
| 40 | rc_t |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame] | 41 | bind_cmd::issue(connection& con) |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 42 | { |
| 43 | msg_t req(con.ctx(), std::ref(*this)); |
| 44 | |
| 45 | auto& payload = req.get_request().get_payload(); |
| 46 | payload.sw_if_index = m_itf.value(); |
| 47 | payload.is_add = 1; |
| 48 | payload.pid = getpid(); |
| 49 | payload.want_dhcp_event = 1; |
| 50 | |
Neale Ranns | 814b47b | 2017-11-09 05:18:04 -0800 | [diff] [blame] | 51 | memset(payload.hostname, 0, sizeof(payload.hostname)); |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 52 | memcpy(payload.hostname, m_hostname.c_str(), |
| 53 | std::min(sizeof(payload.hostname), m_hostname.length())); |
| 54 | |
| 55 | memset(payload.client_id, 0, sizeof(payload.client_id)); |
| 56 | payload.client_id[0] = 1; |
| 57 | std::copy_n(begin(m_client_id.bytes), |
| 58 | std::min(sizeof(payload.client_id), m_client_id.bytes.size()), |
| 59 | payload.client_id + 1); |
| 60 | |
| 61 | VAPI_CALL(req.execute()); |
| 62 | |
| 63 | m_hw_item.set(wait()); |
| 64 | |
| 65 | return rc_t::OK; |
| 66 | } |
| 67 | |
| 68 | std::string |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame] | 69 | bind_cmd::to_string() const |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 70 | { |
| 71 | std::ostringstream s; |
| 72 | s << "Dhcp-config-bind: " << m_hw_item.to_string() |
| 73 | << " itf:" << m_itf.to_string() << " hostname:" << m_hostname; |
| 74 | |
| 75 | return (s.str()); |
| 76 | } |
| 77 | |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame] | 78 | unbind_cmd::unbind_cmd(HW::item<bool>& item, |
| 79 | const handle_t& itf, |
| 80 | const std::string& hostname) |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 81 | : rpc_cmd(item) |
| 82 | , m_itf(itf) |
| 83 | , m_hostname(hostname) |
| 84 | { |
| 85 | } |
| 86 | |
| 87 | bool |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame] | 88 | unbind_cmd::operator==(const unbind_cmd& other) const |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 89 | { |
| 90 | return ((m_itf == other.m_itf) && (m_hostname == other.m_hostname)); |
| 91 | } |
| 92 | |
| 93 | rc_t |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame] | 94 | unbind_cmd::issue(connection& con) |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 95 | { |
| 96 | msg_t req(con.ctx(), std::ref(*this)); |
| 97 | |
| 98 | auto& payload = req.get_request().get_payload(); |
| 99 | payload.sw_if_index = m_itf.value(); |
| 100 | payload.is_add = 0; |
| 101 | payload.pid = getpid(); |
| 102 | payload.want_dhcp_event = 0; |
| 103 | |
| 104 | memcpy(payload.hostname, m_hostname.c_str(), |
| 105 | std::min(sizeof(payload.hostname), m_hostname.length())); |
| 106 | |
| 107 | VAPI_CALL(req.execute()); |
| 108 | |
| 109 | wait(); |
| 110 | m_hw_item.set(rc_t::NOOP); |
| 111 | |
| 112 | return rc_t::OK; |
| 113 | } |
| 114 | |
| 115 | std::string |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame] | 116 | unbind_cmd::to_string() const |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 117 | { |
| 118 | std::ostringstream s; |
| 119 | s << "Dhcp-config-unbind: " << m_hw_item.to_string() |
| 120 | << " itf:" << m_itf.to_string() << " hostname:" << m_hostname; |
| 121 | |
| 122 | return (s.str()); |
| 123 | } |
| 124 | |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame] | 125 | events_cmd::events_cmd(dhcp_config::event_listener& el) |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 126 | : event_cmd(el.status()) |
| 127 | , m_listener(el) |
| 128 | { |
| 129 | } |
| 130 | |
| 131 | bool |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame] | 132 | events_cmd::operator==(const events_cmd& other) const |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 133 | { |
| 134 | return (true); |
| 135 | } |
| 136 | |
| 137 | rc_t |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame] | 138 | events_cmd::issue(connection& con) |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 139 | { |
| 140 | /* |
Neale Ranns | a2ee029 | 2017-11-28 22:29:13 -0800 | [diff] [blame] | 141 | * Set the call back to handle DHCP complete envets. |
| 142 | */ |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 143 | m_reg.reset(new reg_t(con.ctx(), std::ref(*this))); |
| 144 | |
| 145 | /* |
Neale Ranns | a2ee029 | 2017-11-28 22:29:13 -0800 | [diff] [blame] | 146 | * return in-progress so the command stays in the pending list. |
| 147 | */ |
| 148 | return (rc_t::OK); |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | void |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame] | 152 | events_cmd::retire(connection& con) |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 153 | { |
| 154 | } |
| 155 | |
| 156 | void |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame] | 157 | events_cmd::notify() |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 158 | { |
| 159 | m_listener.handle_dhcp_event(this); |
| 160 | } |
| 161 | |
| 162 | std::string |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame] | 163 | events_cmd::to_string() const |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 164 | { |
| 165 | return ("dhcp-events"); |
| 166 | } |
| 167 | } |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame] | 168 | }; |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 169 | /* |
| 170 | * fd.io coding-style-patch-verification: ON |
| 171 | * |
| 172 | * Local Variables: |
| 173 | * eval: (c-set-style "mozilla") |
| 174 | * End: |
| 175 | */ |