blob: 8ea608d809d45884679f7d7bb113731871c8cbc6 [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#ifndef __VOM_DHCP_CONFIG_H__
17#define __VOM_DHCP_CONFIG_H__
Neale Ranns812ed392017-10-16 04:20:13 -070018
Neale Ranns812ed392017-10-16 04:20:13 -070019#include "vom/hw.hpp"
20#include "vom/inspect.hpp"
21#include "vom/interface.hpp"
22#include "vom/object_base.hpp"
23#include "vom/om.hpp"
Neale Ranns812ed392017-10-16 04:20:13 -070024#include "vom/singular_db.hpp"
Neale Ranns812ed392017-10-16 04:20:13 -070025
26namespace VOM {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -070027namespace dhcp_config_cmds {
28class events_cmd;
29};
Neale Ranns812ed392017-10-16 04:20:13 -070030/**
31 * A representation of DHCP client configuration on an interface
32 */
33class dhcp_config : public object_base
34{
35public:
36 /**
Neale Rannsfd920602017-11-23 12:15:00 -080037 * typedef for the DHCP config key type
38 */
39 typedef interface::key_t key_t;
40
41 /**
Neale Ranns812ed392017-10-16 04:20:13 -070042 * Construct a new object matching the desried state
43 */
Neale Ranns54c6dc42018-01-17 10:29:10 -080044 dhcp_config(const interface& itf,
45 const std::string& hostname,
46 bool set_broadcast_flag = true);
Neale Ranns812ed392017-10-16 04:20:13 -070047
48 /**
49 * Construct a new object matching the desried state
50 */
51 dhcp_config(const interface& itf,
52 const std::string& hostname,
Neale Ranns54c6dc42018-01-17 10:29:10 -080053 const l2_address_t& client_id,
54 bool set_broadcast_flag = true);
Neale Ranns812ed392017-10-16 04:20:13 -070055
56 /**
57 * Copy Constructor
58 */
59 dhcp_config(const dhcp_config& o);
60
61 /**
62 * Destructor
63 */
64 ~dhcp_config();
65
66 /**
Neale Rannsfd920602017-11-23 12:15:00 -080067 * Comparison operator - for UT
68 */
69 bool operator==(const dhcp_config& d) const;
70
71 /**
72 * Return the object's key
73 */
74 const key_t& key() const;
75
76 /**
Neale Ranns812ed392017-10-16 04:20:13 -070077 * Return the 'singular' of the DHCP config that matches this object
78 */
79 std::shared_ptr<dhcp_config> singular() const;
80
81 /**
82 * convert to string format for debug purposes
83 */
84 std::string to_string() const;
85
86 /**
87 * Dump all DHCP configs into the stream provided
88 */
89 static void dump(std::ostream& os);
90
91 /**
Neale Rannsfd920602017-11-23 12:15:00 -080092 * Find a DHCP config from its key
Neale Ranns812ed392017-10-16 04:20:13 -070093 */
Neale Rannsfd920602017-11-23 12:15:00 -080094 static std::shared_ptr<dhcp_config> find(const key_t& k);
95
96 /**
97 * A class that listens to DHCP Events
98 */
Neale Ranns812ed392017-10-16 04:20:13 -070099 class event_listener
100 {
101 public:
102 /**
103 * Constructor
104 */
105 event_listener();
106
107 /**
108 * listener's virtual function invoked when a DHCP event is
109 * available to read
110 */
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700111 virtual void handle_dhcp_event(dhcp_config_cmds::events_cmd* cmd) = 0;
Neale Ranns812ed392017-10-16 04:20:13 -0700112
113 /**
114 * Return the HW::item associated with this command
115 */
116 HW::item<bool>& status();
117
118 protected:
119 /**
120 * The HW::item associated with this command
121 */
122 HW::item<bool> m_status;
123 };
124
Neale Ranns812ed392017-10-16 04:20:13 -0700125private:
126 /**
127 * Class definition for listeners to OM events
128 */
129 class event_handler : public OM::listener, public inspect::command_handler
130 {
131 public:
132 event_handler();
133 virtual ~event_handler() = default;
134
135 /**
136 * Handle a populate event
137 */
138 void handle_populate(const client_db::key_t& key);
139
140 /**
141 * Handle a replay event
142 */
143 void handle_replay();
144
145 /**
146 * Show the object in the Singular DB
147 */
148 void show(std::ostream& os);
149
150 /**
151 * Get the sortable Id of the listener
152 */
153 dependency_t order() const;
154 };
155
156 /**
157 * event_handler to register with OM
158 */
159 static event_handler m_evh;
160
161 /**
162 * Enquue commonds to the VPP command Q for the update
163 */
164 void update(const dhcp_config& obj);
165
166 /**
167 * Find or add DHCP config to the OM
168 */
169 static std::shared_ptr<dhcp_config> find_or_add(const dhcp_config& temp);
170
171 /*
172 * It's the OM class that calls singular()
173 */
174 friend class OM;
175
176 /**
177 * It's the singular_db class that calls replay()
178 */
Neale Rannsfd920602017-11-23 12:15:00 -0800179 friend class singular_db<key_t, dhcp_config>;
Neale Ranns812ed392017-10-16 04:20:13 -0700180
181 /**
182 * Sweep/reap the object if still stale
183 */
184 void sweep(void);
185
186 /**
187 * replay the object to create it in hardware
188 */
189 void replay(void);
190
191 /**
192 * A reference counting pointer to the interface on which DHCP config
193 * resides. By holding the reference here, we can guarantee that
194 * this object will outlive the interface
195 */
196 const std::shared_ptr<interface> m_itf;
197
198 /**
199 * The hostname in the DHCP configuration
200 */
201 const std::string m_hostname;
202
203 /**
204 * The option-61 client_id in the DHCP configuration
205 */
206 const l2_address_t m_client_id;
207
208 /**
Neale Ranns54c6dc42018-01-17 10:29:10 -0800209 * Flag to control the setting the of DHCP discover's broadcast flag
210 */
211 const bool m_set_broadcast_flag;
212
213 /**
Neale Ranns812ed392017-10-16 04:20:13 -0700214 * HW configuration for the binding. The bool representing the
215 * do/don't bind.
216 */
217 HW::item<bool> m_binding;
218
219 /**
220 * A map of all Dhcp configs keyed against the interface.
221 */
Neale Rannsfd920602017-11-23 12:15:00 -0800222 static singular_db<key_t, dhcp_config> m_db;
Neale Ranns812ed392017-10-16 04:20:13 -0700223};
224};
225
226/*
227 * fd.io coding-style-patch-verification: ON
228 *
229 * Local Variables:
230 * eval: (c-set-style "mozilla")
231 * End:
232 */
233
234#endif