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