VPP Object Model (VOM)
[vpp.git] / src / vpp-api / vom / dhcp_config.cpp
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
16 #include "vom/dhcp_config.hpp"
17 #include "vom/cmd.hpp"
18
19 namespace VOM {
20 /**
21  * A DB of all DHCP configs
22  */
23 singular_db<interface::key_type, dhcp_config> dhcp_config::m_db;
24
25 dhcp_config::event_handler dhcp_config::m_evh;
26
27 dhcp_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
35 dhcp_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
45 dhcp_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
53 dhcp_config::~dhcp_config()
54 {
55   sweep();
56
57   // not in the DB anymore.
58   m_db.release(m_itf->key(), this);
59 }
60
61 void
62 dhcp_config::sweep()
63 {
64   if (m_binding) {
65     HW::enqueue(new unbind_cmd(m_binding, m_itf->handle(), m_hostname));
66   }
67   HW::write();
68 }
69
70 void
71 dhcp_config::dump(std::ostream& os)
72 {
73   m_db.dump(os);
74 }
75
76 void
77 dhcp_config::replay()
78 {
79   if (m_binding) {
80     HW::enqueue(
81       new bind_cmd(m_binding, m_itf->handle(), m_hostname, m_client_id));
82   }
83 }
84
85 std::string
86 dhcp_config::to_string() const
87 {
88   std::ostringstream s;
89   s << "Dhcp-config: " << m_itf->to_string() << " hostname:" << m_hostname
90     << " client_id:[" << m_client_id << "] " << m_binding.to_string();
91
92   return (s.str());
93 }
94
95 void
96 dhcp_config::update(const dhcp_config& desired)
97 {
98   /*
99  * the desired state is always that the interface should be created
100  */
101   if (!m_binding) {
102     HW::enqueue(
103       new bind_cmd(m_binding, m_itf->handle(), m_hostname, m_client_id));
104   }
105 }
106
107 std::shared_ptr<dhcp_config>
108 dhcp_config::find_or_add(const dhcp_config& temp)
109 {
110   return (m_db.find_or_add(temp.m_itf->key(), temp));
111 }
112
113 std::shared_ptr<dhcp_config>
114 dhcp_config::singular() const
115 {
116   return find_or_add(*this);
117 }
118
119 dhcp_config::event_listener::event_listener()
120   : m_status(rc_t::NOOP)
121 {
122 }
123
124 HW::item<bool>&
125 dhcp_config::event_listener::status()
126 {
127   return (m_status);
128 }
129
130 dhcp_config::event_handler::event_handler()
131 {
132   OM::register_listener(this);
133   inspect::register_handler({ "dhcp" }, "DHCP configurations", this);
134 }
135
136 void
137 dhcp_config::event_handler::handle_replay()
138 {
139   m_db.replay();
140 }
141
142 void
143 dhcp_config::event_handler::handle_populate(const client_db::key_t& key)
144 {
145   // FIXME
146 }
147
148 dependency_t
149 dhcp_config::event_handler::order() const
150 {
151   return (dependency_t::BINDING);
152 }
153
154 void
155 dhcp_config::event_handler::show(std::ostream& os)
156 {
157   m_db.dump(os);
158 }
159 }
160
161 /*
162  * fd.io coding-style-patch-verification: ON
163  *
164  * Local Variables:
165  * eval: (c-set-style "mozilla")
166  * End:
167  */