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_CLIENT_H__
17 #define __VOM_DHCP_CLIENT_H__
20 #include "vom/inspect.hpp"
21 #include "vom/interface.hpp"
22 #include "vom/object_base.hpp"
24 #include "vom/prefix.hpp"
25 #include "vom/singular_db.hpp"
28 namespace dhcp_client_cmds {
32 * A representation of DHCP client on an interface
34 class dhcp_client : public object_base
38 * typedef for the DHCP client key type
40 typedef interface::key_t key_t;
42 struct state_t : enum_base<state_t>
44 const static state_t DISCOVER;
45 const static state_t REQUEST;
46 const static state_t BOUND;
48 static const state_t& from_vpp(int i);
52 * Private constructor taking the value and the string name
54 state_t(int v, const std::string& s);
63 lease_t(const state_t& state,
64 std::shared_ptr<interface> itf,
65 const boost::asio::ip::address& router_address,
66 const route::prefix_t& host_prefix,
67 const std::string& hostname,
68 const mac_address_t& mac);
70 std::string to_string() const;
73 std::shared_ptr<interface> itf;
74 boost::asio::ip::address router_address;
75 route::prefix_t host_prefix;
81 * A class that listens to DHCP Events
92 * listener's virtual function invoked when a DHCP event is
95 virtual void handle_dhcp_event(std::shared_ptr<lease_t> e) = 0;
98 * Return the HW::item associated with this command
100 HW::item<bool>& status();
104 * The HW::item associated with this command
106 HW::item<bool> m_status;
110 * Construct a new object matching the desried state
112 dhcp_client(const interface& itf,
113 const std::string& hostname,
114 bool set_broadcast_flag = true,
115 event_listener* ev = nullptr);
118 * Construct a new object matching the desried state
120 dhcp_client(const interface& itf,
121 const std::string& hostname,
122 const l2_address_t& client_id,
123 bool set_broadcast_flag = true,
124 event_listener* ev = nullptr);
129 dhcp_client(const dhcp_client& o);
137 * Comparison operator - for UT
139 bool operator==(const dhcp_client& d) const;
142 * Return the object's key
144 const key_t& key() const;
147 * Return the 'singular' of the DHCP client that matches this object
149 std::shared_ptr<dhcp_client> singular() const;
152 * convert to string format for debug purposes
154 std::string to_string() const;
157 * Dump all DHCP clients into the stream provided
159 static void dump(std::ostream& os);
162 * Find a DHCP client from its key
164 static std::shared_ptr<dhcp_client> find(const key_t& k);
167 * return the current lease data
169 const std::shared_ptr<lease_t> lease() const;
173 * Class definition for listeners to OM events
175 class event_handler : public OM::listener, public inspect::command_handler
179 virtual ~event_handler() = default;
182 * Handle a populate event
184 void handle_populate(const client_db::key_t& key);
187 * Handle a replay event
189 void handle_replay();
192 * Show the object in the Singular DB
194 void show(std::ostream& os);
197 * Get the sortable Id of the listener
199 dependency_t order() const;
203 * event_handler to register with OM
205 static event_handler m_evh;
208 * Enquue commonds to the VPP command Q for the update
210 void update(const dhcp_client& obj);
213 * Find or add DHCP client to the OM
215 static std::shared_ptr<dhcp_client> find_or_add(const dhcp_client& temp);
218 * It's the OM class that calls singular()
223 * It's the singular_db class that calls replay()
225 friend class singular_db<key_t, dhcp_client>;
228 * Sweep/reap the object if still stale
233 * replay the object to create it in hardware
237 void lease(std::shared_ptr<lease_t> l);
240 * A reference counting pointer to the interface on which DHCP client
241 * resides. By holding the reference here, we can guarantee that
242 * this object will outlive the interface
244 const std::shared_ptr<interface> m_itf;
247 * The hostname in the DHCP client
249 const std::string m_hostname;
252 * The option-61 client_id in the DHCP client
254 const l2_address_t m_client_id;
257 * Flag to control the setting the of DHCP discover's broadcast flag
259 const bool m_set_broadcast_flag;
262 * HW configuration for the binding. The bool representing the
265 HW::item<bool> m_binding;
268 * A pointer to an event listener for client events
270 event_listener* m_evl;
273 * Current lease state for this client
275 std::shared_ptr<lease_t> m_lease;
277 std::shared_ptr<dhcp_client_cmds::events_cmd> m_event_cmd;
279 void handle_dhcp_event(std::shared_ptr<lease_t> e);
282 * A map of all Dhcp clients keyed against the interface.
284 static singular_db<key_t, dhcp_client> m_db;
286 static std::weak_ptr<dhcp_client_cmds::events_cmd> m_s_event_cmd;
287 static std::shared_ptr<dhcp_client_cmds::events_cmd> get_event_cmd();
289 class dhcp_client_listener : public event_listener
293 * listener's virtual function invoked when a DHCP event is
296 void handle_dhcp_event(std::shared_ptr<lease_t> e);
298 static dhcp_client_listener m_listener;
303 * fd.io coding-style-patch-verification: ON
306 * eval: (c-set-style "mozilla")