blob: 8bc7c7c3af301230aa401cf6981f470f9c2aed34 [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)
30 , m_binding(true)
31{
32}
33
34l3_binding::l3_binding(const l3_binding& o)
35 : m_itf(o.m_itf)
36 , m_pfx(o.m_pfx)
37 , m_binding(true)
38{
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
74std::string
75l3_binding::to_string() const
76{
77 std::ostringstream s;
78 s << "L3-config:[" << m_itf->to_string() << " prefix:" << m_pfx.to_string()
79 << " " << m_binding.to_string() << "]";
80
81 return (s.str());
82}
83
84void
85l3_binding::update(const l3_binding& desired)
86{
87 /*
88 * the desired state is always that the interface should be created
89 */
90 if (!m_binding) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -070091 HW::enqueue(
92 new l3_binding_cmds::bind_cmd(m_binding, m_itf->handle(), m_pfx));
Neale Ranns812ed392017-10-16 04:20:13 -070093 }
94}
95
96std::shared_ptr<l3_binding>
97l3_binding::find_or_add(const l3_binding& temp)
98{
99 return (m_db.find_or_add(make_pair(temp.m_itf->key(), temp.m_pfx), temp));
100}
101
102std::shared_ptr<l3_binding>
103l3_binding::singular() const
104{
105 return find_or_add(*this);
106}
107
108void
109l3_binding::dump(std::ostream& os)
110{
111 m_db.dump(os);
112}
113
114std::ostream&
115operator<<(std::ostream& os, const l3_binding::key_type_t& key)
116{
117 os << "[" << key.first << ", " << key.second << "]";
118
119 return (os);
120}
121
122std::deque<std::shared_ptr<l3_binding>>
123l3_binding::find(const interface& i)
124{
125 /*
126 * Loop throught the entire map looking for matching interface.
127 * not the most efficient algorithm, but it will do for now. The
128 * number of L3 configs is low and this is only called during bootup
129 */
130 std::deque<std::shared_ptr<l3_binding>> l3s;
131
132 auto it = m_db.cbegin();
133
134 while (it != m_db.cend()) {
135 /*
136 * The key in the DB is a pair of the interface's name and prefix.
137 * If the keys match, save the L3-config
138 */
139 auto key = it->first;
140
141 if (i.key() == key.first) {
142 l3s.push_back(it->second.lock());
143 }
144
145 ++it;
146 }
147
148 return (l3s);
149}
150
151l3_binding::event_handler::event_handler()
152{
153 OM::register_listener(this);
154 inspect::register_handler({ "l3" }, "L3 bindings", this);
155}
156
157void
158l3_binding::event_handler::handle_replay()
159{
160 m_db.replay();
161}
162
163void
164l3_binding::event_handler::handle_populate(const client_db::key_t& key)
165{
166 /**
167 * This is done while populating the interfaces
168 */
169}
170
171dependency_t
172l3_binding::event_handler::order() const
173{
174 return (dependency_t::BINDING);
175}
176
177void
178l3_binding::event_handler::show(std::ostream& os)
179{
180 m_db.dump(os);
181}
182}
183
184/*
185 * fd.io coding-style-patch-verification: ON
186 *
187 * Local Variables:
188 * eval: (c-set-style "mozilla")
189 * End:
190 */