blob: 17144a67ca9c119a7bfec2b98471af4e09019dfd [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/bridge_domain.hpp"
Neale Ranns9ef1c0a2017-11-03 04:39:05 -070017#include "vom/bridge_domain_cmds.hpp"
Neale Ranns812ed392017-10-16 04:20:13 -070018#include "vom/interface.hpp"
19#include "vom/l2_binding.hpp"
Neale Ranns812ed392017-10-16 04:20:13 -070020
21namespace VOM {
Neale Ranns10e7a9f2017-11-14 08:40:43 -080022
23const bridge_domain::learning_mode_t bridge_domain::learning_mode_t::ON(1,
24 "on");
25const bridge_domain::learning_mode_t bridge_domain::learning_mode_t::OFF(0,
26 "off");
27
28bridge_domain::learning_mode_t::learning_mode_t(int v, const std::string& s)
29 : enum_base<bridge_domain::learning_mode_t>(v, s)
30{
31}
32
Neale Ranns812ed392017-10-16 04:20:13 -070033/**
34 * A DB of al the interfaces, key on the name
35 */
36singular_db<uint32_t, bridge_domain> bridge_domain::m_db;
37
38bridge_domain::event_handler bridge_domain::m_evh;
39
40/**
41 * Construct a new object matching the desried state
42 */
Neale Ranns10e7a9f2017-11-14 08:40:43 -080043bridge_domain::bridge_domain(uint32_t id, const learning_mode_t& lmode)
Neale Ranns812ed392017-10-16 04:20:13 -070044 : m_id(id)
Neale Ranns10e7a9f2017-11-14 08:40:43 -080045 , m_learning_mode(lmode)
Neale Ranns812ed392017-10-16 04:20:13 -070046{
47}
48
49bridge_domain::bridge_domain(const bridge_domain& o)
50 : m_id(o.m_id)
Neale Ranns10e7a9f2017-11-14 08:40:43 -080051 , m_learning_mode(o.m_learning_mode)
Neale Ranns812ed392017-10-16 04:20:13 -070052{
53}
54
Neale Rannsfd920602017-11-23 12:15:00 -080055const bridge_domain::key_t&
56bridge_domain::key() const
57{
58 return (m_id.data());
59}
60
Neale Ranns812ed392017-10-16 04:20:13 -070061uint32_t
62bridge_domain::id() const
63{
64 return (m_id.data());
65}
66
Neale Rannsfd920602017-11-23 12:15:00 -080067bool
68bridge_domain::operator==(const bridge_domain& b) const
69{
Neale Ranns55d03782017-10-21 06:34:22 -070070 return ((m_learning_mode == b.m_learning_mode) && id() == b.id());
Neale Rannsfd920602017-11-23 12:15:00 -080071}
72
Neale Ranns812ed392017-10-16 04:20:13 -070073void
74bridge_domain::sweep()
75{
76 if (rc_t::OK == m_id.rc()) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -070077 HW::enqueue(new bridge_domain_cmds::delete_cmd(m_id));
Neale Ranns812ed392017-10-16 04:20:13 -070078 }
79 HW::write();
80}
81
82void
83bridge_domain::replay()
84{
85 if (rc_t::OK == m_id.rc()) {
Neale Ranns10e7a9f2017-11-14 08:40:43 -080086 HW::enqueue(new bridge_domain_cmds::create_cmd(m_id, m_learning_mode));
Neale Ranns812ed392017-10-16 04:20:13 -070087 }
88}
89
90bridge_domain::~bridge_domain()
91{
92 sweep();
93
94 // not in the DB anymore.
95 m_db.release(m_id.data(), this);
96}
97
98std::string
99bridge_domain::to_string() const
100{
101 std::ostringstream s;
Neale Ranns17d2c4f2017-12-13 00:55:58 -0800102 s << "bridge-domain:[" << m_id.to_string()
103 << " learning-mode:" << m_learning_mode.to_string() << "]";
Neale Ranns812ed392017-10-16 04:20:13 -0700104
105 return (s.str());
106}
107
108std::shared_ptr<bridge_domain>
Neale Rannsfd920602017-11-23 12:15:00 -0800109bridge_domain::find(const key_t& key)
Neale Ranns812ed392017-10-16 04:20:13 -0700110{
Neale Rannsfd920602017-11-23 12:15:00 -0800111 return (m_db.find(key));
Neale Ranns812ed392017-10-16 04:20:13 -0700112}
113
114void
115bridge_domain::update(const bridge_domain& desired)
116{
117 /*
Neale Rannsfd920602017-11-23 12:15:00 -0800118 * the desired state is always that the interface should be created
119 */
Neale Ranns812ed392017-10-16 04:20:13 -0700120 if (rc_t::OK != m_id.rc()) {
Neale Ranns10e7a9f2017-11-14 08:40:43 -0800121 HW::enqueue(new bridge_domain_cmds::create_cmd(m_id, m_learning_mode));
Neale Ranns812ed392017-10-16 04:20:13 -0700122 }
123}
124
125std::shared_ptr<bridge_domain>
126bridge_domain::find_or_add(const bridge_domain& temp)
127{
128 return (m_db.find_or_add(temp.m_id.data(), temp));
129}
130
131std::shared_ptr<bridge_domain>
132bridge_domain::singular() const
133{
134 return find_or_add(*this);
135}
136
137void
138bridge_domain::dump(std::ostream& os)
139{
140 m_db.dump(os);
141}
142
143void
144bridge_domain::event_handler::handle_populate(const client_db::key_t& key)
145{
146 /*
Neale Ranns1d781552017-11-27 04:52:35 -0800147 * dump VPP Bridge domains
148 */
149 std::shared_ptr<bridge_domain_cmds::dump_cmd> cmd =
150 std::make_shared<bridge_domain_cmds::dump_cmd>();
Neale Ranns812ed392017-10-16 04:20:13 -0700151
152 HW::enqueue(cmd);
153 HW::write();
154
155 for (auto& record : *cmd) {
156 auto& payload = record.get_payload();
157
158 bridge_domain bd(payload.bd_id);
159
160 VOM_LOG(log_level_t::DEBUG) << "dump: " << bd.to_string();
161
162 /*
Neale Rannsa2ee0292017-11-28 22:29:13 -0800163 * Write each of the discovered bridge-domains into the OM,
164 * but disable the HW Command q whilst we do, so that no
165 * commands are sent to VPP
166 */
Neale Ranns812ed392017-10-16 04:20:13 -0700167 OM::commit(key, bd);
168
169 /**
Neale Rannsa2ee0292017-11-28 22:29:13 -0800170 * For each interface in the BD construct an l2_binding
171 */
Neale Ranns812ed392017-10-16 04:20:13 -0700172 for (unsigned int ii = 0; ii < payload.n_sw_ifs; ii++) {
173 std::shared_ptr<interface> itf =
174 interface::find(payload.sw_if_details[ii].sw_if_index);
175 l2_binding l2(*itf, bd);
176 OM::commit(key, l2);
177 }
178 }
179}
180
181bridge_domain::event_handler::event_handler()
182{
183 OM::register_listener(this);
184 inspect::register_handler({ "bd", "bridge" }, "Bridge Domains", this);
185}
186
187void
188bridge_domain::event_handler::handle_replay()
189{
190 m_db.replay();
191}
192
193dependency_t
194bridge_domain::event_handler::order() const
195{
196 return (dependency_t::TABLE);
197}
198
199void
200bridge_domain::event_handler::show(std::ostream& os)
201{
202 m_db.dump(os);
203}
204}
205
206/*
207 * fd.io coding-style-patch-verification: ON
208 *
209 * Local Variables:
210 * eval: (c-set-style "mozilla")
211 * End:
212 */