VOM: vxlan-gbp
[vpp.git] / extras / vom / vom / gbp_bridge_domain.cpp
1 /*
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/gbp_bridge_domain.hpp"
17 #include "vom/gbp_bridge_domain_cmds.hpp"
18 #include "vom/interface.hpp"
19 #include "vom/l2_binding.hpp"
20 #include "vom/singular_db_funcs.hpp"
21
22 namespace VOM {
23
24 /**
25  * A DB of al the interfaces, key on the name
26  */
27 singular_db<uint32_t, gbp_bridge_domain> gbp_bridge_domain::m_db;
28
29 gbp_bridge_domain::event_handler gbp_bridge_domain::m_evh;
30
31 /**
32  * Construct a new object matching the desried state
33  */
34 gbp_bridge_domain::gbp_bridge_domain(const bridge_domain& bd,
35                                      const interface& bvi)
36   : m_id(bd.id())
37   , m_bd(bd.singular())
38   , m_bvi(bvi.singular())
39 {
40 }
41
42 gbp_bridge_domain::gbp_bridge_domain(const bridge_domain& bd)
43   : m_id(bd.id())
44   , m_bd(bd.singular())
45 {
46 }
47
48 gbp_bridge_domain::gbp_bridge_domain(const bridge_domain& bd,
49                                      const interface& bvi,
50                                      const interface& uu_fwd)
51   : m_id(bd.id())
52   , m_bd(bd.singular())
53   , m_bvi(bvi.singular())
54   , m_uu_fwd(uu_fwd.singular())
55 {
56 }
57
58 gbp_bridge_domain::gbp_bridge_domain(const bridge_domain& bd,
59                                      const std::shared_ptr<interface> bvi,
60                                      const std::shared_ptr<interface> uu_fwd)
61   : m_id(bd.id())
62   , m_bd(bd.singular())
63   , m_bvi(bvi->singular())
64   , m_uu_fwd(uu_fwd->singular())
65 {
66 }
67
68 gbp_bridge_domain::gbp_bridge_domain(const bridge_domain& bd,
69                                      const interface& bvi,
70                                      const std::shared_ptr<interface> uu_fwd)
71   : m_id(bd.id())
72   , m_bd(bd.singular())
73   , m_bvi(bvi.singular())
74   , m_uu_fwd(uu_fwd->singular())
75 {
76 }
77
78 gbp_bridge_domain::gbp_bridge_domain(const gbp_bridge_domain& bd)
79   : m_id(bd.id())
80   , m_bd(bd.m_bd)
81   , m_bvi(bd.m_bvi)
82   , m_uu_fwd(bd.m_uu_fwd)
83 {
84 }
85
86 const gbp_bridge_domain::key_t
87 gbp_bridge_domain::key() const
88 {
89   return (m_bd->key());
90 }
91
92 uint32_t
93 gbp_bridge_domain::id() const
94 {
95   return (m_bd->id());
96 }
97
98 const std::shared_ptr<bridge_domain>
99 gbp_bridge_domain::get_bridge_domain()
100 {
101   return m_bd;
102 }
103
104 const std::shared_ptr<interface>
105 gbp_bridge_domain::get_bvi()
106 {
107   return m_bvi;
108 }
109
110 bool
111 gbp_bridge_domain::operator==(const gbp_bridge_domain& b) const
112 {
113   bool equal = true;
114
115   if (m_bvi && b.m_bvi)
116     equal &= (m_bvi->key() == b.m_bvi->key());
117   else if (!m_bvi && !b.m_bvi)
118     ;
119   else
120     equal = false;
121
122   if (m_uu_fwd && b.m_uu_fwd)
123     equal &= (m_uu_fwd->key() == b.m_uu_fwd->key());
124   else if (!m_uu_fwd && !b.m_uu_fwd)
125     ;
126   else
127     equal = false;
128
129   return ((m_bd->key() == b.m_bd->key()) && equal);
130 }
131
132 void
133 gbp_bridge_domain::sweep()
134 {
135   if (rc_t::OK == m_id.rc()) {
136     HW::enqueue(new gbp_bridge_domain_cmds::delete_cmd(m_id));
137   }
138   HW::write();
139 }
140
141 void
142 gbp_bridge_domain::replay()
143 {
144   if (rc_t::OK == m_id.rc()) {
145     if (m_bvi && m_uu_fwd)
146       HW::enqueue(new gbp_bridge_domain_cmds::create_cmd(m_id, m_bvi->handle(),
147                                                          m_uu_fwd->handle()));
148     else
149       HW::enqueue(new gbp_bridge_domain_cmds::create_cmd(
150         m_id, handle_t::INVALID, handle_t::INVALID));
151   }
152 }
153
154 gbp_bridge_domain::~gbp_bridge_domain()
155 {
156   sweep();
157
158   // not in the DB anymore.
159   m_db.release(m_id.data(), this);
160 }
161
162 std::string
163 gbp_bridge_domain::to_string() const
164 {
165   std::ostringstream s;
166   s << "gbp-bridge-domain:[" << m_bd->to_string();
167
168   if (m_bvi)
169     s << " bvi:" << m_bvi->to_string();
170   if (m_uu_fwd)
171     s << " uu-fwd:" << m_uu_fwd->to_string();
172
173   s << "]";
174
175   return (s.str());
176 }
177
178 std::shared_ptr<gbp_bridge_domain>
179 gbp_bridge_domain::find(const key_t& key)
180 {
181   return (m_db.find(key));
182 }
183
184 void
185 gbp_bridge_domain::update(const gbp_bridge_domain& desired)
186 {
187   /*
188    * the desired state is always that the interface should be created
189    */
190   if (rc_t::OK != m_id.rc()) {
191     if (m_bvi && m_uu_fwd)
192       HW::enqueue(new gbp_bridge_domain_cmds::create_cmd(m_id, m_bvi->handle(),
193                                                          m_uu_fwd->handle()));
194     else
195       HW::enqueue(new gbp_bridge_domain_cmds::create_cmd(
196         m_id, handle_t::INVALID, handle_t::INVALID));
197   }
198 }
199
200 std::shared_ptr<gbp_bridge_domain>
201 gbp_bridge_domain::find_or_add(const gbp_bridge_domain& temp)
202 {
203   return (m_db.find_or_add(temp.m_id.data(), temp));
204 }
205
206 std::shared_ptr<gbp_bridge_domain>
207 gbp_bridge_domain::singular() const
208 {
209   return find_or_add(*this);
210 }
211
212 void
213 gbp_bridge_domain::dump(std::ostream& os)
214 {
215   db_dump(m_db, os);
216 }
217
218 void
219 gbp_bridge_domain::event_handler::handle_populate(const client_db::key_t& key)
220 {
221   /*
222    * dump VPP Bridge domains
223    */
224   std::shared_ptr<gbp_bridge_domain_cmds::dump_cmd> cmd =
225     std::make_shared<gbp_bridge_domain_cmds::dump_cmd>();
226
227   HW::enqueue(cmd);
228   HW::write();
229
230   for (auto& record : *cmd) {
231     auto& payload = record.get_payload();
232
233     std::shared_ptr<interface> uu_fwd =
234       interface::find(payload.bd.uu_fwd_sw_if_index);
235     std::shared_ptr<interface> bvi =
236       interface::find(payload.bd.bvi_sw_if_index);
237
238     if (uu_fwd && bvi) {
239       gbp_bridge_domain bd(payload.bd.bd_id, bvi, uu_fwd);
240       OM::commit(key, bd);
241       VOM_LOG(log_level_t::DEBUG) << "dump: " << bd.to_string();
242     } else if (bvi) {
243       gbp_bridge_domain bd(payload.bd.bd_id, *bvi);
244       OM::commit(key, bd);
245       VOM_LOG(log_level_t::DEBUG) << "dump: " << bd.to_string();
246     }
247   }
248 }
249
250 gbp_bridge_domain::event_handler::event_handler()
251 {
252   OM::register_listener(this);
253   inspect::register_handler({ "gbd", "gbridge" }, "GBP Bridge Domains", this);
254 }
255
256 void
257 gbp_bridge_domain::event_handler::handle_replay()
258 {
259   m_db.replay();
260 }
261
262 dependency_t
263 gbp_bridge_domain::event_handler::order() const
264 {
265   return (dependency_t::TABLE);
266 }
267
268 void
269 gbp_bridge_domain::event_handler::show(std::ostream& os)
270 {
271   db_dump(m_db, os);
272 }
273 }
274
275 /*
276  * fd.io coding-style-patch-verification: ON
277  *
278  * Local Variables:
279  * eval: (c-set-style "mozilla")
280  * End:
281  */