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