VPP Object Model (VOM)
[vpp.git] / src / vpp-api / vom / dhcp_config_cmds.cpp
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
16 #include "vom/dhcp_config.hpp"
17
18 DEFINE_VAPI_MSG_IDS_DHCP_API_JSON;
19
20 namespace VOM {
21 dhcp_config::bind_cmd::bind_cmd(HW::item<bool>& item,
22                                 const handle_t& itf,
23                                 const std::string& hostname,
24                                 const l2_address_t& client_id)
25   : rpc_cmd(item)
26   , m_itf(itf)
27   , m_hostname(hostname)
28   , m_client_id(client_id)
29 {
30 }
31
32 bool
33 dhcp_config::bind_cmd::operator==(const bind_cmd& other) const
34 {
35   return ((m_itf == other.m_itf) && (m_hostname == other.m_hostname));
36 }
37
38 rc_t
39 dhcp_config::bind_cmd::issue(connection& con)
40 {
41   msg_t req(con.ctx(), std::ref(*this));
42
43   auto& payload = req.get_request().get_payload();
44   payload.sw_if_index = m_itf.value();
45   payload.is_add = 1;
46   payload.pid = getpid();
47   payload.want_dhcp_event = 1;
48
49   memcpy(payload.hostname, m_hostname.c_str(),
50          std::min(sizeof(payload.hostname), m_hostname.length()));
51
52   memset(payload.client_id, 0, sizeof(payload.client_id));
53   payload.client_id[0] = 1;
54   std::copy_n(begin(m_client_id.bytes),
55               std::min(sizeof(payload.client_id), m_client_id.bytes.size()),
56               payload.client_id + 1);
57
58   VAPI_CALL(req.execute());
59
60   m_hw_item.set(wait());
61
62   return rc_t::OK;
63 }
64
65 std::string
66 dhcp_config::bind_cmd::to_string() const
67 {
68   std::ostringstream s;
69   s << "Dhcp-config-bind: " << m_hw_item.to_string()
70     << " itf:" << m_itf.to_string() << " hostname:" << m_hostname;
71
72   return (s.str());
73 }
74
75 dhcp_config::unbind_cmd::unbind_cmd(HW::item<bool>& item,
76                                     const handle_t& itf,
77                                     const std::string& hostname)
78   : rpc_cmd(item)
79   , m_itf(itf)
80   , m_hostname(hostname)
81 {
82 }
83
84 bool
85 dhcp_config::unbind_cmd::operator==(const unbind_cmd& other) const
86 {
87   return ((m_itf == other.m_itf) && (m_hostname == other.m_hostname));
88 }
89
90 rc_t
91 dhcp_config::unbind_cmd::issue(connection& con)
92 {
93   msg_t req(con.ctx(), std::ref(*this));
94
95   auto& payload = req.get_request().get_payload();
96   payload.sw_if_index = m_itf.value();
97   payload.is_add = 0;
98   payload.pid = getpid();
99   payload.want_dhcp_event = 0;
100
101   memcpy(payload.hostname, m_hostname.c_str(),
102          std::min(sizeof(payload.hostname), m_hostname.length()));
103
104   VAPI_CALL(req.execute());
105
106   wait();
107   m_hw_item.set(rc_t::NOOP);
108
109   return rc_t::OK;
110 }
111
112 std::string
113 dhcp_config::unbind_cmd::to_string() const
114 {
115   std::ostringstream s;
116   s << "Dhcp-config-unbind: " << m_hw_item.to_string()
117     << " itf:" << m_itf.to_string() << " hostname:" << m_hostname;
118
119   return (s.str());
120 }
121
122 dhcp_config::events_cmd::events_cmd(event_listener& el)
123   : event_cmd(el.status())
124   , m_listener(el)
125 {
126 }
127
128 bool
129 dhcp_config::events_cmd::operator==(const events_cmd& other) const
130 {
131   return (true);
132 }
133
134 rc_t
135 dhcp_config::events_cmd::issue(connection& con)
136 {
137   /*
138  * Set the call back to handle DHCP complete envets.
139  */
140   m_reg.reset(new reg_t(con.ctx(), std::ref(*this)));
141
142   /*
143  * return in-progress so the command stays in the pending list.
144  */
145   return (rc_t::INPROGRESS);
146 }
147
148 void
149 dhcp_config::events_cmd::retire(connection& con)
150 {
151 }
152
153 void
154 dhcp_config::events_cmd::notify()
155 {
156   m_listener.handle_dhcp_event(this);
157 }
158
159 std::string
160 dhcp_config::events_cmd::to_string() const
161 {
162   return ("dhcp-events");
163 }
164 }
165
166 /*
167  * fd.io coding-style-patch-verification: ON
168  *
169  * Local Variables:
170  * eval: (c-set-style "mozilla")
171  * End:
172  */