blob: 8e81a27f96e6bfdfdb2b8a5019e1ed9366ba4b03 [file] [log] [blame]
Neale Ranns812ed392017-10-16 04:20:13 -07001/*
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 Ranns9ef1c0a2017-11-03 04:39:05 -070016#include "vom/dhcp_config_cmds.hpp"
Neale Ranns812ed392017-10-16 04:20:13 -070017
18DEFINE_VAPI_MSG_IDS_DHCP_API_JSON;
19
20namespace VOM {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -070021namespace dhcp_config_cmds {
22
23bind_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 Ranns812ed392017-10-16 04:20:13 -070027 : rpc_cmd(item)
28 , m_itf(itf)
29 , m_hostname(hostname)
30 , m_client_id(client_id)
31{
32}
33
34bool
Neale Ranns9ef1c0a2017-11-03 04:39:05 -070035bind_cmd::operator==(const bind_cmd& other) const
Neale Ranns812ed392017-10-16 04:20:13 -070036{
37 return ((m_itf == other.m_itf) && (m_hostname == other.m_hostname));
38}
39
40rc_t
Neale Ranns9ef1c0a2017-11-03 04:39:05 -070041bind_cmd::issue(connection& con)
Neale Ranns812ed392017-10-16 04:20:13 -070042{
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
51 memcpy(payload.hostname, m_hostname.c_str(),
52 std::min(sizeof(payload.hostname), m_hostname.length()));
53
54 memset(payload.client_id, 0, sizeof(payload.client_id));
55 payload.client_id[0] = 1;
56 std::copy_n(begin(m_client_id.bytes),
57 std::min(sizeof(payload.client_id), m_client_id.bytes.size()),
58 payload.client_id + 1);
59
60 VAPI_CALL(req.execute());
61
62 m_hw_item.set(wait());
63
64 return rc_t::OK;
65}
66
67std::string
Neale Ranns9ef1c0a2017-11-03 04:39:05 -070068bind_cmd::to_string() const
Neale Ranns812ed392017-10-16 04:20:13 -070069{
70 std::ostringstream s;
71 s << "Dhcp-config-bind: " << m_hw_item.to_string()
72 << " itf:" << m_itf.to_string() << " hostname:" << m_hostname;
73
74 return (s.str());
75}
76
Neale Ranns9ef1c0a2017-11-03 04:39:05 -070077unbind_cmd::unbind_cmd(HW::item<bool>& item,
78 const handle_t& itf,
79 const std::string& hostname)
Neale Ranns812ed392017-10-16 04:20:13 -070080 : rpc_cmd(item)
81 , m_itf(itf)
82 , m_hostname(hostname)
83{
84}
85
86bool
Neale Ranns9ef1c0a2017-11-03 04:39:05 -070087unbind_cmd::operator==(const unbind_cmd& other) const
Neale Ranns812ed392017-10-16 04:20:13 -070088{
89 return ((m_itf == other.m_itf) && (m_hostname == other.m_hostname));
90}
91
92rc_t
Neale Ranns9ef1c0a2017-11-03 04:39:05 -070093unbind_cmd::issue(connection& con)
Neale Ranns812ed392017-10-16 04:20:13 -070094{
95 msg_t req(con.ctx(), std::ref(*this));
96
97 auto& payload = req.get_request().get_payload();
98 payload.sw_if_index = m_itf.value();
99 payload.is_add = 0;
100 payload.pid = getpid();
101 payload.want_dhcp_event = 0;
102
103 memcpy(payload.hostname, m_hostname.c_str(),
104 std::min(sizeof(payload.hostname), m_hostname.length()));
105
106 VAPI_CALL(req.execute());
107
108 wait();
109 m_hw_item.set(rc_t::NOOP);
110
111 return rc_t::OK;
112}
113
114std::string
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700115unbind_cmd::to_string() const
Neale Ranns812ed392017-10-16 04:20:13 -0700116{
117 std::ostringstream s;
118 s << "Dhcp-config-unbind: " << m_hw_item.to_string()
119 << " itf:" << m_itf.to_string() << " hostname:" << m_hostname;
120
121 return (s.str());
122}
123
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700124events_cmd::events_cmd(dhcp_config::event_listener& el)
Neale Ranns812ed392017-10-16 04:20:13 -0700125 : event_cmd(el.status())
126 , m_listener(el)
127{
128}
129
130bool
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700131events_cmd::operator==(const events_cmd& other) const
Neale Ranns812ed392017-10-16 04:20:13 -0700132{
133 return (true);
134}
135
136rc_t
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700137events_cmd::issue(connection& con)
Neale Ranns812ed392017-10-16 04:20:13 -0700138{
139 /*
140 * Set the call back to handle DHCP complete envets.
141 */
142 m_reg.reset(new reg_t(con.ctx(), std::ref(*this)));
143
144 /*
145 * return in-progress so the command stays in the pending list.
146 */
147 return (rc_t::INPROGRESS);
148}
149
150void
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700151events_cmd::retire(connection& con)
Neale Ranns812ed392017-10-16 04:20:13 -0700152{
153}
154
155void
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700156events_cmd::notify()
Neale Ranns812ed392017-10-16 04:20:13 -0700157{
158 m_listener.handle_dhcp_event(this);
159}
160
161std::string
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700162events_cmd::to_string() const
Neale Ranns812ed392017-10-16 04:20:13 -0700163{
164 return ("dhcp-events");
165}
166}
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700167};
Neale Ranns812ed392017-10-16 04:20:13 -0700168/*
169 * fd.io coding-style-patch-verification: ON
170 *
171 * Local Variables:
172 * eval: (c-set-style "mozilla")
173 * End:
174 */