blob: be7c9d1e24bdfb97d2eafef23141c0baf1f3e05e [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/l3_binding.hpp"
Neale Ranns9ef1c0a2017-11-03 04:39:05 -070017#include "vom/l3_binding_cmds.hpp"
Neale Ranns812ed392017-10-16 04:20:13 -070018
19namespace VOM {
Neale Rannsfd920602017-11-23 12:15:00 -080020singular_db<l3_binding::key_t, l3_binding> l3_binding::m_db;
Neale Ranns812ed392017-10-16 04:20:13 -070021
22l3_binding::event_handler l3_binding::m_evh;
23
24/**
25 * Construct a new object matching the desried state
26 */
27l3_binding::l3_binding(const interface& itf, const route::prefix_t& pfx)
28 : m_itf(itf.singular())
29 , m_pfx(pfx)
Neale Ranns8ac4ce82017-11-17 05:08:55 -080030 , m_binding(true, rc_t::NOOP)
Neale Ranns812ed392017-10-16 04:20:13 -070031{
32}
33
34l3_binding::l3_binding(const l3_binding& o)
35 : m_itf(o.m_itf)
36 , m_pfx(o.m_pfx)
Neale Ranns8ac4ce82017-11-17 05:08:55 -080037 , m_binding(o.m_binding)
Neale Ranns812ed392017-10-16 04:20:13 -070038{
39}
40
41l3_binding::~l3_binding()
42{
43 sweep();
44
45 // not in the DB anymore.
Neale Rannsfd920602017-11-23 12:15:00 -080046 m_db.release(key(), this);
47}
48
49bool
50l3_binding::operator==(const l3_binding& l) const
51{
52 return ((m_pfx == l.m_pfx) && (*m_itf == *l.m_itf));
53}
54
55const l3_binding::key_t
56l3_binding::key() const
57{
58 return (make_pair(m_itf->key(), m_pfx));
Neale Ranns812ed392017-10-16 04:20:13 -070059}
60
61void
62l3_binding::sweep()
63{
64 if (m_binding) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -070065 HW::enqueue(
66 new l3_binding_cmds::unbind_cmd(m_binding, m_itf->handle(), m_pfx));
Neale Ranns812ed392017-10-16 04:20:13 -070067 }
68 HW::write();
69}
70
71void
72l3_binding::replay()
73{
74 if (m_binding) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -070075 HW::enqueue(
76 new l3_binding_cmds::bind_cmd(m_binding, m_itf->handle(), m_pfx));
Neale Ranns812ed392017-10-16 04:20:13 -070077 }
78}
79
80const route::prefix_t&
81l3_binding::prefix() const
82{
83 return (m_pfx);
84}
85
Neale Ranns352ea0c2017-11-14 11:04:28 -080086const interface&
87l3_binding::itf() const
88{
89 return (*m_itf);
90}
91
92l3_binding::const_iterator_t
93l3_binding::cbegin()
94{
95 return m_db.cbegin();
96}
97
98l3_binding::const_iterator_t
99l3_binding::cend()
100{
101 return m_db.cend();
102}
103
Neale Ranns812ed392017-10-16 04:20:13 -0700104std::string
105l3_binding::to_string() const
106{
107 std::ostringstream s;
Neale Rannsfd920602017-11-23 12:15:00 -0800108 s << "L3-binding:[" << m_itf->to_string() << " prefix:" << m_pfx.to_string()
Neale Ranns812ed392017-10-16 04:20:13 -0700109 << " " << m_binding.to_string() << "]";
110
111 return (s.str());
112}
113
114void
115l3_binding::update(const l3_binding& desired)
116{
117 /*
Neale Ranns352ea0c2017-11-14 11:04:28 -0800118 * no updates for the binding. chaning the interface or the prefix is a change
119 * to the
120 * key, hence a new object
121 */
Neale Ranns812ed392017-10-16 04:20:13 -0700122 if (!m_binding) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700123 HW::enqueue(
124 new l3_binding_cmds::bind_cmd(m_binding, m_itf->handle(), m_pfx));
Neale Ranns812ed392017-10-16 04:20:13 -0700125 }
126}
127
128std::shared_ptr<l3_binding>
129l3_binding::find_or_add(const l3_binding& temp)
130{
Neale Rannsfd920602017-11-23 12:15:00 -0800131 return (m_db.find_or_add(temp.key(), temp));
132}
133
134std::shared_ptr<l3_binding>
135l3_binding::find(const key_t& k)
136{
137 return (m_db.find(k));
Neale Ranns812ed392017-10-16 04:20:13 -0700138}
139
140std::shared_ptr<l3_binding>
141l3_binding::singular() const
142{
143 return find_or_add(*this);
144}
145
146void
147l3_binding::dump(std::ostream& os)
148{
149 m_db.dump(os);
150}
151
152std::ostream&
Neale Rannsfd920602017-11-23 12:15:00 -0800153operator<<(std::ostream& os, const l3_binding::key_t& key)
Neale Ranns812ed392017-10-16 04:20:13 -0700154{
155 os << "[" << key.first << ", " << key.second << "]";
156
157 return (os);
158}
159
160std::deque<std::shared_ptr<l3_binding>>
161l3_binding::find(const interface& i)
162{
163 /*
164 * Loop throught the entire map looking for matching interface.
165 * not the most efficient algorithm, but it will do for now. The
166 * number of L3 configs is low and this is only called during bootup
167 */
168 std::deque<std::shared_ptr<l3_binding>> l3s;
169
170 auto it = m_db.cbegin();
171
172 while (it != m_db.cend()) {
173 /*
174 * The key in the DB is a pair of the interface's name and prefix.
175 * If the keys match, save the L3-config
176 */
177 auto key = it->first;
178
179 if (i.key() == key.first) {
180 l3s.push_back(it->second.lock());
181 }
182
183 ++it;
184 }
185
186 return (l3s);
187}
188
189l3_binding::event_handler::event_handler()
190{
191 OM::register_listener(this);
192 inspect::register_handler({ "l3" }, "L3 bindings", this);
193}
194
195void
196l3_binding::event_handler::handle_replay()
197{
198 m_db.replay();
199}
200
201void
202l3_binding::event_handler::handle_populate(const client_db::key_t& key)
203{
204 /**
205 * This is done while populating the interfaces
206 */
207}
208
209dependency_t
210l3_binding::event_handler::order() const
211{
212 return (dependency_t::BINDING);
213}
214
215void
216l3_binding::event_handler::show(std::ostream& os)
217{
218 m_db.dump(os);
219}
220}
221
222/*
223 * fd.io coding-style-patch-verification: ON
224 *
225 * Local Variables:
226 * eval: (c-set-style "mozilla")
227 * End:
228 */