blob: 065504cd19003d19b3b3aba46b8536cd65c059ea [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 {
20singular_db<l3_binding::key_type_t, l3_binding> l3_binding::m_db;
21
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.
46 m_db.release(make_pair(m_itf->key(), m_pfx), this);
47}
48
49void
50l3_binding::sweep()
51{
52 if (m_binding) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -070053 HW::enqueue(
54 new l3_binding_cmds::unbind_cmd(m_binding, m_itf->handle(), m_pfx));
Neale Ranns812ed392017-10-16 04:20:13 -070055 }
56 HW::write();
57}
58
59void
60l3_binding::replay()
61{
62 if (m_binding) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -070063 HW::enqueue(
64 new l3_binding_cmds::bind_cmd(m_binding, m_itf->handle(), m_pfx));
Neale Ranns812ed392017-10-16 04:20:13 -070065 }
66}
67
68const route::prefix_t&
69l3_binding::prefix() const
70{
71 return (m_pfx);
72}
73
Neale Ranns352ea0c2017-11-14 11:04:28 -080074const interface&
75l3_binding::itf() const
76{
77 return (*m_itf);
78}
79
80l3_binding::const_iterator_t
81l3_binding::cbegin()
82{
83 return m_db.cbegin();
84}
85
86l3_binding::const_iterator_t
87l3_binding::cend()
88{
89 return m_db.cend();
90}
91
Neale Ranns812ed392017-10-16 04:20:13 -070092std::string
93l3_binding::to_string() const
94{
95 std::ostringstream s;
96 s << "L3-config:[" << m_itf->to_string() << " prefix:" << m_pfx.to_string()
97 << " " << m_binding.to_string() << "]";
98
99 return (s.str());
100}
101
102void
103l3_binding::update(const l3_binding& desired)
104{
105 /*
Neale Ranns352ea0c2017-11-14 11:04:28 -0800106 * no updates for the binding. chaning the interface or the prefix is a change
107 * to the
108 * key, hence a new object
109 */
Neale Ranns812ed392017-10-16 04:20:13 -0700110 if (!m_binding) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700111 HW::enqueue(
112 new l3_binding_cmds::bind_cmd(m_binding, m_itf->handle(), m_pfx));
Neale Ranns812ed392017-10-16 04:20:13 -0700113 }
114}
115
116std::shared_ptr<l3_binding>
117l3_binding::find_or_add(const l3_binding& temp)
118{
119 return (m_db.find_or_add(make_pair(temp.m_itf->key(), temp.m_pfx), temp));
120}
121
122std::shared_ptr<l3_binding>
123l3_binding::singular() const
124{
125 return find_or_add(*this);
126}
127
128void
129l3_binding::dump(std::ostream& os)
130{
131 m_db.dump(os);
132}
133
134std::ostream&
135operator<<(std::ostream& os, const l3_binding::key_type_t& key)
136{
137 os << "[" << key.first << ", " << key.second << "]";
138
139 return (os);
140}
141
142std::deque<std::shared_ptr<l3_binding>>
143l3_binding::find(const interface& i)
144{
145 /*
146 * Loop throught the entire map looking for matching interface.
147 * not the most efficient algorithm, but it will do for now. The
148 * number of L3 configs is low and this is only called during bootup
149 */
150 std::deque<std::shared_ptr<l3_binding>> l3s;
151
152 auto it = m_db.cbegin();
153
154 while (it != m_db.cend()) {
155 /*
156 * The key in the DB is a pair of the interface's name and prefix.
157 * If the keys match, save the L3-config
158 */
159 auto key = it->first;
160
161 if (i.key() == key.first) {
162 l3s.push_back(it->second.lock());
163 }
164
165 ++it;
166 }
167
168 return (l3s);
169}
170
171l3_binding::event_handler::event_handler()
172{
173 OM::register_listener(this);
174 inspect::register_handler({ "l3" }, "L3 bindings", this);
175}
176
177void
178l3_binding::event_handler::handle_replay()
179{
180 m_db.replay();
181}
182
183void
184l3_binding::event_handler::handle_populate(const client_db::key_t& key)
185{
186 /**
187 * This is done while populating the interfaces
188 */
189}
190
191dependency_t
192l3_binding::event_handler::order() const
193{
194 return (dependency_t::BINDING);
195}
196
197void
198l3_binding::event_handler::show(std::ostream& os)
199{
200 m_db.dump(os);
201}
202}
203
204/*
205 * fd.io coding-style-patch-verification: ON
206 *
207 * Local Variables:
208 * eval: (c-set-style "mozilla")
209 * End:
210 */