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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #ifndef __VOM_DHCP_INTERFACE_H__
17 #define __VOM_DHCP_INTERFACE_H__
19 #include "vom/dump_cmd.hpp"
21 #include "vom/inspect.hpp"
22 #include "vom/interface.hpp"
23 #include "vom/object_base.hpp"
25 #include "vom/rpc_cmd.hpp"
26 #include "vom/singular_db.hpp"
27 #include "vom/sub_interface.hpp"
29 #include <vapi/dhcp.api.vapi.hpp>
33 * A representation of DHCP client configuration on an interface
35 class dhcp_config : public object_base
39 * Construct a new object matching the desried state
41 dhcp_config(const interface& itf, const std::string& hostname);
44 * Construct a new object matching the desried state
46 dhcp_config(const interface& itf,
47 const std::string& hostname,
48 const l2_address_t& client_id);
53 dhcp_config(const dhcp_config& o);
61 * Return the 'singular' of the DHCP config that matches this object
63 std::shared_ptr<dhcp_config> singular() const;
66 * convert to string format for debug purposes
68 std::string to_string() const;
71 * Dump all DHCP configs into the stream provided
73 static void dump(std::ostream& os);
76 * A command class that binds the DHCP config to the interface
79 : public rpc_cmd<HW::item<bool>, rc_t, vapi::Dhcp_client_config>
85 bind_cmd(HW::item<bool>& item,
87 const std::string& hostname,
88 const l2_address_t& client_id);
91 * Issue the command to VPP/HW
93 rc_t issue(connection& con);
95 * convert to string format for debug purposes
97 std::string to_string() const;
100 * Comparison operator - only used for UT
102 bool operator==(const bind_cmd& i) const;
106 * Reference to the HW::item of the interface to bind
108 const handle_t& m_itf;
111 * The DHCP client's hostname
113 const std::string m_hostname;
116 * The DHCP client's ID
118 const l2_address_t m_client_id;
122 * A cmd class that Unbinds Dhcp Config from an interface
125 : public rpc_cmd<HW::item<bool>, rc_t, vapi::Dhcp_client_config>
131 unbind_cmd(HW::item<bool>& item,
133 const std::string& hostname);
136 * Issue the command to VPP/HW
138 rc_t issue(connection& con);
140 * convert to string format for debug purposes
142 std::string to_string() const;
145 * Comparison operator - only used for UT
147 bool operator==(const unbind_cmd& i) const;
151 * Reference to the HW::item of the interface to unbind
153 const handle_t& m_itf;
156 * The DHCP client's hostname
158 const std::string m_hostname;
162 * Forward declartion of the Event Command
167 * A class that listens to DHCP Events
178 * listener's virtual function invoked when a DHCP event is
181 virtual void handle_dhcp_event(events_cmd* cmd) = 0;
184 * Return the HW::item associated with this command
186 HW::item<bool>& status();
190 * The HW::item associated with this command
192 HW::item<bool> m_status;
196 * A functor class represents our desire to recieve interface events
199 : public event_cmd<vapi::Control_ping, vapi::Dhcp_compl_event>
205 events_cmd(event_listener& el);
208 * Issue the command to VPP/HW - subscribe to DHCP events
210 rc_t issue(connection& con);
213 * Retire the command - unsubscribe
215 void retire(connection& con);
217 * convert to string format for debug purposes
219 std::string to_string() const;
222 * Comparison operator - only used for UT
224 bool operator==(const events_cmd& i) const;
227 * called in the VAPI RX thread when data is available.
234 * The listner of this command
236 event_listener& m_listener;
241 * Class definition for listeners to OM events
243 class event_handler : public OM::listener, public inspect::command_handler
247 virtual ~event_handler() = default;
250 * Handle a populate event
252 void handle_populate(const client_db::key_t& key);
255 * Handle a replay event
257 void handle_replay();
260 * Show the object in the Singular DB
262 void show(std::ostream& os);
265 * Get the sortable Id of the listener
267 dependency_t order() const;
271 * event_handler to register with OM
273 static event_handler m_evh;
276 * Enquue commonds to the VPP command Q for the update
278 void update(const dhcp_config& obj);
281 * Find or add DHCP config to the OM
283 static std::shared_ptr<dhcp_config> find_or_add(const dhcp_config& temp);
286 * It's the OM class that calls singular()
291 * It's the singular_db class that calls replay()
293 friend class singular_db<interface::key_type, dhcp_config>;
296 * Sweep/reap the object if still stale
301 * replay the object to create it in hardware
306 * A reference counting pointer to the interface on which DHCP config
307 * resides. By holding the reference here, we can guarantee that
308 * this object will outlive the interface
310 const std::shared_ptr<interface> m_itf;
313 * The hostname in the DHCP configuration
315 const std::string m_hostname;
318 * The option-61 client_id in the DHCP configuration
320 const l2_address_t m_client_id;
323 * HW configuration for the binding. The bool representing the
326 HW::item<bool> m_binding;
329 * A map of all Dhcp configs keyed against the interface.
331 static singular_db<interface::key_type, dhcp_config> m_db;
336 * fd.io coding-style-patch-verification: ON
339 * eval: (c-set-style "mozilla")