blob: a7299267334227d085dc53942e4117f2661ce38b [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 */
23singular_db<interface::key_type, dhcp_config> dhcp_config::m_db;
24
25dhcp_config::event_handler dhcp_config::m_evh;
26
27dhcp_config::dhcp_config(const interface& itf, const std::string& hostname)
28 : m_itf(itf.singular())
29 , m_hostname(hostname)
30 , m_client_id(l2_address_t::ZERO)
31 , m_binding(0)
32{
33}
34
35dhcp_config::dhcp_config(const interface& itf,
36 const std::string& hostname,
37 const l2_address_t& client_id)
38 : m_itf(itf.singular())
39 , m_hostname(hostname)
40 , m_client_id(client_id)
41 , m_binding(0)
42{
43}
44
45dhcp_config::dhcp_config(const dhcp_config& o)
46 : m_itf(o.m_itf)
47 , m_hostname(o.m_hostname)
48 , m_client_id(o.m_client_id)
49 , m_binding(0)
50{
51}
52
53dhcp_config::~dhcp_config()
54{
55 sweep();
56
57 // not in the DB anymore.
58 m_db.release(m_itf->key(), this);
59}
60
61void
62dhcp_config::sweep()
63{
64 if (m_binding) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -070065 HW::enqueue(
66 new dhcp_config_cmds::unbind_cmd(m_binding, m_itf->handle(), m_hostname));
Neale Ranns812ed392017-10-16 04:20:13 -070067 }
68 HW::write();
69}
70
71void
72dhcp_config::dump(std::ostream& os)
73{
74 m_db.dump(os);
75}
76
77void
78dhcp_config::replay()
79{
80 if (m_binding) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -070081 HW::enqueue(new dhcp_config_cmds::bind_cmd(m_binding, m_itf->handle(),
82 m_hostname, m_client_id));
Neale Ranns812ed392017-10-16 04:20:13 -070083 }
84}
85
86std::string
87dhcp_config::to_string() const
88{
89 std::ostringstream s;
90 s << "Dhcp-config: " << m_itf->to_string() << " hostname:" << m_hostname
91 << " client_id:[" << m_client_id << "] " << m_binding.to_string();
92
93 return (s.str());
94}
95
96void
97dhcp_config::update(const dhcp_config& desired)
98{
99 /*
100 * the desired state is always that the interface should be created
101 */
102 if (!m_binding) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700103 HW::enqueue(new dhcp_config_cmds::bind_cmd(m_binding, m_itf->handle(),
104 m_hostname, m_client_id));
Neale Ranns812ed392017-10-16 04:20:13 -0700105 }
106}
107
108std::shared_ptr<dhcp_config>
109dhcp_config::find_or_add(const dhcp_config& temp)
110{
111 return (m_db.find_or_add(temp.m_itf->key(), temp));
112}
113
114std::shared_ptr<dhcp_config>
115dhcp_config::singular() const
116{
117 return find_or_add(*this);
118}
119
120dhcp_config::event_listener::event_listener()
121 : m_status(rc_t::NOOP)
122{
123}
124
125HW::item<bool>&
126dhcp_config::event_listener::status()
127{
128 return (m_status);
129}
130
131dhcp_config::event_handler::event_handler()
132{
133 OM::register_listener(this);
134 inspect::register_handler({ "dhcp" }, "DHCP configurations", this);
135}
136
137void
138dhcp_config::event_handler::handle_replay()
139{
140 m_db.replay();
141}
142
143void
144dhcp_config::event_handler::handle_populate(const client_db::key_t& key)
145{
146 // FIXME
147}
148
149dependency_t
150dhcp_config::event_handler::order() const
151{
152 return (dependency_t::BINDING);
153}
154
155void
156dhcp_config::event_handler::show(std::ostream& os)
157{
158 m_db.dump(os);
159}
160}
161
162/*
163 * fd.io coding-style-patch-verification: ON
164 *
165 * Local Variables:
166 * eval: (c-set-style "mozilla")
167 * End:
168 */