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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include "vom/l3_binding.hpp"
17 #include "vom/l3_binding_cmds.hpp"
20 singular_db<l3_binding::key_t, l3_binding> l3_binding::m_db;
22 l3_binding::event_handler l3_binding::m_evh;
25 * Construct a new object matching the desried state
27 l3_binding::l3_binding(const interface& itf, const route::prefix_t& pfx)
28 : m_itf(itf.singular())
30 , m_binding(true, rc_t::NOOP)
34 l3_binding::l3_binding(const l3_binding& o)
37 , m_binding(o.m_binding)
41 l3_binding::~l3_binding()
45 // not in the DB anymore.
46 m_db.release(key(), this);
50 l3_binding::operator==(const l3_binding& l) const
52 return ((m_pfx == l.m_pfx) && (*m_itf == *l.m_itf));
55 const l3_binding::key_t
56 l3_binding::key() const
58 return (make_pair(m_itf->key(), m_pfx));
66 new l3_binding_cmds::unbind_cmd(m_binding, m_itf->handle(), m_pfx));
76 new l3_binding_cmds::bind_cmd(m_binding, m_itf->handle(), m_pfx));
80 const route::prefix_t&
81 l3_binding::prefix() const
87 l3_binding::itf() const
92 l3_binding::const_iterator_t
98 l3_binding::const_iterator_t
105 l3_binding::to_string() const
107 std::ostringstream s;
108 s << "L3-binding:[" << m_itf->to_string() << " prefix:" << m_pfx.to_string()
109 << " " << m_binding.to_string() << "]";
115 l3_binding::update(const l3_binding& desired)
118 * no updates for the binding. chaning the interface or the prefix is a change
120 * key, hence a new object
124 new l3_binding_cmds::bind_cmd(m_binding, m_itf->handle(), m_pfx));
128 std::shared_ptr<l3_binding>
129 l3_binding::find_or_add(const l3_binding& temp)
131 return (m_db.find_or_add(temp.key(), temp));
134 std::shared_ptr<l3_binding>
135 l3_binding::find(const key_t& k)
137 return (m_db.find(k));
140 std::shared_ptr<l3_binding>
141 l3_binding::singular() const
143 return find_or_add(*this);
147 l3_binding::dump(std::ostream& os)
153 operator<<(std::ostream& os, const l3_binding::key_t& key)
155 os << "[" << key.first << ", " << key.second << "]";
160 std::deque<std::shared_ptr<l3_binding>>
161 l3_binding::find(const interface& i)
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
168 std::deque<std::shared_ptr<l3_binding>> l3s;
170 auto it = m_db.cbegin();
172 while (it != m_db.cend()) {
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
177 auto key = it->first;
179 if (i.key() == key.first) {
180 l3s.push_back(it->second.lock());
189 l3_binding::event_handler::event_handler()
191 OM::register_listener(this);
192 inspect::register_handler({ "l3" }, "L3 bindings", this);
196 l3_binding::event_handler::handle_replay()
202 l3_binding::event_handler::handle_populate(const client_db::key_t& key)
205 * This is done while populating the interfaces
210 l3_binding::event_handler::order() const
212 return (dependency_t::BINDING);
216 l3_binding::event_handler::show(std::ostream& os)
223 * fd.io coding-style-patch-verification: ON
226 * eval: (c-set-style "mozilla")