blob: 5831b2ae22484285184322a93f194a8bbd0a7039 [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"
17#include "vom/cmd.hpp"
18#include "vom/interface.hpp"
19#include "vom/l2_binding.hpp"
20#include "vom/logger.hpp"
21
22namespace VOM {
23/**
24 * A DB of al the interfaces, key on the name
25 */
26singular_db<uint32_t, bridge_domain> bridge_domain::m_db;
27
28bridge_domain::event_handler bridge_domain::m_evh;
29
30/**
31 * Construct a new object matching the desried state
32 */
33bridge_domain::bridge_domain(uint32_t id)
34 : m_id(id)
35{
36}
37
38bridge_domain::bridge_domain(const bridge_domain& o)
39 : m_id(o.m_id)
40{
41}
42
43uint32_t
44bridge_domain::id() const
45{
46 return (m_id.data());
47}
48
49void
50bridge_domain::sweep()
51{
52 if (rc_t::OK == m_id.rc()) {
53 HW::enqueue(new delete_cmd(m_id));
54 }
55 HW::write();
56}
57
58void
59bridge_domain::replay()
60{
61 if (rc_t::OK == m_id.rc()) {
62 HW::enqueue(new create_cmd(m_id));
63 }
64}
65
66bridge_domain::~bridge_domain()
67{
68 sweep();
69
70 // not in the DB anymore.
71 m_db.release(m_id.data(), this);
72}
73
74std::string
75bridge_domain::to_string() const
76{
77 std::ostringstream s;
78 s << "bridge-domain:[" << m_id.to_string() << "]";
79
80 return (s.str());
81}
82
83std::shared_ptr<bridge_domain>
84bridge_domain::find(uint32_t id)
85{
86 /*
87 * Loop throught the entire map looking for matching interface.
88 * not the most efficient algorithm, but it will do for now. The
89 * number of L3 configs is low and this is only called during bootup
90 */
91 std::shared_ptr<bridge_domain> bd;
92
93 auto it = m_db.cbegin();
94
95 while (it != m_db.cend()) {
96 /*
97 * The key in the DB is a pair of the interface's name and prefix.
98 * If the keys match, save the L3-config
99 */
100 auto key = it->first;
101
102 if (id == key) {
103 bd = it->second.lock();
104 break;
105 }
106
107 ++it;
108 }
109
110 return (bd);
111}
112
113void
114bridge_domain::update(const bridge_domain& desired)
115{
116 /*
117 * the desired state is always that the interface should be created
118 */
119 if (rc_t::OK != m_id.rc()) {
120 HW::enqueue(new create_cmd(m_id));
121 }
122}
123
124std::shared_ptr<bridge_domain>
125bridge_domain::find_or_add(const bridge_domain& temp)
126{
127 return (m_db.find_or_add(temp.m_id.data(), temp));
128}
129
130std::shared_ptr<bridge_domain>
131bridge_domain::singular() const
132{
133 return find_or_add(*this);
134}
135
136void
137bridge_domain::dump(std::ostream& os)
138{
139 m_db.dump(os);
140}
141
142void
143bridge_domain::event_handler::handle_populate(const client_db::key_t& key)
144{
145 /*
146 * dump VPP Bridge domains
147 */
148 std::shared_ptr<bridge_domain::dump_cmd> cmd(new bridge_domain::dump_cmd());
149
150 HW::enqueue(cmd);
151 HW::write();
152
153 for (auto& record : *cmd) {
154 auto& payload = record.get_payload();
155
156 bridge_domain bd(payload.bd_id);
157
158 VOM_LOG(log_level_t::DEBUG) << "dump: " << bd.to_string();
159
160 /*
161 * Write each of the discovered interfaces into the OM,
162 * but disable the HW Command q whilst we do, so that no
163 * commands are sent to VPP
164 */
165 OM::commit(key, bd);
166
167 /**
168 * For each interface in the BD construct an l2_binding
169 */
170 for (unsigned int ii = 0; ii < payload.n_sw_ifs; ii++) {
171 std::shared_ptr<interface> itf =
172 interface::find(payload.sw_if_details[ii].sw_if_index);
173 l2_binding l2(*itf, bd);
174 OM::commit(key, l2);
175 }
176 }
177}
178
179bridge_domain::event_handler::event_handler()
180{
181 OM::register_listener(this);
182 inspect::register_handler({ "bd", "bridge" }, "Bridge Domains", this);
183}
184
185void
186bridge_domain::event_handler::handle_replay()
187{
188 m_db.replay();
189}
190
191dependency_t
192bridge_domain::event_handler::order() const
193{
194 return (dependency_t::TABLE);
195}
196
197void
198bridge_domain::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 */