blob: 8071fb15c5753e8614670e7027557d801c04b151 [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
16#include "vom/dhcp_config.hpp"
Neale Ranns9ef1c0a2017-11-03 04:39:05 -070017#include "vom/dhcp_config_cmds.hpp"
Neale Ranns812ed392017-10-16 04:20:13 -070018
19namespace VOM {
20/**
21 * A DB of all DHCP configs
22 */
Neale Rannsfd920602017-11-23 12:15:00 -080023singular_db<interface::key_t, dhcp_config> dhcp_config::m_db;
Neale Ranns812ed392017-10-16 04:20:13 -070024
25dhcp_config::event_handler dhcp_config::m_evh;
26
Neale Ranns54c6dc42018-01-17 10:29:10 -080027dhcp_config::dhcp_config(const interface& itf,
28 const std::string& hostname,
29 bool set_broadcast_flag)
Neale Ranns812ed392017-10-16 04:20:13 -070030 : m_itf(itf.singular())
31 , m_hostname(hostname)
32 , m_client_id(l2_address_t::ZERO)
Neale Ranns54c6dc42018-01-17 10:29:10 -080033 , m_set_broadcast_flag(set_broadcast_flag)
Neale Ranns812ed392017-10-16 04:20:13 -070034 , m_binding(0)
35{
36}
37
38dhcp_config::dhcp_config(const interface& itf,
39 const std::string& hostname,
Neale Ranns54c6dc42018-01-17 10:29:10 -080040 const l2_address_t& client_id,
41 bool set_broadcast_flag)
Neale Ranns812ed392017-10-16 04:20:13 -070042 : m_itf(itf.singular())
43 , m_hostname(hostname)
44 , m_client_id(client_id)
Neale Ranns54c6dc42018-01-17 10:29:10 -080045 , m_set_broadcast_flag(set_broadcast_flag)
Neale Ranns812ed392017-10-16 04:20:13 -070046 , m_binding(0)
47{
48}
49
50dhcp_config::dhcp_config(const dhcp_config& o)
51 : m_itf(o.m_itf)
52 , m_hostname(o.m_hostname)
53 , m_client_id(o.m_client_id)
Neale Ranns54c6dc42018-01-17 10:29:10 -080054 , m_set_broadcast_flag(o.m_set_broadcast_flag)
Neale Ranns812ed392017-10-16 04:20:13 -070055 , m_binding(0)
56{
57}
58
59dhcp_config::~dhcp_config()
60{
61 sweep();
62
63 // not in the DB anymore.
64 m_db.release(m_itf->key(), this);
65}
66
Neale Rannsfd920602017-11-23 12:15:00 -080067bool
68dhcp_config::operator==(const dhcp_config& l) const
69{
70 return ((key() == l.key()) && (m_hostname == l.m_hostname) &&
71 (m_client_id == l.m_client_id));
72}
73
74const dhcp_config::key_t&
75dhcp_config::key() const
76{
77 return (m_itf->key());
78}
79
Neale Ranns812ed392017-10-16 04:20:13 -070080void
81dhcp_config::sweep()
82{
83 if (m_binding) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -070084 HW::enqueue(
85 new dhcp_config_cmds::unbind_cmd(m_binding, m_itf->handle(), m_hostname));
Neale Ranns812ed392017-10-16 04:20:13 -070086 }
87 HW::write();
88}
89
90void
91dhcp_config::dump(std::ostream& os)
92{
93 m_db.dump(os);
94}
95
96void
97dhcp_config::replay()
98{
99 if (m_binding) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700100 HW::enqueue(new dhcp_config_cmds::bind_cmd(m_binding, m_itf->handle(),
101 m_hostname, m_client_id));
Neale Ranns812ed392017-10-16 04:20:13 -0700102 }
103}
104
105std::string
106dhcp_config::to_string() const
107{
108 std::ostringstream s;
109 s << "Dhcp-config: " << m_itf->to_string() << " hostname:" << m_hostname
110 << " client_id:[" << m_client_id << "] " << m_binding.to_string();
111
112 return (s.str());
113}
114
115void
116dhcp_config::update(const dhcp_config& desired)
117{
118 /*
119 * the desired state is always that the interface should be created
120 */
121 if (!m_binding) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700122 HW::enqueue(new dhcp_config_cmds::bind_cmd(m_binding, m_itf->handle(),
123 m_hostname, m_client_id));
Neale Ranns812ed392017-10-16 04:20:13 -0700124 }
125}
126
127std::shared_ptr<dhcp_config>
128dhcp_config::find_or_add(const dhcp_config& temp)
129{
130 return (m_db.find_or_add(temp.m_itf->key(), temp));
131}
132
133std::shared_ptr<dhcp_config>
Neale Rannsfd920602017-11-23 12:15:00 -0800134dhcp_config::find(const key_t& k)
135{
136 return (m_db.find(k));
137}
138
139std::shared_ptr<dhcp_config>
Neale Ranns812ed392017-10-16 04:20:13 -0700140dhcp_config::singular() const
141{
142 return find_or_add(*this);
143}
144
145dhcp_config::event_listener::event_listener()
146 : m_status(rc_t::NOOP)
147{
148}
149
150HW::item<bool>&
151dhcp_config::event_listener::status()
152{
153 return (m_status);
154}
155
156dhcp_config::event_handler::event_handler()
157{
158 OM::register_listener(this);
159 inspect::register_handler({ "dhcp" }, "DHCP configurations", this);
160}
161
162void
163dhcp_config::event_handler::handle_replay()
164{
165 m_db.replay();
166}
167
168void
169dhcp_config::event_handler::handle_populate(const client_db::key_t& key)
170{
171 // FIXME
172}
173
174dependency_t
175dhcp_config::event_handler::order() const
176{
177 return (dependency_t::BINDING);
178}
179
180void
181dhcp_config::event_handler::show(std::ostream& os)
182{
183 m_db.dump(os);
184}
185}
186
187/*
188 * fd.io coding-style-patch-verification: ON
189 *
190 * Local Variables:
191 * eval: (c-set-style "mozilla")
192 * End:
193 */